Q. How do I find and delete files under Linux / UNIX operating systems?
A. Some time it is necessary to find out files and remove them. However, rm command does not support search criteria.
However, with find command you can search for files in a directory and remove them on fly.
You need to combine find and rm command together.
Fortunately find command makes this operation quite easy. You can use find command as follows:
Linux or UNIX - Find and remove file syntax
To remove multiple files such as *.jpg or *.sh with one command find, use
find . -name "FILE-TO-FIND"-exec rm -rf {} \;
OR
find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;
The only difference between above two syntax is that first command can remove directories as well where second command only removes files.
More Examples of find command
(a) Find all files having .bak (*.bak) extension in current directory and remove them:
$ find . -type f -name "*.bak" -exec rm -f {} \;
(b) Find all core files and remove them:
# find / -name core -exec rm -f {} \;
(c) Find all *.bak files in current directory and removes them with confirmation from user:
$ find . -type f -name "*.bak" -exec rm -i {} \;
Output:
rm: remove regular empty file `./data0002.bak'? y rm: remove regular empty file `./d234234234fsdf.bak'? y rm: remove regular empty file `./backup-20-10-2005.bak'? n
Caution: Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to Linux/Unix system.
Above examples are specific to this topic only.
See also : Other find command usage
For detailed information on find command please see Finding/locating files with find command part # 1, Part # 2
Sysadmin To Be Resources
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins
- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
Awesome, this was exactly what I needed to delete a huge amount of files when I ran out of inodes. Just doing an rm * would result in an error, but this deletes the files one at a time. Thanks
do any of the above commands delete .bak files recursively throughout an entire directory tree, or would I need to cd to each directory to delete its .bak files?
Rob,
> would I need to cd to each directory to delete its .bak files?
No
find command will go to each sub directory. For example delete all *.bak from /data2 dir, use
find /data2 -type f -name "*.bak" -exec rm -f {} \;
Why does it require a backslash to terminate with the ;? I need to place this in an applescript under “do shell script” but it hates that backslash. Is there any way around it?
A backslash is required, otherwise shell will treat ;? as part of shell command.
for removing directories in same manner, use command find . -type d -name “DIRNAME” -exec rm -rf {} \;
to recursively search and delete directories use
find . -type d -name “DIRNAME” -exec rm -rf {} \;
Why not use the -delete option of find?
[root@localhost ieee80211-1.2.18]# make
Checking in /lib/modules/2.6.18-53.el5xen for ieee80211 components…
make -C /lib/modules/2.6.18-53.el5xen/build M=/root/Desktop/ieee80211-1.2.18 modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-xen-i686′
CC [M] /root/Desktop/ieee80211-1.2.18/ieee80211_module.o
In file included from /root/Desktop/ieee80211-1.2.18/ieee80211_module.c:52:
/root/Desktop/ieee80211-1.2.18/compat.h:113:
Hi!
I am a linux newbie. I installed RHEL5 on a compaq Presario V3000 laptop.
Now i tried to install the wifi drivers ipw3445… but i am stuck her
error: redefinition of ‘kmemdup’
include/linux/slab.h:208: error: previous definition of ‘kmemdup’ was here
make[2]: *** [/root/Desktop/ieee80211-1.2.18/ieee80211_module.o] Error 1
make[1]: *** [_module_/root/Desktop/ieee80211-1.2.18] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-xen-i686′
make: *** [modules] Error 2
[root@localhost ieee80211-1.2.18]#
Please help
Did you ever find a solution to this problem. If so please let me know the details because I have the exact same issue. Thank you.
This page has just save 3 hours of stressful, worthless work in my life. Thank you so much.
How can I remove files that contain a certain string of text? For example: if file contains any of these symbols, remove the file:
{ }
[ ]
( )
Just to name a few, but there might be others. I need a bash script to do this.
Sweet thanks
awesome…just awesome…- thank you :-) – freakin’ directory was hung…can’t ls to it for nothing or anything for that matter….but finally :-)
What if I wanted to remove every file from my home account except for a certain directory??
For example:
find . -name ‘.UCLA’ -prune -o -exec rm -rf {} +;
This removes everything EXCEPT my .UCLA folder, however I get a message that says:
rm: error can not remove `.’ `..’
How do I get rid of that error message?
Thank you. It met my requiemens. IT was a timly help.
\madhan
I’m trying to remove files that have a certain misc dates
Would like to do a wild card for certain parts of the file name.
Example – 03230800203805V274803.810
Would ****08***********.810 work?
Want to delete the files containing in the postions 5 and 6 for 08.
Thanks,
Nicole
Is it not ‘ | exec ‘ and not ‘ -exec ‘ ? Either way it does not work.
Nobody has ever asked this question online it seems…
Problem: Stupid MAC (which I dont have anymore named files with these characters:
“:”
“?”
etc.. in the file names.. I could not find any file renamer that would rename these files in Windows/DOS..
So I tried FENDORA linux and I can rename the manually one by one but i have hundreds…
HELP!!!!
No RENAME or mv command works.. I trired everything i can find online..
I tried specifing the “:” like this “\:” “\x3a” etc.. and nothing.
does anyone know how to strip these retardted OS-illegal characters out of the names.. in one shot.?
I know the ren command in DOS.. but I have no clue it seems in Linux.
and no i don’t have a mac or access to one..
Thanks ahead of time, for your help.
This should do it:
for FILE in `find . -name ‘*\:*’`; do NEW=`echo $FILE | sed -e ‘s/:/_/’`; mv $FILE $NEW; done
Use -delete flag with find… works much faster than -exec rm
hey, thanks!
Hi,
I am trying to delete files and folders using the below command
find /opt/server/Outward/logs/ -daystart \( -type d ! \) -mtime +264 -exec rm -rf {} \;
I get the below error.
find: /opt/server/Outward/logs/2009-02-04: No such file or directory
The logs folder contains folders and sub-folders. After I execute the command i get the error message as above. What could be the possible reason and the solution.
Thanks
Kiran
Does find’s -delete option support or have something similar to rm’s -f flag? (i.e. force delete a file/directory even if the write permission is not set)? I need to delete files that may not be write enabled (eg r– or r-x when viewed using ls -l).
How to override after find command?
Note :i know find command but dont know how to combine these operations.
cat “I’m repled content” > test.zip .
ex:
1> i have to find files with name “*test*.*” and replace the content of the file with “I’m replaced content”.
2> find the file with size more than 10mb and replace the content of the file with “I’m replaced content”.
Kiran:
Try the -ignore_readdir_race option
Very nice find. Had a bunch of music I copied over from my Mac that had Ableton files in them which weren’t needed on my laptop.
Quick way of getting rid of them.
Thanks a bunch.
Fantastic tips. These helped me create some commands to add to my ~/.bash_logout file to perform file/directory cleanup on logout. Cheers!
find . -type f -name “FILE-TO-FIND” -delete;
#include
int main()
{
execl(“/bin/rm”, “rm”, “-f”, “/home/cc/rr* “, (char*)0);
printf(“end”);
return 0;
}
In the above program, I want to delete all file /home/cc/rr*. With above program it is not working, I mean it is not deleting. Please help.
this culls directories older than 10 days:
find /somePath -type d -name ‘someNamePrefix*’ -mtime +10 -print | xargs rm -rf ;
Thanks for the code Riley! I have been looking for something like this.
-delete will also work in a directory with say… hundreds of thousands of files, -exec rm -rf will crash.
I have never seen the exec rm -rf crash and I run it to scan over 150,000 files throughout the entire OS.
For instance
/bin/rm: Argument list too long
-bash-3.2$ ls -l | wc -l
161995
-bash-3.2$ find . -delete
-bash-3.2$
i tried to use the command to find and delete files
find . -name “find name” -exec rm {}\
but the return message is missing argument to ‘-exec’
what have I done wrong?
Add semicolon (;) to command:
find . -name “find name” -exec rm {}\ ;
thanks
can we combine locate with rm ?
thanks
Thanks!
How can check and redirect to a single file with the file names that contains the certain string of text? For example:
If my files abc.txt, aaa.txt and bbb.txt where both aaa and bbb contains ERROR, and abc not. I need to point out the names aaa.txt and bbb.txt to another file ccc.txt…
Just to name a few, but there might be others. I need a bash script to do this.
Am searching for a solution from a week…:(
is there supposed to be a space before -exec in the example?
find . -type f -name “FILE-TO-FIND” -exec rm -f {} \;
how to delete only c & java programs in red hat linux….
Thanks Dude! I really need it.