echo "Hello" > hello.txt
more hello.txt
echo "World" >> hello.txt
more hello.txt
gcc no_file.c
gcc no_file.c 2> gcc_errors.txt
more gcc_errors.txt
more out_and_err.py
./out_and_err.py > out 2> err
more out
more err
more simple.py
./simple.py < numbers.txt
more numbers.txt
diff - numbers.txt <<EOF
40
1
2
3
EOF
diff - numbers.txt <<< "Hello"
ls -lh | wc -l
find ~/ -size +100M | head
tee
command takes in a stream as input, and outputs that stream both to STDOUT and to the specified filepip2 install -U scipy |& tee scipy.log
more scipy.log
diff <(ls -lh .) <(ls -lh ~/CMSC331)
head -n1 part1.tsv
head -n1 part2.csv
paste <(cut -f2 part1.tsv) <(cut -f2 part2.csv -d,)
gcc no_file 2>/dev/null
rm
command a long list of directories to deleterm
may breakrm
on each of the directories in turnfind
echo 1 2 3 4 | xargs ls
ls *.html | xargs file
ls *.png | xargs -I{} convert {} {}.jpg
rm *.jpg
ls *.png > pngs
more pngs
xargs -IFILE convert FILE FILE.jpg < pngs
ls *.jpg
if
block must end with fi
then
keyword is required in bashelif
and if
if CONDITIONAL; then
#CODE
elif CONDITIONAL; then
#CODE
else
#CODE
fi
if
block must end with fi
then
keyword is required in bashelif
and if
if CONDITIONAL
then
#CODE
elif CONDITIONAL
then
#CODE
else
#CODE
fi
test
command[
command (an alias of test
)[[
syntax !
in front of itif [ 1 -eq 7 ]; then
echo "What math are you doing?"
else
echo "One is not equal to 7"
fi
if [ 1 -ne 7 ]; then
echo "One is not equal to 7"
else
echo "What math are you doing?"
fi
if [ ! 1 -eq 7 ]; then
echo "What math are you doing?"
else
echo "One is not equal to 7"
fi
a=1
b=2
if [ $a -lt $b ]; then
echo "$a is smaller than $b"
else
echo "$b is smallter than $a"
fi
string1="A string"
string2="Another string"
string3=
if [[ $string1 = $string1 ]]; then
echo "The strings are the same"
fi
if [[ -z $string3 ]]; then
echo "The string is empty"
fi
if [[ -n $string2 ]]; then
echo "The string is not empty"
fi
more a_missing_file
if [[ ! -e 'a_missing_file' ]]; then
echo "Lets make a file" > a_missing_file
fi
touch an_empty_file
if [[ -e 'an_empty_file' ]]; then
echo "An empty file exists"
fi
if [[ -s 'an_empty_file' ]]; then
echo "The file isn't empty"
fi
if [ -f . ]; then
echo "This directory isn't a file...something is messed up"
else
echo "All is right in the world"
fi
case
and end with the keyword esac
expression="This is a String"
case $expression in
0)
echo "The variable is 0"
;;
*ing)
echo "The variable ends in ing"
;;
*String)
echo "The variable ends in String"
;;
*)
echo "This is the default"
;;
esac
for EXPRESSION(S); do
# CODE_GOES_HERE
done
for variable in list; do
for x in 1 2 3; do
echo $x;
done
my_array=(1 2 3)
for y in ${my_array[@]}; do
echo $y
done
for f in *.html; do
wc -l $f
done
for f in $(ls); do
if [[ $f == *.html ]]; then
wc $f
fi
done
for (( START ; END ; CHANGE)); do
for ((x = 1; x < 4; x++)); do
echo $x
done
for ((x = 1; x < 4; x += 2)); do
echo $x
done
seq
command, which returns a list of numbers seq
command isseq START INCREASE? END
for i in $(seq 1 3); do
echo $i
done
for i in $(seq 0 2 10); do
echo $i
done
{A_LIST,OF,OPTIONS}
{START..END}
echo Lecture0{0,1,2,3,4,5}.html | xargs ls -lh | cut -f6,7,8 -d' '
for i in {0..5}; do
ls -lh Lecture0$i.html | cut -f6,7,8 -d' '
done
do
expression after the conditionwhile CONDITION; do
#CODE_HERE
done
string='Some Characters'
while [[ -n $string ]]; do
echo ${string:0:1}
string=${string:1}
done
until
loop is almost identical to the while
loop, but continues until the statement is Trueuntil
is still places at the top of the loop and checked before entering ituntil
is until CONDITIONAL; do
#CODE GOES HERE
done
string='Some Characters'
until [[ -z $string ]]; do
echo ${string:0:1}
string=${string:1}
done