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

Web Server Files and mySQL backup and restore

Backup: mysqldump -u [username] -p [password] [databasename] > /home/shares/backup_db.sql   Restore: mysql -u root -p…

Check Servers NSLookup of a Listener to Match Active Node IP

$servers="SQL01","SQL02","SQL03","SQL04" $listener="halistener01" $activeNode="10.10.10.5" # Dynamic Credential method 1 $who = whoami if ($who.Substring($who.length-2, 2)="-admin"){$username=$who;} else…

Resolve Windows 2003 Logon Screen Blackout Problems

The problem:Administrators are unable to logon to a Windows 2003 Server. This is what they…