Be cautious with Linux find command __linux

Source: Internet
Author: User
Guide When using the Find command under Linux, use the-OK option to prevent files from being accidentally deleted, which will request your permission before any files are removed.

When using the Find command under Linux, use the-OK option to prevent files from being accidentally deleted, which will request your permission before any files are removed.

A friend recently reminded me of a useful option to run the Find command more cautiously, which is-ok. In addition to an important distinction, it works like-exec, which causes the Find command to request permissions before performing the specified operation.

Here's an example. If you use the Find command to locate files and delete them, you may be using the following command:

$ find. -name runme-exec rm {} \;

Any file named "Runme" in the current directory and its subdirectories will be deleted immediately--you will, of course, have permission to delete them. Instead, you will see something like this, but the Find command will request permission before deleting the file (-OK). Answer y means "yes" to allow the Find command to continue and delete files one by one.

$ find. -name runme-ok rm {} \;
< RM .... /bin/runme >?
-execdir command is also a choice

Another option that can be used to modify the find command behavior and possibly make it more manageable is-execdir. -exec runs any of the specified commands, and-execdir runs the specified command from the directory where the file is located, rather than running the named commands in the directory where the Find command is running. Here are two examples of it:

$ pwd
/home/shs
$ find.-name runme-execdir pwd \;
/home/shs/bin
$ find. -name runme-execdir ls \;
LS RM runme

So far it's good. Keep in mind, however, that-execdir also executes the command in the directory of matching files. If you run the following command and the directory contains a file named "LS", it will run the file even if it does not have permission to execute. Using-exec or-execdir is similar to running a command from source.

$ find. -name runme-execdir ls \;
Running the/home/shs/bin/ls File
$ find. -name runme-execdir rm {} \;
This are an imposter RM command
$ ls-l Bin Total
-r-x------1 shs shs 18:12 ls
-rwxr-x---1 shs shs-Oct 18:29 RM
-rw-r w-r--1 SHS SHS 18:55 Runme
Cat Bin/ls
echo Running the $ file
$ cat bin/rm echo This is a
imposter RM command
The -okdir option will also request permissions

To be more cautious, you can use the-okdir option. Similar to-OK, this option will request permissions to run the command.

$ find. -name runme-okdir rm {} \;
< RM .... /bin/runme >?

You can also carefully specify the full path of the command you want to use to avoid any problems with a fake command like the one above.

$ find. -name runme-execdir/bin/rm {} \;

Find command In addition to the default printing there are many options, some can make your file search more accurate, but it is always good to be cautious.

Original from: https://www.linuxprobe.com/use-linux-find.html

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.