LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Linux command that can do.... (https://www.linuxquestions.org/questions/linux-general-1/linux-command-that-can-do-722720/)

linux_2007_ 04-30-2009 09:37 AM

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

crabboy 04-30-2009 09:44 AM

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

monsm 04-30-2009 09:46 AM

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. :)

colucix 04-30-2009 09:49 AM

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.

crabboy 04-30-2009 09:54 AM

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 {} \;

ghostdog74 04-30-2009 11:28 AM

if you have GNU find, put + behind is same as xargs
Code:

find ..... -exec rm -rf {} +


All times are GMT -5. The time now is 10:38 AM.