Use the Linux find command with caution

Introduction When using the find command under Linux, use the -ok option to avoid accidental deletion of files, which will ask for your permission before removing any files.

When using the find command under Linux, use the -ok option to avoid accidental deletion of files, which will ask for your permission before removing any files.

A friend recently reminded me that there is a useful option to run the find command more cautiously, and it's -ok. It works similarly to -exec except for one important difference, which causes the find command to request permission before performing the specified action.

Here is an example. If you used the find command to find files and delete them, you probably used the following command:

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

Any files named "runme" in the current directory and its subdirectories will be deleted immediately - you have permission to delete them, of course. Use the -ok option instead, and you'll see something like this, but the find command will ask for permission before deleting the file. Answering y for "yes" will allow the find command to continue and delete files one by one.

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

< rm ... ./bin/runme > ?

The -execdir command is also an option

Another option that can be used to modify the behavior of the find command, and possibly make it more controllable, is -execdir. -exec runs whatever command is specified, while -execdir runs the specified command from the directory where the file is located, not from the directory where the find` command is run. 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 so good. But keep in mind that -execdir will also execute the command in the directory of the matching file. If you run the command below, and the directory contains a file named "ls", it will run the file even if it doesn't have execute permission. Using -exec or -execdir is similar to running a command through source.

$ find . -name runme -execdir ls \;

Running the /home/shs/bin/ls file

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

This is an imposter rm command

$ ls -l bin

total 12

-r-x------ 1 shs shs 25 Oct 13 18:12 ls

-rwxr-x--- 1 shs shs 36 Oct 13 18:29 rm

-rw-rw-r-- 1 shs shs 28 Oct 13 18:55 runme

cat bin/ls

echo Running the $0 file

$ cat bin/rm

echo This is an imposter rm command

The -okdir option also asks for permissions

To be more cautious, use the -okdir option. Like -ok, this option will request permission to run the command.

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

< rm ... ./bin/runme > ?

You can also be careful to specify the full path to the command you want to use to avoid any problems with bogus commands like the one above.

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

The find command has many options other than printing by default, and some can make your file searches more precise, but it's always good to be cautious.

The original text comes from: https://www.linuxprobe.com/use-linux-find.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325114335&siteId=291194637