https://bits-sos.github.io/slides/linux-basics.html
Get the same setup:
git clone https://github.com/BITS-SOS/linux-basics
cd linux-basics
pwd
pwd
-> /home/<user>/linux-basics
cd dir
pwd
cd ..
pwd
cd dir
pwd
-> /home/<user>/linux-basics/dir
cd ..
pwd
-> /home/<user>/linux-basics
ls dir
ls dir
-> file.txt
tree dir
tree dir
dir
└── file.txt
1 directory, 1 file
mv hello.c hello2.c
ls
mv hello.c hello2.c
ls
-> dir hello2.c ...
mkdir dir/dir2
ls dir
mkdir dir/dir2
ls dir
-> file.txt dir2
touch dir/file2.txt
ls dir
touch dir/file2.txt
ls dir
-> dir2 file.txt file2.txt
rm dir/file2.txt
rm -r dir/dir2
ls dir
rm dir/file2.txt
rm -r dir/dir2
ls dir
-> file.txt
cp dir/file.txt dir/file2.txt
ls dir
cp dir/file.txt dir/file2.txt
ls dir
-> file.txt file2.txt
man man
echo "Hello, World!"
echo "Hello, World!"
-> Hello, World!
cat linux-basics.sh
less linux-basics.sh
head -n 1 linux-basics.sh
head -n 1 linux-basics.sh
-> #!/usr/bin/bash
tail -n 1 linux-basics.sh
tail -n 1 linux-basics.sh
-> echo $(expr $STEP + 1) > /tmp/STEP
grep "getopts" *
cat linux-basics.sh | grep "bash"
echo "ok" > ok.txt
echo "still ok" >> ok.txt
echo "h e l l o" | cut -d" " -f2
echo "hello" | tee hello.txt
chmod -x linux_basics.sh
chown -R $user file/dir
sudo chattr +i hello.txt
wget https://bits-sos.github.io/images/bits_sos.png
curl https://wttr.in
tar -xvf archive.tar
unzip zipped.zip
find . -name "*.sh"
fzf
sleep 3s;
pkill -SIGUSR1 <process>
killall process
alias vi="nvim"
mkcd() { mkdir $1 && cd $1; }
w="world"
echo "hello $w"
$? -> exit status
read var
getopts
test expr
[ expr ]
[ -f file ]
[ -z empty_string ]
[ "stra" != "strb" ]
[ $x -gt 4 ]
[[]] -> not really standard, but has neat features like regex matching
exit 0 -> success
exit 1 -> failure
let "a = 2 + 6/3"
expr "2 + 6/3"
$(( 2 + 6/3 ))
${#w} -> length of str in w
if predicate; then
action
elif predicate2; then
action2
else
action3
fi
case $i in
a)
s1
;;
b | c)
s2
;;
d)
s3
;;
*)
s4
;;
esac
while pred; do
something
done
for i in 1 2 3 4 5; do
echo "count: $i"
done
#!/bin/bash
read -p "Enter the name of the directory: " dirname
read -p "Enter the prefix to be removed: " prefix
# Check if the given directory exists
if [ -d "$dirname" ]
then
# Loop through all the files in the directory
for file in "$dirname"/*"$prefix"*
do
# Remove prefix from filename
newname=$(echo $file | sed "s/$prefix//")
mv "$file" "$newname"
echo "File $file renamed to $newname"
done
else
echo "Directory $dirname does not exist."
fi
#!/bin/bash
read -p "Enter the name of the directory: " dirname
read -p "Enter the desired width of images: " width
for file in $dirname/*.{jpg,png,jpeg}; do
if [ -f "$file" ]; then
echo "Resizing $file..."
convert "$file" -resize "$width" "$file"
fi
done
echo "All images in $dirname have been resized to $width pixels wide."
Write a script to recursively copy all the files in the demo directory to a directory called BACKUP