Common linux commands — find command’s Exec

Linux command — exec of find command

Example 1: the LS – L command is placed in the – exec option of the find command. Example 2: find the files whose change time is before n days in the directory and delete them. Example 3: find the files whose change time is before n days in the directory and delete them. Give a prompt before deleting. Example 4: grep is used in – exec Command instance 5: find the file and move to the specified directory instance 6: execute CP command with exec option

We often use the find query file. After querying, we will do some common operations, such as deleting RM – RF. At this time, we don’t want to use the pipe character, so we can use exec to solve this problem.

Exec explains:

-The exec parameter is followed by a command, such as RM – F, whose termination is marked by a semicolon. Therefore, the semicolon after this command is indispensable. Considering that the semicolon has different meanings in different systems, a backslash is added in front of it.

The curly brackets represent the file name found by find.

When using find, as long as you write the desired operation in a file, you can use exec to cooperate with find search, which is very convenient. In some operating systems, only the – exec option is allowed to execute commands such as LS or LS – L. Most users use some options to find old files and delete them. It is suggested that before the RM command is executed to delete a file, it is better to use the LS command to check that they are the files to be deleted. The exec option is followed by the command or script to be executed, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the exec option, you must also use the print option. If you verify the find command, you will find that the command only outputs the relative path and file name from the current path.

Example 1: the LS – L command is placed in the – exec option of the find command

1. Command:
find. – type F – exec LS – L {}; the find command matches all the ordinary files in the current directory and lists them with the LS – L command in the – exec option.

This command is a bit of a pit, but it’s really easy to use. It’s because I received a prompt when I entered: find omitted the – exec parameter

2. Solution:
(1) pay attention to a pair of {}, a space and a \, and finally a semicolon
(2) use “;” ‘;’ in; to arouse them.

Example 2: search the directory for the files whose change time is n days ago and delete them

1. Command:

find . -type f -mtime +14 -exec rm {} ;

Before deleting files in any way in the shell, you should check the corresponding files first, and be careful. When using commands such as MV or RM,
you can use the – exec option security mode, which will prompt you before operating on each matched file.

Example 3: find the files whose change time is n days ago in the directory and delete them. Give a prompt before deleting them

1. Command:

find . -name “*.log” -mtime +5 -ok rm {} ;

Find all the files in the current directory that end with. Log and have been changed for more than 5 days, delete them, and give a prompt before deleting them. Press y to confirm and N to cancel.

Example 4: using grep command in – Exec

1. Command:

find /etc -name “passwd” -exec grep “root” {} ;

Any form of command can be used in the – exec option. In the above example, we use the grep command. The find command first matches all the files named “passwd”, and then executes the grep command to see if there is a root user in these files.

Example 5: find the file and move it to the specified directory

1. Command:

find . -name “*.log” -exec mv {} … ;    //… Is the path name

Example 6: executing CP command with exec option

1. Command:

find . -name “*.log” -exec cp {} test3 ;

I’m not careful. Test3 is a directory, otherwise CP won’t go in.

Read More: