LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Very simple CLI question (https://www.linuxquestions.org/questions/linux-newbie-8/very-simple-cli-question-775576/)

togo59 12-14-2009 10:27 AM

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.

SethsdadtheLinuxer 12-14-2009 10:34 AM

the -R flag is probably what you would want. Try doing a "man rm" for details on your particular distro.

togo59 12-14-2009 10:41 AM

Quote:

Originally Posted by SethsdadtheLinuxer (Post 3791091)
the -R flag is probably what you would want.

Nope
Quote:

Originally Posted by SethsdadtheLinuxer (Post 3791091)
Try doing a "man rm" for details on your particular distro.

Nothing of use. Hence the question.

Vrajgh 12-14-2009 10:43 AM

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.

transformania 12-14-2009 11:09 AM

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.

togo59 12-14-2009 11:09 AM

Quote:

Originally Posted by Vrajgh (Post 3791101)
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. :)

b0uncer 12-14-2009 11:25 AM

Quote:

Originally Posted by togo59 (Post 3791126)
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.

togo59 12-14-2009 11:29 AM

Excellent! Many thanks.


All times are GMT -5. The time now is 03:50 PM.