Bash script deletes hidden files generated by Mac

Go from Mac to Ubuntu 20 04. All folders bear the brand of MAC dignity, that is DS_Store file, GUI + mouse click (+ keyboard tapping) are obviously not suitable for Linux users. Bash, as an excellent scripting language, should really be used to do repetitive work. (in fact, it only needs the find + rm command to realize this function, so the only feature of this deletion script I can find is to use the GUI to directly drag the directory to be processed to Terminal, which may be faster than directly clicking tab??? A Linux user who only knows basic operations is also "possible".)

The general idea continues the previous one Unix cache file processing script.

WBS

Thumbs.db is a "thumbnail database", which is not unique to macos, but migration plus "cleanliness". I just want to leave what I really need. If I don't want to delete it, I can make corresponding comments in the code block of judging the file name.

Problems under ubuntu

There is a single quotation mark after the directory is dragged into Terminal

The same is Unix, but not Mac. Other Linux distributions are unknown.

read in bash reads strings, so if there are single quotation marks that need to be pinched, you can intercept variables with # and%.

${string#substring}: delete the shortest substring matching $substring from the beginning of variable $string.

${string%substring}: delete the shortest substring matching $substring from the end of variable $string.

reference resources: https://blog.csdn.net/dongwuming/article/details/50605911

Code implementation of pinching head and tail function:

name=${name#*\'}
name=${name%\'*}

Together, write the error information of name=${${name # * \ '}% \' *}: ${${name # * \ '}% \' *}: wrong replacement. I hope those who know will leave a message in the comment area. Thank you!

[[: not found error

When [[]] is used under ubuntu, it may report [[: not found. This is because ubuntu uses dash instead of bash by default. You can use sudo bash xxxx.sh to execute scripts or change the default to bash.

should be written down

  1. Bash (Bourne again shell) can be understood as an upgraded version of sh (Bourne Shell), but at first it was thought that sh was the command of bash language... Then use sh * * * SH executing the script will report inexplicable syntax errors, such as removemacfile SH: 3: syntax error: "(" unexpected ", so bash ***.sh or. / * * *. SH is recommended for script execution (permission needs to be granted).

  2. .DS_ The store file is a hidden file, so when reading the directory, you need to use ls -a, and a "." And "..." directories, but their values are considered bad variable name by bash.

  3. bash has strict grammar rules. What I make the most mistakes is the use of brackets.

    1. Spaces should be added between brackets and variables.

    2. The difference between single bracket and double bracket, double bracket is recommended, which is closer to the usual programming habits.

      Details of the differences can be found Single bracket and double bracket in BASH.

  4. Considering that there are escape characters such as spaces in the folder, it is necessary to enclose double quotation marks when calling variables.

GIF demo

source code

#! /bin/bash

function mac_files_del() {
    ls -a "$1" \
    | while read file;
    do
        # because of hidden file included "." and "..", they are bad variable name
        if [[ "$file" != "." &&  "$file" != ".." ]];
        then
            if [[ -d "$1/$file" ]];
            then
                if [[ "$file" = "__MACOSX" ]];
                then
                    rm -rf "$1/$file"
                    echo "$1/$file"
                else
                    mac_files_del "$1/$file"
                fi
            elif [[ "$file" = "._*" ]] || [[ "$file" =~ ".DS_Store" ]] || [[ "$file" = "Thumbs.db" ]];
            then
                rm -rf "$1/$file"
                # print path that is deleted
                echo "$1/$file"
            fi
        fi
    done
}

function mac_files_move() {
    ls -a "$1" \
    | while read file;
    do
        if [[ "$file" != "." &&  "$file" != ".." ]];
        then
            if [[ -d "$1/$file" ]];
            then
                if [[ "$file" = "__MACOSX" ]];
                then
                    mkdir -p "$move_path${1#*$dname}"
                    mv "$1/$file" "$move_path${1#*$dname}"
                else
                    mac_files_move "$1/$file"
                fi
            elif [[ "$file" = "._*" ]] || [[ "$file" =~ ".DS_Store" ]] || [[ "$file" = "Thumbs.db" ]];
            then
                # first to mkdir dir
                mkdir -p "$move_path${1#*$dname}"
                mv "$1/$file" "$move_path${1#*$dname}"
            fi
        fi
    done
}

echo "Please enter a folder to delete:"
read name
# remove the start and end char("'") of path by regex
name="${name#*\'}"
name="${name%\'*}"
# if delete, please commit the following 3 lines
# dname=$(dirname "$name")
# mkdir -p "$dname/mac_files_recover"
# move_path="$dname/mac_files_recover"

echo "------ Begin Handling Mac files ------"
# mac_files_move "$name"
mac_files_del "$name"
echo "-------------- In the end --------------"

GitHub address: https://github.com/childtclx/macfiles-rm

Tags: bash

Posted by salasilm on Sat, 02 Apr 2022 22:28:37 +0300