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:
- Stata external commands: the most common and up-to-date commands
- Linux_ day07_ 05_ Configure common add, delete, modify and query commands in network
- Linux find file text command (find | grep)
- Error response from daemon: OCI runtime exec failed: exec failed: container_linux.go:345:
- standard_init_linux.go:211: exec user process caused “exec format error”
- What are the common clearing commands in MATLAB?
- Docker run failed with the following error in logs, standard_ init_ linux.go:211 : exec user process caused “exec format error“
- Several methods of executing multiple commands in Linux shell
- CONDA creating virtual environment and common CONDA commands
- Solve cannot execute binary file: exec format error under Linux
- Linux common problems and Solutions
- Linux basic command execution error: the solution of command not found
- Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project Hello
- Common problems and solutions under Linux
- Common command of docker
- standard_init_linux.go:178: exec user process caused “no such file or directory”
- Linux command line cursor moving skills
- command ‘x86_64-linux-gnu-gcc’ failed with exit status 1
- Linux remote copy command and not a regular file solution
- Method of modifying file and folder permission by Chmod command in Linux