Use the Linux find command with caution
When using the find command in Linux, use the-OK option to avoid accidental deletion of files. This option will request your permission before removing any files.
Recently, a friend reminded me of a useful option to run it more cautiously.findCommand, which is-ok. In addition to an important difference, it works in a way similar-execSimilarfindCommand to request permissions before executing the specified operation.
Here is an example. If you usefindCommand to find and delete files. You may use the following command:
$ find.-name runme -execrm{} \;
Any files named "runme" in the current directory and Its subdirectories will be immediately deleted-of course, you must have the permission to delete them. Switch-okOption, you will see something like this,findThe command will request the permission before deleting the file. AnsweryIndicates that "yes" will allowfindCommand to continue and delete files one by one.
$ find.-name runme -ok rm{} \;
<rm..../bin/runme >?
-Execdir command is also an option
Another one can be used to modifyfindWhich of the following options can make command behavior more controllable?-execdir.-execWill run any specified command, and-Execdir: run the specified command from the directory where the file is located, instead of runningFind 'command directory to run the specified command. Here are two examples:
$ pwd
/home/shs
$ find.-name runme -execdir pwd \;
/home/shs/bin
$ find.-name runme -execdir ls \;
lsrm runme
So far it has been quite good. But remember,-execdirThe command is also executed in the directory of the matching file. If you run the following command and the directory contains a file named "ls ",NoExecute Permission, it will also run the file. Use-execOr-execdirSimilarsourceTo run the command.
$ find.-name runme -execdir ls \;
Running the /home/shs/bin/lsfile
$ find.-name runme -execdir rm{} \;
Thisis an imposter rm command
$ ls-l bin
total 12
-r-x------1 shs shs 25Oct1318:12ls
-rwxr-x---1 shs shs 36Oct1318:29rm
-rw-rw-r--1 shs shs 28Oct1318:55 runme
$ cat bin/ls
echoRunning the $0 file
$ cat bin/rm
echoThisis an imposter rm command
-The okdir option also requests permissions.
Be more cautious and use-okdir. Similar-ok.
$ find.-name runme -okdir rm{} \;
<rm..../bin/runme >?
You can also specify the full path of the command you want to use with caution to avoid any problems with fake commands like above.
$ find.-name runme -execdir /bin/rm{} \;
findIn addition to printing by default, the command also has many options, some of which can make your file search more accurate, but it is always good to be cautious.
Add the Network World community to Facebook and LinkedIn for comments.
Https://www.bkjia.com/Linux/2018-02/150951.htm