Copy sequential files into sequential directories

-1

0

I need to copy multiple, sequential files into sequential directories. The files have the julian date (day of year) with other info in the file name. The directories are listed by julian date (day of year). I need each file to correspond with the julian date.

To clarify....

I have 365 files labeled "test_001.txt" through "test_365.txt" in "/home/aaa". Each file needs to be moved into it's own directory in "/data" labeled "001" through "365".

Hope this clears it up. Thanks!

gbh

Posted 2015-02-20T18:43:20.517

Reputation: 1

Example data please – roaima – 2015-02-20T18:44:33.747

Are the source files and directories in the same place or in different parts of the filesystem? – roaima – 2015-02-20T18:45:40.580

They are in different parts, but on the same server. – gbh – 2015-02-20T18:46:38.410

@gbh Please edit your most and make it more clearer with some clear example. – kenorb – 2015-02-20T18:54:00.227

Answers

0

Something like this should work

for K in $(seq -w 1 365)
do
    mv *_${K}.* "/data/$K" 2>/dev/null
done

roaima

Posted 2015-02-20T18:43:20.517

Reputation: 1 006

Thanks for the assist! But that only moved one file, the one labeled with 365. I need to move all the files. Thanks again! That is a great start! – gbh – 2015-02-20T20:35:19.637

@gbh sorry, I'm currently on a phone and the seq lost its starting 1 value. Post edited – roaima – 2015-02-20T21:22:33.420

Thanks again! But sadly, it had the same result. I did figure it out, with the help of your code. It is below. – gbh – 2015-02-20T21:37:56.280

@gbh perhaps your version of seq doesn't have the -w option, which ensures all output sequences have the same length (i.e. three characters in this scenario)? Other than that I can't see why this shouldn't work for you - unless your files don't match the glob *_NNN.* for numeric values of NNN. I suppose you could use test_${K}.txt if you want to be precise about the matching. – roaima – 2015-02-20T22:24:16.713

-1

for K in $(seq -f "test_%200g" 365); do cp *_${K}.* "/data/$K" 2>/dev/null; done

From what I've found out, it appears that in the "seq" command, the '%2' represents an integer and each '0' represents a place, i.e. ones, tens. The 'g' closes the expression. I may be wrong, but it works! Thanks for the help!

gbh

Posted 2015-02-20T18:43:20.517

Reputation: 1