A powerful but lesser known command line option in find command
Let us say you want to find all the files that are ending with .tmp and remove them. I have always used the following command:
This will find all the files that are ending with .tmp, and for each file rm is invoked with the file name as argument. Hence if you have 100 files, the rm command will be invoked 100 times.
There is a better way of doing this:
The "+" at the end tells the find command to concatenate all the file names with a space in between them, and invoke rm only once. To understand how it works, let me slightly change the command and show you:
Isn't that cool?
find . -name '*.tmp' -exec rm -f {} \;
This will find all the files that are ending with .tmp, and for each file rm is invoked with the file name as argument. Hence if you have 100 files, the rm command will be invoked 100 times.
There is a better way of doing this:
find . -name '*.tmp' -exec rm -f {} +
The "+" at the end tells the find command to concatenate all the file names with a space in between them, and invoke rm only once. To understand how it works, let me slightly change the command and show you:
$ find . -name '*.tmp' -exec echo {} \;
./b.tmp
./a.tmp
$ find . -name '*.tmp' -exec echo {} +
./b.tmp ./a.tmp
Isn't that cool?
Comments