Posted On October 28, 2021

Bash Shell: Rename Files – Prepend and Append

kimconnect 0 comments
blog.KimConnect.com >> Codes , Linux >> Bash Shell: Rename Files – Prepend and Append

Pre-pending

# Prepend any file that doesn't have the word "/thumbs_" in its full path
regex="\/thumbs_"
for file in */thumbs/*.jpg ; do
    if [[ "$file"  =~ ${regex} ]]
    then
      echo "skipping: $file"
    else      
      mv "$file" "$(dirname "$file")/thumbs_$(basename "$file")"
    fi
done

Appending

# Append any file that doesn't have the word ".bak" in its full path
regex=".bak"
for file in */thumbs/*.jpg ; do
    if [[ "$file"  =~ ${regex} ]]
    then
      echo "skipping: $file"
    else      
      mv "$file" "$(dirname "$file")/$(basename "$file").bak"
    fi
done
# Appending file name right before extension
keyword=dragon
for file in *.jpg; do
    echo mv -- "$file" "${file%.jpg}_$keyword.jpg"
done

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Quick & Useful Snippet to Set SSL TLS Protocol of PowerShell

$requiredTls='Tls12' $availableSslProtocols=[enum]::getnames([net.securityprotocoltype]) if([Net.ServicePointManager]::SecurityProtocol -notin $requiredTls -and $requiredTls -in $availableSslProtocols){ [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::$requiredTls }

Basic JavaScript: Manipulate Arrays With pop()

// Setupvar myArray = [["John", 23], ["cat", 2]];// Only change code below this line.var removedFromMyArray=myArray.pop();

List Autorun Services

# $cred = get-Credential -credential kdoan-a $Username = 'kimconnect\kim-Admin $Password = 'PASSWORD' $pass = ConvertTo-SecureString…