Linux Basics

Link to Slides

https://bits-sos.github.io/slides/linux-basics.html

qr-code-linux-basics.png

Before We Start

Get the same setup:

git clone https://github.com/BITS-SOS/linux-basics
cd linux-basics

Some Absolute Basics

tux.png

Files

  • In Linux, everything is a file.
  • File system hierarchy: /, /bin, /lib, /usr, etc.
  • Files have ownership and access permissions.
  • Linux supports various file types like regular files, directories, symlinks, etc.

Users and Groups

  • Users are accounts that access system resources with unique usernames and IDs.
  • Groups are collections of users that share permissions and access levels.
  • Users can be assigned permissions to files and directories.
  • New users and groups can be created with useradd and groupadd respectively.
  • (IMPORTANT) Only use the root user or sudo when you absolutely know what you are doing.

Permissions

  • There are three types of permissions: read, write, and execute, which can be assigned to three different classes of users: owner, group, and others.
  • The chmod command is used to modify permissions and the chown and chgrp commands are used to change the ownership and group of a file or directory.
  • The umask command can be used to set default permissions for newly created files and directories.

History of Terminals

VT100.jpg

1960s and 1970s - Mainframes

  • Mainframes were big computers a lot of people accessed at the same time
  • People used "terminals" to access these mainframes and perform tasks on them
  • Earliest form was teletype machine (tty)
  • Video terminals came around later, but were expensive
    • They came with color output, fancy cursor movements and other features

Personal Computers

  • Terminal Emulators simulated video terminals on pesonal computers
  • First ever terminal emulator was on the PDP-11

Shells

  • User interface that lets users interact with the OS through a CLI
  • First shell was Multic's MIDAS
  • "Thompson shell" or sh came with UNIX
  • Bourne shell, C shell, Korn shell came around later
  • Linux comes with Bourne Again Shell (bash)
  • ZSH and Fish are other popular options

Navigation

navigation.jpg

pwd

pwd

pwd

pwd
-> /home/<user>/linux-basics

cd

cd dir
pwd
cd ..
pwd

cd

cd dir
pwd
-> /home/<user>/linux-basics/dir
cd ..
pwd
-> /home/<user>/linux-basics

ls

ls dir

ls

ls dir
-> file.txt

tree

tree dir

tree

tree dir
dir
└── file.txt

1 directory, 1 file

mv

mv hello.c hello2.c
ls

mv

mv hello.c hello2.c
ls
-> dir hello2.c ...

mkdir

mkdir dir/dir2
ls dir

mkdir

mkdir dir/dir2
ls dir
-> file.txt dir2

touch

touch dir/file2.txt
ls dir

touch

touch dir/file2.txt
ls dir
-> dir2 file.txt file2.txt

rm

rm dir/file2.txt
rm -r dir/dir2
ls dir

rm

rm dir/file2.txt
rm -r dir/dir2
ls dir
-> file.txt

cp

cp dir/file.txt dir/file2.txt
ls dir

cp

cp dir/file.txt dir/file2.txt
ls dir
-> file.txt file2.txt

Common Commands

bash.svg

man

man man

echo

echo "Hello, World!"

echo

echo "Hello, World!"
-> Hello, World!

cat

cat linux-basics.sh

less

less linux-basics.sh

head

head -n 1 linux-basics.sh

head

head -n 1 linux-basics.sh
-> #!/usr/bin/bash

tail

tail -n 1 linux-basics.sh

tail

tail -n 1 linux-basics.sh
-> echo $(expr $STEP + 1) > /tmp/STEP

grep

grep "getopts" *

|

cat linux-basics.sh | grep "bash"

>

echo "ok" > ok.txt

>>

echo "still ok" >> ok.txt

cut

echo "h e l l o" | cut -d" " -f2

tee

echo "hello" | tee hello.txt

chmod

chmod -x linux_basics.sh

chown

chown -R $user file/dir

chattr

sudo chattr +i hello.txt

wget

wget https://bits-sos.github.io/images/bits_sos.png

curl

curl https://wttr.in

tar

tar -xvf archive.tar

unzip

unzip zipped.zip

find

find . -name "*.sh"

fzf

fzf

sleep

sleep 3s;

pkill

pkill -SIGUSR1 <process>

killall

killall process

alias

alias vi="nvim"

function

mkcd() { mkdir $1 && cd $1; }

Branching and Looping

looping.jpg

variables

w="world"
echo "hello $w"
$? -> exit status
read var
getopts

test

test expr
[ expr ]
[ -f file ]
[ -z empty_string ]
[ "stra" != "strb" ]
[ $x -gt 4 ]
[[]] -> not really standard, but has neat features like regex matching

exit

exit 0 -> success
exit 1 -> failure

arithmetic

let "a = 2 + 6/3"
expr "2 + 6/3"
$(( 2 + 6/3 ))
${#w} -> length of str in w

if

if predicate; then
    action
elif predicate2; then
    action2
else
    action3
fi

match case

case $i in
    a)
        s1
        ;;
    b | c)
        s2
        ;;
    d)
        s3
        ;;
    *)
        s4
        ;;
esac

while loop

while pred; do
    something
done

for loop

for i in 1 2 3 4 5; do
    echo "count: $i"
done

Shell Scripting Examples

shell.jpg

Remove Prefix

#!/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

Resize Images in a Directory

#!/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."

Task

Write a script to recursively copy all the files in the demo directory to a directory called BACKUP