Use the Linux find command with caution

Source: Internet
Author: User

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:

  1. $ 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.

  1. $ find.-name runme -ok rm{} \;
  2. <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:

  1. $ pwd
  2. /home/shs
  3. $ find.-name runme -execdir pwd \;
  4. /home/shs/bin
  1. $ find.-name runme -execdir ls \;
  2. 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.

  1. $ find.-name runme -execdir ls \;
  2. Running the /home/shs/bin/lsfile
  1. $ find.-name runme -execdir rm{} \;
  2. Thisis an imposter rm command
  1. $ ls-l bin
  2. total 12
  3. -r-x------1 shs shs 25Oct1318:12ls
  4. -rwxr-x---1 shs shs 36Oct1318:29rm
  5. -rw-rw-r--1 shs shs 28Oct1318:55 runme
  1. $ cat bin/ls
  2. echoRunning the $0 file
  3. $ cat bin/rm
  4. echoThisis an imposter rm command

 

-The okdir option also requests permissions.

Be more cautious and use-okdir. Similar-ok.

  1. $ find.-name runme -okdir rm{} \;
  2. <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.

  1. $ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.