Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Batch move files to new directories
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
tabanus
l33t
l33t


Joined: 11 Jun 2004
Posts: 638
Location: UK

PostPosted: Fri Apr 13, 2012 5:10 pm    Post subject: Batch move files to new directories Reply with quote

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
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3256

PostPosted: Fri Apr 13, 2012 5:19 pm    Post subject: Reply with quote

Code:
for f in *.pdf; do
    echo 'mkdir -p "${f%%_*}"'
    echo 'mv "$f" "${f%%_*}"'
done
Change and embellish according to your requirements 8)
_________________
emerge --quiet redefined | E17 vids: I, II | Now using kde5 | e is unstable :-/
Back to top
View user's profile Send private message
tabanus
l33t
l33t


Joined: 11 Jun 2004
Posts: 638
Location: UK

PostPosted: Sat Apr 14, 2012 10:51 am    Post subject: Reply with quote

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
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Apr 14, 2012 11:22 am    Post subject: Reply with quote

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
View user's profile Send private message
tabanus
l33t
l33t


Joined: 11 Jun 2004
Posts: 638
Location: UK

PostPosted: Sat Apr 14, 2012 11:41 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum