If you’re any kind of developer it’s big chance you’re using linux as application server. Sooner or later will need the command that find the files and remove them (or another do something else with them).
There is an rm
command but it doesn’t support search criteria.
However ther is find
command. With find
you can search for files in directory and remove them on the fly.
To do this you need to combine both commands together.
Syntax of find
command is quite easy:
1 2 |
$ find ROOT_FOLDER CRITERIA |
as ROOT_FOLDER can be /
, .
as current directory ot any other path. ROOT_FOLDER is the starting point for search.
CRITERIA can be many kinds, for example:
-type f
serach for files onlby (d – directories)
-name "PATTERN"
serach for files with name in pattern,
-iname "PATTERN"
the same as above but case insensitive,
-depth 2
search only in descendants deeper up to 2 steps from root_folder
-empty
find empty files/directories
-size 123k
find files with it’s size 123kilobytes.
-mtime -4h30m
search for files that was modificated in last 4 hours and 30 minutes,
there is a lot of another criteria, for more use man pages.
There is another option for find: -exec
. with this we can run command with found files.
1 2 |
$ find . -type f -iname "*.jpg" -exec rm -f {} \; |
{}
near the end means that command should be executed with evry found file, \;
ends the command
In command above we search for all files with jpg
exension from current directory and down, and remove them.
If we want to confirm every file’s remove, we should use:
1 2 |
find . -type f -name "*.bak" -exec rm -i {} \; |
Good luck :)
.