Skip to content

Instantly share code, notes, and snippets.

@goooooouwa
Last active May 5, 2024 05:50
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
#!/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
# Bash Brace Expansion example. see gnu.org/software/bash/manual/html_node/Brace-Expansion.html
echo a{d,c,b}e #ade ace abe
# remove the line immediately after front matter
for filename in $TEMP/*; do
echo $filename
sed '8d' $filename >tmpfile
mv tmpfile $filename
done
# How to strip out all of the links of an HTML file in Bash or grep or batch and store them in a text file
# 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.sh
for file in *.PNG; do convert $file -resize 400 ../output/resized-$file; done
# add_prefix_to_filenames.sh
# stackoverflow.com/questions/208181/how-to-rename-with-prefix-suffix
for i in a b; do mv {,PREFIX_}$i; done
# changing all occurrences 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" {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment