Skip to content
Advertisement

moving files between folders not working

i’m trying to move more than one million files from one folder to another using a script but i’m not getting any results i.e no files moved and i did not get any errors,the script checks if they are more than 20 records in a folder and it moves them in another specified folder,here is my script.

#!/bin/bash

cd "/home/admin/Lulu/Bo/"

check="ls | wc -l"
if [ $(check) -gt 1 ]
then
   find ./ -name " oAS1_201613*.tar.gz"|grep -v bak| xargs -t -I {} mv {} /RS/2011/
fi

any suggestion on how this can be done?

Advertisement

Answer

One possible reason for finding no files might be that the filename you’ve specified is incorrect. Do your files really start with a space character?

Is there a reason you’re bothering to count the items? Wouldn’t the existence of files matching your find criteria imply that $(check) -gt 1? If so, you can eliminate the check — find will do nothing if it sees no files that match.

#!/bin/bash

src="/home/admin/Lulu/Bo/"

find "$src/" 
  -type f
  -name "oAS1_201613*.tar.gz" 
  -not -name "*bak*" 
  -print0 |
xargs -0 -t -I {} mv "{}" /RS/2011/

Note the -print0 and xargs -0, which allow you to handle files with whitespace in their names.

Note that you could also run your mv as an option to find. Instead of -print0, you might use this:

find "$src/" ... -exec mv -v "{}" /RS/2011/ ;

If you REALLY wanted to emulate the functionality of xargs -t, you could do that via multiple -exec commands.

One of the benefits of using xargs is sometimes the -P option, which lets you run things in parallel. At first glance, it would seem like a “big” job of moving a million files would be a candidate for this, but it’s likely you’ll be IO-bound anyway, so running your mv’s in parallel might not be so helpful, in which case find .. -exec might be just as performant as the pipe to xargs.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement