How can I delete one file from every place on my disk using Linux? In Windows’ I’d open up a command window and use the command
del archive.txt /s
but what’s the Linux equivalent?
Alright, I know about the del command in DOS, but I don’t know about the “/s” flag. I had to dig for quite a while before I finally found out that the “/s” switch deletes all files from subdirectories. As I suspected, what you’re trying to do is delete all files with the specified name regardless of where they are on your PC.
First off, this seems rather dangerous! If nothing else, I’d certainly start out in my own home directory (just use cd without any arguments and you’ll be taken there directly) to ensure that I didn’t accidentally delete a file that some other program (or user!) needed.
In any case, the Linux command you seek is find and it’s far more powerful than the DOS del command, but, um, more complex too.
To find all occurrences of a specific filename at the current point in the file system and below, use this command:
find . -name archive.txt
The output will be a list of fully-qualified filenames like “./save/archive/archive.txt” and similar. There are a couple of ways to delete this set of files, but I always use the xargs command in a pipe:
find . -name archive.txt | xargs rm
If you’re still unsure, add the “-i” flag to the rm command and it’ll ask you to confirm each and every deletion before it occurs.
@monsieur it should be “type -f” to only delete files:
find . -type f “*.txt”
will locate all files with txt extensions.
UNIX is case-sensitive, so you may also want to test for all variations of txt (txt, TXT, TxT, etc) by using the ‘iname’ argument:
find . -type f -iname “*.txt”
Hi, this question could be a little simple. I have google but i don´t find the answer.
I want to delete a file with interactive prompt.
Something like this
rm -i *.txt
Of course the typical question will be prompted!
Yes to delete the file, No to skip the file.
Ok what i want is that once the interactive prompt start, and that is important, answer all the questions with yes.
I don´t want any force like rm -f *.txt, because it doesnt show what i want to delete. I want to see the first question. It shows me the first file that will be deleted and then i want to confirm all!!.
This is important for me to be sure that i remove what i want.
Thanks in advance if you can find the answer.
how would i remove a file with alot of spaces in the name. the filename is actually a sentense. with a “.” at the end. it dosent show up in gui view but in terminal it shows up as ” blah blah balh.~ ”
How do i get rid of that
you may also want to specify “-type d” to be sure you’re only deleting _FILES_
True enough! You need to use “-print0” in the find command and the “-0” flag to xargs. That shields the spaces in filenames from being misinterpreted in the pipeline and it all works fine.
the method works .. however if the filename has spaces.. the \’find\’ command doesn\’t escape the space characters so xargs wld give the wrong input to rm..
how do I escape the spaces?