Skip to content

Instantly share code, notes, and snippets.

@2jiwon
Created March 6, 2018 17:02
Show Gist options
  • Save 2jiwon/00c9481745ad8e4a17eb187ecd2d12bd to your computer and use it in GitHub Desktop.
Save 2jiwon/00c9481745ad8e4a17eb187ecd2d12bd to your computer and use it in GitHub Desktop.
A Bash Shell Script to replace a string in files of a directory recursively.
#!/bin/bash
Files=(`grep -r "\.sgk" ./ | cut -d: -f1 | uniq`)
for file in "${Files[@]}"
do
echo "Filename: $file"
grep "\.sgk" "$file" | more
done
echo ""
answer=N
echo -n "Is this result right? Do you want to change the string of the files? (y/N)"
read answer
if [[ "$answer" == [Yy] ]]
then
for file in "${Files[@]}"
¦ do
¦ ¦ sed -i "s:\.sgk:\.php:g" "$file"
¦ done
¦ if [[ $? == "0" ]]; then
¦ ¦ echo -e "Done.\n"
¦ fi
else
echo -e "Okay,bye...\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment