Move file names with path to a file in a directory

2019-08-17 18:53发布

问题:

I was moving only the file names in a text file which was working using:

cd /data/sources/
find $PWD -type f -name "Asset_*">> /data/processing/asset_files.txt

But, I also need to:

  1. Move Asset_* files also in /data/processing/.
  2. Move first 1000 files into /data/processing/.

How do I add these two things?

回答1:

Refactoring answer of @Dominique you can use find command and move the files. THis can take a lot of time (depend of number of files) And the command is like:

cd /data/sources/
find $PWD -type f -name "Asset_*" -exec mv {} /target_dir \;

If you want to move exactly 1000 files you can do it with something like

a=1
cd /data/sources/
for i in $PWD/Asset_*
do
mv "$i" /target_dir
a=$(($a + 1))
if [ "$a" -gt 1000 ]
then break
fi
done


回答2:

You're on the wrong track, wanting to copy/move 1000 entries. You seem to have a lot of Asset_* files (over 1000) and by limiting the number to 1000, you want to check if the behaviour is right and then do it for the whole bunch.
Although this seems correct, the limiting to 1000 is quite a burden and will make your script complicated for no purpose.
I'd propose another approach : imagine you have filenames like this:

Asset_00000
Asset_00001
Asset_00002
...
Asset_00010
Asset_00011
...
Asset_99999

Then, you might first try to do you thing, using Asset_0000*, and then, once you've verified everything is correct, do it for all Asset_* files.

As far as the moving is concerned, you can do this as follows:

find /directory -name "Asset_0000*" -exec mv {} /target_directory \;


标签: bash shell unix