Parsing a / from a file name

Discussion in 'Shell Script' started by GlobalNerds, Oct 10, 2022.

  1. GlobalNerds

    GlobalNerds New Member

    Joined:
    Oct 5, 2022
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    3
    I have a shell script that I run on Mac computers. It searches for certain characters and replaces them with a - The only character that does not work is the /
    Using this for One Drive Sync issues as it does not allow the / in the file name and will not sync.

    I have tried using / and // in the code, but nothing is working correctly. Any ideas?

    Code:
    type or pastebadchars='|?*,#;:&@+<>%$~{}\/'
    
    find . -name "*[$badchars]*" | while read -r file
    do
       target_name=$(echo "$file" | tr "$badchars" - )
    echo "Target Name" $target_name
       if [ "$file" != "$target_name" ]; then
           if [ -e $target_name ]; then
               echo "WARNING: $target_name already exists, file not renamed"
           else
               echo "Move $file to $target_name"
               mv "$file" "$target_name"
           fi
       fi
    done
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice