Solved

Need to delete filename from all subdirectories in Linux

Posted on 2010-11-30
8
964 Views
Last Modified: 2013-12-05
I have a single filename that exists thousands of times in the subdirectory tree, and I need to delete it everywhere it exists.

Easy enough in DOS:

delete /s myfile.txt

A similar approach in Linux doesn't work:

rm -r myfile.txt

because it will only remove from the current directory. What is the best approach?
0
Comment
Question by:jbbarnes
8 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 34237962
That's a task for "find":

find /start/dir -type f -name myfile.txt | xargs rm

find searches all subdirectories according to the given criteria. Results are passed to xargs which in turn calls rm with a number of parameters (= files to delete) suitable for this command.

wmp

0
 

Author Comment

by:jbbarnes
ID: 34238034
Hi, I can see how that would work. Thanks.

I do get an error, though, when I try it:

rm: missing operand

The xargs man page gives an example along these lines:

find /start/dir -type f -name myfile.txt -print | xargs rm -f
0
 
LVL 2

Assisted Solution

by:koffu
koffu earned 250 total points
ID: 34238058
in previous post command is valid, but with space after "xargs rm ". Also, if your filename will have spaces or another special symbols - it will not work.
try
find /start/dir -type f -name myfile.txt -delete
or
find /start/dir -type f -name myfile.txt -print0 | xargs -n1 -0 -I{} rm {}
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34238060
Well,

"-print" should be the default action of "find", and "rm -f" is for omitting the confirmation prompt before deleting a write-protected file.

Both things are not vital for the command to work.
Try adding "-print", perhaps your "find" would need it.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34238087
Not every find implementation has the "-delete" option, and "xargs -n1" would fork a new instance of rm for each one of the "thousands" of files, which is suboptimal for performance.
Never heard of the need to have a space after rm.

0
 
LVL 2

Expert Comment

by:koffu
ID: 34238282
rm also will accept a some filenames list to remove, but if user have a thousands copies of this file - multidelete is not a good solution. To avoid performance issues we can use -P key to run rm processes in parallel or use -n 10 for example.
without space we will get something like 'rmsomefile', not valid 'rm somefile'.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34238325
>> without space we will get something like 'rmsomefile', not valid 'rm somefile' <<

Nope. Did you ever run such a command, or is it all theory?
0
 
LVL 19

Expert Comment

by:simon3270
ID: 34238610
xargs will definitelyadd a space after the command.  The "missing operand" error is probbaly that there were no matching files from the "find" command, so xargs just ran the rm command with no files to delete.   The message does not appear if you give "rm" the "-f" parameter.

If you want to run this more than once, you could run the "find" command to count the number of matching files e.g.

numfiles=$(find /start/dir -name myfile.txt | wc -l)
if [ $numfiles -gt 0 ]
then
  find /start/dir -name myfile.txt | xargs rm
fi

One other thing to try is to add "-t" to the xargs command - this will print out the command it is about to run, including all of the parameters:

find /start/dir -name myfile.txt | xargs -t rm
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to learn Linux? 10 59
Centos 6 User Can't Assign Password 2 41
How yum repo uses sqlite3 to keep metadata about the repository? 2 31
centos commands 6 48
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now