LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 07-10-2010, 11:06 AM   #1
rhklinux
Member
 
Registered: Jan 2010
Location: india/pune
Distribution: Arch Fedora20
Posts: 126

Rep: Reputation: 18
Question 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
 
Old 07-10-2010, 11:21 AM   #2
Bratmon
Member
 
Registered: Jul 2009
Location: 75.126.162.205:80
Distribution: Arch / Mint 17
Posts: 297
Blog Entries: 3

Rep: Reputation: 50
See
Code:
man find
 
0 members found this post helpful.
Old 07-10-2010, 11:44 AM   #3
rhklinux
Member
 
Registered: Jan 2010
Location: india/pune
Distribution: Arch Fedora20
Posts: 126

Original Poster
Rep: Reputation: 18
i used locate to find files of specific extension , plz tell me how to delete them ??
 
Old 07-10-2010, 11:48 AM   #4
Bratmon
Member
 
Registered: Jul 2009
Location: 75.126.162.205:80
Distribution: Arch / Mint 17
Posts: 297
Blog Entries: 3

Rep: Reputation: 50
Pipe it to rm.
 
0 members found this post helpful.
Old 07-10-2010, 11:49 AM   #5
rhklinux
Member
 
Registered: Jan 2010
Location: india/pune
Distribution: Arch Fedora20
Posts: 126

Original Poster
Rep: Reputation: 18
i dont know how to pipe ,plz give me code !!
 
Old 07-10-2010, 11:53 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,650

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by rhklinux View Post
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.
 
Old 07-11-2010, 04:50 AM   #7
saptu
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Rep: Reputation: 1
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
 
Old 07-11-2010, 05:22 AM   #8
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
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!

Last edited by colucix; 07-11-2010 at 05:24 AM.
 
Old 07-11-2010, 08:08 AM   #9
Bratmon
Member
 
Registered: Jul 2009
Location: 75.126.162.205:80
Distribution: Arch / Mint 17
Posts: 297
Blog Entries: 3

Rep: Reputation: 50
Quote:
Originally Posted by colucix View Post
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
 
Old 07-11-2010, 11:28 PM   #10
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
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)
 
Old 07-12-2010, 12:07 AM   #11
rhklinux
Member
 
Registered: Jan 2010
Location: india/pune
Distribution: Arch Fedora20
Posts: 126

Original Poster
Rep: Reputation: 18
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
 
Old 07-12-2010, 12:07 AM   #12
rhklinux
Member
 
Registered: Jan 2010
Location: india/pune
Distribution: Arch Fedora20
Posts: 126

Original Poster
Rep: Reputation: 18
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
 
Old 07-12-2010, 12:14 AM   #13
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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
 
1 members found this post helpful.
  


Reply



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
Shell script to delete old files whenever storage limit is crossed phani.junk1 Linux - General 6 10-19-2009 11:14 AM
Shell script to automatically delete files with the same name as the parent directory pratap.iisc Programming 9 10-12-2009 10:17 AM
How to search for missing files and pass their names on to another shell script djslothario Linux - Newbie 3 08-07-2009 12:59 AM
[SOLVED] search for specific file extension limdel Linux - Newbie 4 06-17-2009 08:06 AM
Making shell script to 'locate' m4a files and delete them bd1308 Linux - Software 3 02-12-2006 10:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:37 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