Search for and delete specific files

Search for and delete specific files

The find command can be used to search for and remove files that have certain things in common.
The arguments are the directory to be searched, the criteria for the files to be searched, the -ok option or the -exec option, and the delete command rm.
●Search for and delete files with the extension “txt”.(with confirmation of deletion)

[root@Lion ~]# ls -l
total 16
-rw-rw-r– 1 root root 0 December 25 04:55 2019 index.html
-rw-rw-r– 1 root root 0 December 25 04:55 2019 policy.html

-rw-rw-r– 1 root root 28 December 25 05:03 2019 test_file.txt
-rw-rw-r– 1 root root 27 December 25 05:05 2019 test_file1.txt

-rw-rw-r– 1 root root 27 December 25 05:06 2019 test_file2.txt
-rw-rw-r– 1 root root 27 December 25 05:09 2019 test_file3.txt

[root@Lion ~]# find . -name “*.txt” -ok rm {} \; ←Search for and delete files
< rm … ./test_file.txt > ? y
< rm … ./test_file1.txt > ? y
< rm … ./test_file2.txt > ? y
< rm … ./test_file3.txt > ? y

[root@Lion ~]# ls -l
合計 0
-rw-rw-r– 1 root root 0 December 25 04:55 2019 index.html
-rw-rw-r– 1 root root 0 December 25 04:55 2019 policy.html

This command searches for files in the current directory with a .txt extension and deletes them.
If you use the -ok option, you will be prompted to confirm the deletion (you can use the -exec option to delete without confirmation).

●Search for and delete files with the extension “html” (without confirmation of deletion)

[root@Lion ~]# ls -l
total 16
-rw-rw-r– 1 root root 0 December 25 04:55 2019 index.html
-rw-rw-r– 1 root root 0 1December 25 04:55 2019 policy.html


[root@Lion ~]# find . -name “*.html” -exec rm {} \;  ←Delete without confirmation

[root@Lion ~]# ls -l
合計 0

タイトルとURLをコピーしました