rename


bash script to remove the word ‘DALL·E’ from all filenames

To remove the word “DALL-E” from all filenames in a directory, you can use a bash script with rename (or mmv if rename isn’t available on your system). Here is a simple bash script to achieve this:

# Iterate over all files in the current directory
for file in *DALL·E*; do
  # Remove 'DALL·E' from the filename
  new_file=$(echo "$file" | sed 's/DALL·E//g')
  # Rename the file
  mv "$file" "$new_file"
done

echo "Renaming completed."

Explanation:

  1. for file in *DALL·E*; do: This loop iterates over all files in the current directory that contain the word “DALL·E”.
  2. new_file=$(echo "$file" | sed 's/DALL-E//g'): This line uses sed to remove the word “DALL·E” from the filename. The s/DALL-E//g pattern tells sed to replace “DALL·E” with nothing, effectively removing it.
  3. mv "$file" "$new_file": This renames the original file to the new filename.
  4. done: This marks the end of the loop.
  5. echo "Renaming completed.": This prints a message indicating that the renaming process is complete.

Usage:

  1. Save the script to a file, for example, rename_files.sh.
  2. Make the script executable:
   chmod +x rename_files.sh
  1. Run the script in the directory where you want to rename the files:
   ./rename_files.sh

This will rename all files in the current directory by removing the word “DALL·E” from their filenames.


Recursively change extension of multiple files using find

Assuming you have a whole bunch of files that you need to change their extension from one to another, you can use the following commands after setting the values for the BEFORE and the AFTER variables to the values you need.

BEFORE='.txt'; AFTER='.csv'; find . -type f -name "*$BEFORE" -exec bash -c 'mv "$1" "${1%$2}$3"' _ '{}' "$BEFORE" "$AFTER" \;

What the above will do is: after setting the two input variables it will call find in the current directory (with recursion) and find all files that their suffix is the value you set in BEFORE. Then, for each match it will create a new shell terminal in which it will rename the file by removing the old suffix and then attaching the new one. We pass the input variables as parameters to the new shell and that is why inside the code of the shell we are using variables $1, $2 and $3. The reason we had to issue a new shell is because we wanted to reuse the ‘{}’ variable.