View previous topic :: View next topic |
Author |
Message |
tabanus l33t
Joined: 11 Jun 2004 Posts: 638 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. _________________ Things you might say if you never took Physics: "I'm overweight even though I don't overeat." - Neil deGrasse Tyson |
|
Back to top |
|
|
ppurka Advocate
Joined: 26 Dec 2004 Posts: 3256
|
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 | Now using kde5 | e is unstable :-/ |
|
Back to top |
|
|
tabanus l33t
Joined: 11 Jun 2004 Posts: 638 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 _________________ Things you might say if you never took Physics: "I'm overweight even though I don't overeat." - Neil deGrasse Tyson |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 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 _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
tabanus l33t
Joined: 11 Jun 2004 Posts: 638 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 _________________ Things you might say if you never took Physics: "I'm overweight even though I don't overeat." - Neil deGrasse Tyson |
|
Back to top |
|
|
|