Solved

Need to delete filename from all subdirectories in Linux

Posted on 2010-11-30
8
975 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

691 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