LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script to search for files of specific extension and delete them all (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-search-for-files-of-specific-extension-and-delete-them-all-819099/)

rhklinux 07-10-2010 11:06 AM

shell script to search for files of specific extension and delete them all
 
Hi i want such a shell script or single line command to delete all the files with extension specified in script
i have bash !!

ex...
delete all files of extension .obj
thanks in adv

Bratmon 07-10-2010 11:21 AM

See
Code:

man find

rhklinux 07-10-2010 11:44 AM

i used locate to find files of specific extension , plz tell me how to delete them ??

Bratmon 07-10-2010 11:48 AM

Pipe it to rm.

rhklinux 07-10-2010 11:49 AM

i dont know how to pipe ,plz give me code !!

TB0ne 07-10-2010 11:53 AM

Quote:

Originally Posted by rhklinux (Post 4029274)
i dont know how to pipe ,plz give me code !!

Spell your words out, and do some of your own research. Read the man pages, and check Google. There are LOTS of examples on how to pipe things, and write shell scripts.

saptu 07-11-2010 04:50 AM

hi

suppose u want to find all the files with the extension .jpg in /

# find / -name *.jpg | rm -rf

hope this might help u if any errors please do correct me

colucix 07-11-2010 05:22 AM

Come on, people.. let's not joke with the remove command! You cannot pipe the result of find or any other command directly to the standard input of rm, since the files to remove have to be passed as arguments. The rm command reserves the standard input for user interaction. Instead, let's wait the OP follows the TB0ne's advice and elaborates something for which we could really give help!

Bratmon 07-11-2010 08:08 AM

Quote:

Originally Posted by colucix (Post 4029757)
Come on, people.. let's not joke with the remove command! You cannot pipe the result of find or any other command directly to the standard input of rm, since the files to remove have to be passed as arguments. The rm command reserves the standard input for user interaction. Instead, let's wait the OP follows the TB0ne's advice and elaborates something for which we could really give help!

I didn't know that.

If the file list didn't have spaces, he could pipe it into
Code:

xargs /bin/rm

simon.sweetman 07-11-2010 11:28 PM

Command to delete all files with .obj extension (from current directory):

Code:

find . -type f -name "*.obj" -print0 | xargs -0 -r rm
Some notes on what options are for.
-type f (only list standard files, not directories, devices, pipes etc)
-print0 | xargs -0 (use null to seperate list of files to allow for spaces in file/directory names). Note character in flags is a numeric zero
-r don't call rm if input is empty (i.e. no files were found)

rhklinux 07-12-2010 12:07 AM

Quote:

Command to delete all files with .obj extension (from current directory):

Code:

find . -type f -name "*.obj" -print0 | xargs -0 -r rm

Some notes on what options are for.
-type f (only list standard files, not directories, devices, pipes etc)
-print0 | xargs -0 (use null to seperate list of files to allow for spaces in file/directory names). Note character in flags is a numeric zero
-r don't call rm if input is empty (i.e. no files were found)
thanks it worked

rhklinux 07-12-2010 12:07 AM

Quote:

Command to delete all files with .obj extension (from current directory):

Code:

find . -type f -name "*.obj" -print0 | xargs -0 -r rm

Some notes on what options are for.
-type f (only list standard files, not directories, devices, pipes etc)
-print0 | xargs -0 (use null to seperate list of files to allow for spaces in file/directory names). Note character in flags is a numeric zero
-r don't call rm if input is empty (i.e. no files were found)
thanks it worked

Telengard 07-12-2010 12:14 AM

This command will search recursively starting at the directory given in /path/to/search/. Every file who's name ends in .jpg will be permanently and irrevocably erased. The deletion is done silently, so don't expect to be told what was deleted. Don't do this unless you are perfectly damn certain it is what you want.

Code:

find /path/to/search/ -type f -name '*.jpg' -delete
Also:

Code:

man find
Also:

Code:

info find
Also:

http://www.gnu.org/software/findutil...tml/index.html


All times are GMT -5. The time now is 12:29 AM.