Skip to content

Instantly share code, notes, and snippets.

@ctokheim
Last active January 11, 2016 22:01
Show Gist options
  • Save ctokheim/5198566 to your computer and use it in GitHub Desktop.
Save ctokheim/5198566 to your computer and use it in GitHub Desktop.
Bash/Zsh (shell): Sh scripting tricks
# run command on certain files in a directory
for file in ./myDir/*.abc
do
cmd [options] $file > output.txt
done
# run command using integer ranges
for i in {1..5}
do
echo "Welcome $i times"
done
# find files matching a pattern (escape *)
find . -name foo\*
# total line count for python source files
wc -l `find . -name '*.py'`
# change shell
chsh -s /bin/zsh
# get basename of file
basename myfile.ext .ext
# make a list variable
MYLIST=('hello' 'bye' 'awesome')
echo $MYLIST[1]
# to iterate over a list variable
for elem in "${MYLIST[@]}"
do
# do whatever on $elem
done
# embed commands using back ticks
echo `basename myfile.ext .ext`
# replace \r's from windows to \n
cat filename.txt | tr '\r' '\n' > newfilename.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment