sleep command after mv command

Hi, I had to see sleep command after mv command in most of the shell scripts in an application. As I understand, script control moves to next statement only after the previous command is complete so, what is the use of having sleep command after the mv command in the scripts? A script is supposed to move around 1000 files in a day, based on the program it uses 10 seconds to sleep for every move. Thus, it spends around 3 hours in a day to do nothing and cause delay. Can I remove sleep command to have the script to run more quicker to minimize the processing time? Please suggest. Thanks!

Check whether the "mv" actually moves the files. A "mv" within the same filesystem will just rename the inode and has little impact even for 1000 files.

If the files are moved to a different filesystem then there may be a reason to have a delay between "mv" statements to reduce the impact on overall system performance. This depends on the size of the files, the ambient load on the system and the type of filesystem involved. If for example these are mirrored filesystems and the files are large, the impact of moving 1000 files back-to-back could be high. With mirrored filesystems and large files the disc activity will continue long after the "mv" command has returned to the command line.

Check wheter the "mv" actually moves the files.

Can you provide some sample code? Also, can you tell us if you are moving the files in the same or different filesystem, for as methyl points out this will change how man mv (linux) performs its actions; and if either (or both) filesystem is local or remote to the system you are running your script on.

Thanks for looking into it. Scripts moves files from one directory to the other directory in the same file system in a loop.

for file in `cat ${WK_DIR}/file_list`
do
    IND=`ls $file | cut -c10-10`
    if [[ $IND = [K-S] || $IND = W ]]
    then
       mv -f ${SOURCE_DIR}/$file ${TARGET_DIR}/$file
       sleep 10
    fi
done
exit

where WK_DIR, SOURCE_DIR and TARGET_DIR are the different directories under same file system.

'mv' (as well as almost every UNIX command or script) will return 0 if it succeeds, which should end up in the shell variable "$?".

You can also look into using the "||" operand to execute something if a command fails (e.g. is non-zero).

mv -f ${SOURCE_DIR}/$file ${TARGET_DIR}/$file || echo "Error moving file ${SOURCE_DIR}/$file ${TARGET_DIR}/$file"

I wonder what the application could be waiting for... :confused:

-dufftime

To be quite honest this script is dubious and would give syntax errors in my Shell.
Please give an example of filenames containing a significant character in character 10 and define the rule by which ones should be moved and which ones should not be moved.
Ps. The "sleep" is pointless if the Source and Target directories are definitely in the same filesystem.

I have a couple of questions/comments:

Just an observation, from man bash (linux):The command substitution $(cat file) can be replaced by the equivalent but faster $(< file)

This can be combined into one comparison:

if [[ $IND = [K-SW] ]]

I honestly can't figure out why you need the sleep.
What type of filesystem is being used?

First impressions are to rewrite the script for efficiency and readability.
The "ls" is pointless and the "for" construct is prone to issues with filenames containing space characters and can collapse with large numbers of files.

Whether you use the shell inward redirect or "cat" is up to you. Imho "cat" is faster and makes the script easier to follow (others on this board differ).

cat "${WK_DIR}/file_list" | while read file
do
     IND=`echo "${file}" | cut -c10-10`
     case "${IND}" in
           "K"|"L"|"M"|"N"|"O"|"P"|"Q"|"R"|"S"|"W")
               mv -f "${SOURCE_DIR}/${file}" ${TARGET_DIR}"
               ;;
     esac
done

Looking at:

Why don't we:

awk '{ print substr($0, 10, 1), $0; }' "${WK_DIR}/file_list" | while read IND file
do

It is just a thought, but since we are already catting the file, why not merge in the "cut" as well.