LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-15-2011, 07:54 AM   #1
garyozzy
Member
 
Registered: Jun 2005
Distribution: RHEL, CentOS, Debian, Ubuntu, Arch
Posts: 181

Rep: Reputation: 30
Sort Many Files in Subdirectories into Directories by File Type


I have a script that uses wget to download certain types of files. Mostly mp3's and pdf's.

I want to sort them into two directories, one for mp3, one for pdf.

The problem is, the files are downloaded by wget into directories many layers (4-5) deep.

In the past, I've used
Code:
mv **/* .
until all the files are in one directory then
Code:
mv *.mp3 mp3/ && mv *.pdf pdf/
How can I either put this in a script to do it for me or have a one-liner to do it? Any help would be appreciated.
 
Old 02-15-2011, 08:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,515
Blog Entries: 1

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
Hi,

Have a look at the find command.

Something like this should do what you want/need: find . -type f -iname "*.mp3" -exec mv {} /path/to/mp3dir/ \;

The above find command starts looking for files in the directory you are standing (the . [dot]), it will only look for normal files (the -type f part) and then look for files ending with mp3 (-iname "*.mp3"), the iname makes sure that it is case insensitive. The -exec ........ part moves files that are found to the /path/to/mp3dir/ directory.

Hope this helps.

edit: You need a second find for the pdf files.
 
1 members found this post helpful.
Old 02-15-2011, 08:05 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
I would probably go with a find into a loop and then use your moves in an if.

Let me know if you need more information?
 
Old 02-15-2011, 08:16 AM   #4
garyozzy
Member
 
Registered: Jun 2005
Distribution: RHEL, CentOS, Debian, Ubuntu, Arch
Posts: 181

Original Poster
Rep: Reputation: 30
This was very helpful! Thanks!

Just a few questions, though, so I can understand .

What does the {} after mv do, as well as the \;?

Quote:
Originally Posted by druuna View Post
Hi,

Have a look at the find command.

Something like this should do what you want/need: find . -type f -iname "*.mp3" -exec mv {} /path/to/mp3dir/ \;

The above find command starts looking for files in the directory you are standing (the . [dot]), it will only look for normal files (the -type f part) and then look for files ending with mp3 (-iname "*.mp3"), the iname makes sure that it is case insensitive. The -exec ........ part moves files that are found to the /path/to/mp3dir/ directory.

Hope this helps.

edit: You need a second find for the pdf files.
 
Old 02-15-2011, 08:22 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,515
Blog Entries: 1

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
Hi,

The {} is replaced by the file(s) that are found by the find command. The \; tells find where the commands used with the -exec option ends.

The find command is very powerful and can do a lot of things, have a look at these url's for some more examples and explanations:

A Unix/Linux "find" Command Tutorial
Linux find command examples

Hope this helps.

Last edited by druuna; 02-15-2011 at 08:33 AM. Reason: fixed spelling
 
Old 02-15-2011, 03:18 PM   #6
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,508

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
Quote:
The {} is replaced by the file(s) that are found by the find command
Perhaps more accurately, the {} is a placeholder for each individual filename that matches the rest of the find arguments. So, it will be given iteratively as an argument to the mv command. The difference is somewhat subtle, and in this example it may not matter, however in many other similar applications it does matter.

man find for details.

--- rod.
 
  


Reply

Tags
shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for easy way to rename MP3 tags then sort files into directories thethinker Linux - Software 3 08-22-2010 10:47 PM
Sort files in directories based on the files date... CharlieMike73 Programming 5 09-09-2009 10:04 PM
[SOLVED] listing directories containing files of certain type David the H. Linux - General 2 08-01-2009 12:05 PM
sort files inside a directory and sub-directories by latest created walidaly Linux - Software 8 07-25-2009 02:45 PM
copy directories and subdirectories Red Squirrel Linux - Newbie 2 03-06-2004 09:18 PM


All times are GMT -5. The time now is 05:25 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration