Find the answer to your Linux question:
Results 1 to 2 of 2
I have a bash script, listed in crontab that archives my junk drawer. THe problem is that for about half a dozen files it gave trying to mv saying Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    3

    Odd behavior with mv - "Directory not empty'


    I have a bash script, listed in crontab that archives my junk drawer.

    THe problem is that for about half a dozen files it gave trying to mv saying
    Code:
    mv: cannot move `/home/eddie/junk/logs' to `/home/eddie/junk/archive/2009_Feb/logs': Directory not empty
    If I copy the exact paths printed above (which are correct) and manually enter
    Code:
    mv /home/eddie/junk/logs /home/eddie/junk/archive/2009_Feb/logs
    and it does as you would expect no trouble.

    So why is it different in my script? The obvious different is the IFS, but I am doing this to allow files with spaces, and it works fine if the destination directory doesn't exist.

    Code:
    #!/bin/bash
     
    
    #change IFS so we can handle files with spaces
    RETURNIFS=$IFS
    #IN case we blow, trap for exit so we can restore IFS
    trap `IFS=$RETURNIFS` 0 1 2 6 9
    
    # copy in place, trap set, change IFS
    IFS=$(echo -en "\v")
    
    
    OLDFILES=$ACTIVE_DIR/*
    set -- $OLDFILES
    
    
    
    # run thoughm, compare time and move out or in.
    for ITEM in $OLDFILES 
    do	
    
    		mv $ITEM $DPATH
    		if [ $? -ne 0 ]
    		then	
    			echo "Error while moving last file" | tee -a $LOGFILE
    			exit 2
    		else
    			printf "\tSuccess\n" | tee -a $LOGFILE
    		fi
    	fi
    done 
    
    IFS=$RETURNIFS

  2. #2
    Just Joined!
    Join Date
    Apr 2009
    Posts
    90
    If you are 100% sure that the folder you want to move to doesn't exist,
    then add a rm -fr before the mv command?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •