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

PowerShell: Get Spectre Meltdown Patching Versions of Hyper-V Hosts

function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 = "https://download.microsoft.com/download/1/D/8/1D8B5022-5477-4B9A-8104-6A71FF9D98AB/WindowsTH-RSAT_WS_1709-x64.msu" $rsat1803…

How to Install SSL Certificate(s) on Various Web Servers

Public facing websites often become become targets of attacks such as eavesdropping, denial of service,…

Centos 5.3 Server preparation for web hosting

These are compatible Repositories: FreshRPMS, Dries, NewRPMS and PlanetCCRMA   Note: to update PHP and…