Skip to content

Instantly share code, notes, and snippets.

@goooooouwa
Last active July 21, 2025 04:08
Show Gist options
  • Save goooooouwa/587b2fae96e7d8593873ec59a603483e to your computer and use it in GitHub Desktop.
Save goooooouwa/587b2fae96e7d8593873ec59a603483e to your computer and use it in GitHub Desktop.
Common shell commands #bash #find #sed
#!/bin/bash
# bash for loop
for i in $(seq 10 $END); do
echo $i
done
# 1.Iterate over collection
for i in a b c; do
echo $i
done
# 2.Iterate over numeric range
for i in {1..5}; do
echo $i
done
# 3.Iterate over files in directory
for filename in *; do
echo $filename
done
# move files from folder to folder
for d in */; do
echo "$d"
mkdir -p ../_notes/$d
mv $d/* ../_notes/$d
done
# rename file with folder name
for d in */; do
echo "$d"
a=$(ls "$d"/filename.*)
echo $a
b=$(echo $a | sed s://filename::)
echo $b
mv "$a" "$b"
done
# Bash Brace Expansion example. see gnu.org/software/bash/manual/html_node/Brace-Expansion.html
echo a{d,c,b}e #ade ace abe
# For loop to exeute some task multiple times
for i in Item1 Item2 ItemN; do
bash ~/task.sh ${i}
done
for i in {1..10}
do
wget https://static.generated.photos/vue-static/human-generator/poses/female/00$i.png
done
# Remove the line immediately after front matter
for filename in $TEMP/*; do
echo $filename
sed '8d' $filename >tmpfile
mv tmpfile $filename
done
# Strip out all of the links of an HTML file in Bash or grep or batch and store them in a text file
# ref: https://stackoverflow.com/questions/21264626/how-to-strip-out-all-of-the-links-of-an-html-file-in-bash-or-grep-or-batch-and-s
sed -n 's/.*href="\([^"]*\).*/\1/p' file
# Batch process images
for file in *.PNG; do convert $file -resize 400 ../output/resized-$file; done
# Add prefix to filenames
# ref: stackoverflow.com/questions/208181/how-to-rename-with-prefix-suffix
for i in a b; do mv {,PREFIX_}$i; done
# Change all occurrences of word in file content in a folder
# sed beginner: changing all occurrences in a folder. see: stackoverflow.com/questions/905144/sed-beginner-changing-all-occurrences-in-a-folder
find . -type f -exec sed -i "s/foo/bar/g" {} \;
# Rename all files of the same name to another name in all subdirectories
find . -name output.txt -type f -execdir mv -T output.txt output_modified.txt ';'
# Batch rename files in directory with date modified timestamp as prefix of the filename
# ref: https://unix.stackexchange.com/questions/43338/renaming-a-bunch-of-files-with-date-modified-timestamp-at-the-end-of-the-filenam
for f in *; do mv -- "$f" "$(date +%Y-%m-%d -r $f)-$f"; done
# Use git conbined with sed/awk to automate text processing tasks
eval "find app/ -type f -exec sed -i 's/target/replacement/g' {} \;"
git diff
echo -n "Here be dragons. Continue?"
read REPLY
if [[ "$REPLY" =~ ^[Yy]$ ]]
then
git add .
git cm "processing $1 complete"
fi
# add namespace to all models
find app/models/ -type f -exec sed -i "1 i module MyModule" {} \;
find app/models/ -type f -exec sed -i "$ a end" {} \;
# you can also automate code indentation, see https://gist.github.com/goooooouwa/d5f40319ab7fb80a7fe4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment