Previous in Forum: Windows 7   Next in Forum: Computer Formatting - Windows XP
Close
Close
Close
9 comments
Rate Comments: Nested
Anonymous Poster

Unix Command to Delete Files

07/21/2010 8:47 AM

Hi,

I have a directory having many sub directories, now I need to delete some files in all sub directories which are having specific extension (say eg.jpg). Please help me on this.

Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".
2
Guru
Hobbies - CNC - New Member Popular Science - Biology - New Member Hobbies - Musician - New Member

Join Date: Dec 2008
Location: Canada
Posts: 3523
Good Answers: 146
#1

Re: Unix Command to Delete Files

07/21/2010 11:23 AM

The way to find out how to use commands in unix is to use the "man" command. That will get you a manual page on any command that you want to use. It's always advised to check your man pages since the commands may be slightly different on different systems or may vary over time.

In this case, rm is the standard "remove" or delete command, so "man rm" to read how to use it.

Afaik, to delete all jpg files from subdirectories, the command would be rm - r *.jpg

The "r" is for "recursive".

__________________
incus opella
Reply Good Answer (Score 2)
Anonymous Poster
#2
In reply to #1

Re: Unix Command to Delete Files

07/21/2010 1:21 PM

hi to use that command should i remain in main directory or i need to go each and every directory to remove all .jpg files??

Reply
Guru
Hobbies - CNC - New Member Popular Science - Biology - New Member Hobbies - Musician - New Member

Join Date: Dec 2008
Location: Canada
Posts: 3523
Good Answers: 146
#3
In reply to #2

Re: Unix Command to Delete Files

07/21/2010 1:29 PM

The point of "-r" or recursive is to execute the command in all subdirectories of the one in which you are working.

__________________
incus opella
Reply
2
Anonymous Poster
#4
In reply to #3

Re: Unix Command to Delete Files

07/21/2010 10:48 PM

Would be handy if that worked but it doesn't, rm -r will remove directories (and their contents recursively), but won't descend the directories looking for matches.

The command to descend a directory hierarchy in unix is "find", the full command you want is (there are multiple ways to do this with find, but this is general):

find . -name "*.jpg" -exec rm {} \;

which means:

"find ." - start at the current directory "." (you could put some other path here)

"-name "*.jpg"" - look for files named *.jpg

"-exec rm {} \;" - execute the command after exec up to the "\;", {} is substituted for the file name matched

See the man page for find for more info (find is one of the more complicated commands in unix).

Reply Good Answer (Score 2)
Anonymous Poster
#6
In reply to #4

Re: Unix Command to Delete Files

07/22/2010 7:09 AM

hi

please check the command

find /MYPATH *.jpg -exec rm {} \;

(MYPATH consists of so many sub directories)

i am getting error like *.jpg invalid status

please help me on this

Reply
Anonymous Poster
#7
In reply to #6

Re: Unix Command to Delete Files

07/22/2010 9:38 AM

You need quotes around the "*.jpg" otherwise it is expanded by the shell: find /MYPATH "*.jpg" -exec rm {} \;

Reply
Anonymous Poster
#8
In reply to #7

Re: Unix Command to Delete Files

07/23/2010 1:42 AM

hi

this command find /MYPATH "*.jpg" -exec rm {} \; removing all other files having other extension (eg:file.sql)

Reply
Anonymous Poster
#9
In reply to #4

Re: Unix Command to Delete Files

08/02/2010 5:58 AM

Its working thank you

Reply
3
Guru
Panama - Member - New Member Hobbies - CNC - New Member Engineering Fields - Marine Engineering - New Member Engineering Fields - Retired Engineers / Mentors - New Member

Join Date: Dec 2006
Location: Panama
Posts: 4273
Good Answers: 213
#5

Re: Unix Command to Delete Files

07/21/2010 11:01 PM

Something you should be aware of, especially when using a potentially destructive command like rm: some of these commands can vary slightly or significantly, depending on the shell you are using (sh, bash, csh, etc.), and whether you are operating as root or logged in as a regular user. I am not sure how much rm varies from one implementation to the next, but I have noticed other commands with serious potential to do some serious damage to your file system if you don't understand how it behaves in your particular environment. ALWAYS use the man command when trying out a new command for the first time...

Reply Good Answer (Score 3)
Reply to Forum Thread 9 comments

Good Answers:

These comments received enough positive votes to make them "good answers".
Copy to Clipboard

Users who posted comments:

Anonymous Poster (6); artsmith (2); cwarner7_11 (1)

Previous in Forum: Windows 7   Next in Forum: Computer Formatting - Windows XP

Advertisement