Ubuntu 20.04.2 LTS and Bash

Ah a new Ubuntu, what a breath of fresh air. What new here then? Not sure right now, the version that was installed from bootable usb is Ubuntu 20.04.2 LTS Focal Fossa.

On Ubuntu:

  • Print screen is shift+ prtsc

Some of the code used in this session is stored here, ssh was configured towards Github.

https://github.com/spawnmarvel/azure-arm-bash

Files

https://help.ubuntu.com/community/LinuxFilesystemTreeOverview

Main directories

  • /bin is a place for most commonly used terminal commands, like ls, mount, rm, etc.
  • /boot contains files needed to start up the system, including the Linux kernel, a RAM disk image and bootloader configuration files.
  • /dev contains all device files, which are not regular files but instead refer to various hardware devices on the system, including hard drives.
  • /etc contains system-global configuration files, which affect the system’s behavior for all users.
  • /home home sweet home, this is the place for users’ home directories.
  • /lib contains very important dynamic libraries and kernel modules
  • /media is intended as a mount point for external devices, such as hard drives or removable media (floppies, CDs, DVDs).
  • /mnt is also a place for mount points, but dedicated specifically to “temporarily mounted” devices, such as network filesystems.
  • /opt can be used to store additional software for your system, which is not handled by the package manager.
  • /proc is a virtual filesystem that provides a mechanism for kernel to send information to processes.
  • /root is the superuser’s home directory, not in /home/ to allow for booting the system even if /home/ is not available.
  • /run is a tmpfs (temporary file system) available early in the boot process where ephemeral run-time data is stored. Files under this directory are removed or truncated at the beginning of the boot process.(It deprecates various legacy locations such as /var/run, /var/lock, /lib/init/rw in otherwise non-ephemeral directory trees as well as /dev/.* and /dev/shm which are not device files.)
  • /sbin contains important administrative commands that should generally only be employed by the superuser.
  • /srv can contain data directories of services such as HTTP (/srv/www/) or FTP.
  • /sys is a virtual filesystem that can be accessed to set or obtain information about the kernel’s view of the system.
  • /tmp is a place for temporary files used by applications.
  • /usr contains the majority of user utilities and applications, and partly replicates the root directory structure, containing for instance, among others, /usr/bin/ and /usr/lib.
  • /var is dedicated to variable data, such as logs, databases, websites, and temporary spool (e-mail etc.) files that persist from one boot to the next. A notable directory it contains is /var/log where system log files are kept.

Packages and Package Management

https://help.ubuntu.com/community/InstallingSoftware

Bash Scripting Tutorial

https://linuxconfig.org/bash-scripting-tutorial-for-beginners

Create a new file called task.sh containing all the above commands, each on a separate line. Once ready, make your new file executable using chmod command with an option +x. Lastly, execute your new script by prefixing its name with ./.

file.sh # create, touch
chmod +x file # make your new file executable using
./file # to execute it

Check the bash, type echo $SHELL

The result should be /bin/bash

There are various other shell interpreters available, such as Korn shell, C shell and more. From this reason, it is a good practice to define the shell interpreter to be used explicitly to interpret the script’s content.

File names and permission

By default any newly created file are not executable before permission is set.

chmod +x filename

The file extension on GNU/Linux systems mostly does not have any meaning apart from what we see with ls for example.

Relative vs absolute path

pwd # current location
cd / # root
ls # list files
cd /home/espen/snap/ # absolute path
cd ../test/ #relative path
pwd /home/espen/test
cd ../
pwd /home/espen

Simple backup script

# Use man command to display manual page of any desired command. 
man ls

echo "Performing bck"
tar -czf /home/espen/test_bck.tag.gz /home/espen/test
echo "Backup done"
tar: Removing leading `/' from member names

# Despite the message's informative nature, it is sent to stderr descriptor. In a nutshell, the message is telling us that the absolute path has been removed thus extraction of the compressed file not overwrite any existing files. 

Variables

#!/bin/bash
greeting="Welcome"
user=$(whoami) # command substitution. Meaning that the output of the whoami command will be directly assigned to the user variable. 
day=$(date +%A)
echo "$greeting mister $user, today it is $day"
echo "Bash version: $BASH_VERSION"
a=10 # assign var
b=2
echo $a
echo $[$a + $b] # stdout
tot=$(($a + $b)) # assign var a + b
echo $tot

Simple backup so far

#!/bin/bash

#This script is used to backup a user's home dir to /tmp
echo "Starting backup"
user=$(whoami)
input=/home/$user # /test
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz

tar -czf $output $input
echo "Backup of $input completed. Details:"
ls -l $output

tar: Removing leading `/’ from member names
Despite the message’s informative nature, it is sent to stderr descriptor. In a nutshell, the message is telling us that the absolute path has been removed thus extraction of the compressed file not overwrite any existing files.

Functions

One tab right or 4 spaces

tar command

  • c – create a archive file.
  • x – extract a archive file.
  • v – show the progress of archive file.
  • f – filename of archive file.
  • t – viewing content of archive file.
  • j – filter archive through bzip2.
  • z – filter archive through gzip.
  • r – append or update files or directories to existing archive file.

Moving on…

  • find command
  • type, describe a command. For each name, indicate how it would be interpreted if used as a command name.
  • wc, print byte, word, and line counts, count the number of bytes, whitespace-separated words, and newlines in each given FILE, or standard input if none are given or for a FILE of ‘-‘.

https://ss64.com/bash/find.html

#!/bin/bash

#This script is used to backup a user's home dir to /tmp
echo "Starting backup"
user=$(whoami)
# to much data here
# input=/home/$user
# using the test folder to simulate

input=/home/$user/test
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz

# func total files in dir
function total_files {
    find $1 -type f | wc -l
}

# func total dirs
function total_directories {
    find $1 -type d | wc -l
}

tar -czf $output $input 2> /dev/null

echo -n "Files to include:"
total_files $input

echo -n "Dirs to include:"
total_directories $input

echo "Backup of $input completed. Details:"
ls -l $output

And the output:

Starting backup
Files to include:4
Dirs to include:2
Backup of /home/espen/test completed. Details:
-rw-rw-r– 1 espen espen 274 feb. 24 20:50 /tmp/espen_home_2021-02-24_205040.tar.gz

Remember, tar: Removing leading `/’ from member names
Now that we have a basic understanding of the output redirection we can eliminate this unwanted stderr message by redirecting it with 2> notation to /dev/null. Imagine /dev/null as a data sink, which discards any data redirected to it.

Side note…multiple args in function

# function multiple args
function mult_args {
    echo $1
    echo $2
}
var1="var1"
var2=2
mult_args $var1 $var2

Numeric and String Comparisons

The rest of the tutorial is at the repos…

https://github.com/spawnmarvel/azure-arm-bash/tree/master/bash-snippets