LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-24-2007, 09:34 PM   #1
speakfree31
LQ Newbie
 
Registered: Mar 2007
Location: New Mexico
Distribution: Arch Linux
Posts: 7

Rep: Reputation: 0
Question Question on deleting in command line


I am using Arch, and I have a ton of music on my computer. Well as all you of you that use bit torrent probably know anytime you download an album you usually download a ton of cover art. So I want to delete this cover art without having to go folder by folder...does anyone know how I can delete all the .jpg files from my music folder and all the folders it contains in one command. Im pretty sure you can do it with like sed or awk but I dont really know much about those 2. So anyways if anyone can tell me how to do this Id appreciate it...thanks
 
Old 03-24-2007, 09:40 PM   #2
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 59
If all your music is in a general folder called "My Music" in /home/user, the command would be:

rm -fr *.jpg /home/user/My\ Music

The "rm" is for remove
The -f is for force without asking me to confirm every file individualy
The -r is for recursive (all subdirectories of My Music)
The *.jpg is a wild card meaning everything ending with .jpg

Last edited by Junior Hacker; 03-24-2007 at 09:45 PM.
 
Old 03-24-2007, 09:44 PM   #3
speakfree31
LQ Newbie
 
Registered: Mar 2007
Location: New Mexico
Distribution: Arch Linux
Posts: 7

Original Poster
Rep: Reputation: 0
Im a little nervous to to use -fr, I want to be sure it will only get rid of the jpg's I tried regular rm but its not recursive so it doesnt work, but its like 60gb of music I REALLY dont want to accidentaly delete it ya know?
 
Old 03-24-2007, 09:47 PM   #4
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 59
Try:

rm -r *.jpg /home/user/My\ Music
 
Old 03-24-2007, 09:50 PM   #5
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Quote:
Originally Posted by speakfree31
Im a little nervous to to use -fr, I want to be sure it will only get rid of the jpg's I tried regular rm but its not recursive so it doesnt work, but its like 60gb of music I REALLY dont want to accidentaly delete it ya know?
That's good, because the rm command doesn't work that way. That command would have deleted your entire My Music directory. Instead, try this:
Code:
find /path/to/mymusic -name '*.jpg' -exec rm {} \;
 
Old 03-24-2007, 10:18 PM   #6
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
By way of explanation, the command "rm -r *.jpg /home/user/My\ Music" won't work for two reasons:
1) rm takes a list of file paths, not a pattern and a path.
2) Even if it did, the shell handles wildcard expansion, so rm would never actually see the *.jpg.

What would actually happen if you ran that command is that the shell would expand *.jpg to all the .jpg files (if any) in the current directory, then pass the expanded parameter list to rm. So after expansion, the command that was actually executed would look something like this:
rm -r ./foo.jpg ./bar.jpg ./baz.jpg /home/user/My\ Music
This would recursively delete each file in the parameter list, completely wiping out My Music.

By contrast, in the command I posted above, we use find to locate all the files that match the pattern *.jpg. The -exec option will then cause find to call the given command on each file in finds. You can simply replace "rm" with "echo" to see what files would be deleted.
 
Old 03-24-2007, 10:36 PM   #7
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 59
I just ran a test without the -f, and it did ask to remove any file ending with a . and three letters. It asked to remove the .jpg first, then the .mp3, then descend into a subdirectory and asked for confirmation for the .jpg's in it again before the .mp3's. So it's a good thing freespeaker got nervous.
 
  


Reply

Tags
arch, command, deleting, line


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files from command line goes to trash? HSN Linux - Enterprise 1 02-18-2007 11:38 PM
Deleting from command line Reaver76 Linux - Newbie 2 09-27-2006 04:42 PM
Command line question shadoweyez Linux - Software 2 11-18-2005 06:28 PM
problem deleting file using solaris command line. szehanz Solaris / OpenSolaris 2 03-22-2005 07:52 PM
command line question phonecian Linux - Software 2 10-09-2004 08:36 AM


All times are GMT -5. The time now is 04:16 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 Google+: linuxquestions
Open Source Consulting | Domain Registration