find and remove files with space using find command on Linux - linux

I'm trying to remove all thumbs.db files in a Windows partition using find command in Ubuntu:
find . -iname "*.db"|while read junk;do rm -rfv $junk;done
But it's not working for me and nothing happens! I think I found the problem, the white spaces in directory names!
I did this trick to remove my junk files before on previous version of Ubuntu but now on latest version of Ubuntu I can't.
Is there any bug in my command?

I'd do it this way:
find . -iname 'thumbs.db' -exec rm -rfv {} +
This way, it still works even if your directories contain whitespace in their names.

just to throw this out there
find . -name "*.pyc" -delete

I'm not sure why you're using while.
find . -iname 'thumbs.db' -exec rm -rfv {} \;
...should suffice (and only delete the files you want to, not any BDB files that may be laying around).

The code looks good and works on arch and debian. Maybe there are no files matching "*.db"?
As a sidenote: I might not be a good idea to delete all files with the suffix ".db", because you can accidently delete other files than "Thumbs.db"

First check if the first part of your command, that is:
find . -iname "*.db"
is returning anything.
If it does then you can use xargs as follows to accomplish your task:
find . -iname "*.db" | xargs rm -rfv
UPDATE: From comments, this is unsafe, specially if there are spaces in directory/file names. You will need to use -print0 / xargs -0 to make it safe.

Related

How do I write a command to search and remove files, even in cases where the files can't be found?

I’m using Amazon Linux with the bash shell. I want to find and remove some PDF files in a single line, so I tried
find /home/jboss/.jenkins/jobs/myco/workspace/ebook/ -name '*.pdf' | xargs rm
This works fine if there are PDF files. But if there are none, I get the error
rm: missing operand
Is there any way to write the above statement in a single line so that it will not fail, even if there are no files to remove?
This can easily be achieved using the -r flag to xargs.
I also recommend using "special character tolerant" version:
find /home/jboss/.jenkins/jobs/myco/workspace/ebook/ -name '*.pdf' -print0 | xargs -0 -r rm
Have you tried doing it all within the find command?
find /home/jboss/.jenkins/jobs/myco/workspace/ebook/ -name '*.pdf' -exec rm -f {} \;
I've always used the construct above though I believe you can also use the switch -delete which may be a bit more efficient. If you do use it remember to put -delete at the end as find is evaluated left to right as an expression.
You don't even need find, you can simply use rm, it supports basic pattern matching. Just do the following:
rm -f path/*.pdf

Iteratively remove file type

I'm trying to delete the files that visual sourcesafe inserts into various folders. It's this file:
vssver2.scc
Since I have many nested folders, I'd like to do this recursively from the parent folder. What would the linux code be to delete all files with .scc extension? (I'm on a mac).
Thanks.
Look for them and remove:
find . -name "*.scc" -exec rm {} +
To make sure you are going to delete the correct files, you can replace the rm with ls so that it will show these files.
Also, you can replace find . with find /your/path to indicate the exact path from which you want to remove. With find . it will start from the current path.
find . -name ".scc" -print0 | xargs -0 rm -rf

How to remove a file with specific filename in Linux from all the folders inside current directory?

My project has a number of directories and also has a large number of sub-projects. When I move from one PC to another all the Makefiles are creating problems in building the project.
If I remove the Makefiles then there is no error in building. How can I remove all the Makefile* files from the parent directory in Linux? Is there any command to perform this? There are hundreds of Makefiles. So removing them one by one is hard. Please suggest a method to do this.
You can use -delete option.
find . -name "filename" -type f -delete
http://en.wikipedia.org/wiki/Find#Delete_files_and_directories
find . -name "name-here" -exec rm -rf {} \;
find . name "filename" -type f|xargs rm -rf

how to delete all files with a path pattern

I have a backup location, which uses hardlinks to store existing or changed files. The location of these backups mimick the linux file system with a date part in it.
For example I have files
/backup/servername/2012-06-26T00.43.01/www.website.com/file1.html
/backup/servername/2012-06-26T06.43.01/www.website.com/file1.html
/backup/servername/2012-06-26T06.43.01/www.website.com/file2.html
/backup/servername/2012-06-26T12.43.01/www.website.com/file1.html
/backup/servername/2012-06-26T12.43.01/www.website.com/file2.html
How can I find all files which have www.website.com in them, so I can delete them
I have this command combination to delete files I can find with find, but I can't figure out how to find these files.
find . -name 'filename.*' -print0 | xargs -0 rm
You're being a little loose with your terminology, so it's a kind of tough to understand what exactly you want. However, if I understood you correctly, you want to delete all the files within a directory called www.website.com:
find . -wholename '*/www.website.com/*.html' -delete
if i understood you right you can use smth like this: find /backup/servername/2012-06-26T12.43.01/www.website.com/ -iname '*file*' -print0 | xargs -0 rm

Why | doesn't work with find?

Why does the following command aiming to remove recursively all .svn folders
find . -name ".svn" | rm -rfv
doesn't work ?
I know the find command provides the -exec option to solve this issue but I just want to understand what is happening there.
In your example, the results from find are passed to rm's STDIN. rm doesn't expect its arguments in STDIN, though.
Here is an example how input redirecting works.
rm does not read file names from standard input, so any data piped to it is ignored.
The only thing it uses standard input for is checking whether it's a terminal, so it can determine whether to prompt.
It doesn't work because rm does not accept a list of file names on its standard input stream.
Just for reference, the safest way to handle this in the case of directories that might contain spaces is:
find . -name .svn -exec rm -frv {} \;
Or, if you are shooting for speed:
find . -name .svn -print0 | xargs -0 rm -frv
find do works with | ( for example find ~ -name .svn | grep "a") but the problem is with rm
This question is similar to this other answered question. Hope this helps.
How do I include a pipe | in my linux find -exec command?

Resources