Last active
April 1, 2019 07:02
-
-
Save mikbuch/d3c4c4c0f0c42f4019bbde1f86ae7c27 to your computer and use it in GitHub Desktop.
Rename several file based on the substring each of them contains.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Takes all files within current directory and replaces one substring with | |
# another. This functionality is missing from standard linux shell (maybe | |
# it is possible to achieve with some complex awk command with regex, but | |
# I'd prefer to have a plain command for this). | |
# | |
# | |
# e.g. files: | |
# | |
# left-hand.txt | |
# left-hand_01.txt | |
# left-hand_02.txt | |
# | |
# bash mass_rename.sh left-hand right-hand | |
# | |
# right-hand.txt | |
# right-hand_01.txt | |
# right-hand_02.txt | |
# | |
# Add aliast in your bashrc/zshrc file with: | |
# alias mass_rename='bash X' | |
# where X is the path this file is stored | |
# | |
#TODO: automatize downloading and aliasing the file. | |
string_orig=$1 | |
string_mod=$2 | |
fileslist=$(ls -1) | |
for i in $fileslist; do | |
if [[ $i == *$string_orig* ]]; then | |
new_filename=$(echo $i | sed "s/$string_orig/$string_mod/") | |
echo "mv $i $new_filename" | |
mv $i $new_filename | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment