LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 12-14-2009, 10:27 AM   #1
togo59
LQ Newbie
 
Registered: Dec 2009
Location: Reading, UK
Distribution: Ubuntu 9.10
Posts: 5

Rep: Reputation: 0
Very simple CLI question


Very dumb question..

How do I apply a command like ls or rm to all files in all the directories below the current one (to arbitrary depth)?

E.g. suppose I want to delete all files that have a filename containing a specific pattern without knowing (or caring) which directory they're in.

I know about locate and find; I want to use something like:
Code:
rm -ir */*/*.~lock*
to delete all those files left over from some broken editing/open-office crash.
 
Old 12-14-2009, 10:34 AM   #2
SethsdadtheLinuxer
Member
 
Registered: Jun 2007
Posts: 152

Rep: Reputation: 37
the -R flag is probably what you would want. Try doing a "man rm" for details on your particular distro.
 
Old 12-14-2009, 10:41 AM   #3
togo59
LQ Newbie
 
Registered: Dec 2009
Location: Reading, UK
Distribution: Ubuntu 9.10
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by SethsdadtheLinuxer View Post
the -R flag is probably what you would want.
Nope
Quote:
Originally Posted by SethsdadtheLinuxer View Post
Try doing a "man rm" for details on your particular distro.
Nothing of use. Hence the question.
 
Old 12-14-2009, 10:43 AM   #4
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
You'll need to use the "find" command with -name to identify files by pattern and then the "-exec" to apply certain commands to each found file.
 
1 members found this post helpful.
Old 12-14-2009, 11:09 AM   #5
transformania
LQ Newbie
 
Registered: Dec 2009
Posts: 1

Rep: Reputation: 0
Yes, the "-exec" parameter for the find command is great.

So it would be something like the following example, if you wanted to delete all files that had a ".tmp" extension...

(be sure you're already in the folder where you want to start looking)...

find . -name *.tmp -exec rm -f {} \;

I suggest running this, first, just to make sure the output is what you want to delete...

find . -name *.tmp

So if what it spits out looks like the files you want to nuke, then go back add the "-exec..." stuff as show in the first command above.
 
1 members found this post helpful.
Old 12-14-2009, 11:09 AM   #6
togo59
LQ Newbie
 
Registered: Dec 2009
Location: Reading, UK
Distribution: Ubuntu 9.10
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Vrajgh View Post
You'll need to use the "find" command with -name to identify files by pattern and then the "-exec" to apply certain commands to each found file.
I was experimenting with "find . -name xyz" but thought there must be a simpler way. Glad I'm not going mad.

I had been using that other great Linux command -- rsync -- to synchronise several disks but it's additive; delete a file somewhere and you get it straight back on the next rsync run. I wanted a simple way to prune junk selectively.

Many thanks.

EDIT: Cross posts -- thank you as well Transformania !

EDIT again: in fact I used
Code:
find . -name *.~lock* -exec rm -i {} \;
and it's interactive.

Last edited by togo59; 12-14-2009 at 11:14 AM.
 
Old 12-14-2009, 11:25 AM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by togo59 View Post
EDIT again: in fact I used
Code:
find . -name *.~lock* -exec rm -i {} \;
and it's interactive.
I'd like to add that in some cases you may bump into filenames that just might mess up the command, so it's a good practise to escape the curly brackets using either \ or quotes. Using the above command as an example,

Code:
find . -name *.~lock* -exec rm -i '{}' \;
In a lot of cases it doesn't matter, as usual.

Last edited by b0uncer; 12-14-2009 at 11:27 AM.
 
1 members found this post helpful.
Old 12-14-2009, 11:29 AM   #8
togo59
LQ Newbie
 
Registered: Dec 2009
Location: Reading, UK
Distribution: Ubuntu 9.10
Posts: 5

Original Poster
Rep: Reputation: 0
Excellent! Many thanks.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Another Simple Scriptlet To Make The Unix And Linux CLI More User Friendly LXer Syndicated Linux News 0 11-21-2008 03:40 PM
simple CLI tool for agregated total received on eth0 (simpler than bandwidth) Emmanuel_uk Linux - Newbie 3 08-17-2005 03:29 PM
simple CLI question about size of HD? Lleb_KCir Linux - General 4 06-10-2004 09:00 PM
Simple Newb Question: How do I check what version of Redhat is Installed from the CLI praefex Linux - General 1 01-27-2004 07:30 PM
Trouble moving characters with C in a simple CLI program WindowsBurner Programming 4 12-11-2003 08:00 AM

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

All times are GMT -5. The time now is 07:22 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration