I have a command that copies file from one dir to another
FILE_COLLECTOR_PATH="/var/www/";
FILE_BACKUP_PATH='/home/'
ls $FILE_COLLECTOR_PATH | head -${1} | xargs -i basename {} | xargs -t -i cp $FILE_COLLECTOR_PATH{} "${FILE_BACKUP_PATH}{}-`date +%F%H%M%S%N`"
I loop it in a shell script like,
#!/bin/sh
SLEEP=120
FILE_COLLECTOR_PATH="/var/www/";
FILE_BACKUP_PATH='/home/'
while true
do
ls $FILE_COLLECTOR_PATH | head -${1} | xargs -i basename {} | xargs -t -i cp $FILE_COLLECTOR_PATH{} "${FILE_BACKUP_PATH}{}-`date +%F%H%M%S%N`"
sleep ${SLEEP}
done
But it seems to move only 10 files and not all files in the dir, Why? It should suppose to move all files.
In general, don’t try to parse the output of
lsin a script. You can end up with many different types of subtle problems. There is almost always a better tool for the job. Many times, this tool isfind. For example, to generate a list of all of the files in a directory and do something to each of them, you would do something like this:The
-print0and-0arguments allowfindandxargsto communicate filenames in a way that handles special characters (like spaces) correctly.The
findcommand has other options that you may find useful in a backup script (which is what it appears you are building). Options like-mminand-newerwill enable you to only back up files that have changed since the last iteration.