Linux or UNIX - Find and remove file syntax
To remove multiple files such as *.jpg or *.sh with one command find, use
OR
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:
(b) Find all core files and remove them:
(c) Find all *.bak files in current directory and removes them with confirmation from user:
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'? nCaution: 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.
To remove multiple files such as *.jpg or *.sh with one command find, use
Code Select
find . -name "FILE-TO-FIND"-exec rm -rf {} \;
OR
Code Select
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:
Code Select
$ find . -type f -name "*.bak" -exec rm -f {} \;
(b) Find all core files and remove them:
Code Select
# find / -name core -exec rm -f {} \;
(c) Find all *.bak files in current directory and removes them with confirmation from user:
Code Select
$ 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'? nCaution: 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.