| View previous topic :: View next topic |
| Author |
Message |
tabanus Guru


Joined: 11 Jun 2004 Posts: 577 Location: UK
|
Posted: Fri Apr 13, 2012 5:10 pm Post subject: Batch move files to new directories |
|
|
Any scripting magicians able to help me with this?
I have about 1000 files named so:
x_registration.pdf
Where x is a number that can be anything from 1 to about 110000
These files all sit in one directory. Is it possible to move them automatically to new directories based on the value of x?
For example 987_registration.pdf would be moved to folder /mnt/ftp/clients/987/documents.
Thanks. _________________ Never underestimate a hamster. |
|
| Back to top |
|
 |
ppurka Advocate

Joined: 26 Dec 2004 Posts: 3047
|
Posted: Fri Apr 13, 2012 5:19 pm Post subject: |
|
|
| Code: | for f in *.pdf; do
echo 'mkdir -p "${f%%_*}"'
echo 'mv "$f" "${f%%_*}"'
done | Change and embellish according to your requirements  _________________ emerge --quiet redefined | E17 vids: I, II |
|
| Back to top |
|
 |
tabanus Guru


Joined: 11 Jun 2004 Posts: 577 Location: UK
|
Posted: Sat Apr 14, 2012 10:51 am Post subject: |
|
|
Thanks for the quick reply.
When I run the script on some test documents I get the error:
| Code: | | mv: `2.pdf' and `2.pdf' are the same file |
ie. the value of 'f' is '2.pdf' rather than '2'.
How would I strip off just the number from the file?
Thanks _________________ Never underestimate a hamster. |
|
| Back to top |
|
 |
John R. Graham Administrator


Joined: 08 Mar 2005 Posts: 6435 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Apr 14, 2012 11:22 am Post subject: |
|
|
You didn't give us enough information on the file pattern. Ppurka's solution reasonably assumed that the "_" would always be there after the number. Just so I get it straight, what other rules / patterns should I be aware of?
- John _________________ This space intentionally left blank. |
|
| Back to top |
|
 |
tabanus Guru


Joined: 11 Jun 2004 Posts: 577 Location: UK
|
Posted: Sat Apr 14, 2012 11:41 am Post subject: |
|
|
OK, I'm officially retarded.
I forgot to put _docs in the test data file names. It all works perfectly now.
Thanks  _________________ Never underestimate a hamster. |
|
| Back to top |
|
 |
|