Use shell script to delete specified suffix files

foreword

Sometimes we delete some files with specified suffixes in batches. Next, let’s take deleting .meta files in a certain folder as an example ( if you want to delete files with other suffix formats, you can read the tips in the fourth part ).

1. Generate executable files

Tip: Before performing the delete operation, we first create an executable file.
1. Create a new file (not a folder) with no suffix format, for example, the file name is clear_meta .
2. Open the terminal, and cd to the directory where clear_meta is located.
3. Enter the command sudo chmod u+x clear_meta in the terminal .
4. After successful execution, the clear_meta file becomes an executable file.

2. Edit the clear_meta file

Tip: Open clear_meta with developer tools and edit the following command lines.

#!/bin/bash
function clearmeta()
{
    
    
	cd $1
	echo $1
    cfilelist=$(ls -A)
    for cfilename in $cfilelist  
    do   
        s=$cfilename
        arr=(${
    
    s//./ })  

    	if [[ ${
    
    arr[${
    
    #arr[@]}-1]} == meta ]]  # 注释1语句
        then   
        	sudo rm -rf $cfilename
        elif [ -d $cfilename ]
        then
            clearmeta $(pwd)"/"$cfilename
        fi
    done 
    cd ..
}

clearmeta $1

3. Perform the delete.meta operation

1. For example, we have a folder named public , which contains many subdirectories and files, and of course there are corresponding .meta files, as shown in the figure below. We need to delete the .meta file in it .
insert image description here
2. Open the terminal, and directly drag the clear_meta file in ( do not press Enter at this time, and do not enter other characters ); then, drag the public file in, and then click Enter to perform the operation. 3. The terminal will output a series of delete operation logs, and wait for the execution to end. The .meta
file in our public folder has been deleted.

4. Friendly Tips

To delete suffix files in other formats, you only need to change the above statement of Note 1 into this code (take deleting .txt as an example).

${
    
    arr[${
    
    #arr[@]}-1]} == txt

In addition, if you want to learn more about using scripts to manipulate files, you can check the article Using shell scripts to modify file names in batches , I hope it will be helpful to you.

Guess you like

Origin blog.csdn.net/HYNN12/article/details/107152788