-
-
Save Sonic0/d1bb2310b1fb83909277b1f04ae712fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# This replace spaces in a file name | |
# with underscores as the default. | |
# replaceit | |
# file 1.txt will be file_1.txt | |
# You can provide an argument for replacement. | |
# replaceit - | |
# file-1.txt will be file_1.txt | |
# replaceit -f - | |
# This will replace this-file-1.md to this_file_1.md | |
# replaceit -f - -t x | |
# his will replace this-file-1.md to thisxfilex1.md | |
usage() | |
{ | |
echo "Usage: $0 [ -m FROM ] [ -t TO ]" | |
exit 2 | |
} | |
################### | |
# main script | |
unset FROM TO | |
# set defaults | |
TO="_" | |
FROM=" " | |
while getopts "f:t:?h" opt; do | |
case $opt in | |
f) FROM=$OPTARG ;; | |
t) TO=$OPTARG ;; | |
h|*) usage ;; | |
esac | |
done | |
echo "We are replacing $FROM to ${TO}." | |
for f in * | |
do | |
new="${f//$FROM/$TO}" | |
echo "$new" | |
if [ "$new" != "$f" ]; then | |
if [ -e "$new" ]; then | |
echo "Not renaming $f because $new already exists" | |
else | |
echo "Moving $f to ${new}." | |
mv "$f" "$new" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment