da_Aresinger

Bash batch rename

Oct 19th, 2023 (edited)
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.65 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. # Read the old substring from user input
  4. read -p "Enter the substring to replace: " old_substring
  5.  
  6. # Read the new substring from user input
  7. read -p "Enter the new substring: " new_substring
  8.  
  9. # Loop over all files in the current directory that contain the old substring
  10. for file in *"$old_substring"*; do
  11.   # Rename the file by replacing the old substring with the new substring
  12.   # The string manipulation uses parameter expansion
  13.   # this method allows a regex-like string manipulation
  14.   new_file="${file/$old_substring/$new_substring}"
  15.   mv -- "$file" "$new_file"
  16.   # Echo the new file name
  17.   printf "$file\t->\t$new_file\n"
  18. done
Advertisement
Add Comment
Please, Sign In to add comment