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

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

Related

`find` command core dumps in directory with too many files

I have a folder that contains so many files/folders inside it that even basic commands like du and find are crashing. I'd like to clean up some old files from it... but obviously I can't do that with the find command...
# find /opt/graphite/storage/whisper -mtime +30 -type f -delete
Aborted (core dumped)
What command or trick can I use to delete files from that folder since find isn't working?
I believe the best way to go is using a simple for-loop: the problem is that find loads all found information in memory, and only once this is done, it starts deleting.
However, a loop can solve this:
for f in $(ls -a)
do
if <check_last_modification_date>($f)
then rm -r $f
fi
done
Concerning the last modification date check, there are plenty of ways to do this, as explained here.
For find command using the -exec option worked for me to delete the files.
# find /opt/graphite/storage/whisper -mtime +30 -type f -exec rm -f {} \;

removing some files out of several folders

I have a question about removing some files out of several folders.
To be more specific: There are 5 Folders which are only the same by a few characters. For example: o1_FolderF_xy and zz_FolderF_34. And in each folder with the characters "FolderF" I want to delete all the files which starts with "filename".
The last time I did it by hand.
Will this work? Or do i need a script with a loop?
rm -rf /path/toFolder/*FolderF*/filename*
I'm sorry, I think for most it's a stupid question. But I'm new to all the stuff and I just do not want to go wrong with the delete
Your suggested command will work just fine.
You could use find instead:
find /path -name 'filename*' -exec rm {} \;
Basically it search's files with filename pattern on /path directory and for each file it executes rm.
Or, if you want to just check into those specific directories:
find /path -wholename '*folder*/filename*' -exec rm {} \;

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

Remove files for a lot of directories - Linux

How can I remove all .txt files present in several directories
Dir1 >
Dir11/123.txt
Dir12/456.txt
Dir13/test.txt
Dir14/manifest.txt
In my example I want to run the remove command from Dir1.
I know the linux command rm, but i don't know how can I make this works to my case.
PS.: I'm using ubuntu.
To do what you want recursively, find is the most used tool in this case. Combined with the -delete switch, you can do it with a single command (no need to use -exec (and forks) in find like other answers in this thread) :
find Dir1 -type f -name "*.txt" -delete
if you use bash4, you can do too :
( shopt -s globstar; rm Dir1/**/*.txt )
We're not going to enter sub directories so no need to use find; everything is at the same level. I think this is what you're looking for: rm */*.txt
Before you run this you can try echo */*.txt to see if the correct files are going to be removed.
Using find would be useful if you want to search subfolders of subfolders, etc.
There is no Dir1 in the current folder so don't do find Dir1 .... If you run the find from the prompt above this will work:
find . -type f -name "*.txt" -delete

find and remove files with space using find command on 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.

Resources