Skip to content

Instantly share code, notes, and snippets.

@mir4a
Created July 2, 2019 09:51
Show Gist options
  • Save mir4a/20d2e4ef845fadb69f9f4a2e75b97d83 to your computer and use it in GitHub Desktop.
Save mir4a/20d2e4ef845fadb69f9f4a2e75b97d83 to your computer and use it in GitHub Desktop.
#!/bin/sh
while [ "$#" -gt 0 ]; do
case "$1" in
-s) substr="$2"; shift 2;;
-r) replace="$2"; shift 2;;
-p) filepath="$2"; shift 2;;
--substr=*) substr="${1#*=}"; shift 1;;
--replace=*) replace="${1#*=}"; shift 1;;
--filepath=*) filepath="${1#*=}"; shift 1;;
--substr|--replace|--filepath) echo "$1 requires an argument" >&2; exit 1;;
-*) echo "unknown option: $1" >&2; exit 1;;
*) handle_argument "$1"; shift 1;;
esac
done
if [ "$filepath" = "" ]; then
echo "no --filepath option"
exit 2;
fi
if [ "$substr" = "" ]; then
echo "no --substr option"
exit 2;
fi
cd "$filepath" || exit
files=$(ls -- *"$substr"*)
if [ "$files" = "" ]; then
echo "no files have been found"
exit 2;
fi
moveFiles() {
for f in $files;do
tmp=$(echo "$f" | sed -e s/"$substr"/"$replace"/g)
mv -v "$f" "$tmp"
done
}
# Ask for prompt
echo "Are you sure you want to move these files? (Y/n)"
echo "files to be moved:"
echo "------------------"
echo "$files"
echo "------------------"
echo "(Y/n)?"
read -r are_you_sure
if [ "$are_you_sure" = "n" ]; then
exit 0;
fi
if [ "$are_you_sure" = "Y" ]; then
moveFiles
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment