LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-30-2009, 09:37 AM   #1
linux_2007_
Member
 
Registered: Feb 2007
Posts: 80

Rep: Reputation: 15
Linux command that can do....


Hello,

Is there any command in Linux (SuSE) that can delete some specific files in many directories, say 100 ? There are a number of files in these directories on my hard disk which all have same names (or same starting name), and they are useless and have taken much of the disk capacity, so I need to delete them but it is impossible to go into each of them one by one. If you know such a command, I would appreciate it if you could let me know.

Thanks a lot!

Ally
 
Old 04-30-2009, 09:44 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
This will work for as many files as it finds. I'd suggest running it first without the '|xargs rm -f' so you can see what it finds before deleting. This can be very destructive if you make a mistake with the file pattern.

Code:
$ find /starting/dir -name 'filename' | xargs rm -f
 
Old 04-30-2009, 09:46 AM   #3
monsm
Member
 
Registered: Feb 2005
Location: London, UK
Distribution: Gentoo
Posts: 568

Rep: Reputation: 37
Never tried anything like that, but you might want to look at the man pages for 'find'. Its got an exec part. I wonder if you'll be able to use 'rm in that part.
Not sure if a normal pipe would work, so I guess a little script could do it if you can't find a soluition with 'find'

Mons
EDIT: crabboy pressed send first, and with a more full answer.

Last edited by monsm; 04-30-2009 at 09:48 AM.
 
Old 04-30-2009, 09:49 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use the find command with the -exec option to remove the resulting files. For example, suppose you have some files whose name start with "test", all under the /home directory:
Code:
find /home -type f -name test\* -exec echo rm {} \;
the echo command is for testing purpose. It will just print the actual command to execute. Check carefully if the listed files are all those you want to remove, then run the command again without echo to actually remove them. Alternatively you can do
Code:
find /home -type f -name test\* -ok rm {} \;
using -ok in place of -exec force it to ask for confirmation before removing a file.
 
Old 04-30-2009, 09:54 AM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Using -exec would work, but it will execute rm for each file that it finds. xargs collects a bunch of filenames and then calls rm with the filenames as the parameter. For a hundred files or so the difference will be small, but for thousands of files you'll notice a difference in the speed.
Code:
find /starting/dir -name 'filename' -exec rm {} \;
 
Old 04-30-2009, 11:28 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have GNU find, put + behind is same as xargs
Code:
find ..... -exec rm -rf {} +
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
unix command to linux command leonidas Linux - General 1 09-10-2004 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:08 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration