Reading Material
Getting Started with Linux & Shells Lesson 1 of 14
In Progress

Introduction to Linux Command Shells

Linux is an operating system, like macOS or Windows. It is also the most popular Open Source and free, as in freedom, operating system. It powers the vast majority of the servers that compose the Internet. It's the base upon which everything is built, not just that, Android is based on a modified version of Linux.

The Linux "core" (called the kernel) was born in 1991 in Finland, and it went a really long way from its humble beginnings. It went on to be the kernel of the GNU Operating System, creating the duo GNU/Linux.

In this chapter, we will take a look at what Linux Shell is and take a look at useful Linux Commands:

A Linux shell is a command-line interface for interacting with the operating system. 

It allows users to execute commands, manipulate files, and run scripts by typing commands into the terminal.

The shell acts as an intermediary between the user and the operating system's kernel, allowing users to interact with the system in a more human-readable way. The shell takes user commands as input, interprets them, and passes them to the kernel for execution.

There are several different shells available for Linux, including the Bourne shell (sh), the C shell (csh), the Korn shell (ksh), and the most widely used, the Bourne Again shell (bash).

In bash, the user can navigate the file system, execute programs, manage processes, and automate tasks using commands and scripting. Bash provides various built-in commands, as well as the ability to execute external commands and scripts.


Reading Material
Getting Started with Linux & Shells Lesson 2 of 14
In Progress

Linux Basics

Linux is a popular operating system for ethical hacking due to its open-source nature, which allows for customization and flexibility. It also offers a wide range of tools for various hacking tasks, such as network scanning, vulnerability assessment, and penetration testing.

Linux is also very stable and generally affords very high performance to the end user. However, it can be more difficult for beginners and does not have as many hardware drivers as Windows

Since Linux is free and open-source, the source code can be modified and distributed commercially or non-commercially by anyone. Linux-based operating systems run on servers, mainframes, desktops, embedded systems such as routers, televisions, video game consoles, and more. 

The overall Android operating system that runs on smartphones and tablets is based on the Linux kernel, and because of this, Linux is the most widely installed operating system. Linux is an operating system like WindowsiOSAndroid, or macOS

Some popular Linux distributions for ethical hacking include Kali Linux, which is specifically designed for penetration testing, and Ubuntu, which is a general-purpose distribution that can be customized for hacking tasks.

Features of Linux

Linux is a free and open-source operating system that has a number of features that make it popular among users, including:

Multi-user support: 

Linux allows multiple users to access and use the system at the same time, and each user can have their own settings and configurations.

Multi-tasking: 

Linux supports multi-tasking, meaning that multiple programs can run simultaneously without interfering with each other.

Command-line interface

Linux offers a powerful command-line interface, which allows users to perform various tasks and access the system's full functionality.

File permissions: 

Linux has a robust file permission system that allows users to control access to files and directories. This can be used to protect sensitive information and ensure that only authorized users can access it.

Customizability

Linux is highly customizable, allowing users to tailor the system to their specific needs. This includes the ability to modify the source code and create a custom distribution. 

Stability and reliability

Linux is known for its stability and reliability, with many users reporting that their systems run for months or even years without needing to be rebooted.

Large software repository

Linux has a wide range of software that is available through repositories, which makes it easy to find and install new software.

Networking support

Linux has built-in support for various network protocols, making it easy to connect to other devices and networks.

Security

Linux has a number of security features that make it a popular choice for servers and other critical systems, including the ability to assign granular permissions and access control, a built-in firewall, and the ability to run applications in a sandboxed environment.

Reading Material
Getting Started with Linux & Shells Lesson 3 of 14
In Progress

File Structure inside of Linux

On a Linux system, almost everything is a file, and if it is not a file, then it is a process. Here is a general diagram for file structure in Linux.

There are certain exceptions in a Linux file system:

  • DirectoriesFiles that are lists of other fles.
  • Special file—The mechanism used for inout and output. /dev are special fles.
  • Links—A system to make fle or directory visible in multiple parts of the systems.
  • Sockets—A special fle type, similar to TCP/IP sockets providing inter-process networking.
  • Pipes—More or less like sockets; they form a way for process to communicate with each other with out using network socket.
File types in a long list:

Symbol

Meaning

-

Regular fle

d

Directory

l

Link

c

Special fle

s

Socket

p

Named pipe

b

Block device

 Subdirectories of the root directory:

In Linux, the file system is organized in a hierarchical structure, with the root directory (/) being the top-level directory. All other files and directories are contained within the root directory.

Directory

Content

/bin

Common programs, shared by the system, the system administrator, and the users.

/boot

The startup fles and the kernel, vmlinuz. In some recent distributions also grub data. Grub is the GRand Unifed Boot loader and is an attempt to get rid of the many different boot-loaders we know today.

/dev

Contains references to all the CPU peripheral hardware, which are represented as files with special properties.

/etc

Most important system confguration fles are in/etc., this directory contains data similar to those in the Control Panel in Windows

/home

Home directories of the common users.

/initrd

(on some distributions) Information for booting. Do not remove!

/lib

Library fles, includes fles for all kinds of programs needed by the system and the users.

/lost+found

Every partition has a lost+found in its upper directory. Files that were saved during failures are here.

/misc

For miscellaneous purposes.

/mnt

Standard mount point for external file systems, for example, a CD-ROM or a digital camera.

/net

Standard mount point for entire remote file systems.

/opt

Typically contains extra and third-party software.

/proc

A virtual file system containing information about system resources. More information about the meaning of the files in proc is obtained by entering the command man proc in a terminal window. The file proc.txt discusses the virtual file system in detail.

/root

The administrative user’s home directory. Mind the difference between /, the root directory and /root, the home directory of the root user.

/sbin

Programs for use by the system and the system administrator.

/tmp

Temporary space for use by the system, cleaned upon reboot, so don’t use this for saving any work!

/usr

Programs, libraries, documentation, etc., for all user-related programs.

/var

Storage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.

Reading Material
File & Directory Management Lesson 4 of 14
In Progress

File Management Linux Commands

File management in Linux is accomplished through the use of various commands. 

Here are some common Linux commands used for file management:

ls

The ls (list) command is used to list the contents of a directory in Linux. The basic syntax for ls is as follows:

ls[OPTIONS] [DIRECTORY]
username@Technoscience:~$ls    # This command lists the contents of the current directory
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
username@Technoscience:~$
username@Technoscience:~$ls /bin/       #List the contents of the directory of /bin
archdetect              hostname               ping4autopartition           hw-detect              ping6
autopartition-crypto    in-target              plymouthautopartition-loop      ip                     preseed_command
autopartition-lvm       journalctl             psbash                    kbd_mode               pwd
block-attr              kill                   rbashblockdev-keygen         kmod                   readlink
blockdev-wipe           less                   redbtrfs                   lessecho               register-module
btrfsck                 lessfile               rmbtrfs-debug-tree        lesskey                rmdir
btrfs-find-root         lesspipe               rnanobtrfs-image             list-devices           run-parts
btrfs-map-logical       ln                     search-pathbtrfs-select-super      loadkeys               sed
btrfstune               login                  select_mountoptionsbtrfs-zero-log          loginctl               select_mountpoint
bunzip2                 log-output             setfaclbusybox                 lowntfs-3g             setfont
bzcat                   ls                     setupconbzcmp                   lsblk                  sh
bzdiff                  lsmod                  sh.distribbzegrep                 mapdevfs               sleep
bzexe                   mkdir                  ssbzfgrep                 mkfs.btrfs             static-sh
bzgrep                  mknod                  sttybzip2                   mktemp                 su
bzip2recover            more                   syncbzless                  mount                  sysfs-update-devnames
bzmore                  mountpoint             systemctlcat                     mt                     systemd
chacl                   mt-gnu                 systemd-ask-passwordcheck-missing-firmware  mv                     systemd-escape
chgrp                   nano                   systemd-hwdbchmod                   nc                     systemd-inhibit
chown                   nc.openbsd             systemd-machine-id-setupchvt                    netcat                 systemd-notify
cp                      netstat                systemd-sysuserscpio                    networkctl             systemd-tmpfiles
dash                    nisdomainname          systemd-tty-ask-password-agentdate                    ntfs-3g                tar
dd                      ntfs-3g.probe          tempfiledebconf-get             ntfscat                touch
debconf-set             ntfscluster            truedf                      ntfscmp                udevadm
dmesg                   ntfsfix                umountdir                     ntfsfallocate          ulockmgr_server
dnsdomainname           ntfsinfo               unamedomainname              ntfsls                 uncompress
dumpkeys                ntfsmove               unicode_startecho                    ntfsrecover            update-dev
ed                      ntfssecaudit           user-paramsegrep                   ntfstruncate           vdir
false                   ntfsusermap            wdctlfetch-url               ntfswipe               which
fgconsole               open                   whiptailfgrep                   openvt                 ypdomainname
findmnt                 parted_devices         zcatfsck.btrfs              parted_server          zcmp
fuser                   partman                zdifffusermount              partman-command        zegrep
getfacl                 partman-commit         zfgrepget_mountoptions        partmap                zforce
grep                    perform_recipe         zgrepgunzip                  perform_recipe_by_lvm  zless
gzexe                   pidof                  zmoregzip                    ping                   znew
username@Technoscience:~$
username@Technoscience:~$ ls -al /bin/
total 15624
drwxr-xr-x  2 root root    4096 Mar 12  2020 .
drwxr-xr-x 23 root root    4096 Feb  1 18:11 ..
-rwxr-xr-x  1 root root    6216 Mar 23  2019 archdetect
-rwxr-xr-x  1 root root    1091 Feb 23  2018 autopartition
-rwxr-xr-x  1 root root    1866 Nov  9  2017 autopartition-crypto
-rwxr-xr-x  1 root root    8684 Feb  2  2012 autopartition-loop
-rwxr-xr-x  1 root root     139 Aug 12  2014 autopartition-lvm
-rwxr-xr-x  1 root root 1113504 Jun  7  2019 bash
-rwxr-xr-x  1 root root     495 Dec 30  2017 block-attr
-rwxr-xr-x  1 root root    4683 Dec 23  2015 blockdev-keygen
-rwxr-xr-x  1 root root   10104 Feb  3  2020 blockdev-wipe
-rwxr-xr-x  1 root root  716464 Mar 13  2018 btrfs
lrwxrwxrwx  1 root root       5 Feb  1 18:01 btrfsck -> btrfs
-rwxr-xr-x  1 root root  375952 Mar 13  2018 btrfs-debug-tree
-rwxr-xr-x  1 root root  371856 Mar 13  2018 btrfs-find-root
-rwxr-xr-x  1 root root  396432 Mar 13  2018 btrfs-image
-rwxr-xr-x  1 root root  375952 Mar 13  2018 btrfs-map-logical
<SNIP>
cd[DIRECTORY]
username@Technoscience:~$ mkdir newdir
username@Technoscience:~$ cd newdir/
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$ cd ..   # Back to the home directory
username@Technoscience:~$
username@Technoscience:~$ mkdir newdir
username@Technoscience:~$ mkdir newdir1
username@Technoscience:~$ cd newdir
username@Technoscience:~/newdir$ cd ../newdir1
username@Technoscience:~/newdir1$
username@Technoscience:~/newdir1$ cd /etc
username@Technoscience:/etc$
cp[OPTIONS] SOURCE DESTINATION
username@Technoscience:~$ touch file.txt
username@Technoscience:~$ cp file.txt newfile.txt     # Copy the file "file.txt" to new file named "newfile.txt"
username@Technoscience:~$
username@Technoscience:~$ mkdir dir
username@Technoscience:~$ cp -r dir newdir       # Recursively copies the directory "dir" to new directory named "newdir"
username@Technoscience:~$
mv[OPTIONS] SOURCE DESTINATION
username@Technoscience:~$ touch file.txt
username@Technoscience:~$ mv file.txt newfile.txt       # Rename the file "file.txt" to "newfile.txt"
username@Technoscience:~$ 
username@Technoscience:~$ mkdir dir newdir
username@Technoscience:~$ mv dir newdir   #Move the directory "dir" to a new location "newdir"
username@Technoscience:~$
username@Technoscience:~$ touch file1.txt
username@Technoscience:~$ touch file2.txt
username@Technoscience:~$ mkdir dir
username@Technoscience:~$ mv file1.txt file2.txt dir   # Move multiple files "file1.txt" and "file2.txt" to the directory "dir"
username@Technoscience:~$
rm[OPTIONS] [FILE]
username@Technoscience:~$ touch file.txt
username@Technoscience:~$ rm file.txt       # Deletes the file "file.txt"
username@Technoscience:~$
username@Technoscience:~$ mkdir dir
username@Technoscience:~$ rm -r dir       # Recursively deletes the directory dir and its contents
username@Technoscience:~$
mkdir[OPTIONS] DIRECTORY
username@Technoscience:~$ mkdir newdir   # Create a new directory named "newdir"
username@Technoscience:~$
username@Technoscience:~$ mkdir dir1 dir2
username@Technoscience:~$
username@Technoscience:~$ mkdir -p a/b/c 
username@Technoscience:~$
rmdir[OPTIONS] DIRECTORY
username@Technoscience:~$ mkdir emptydir    # Create a directory named "emptydir"
username@Technoscience:~$ rmdir emptydir    # Remove the empty directory "emptydir"
username@Technoscience:~$
username@Technoscience:~$ mkdir dir1 dir2
username@Technoscience:~$ rmdir dir1 dir2
username@Technoscience:~$
username@Technoscience:~$ rmdir -rf newdir
username@Technoscience:~$
touch[OPTIONS] FILE
username@Technoscience:~$ touch newfile.txt
username@Technoscience:~$
username@Technoscience:~$ touch -a file.txt
username@Technoscience:~$ 
chmod[OPTIONS] MODE FILE
username@Technoscience:~/newdir$ touch newfile.txt
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 01:49 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rw-rw-r--  1 username username    0 Feb  2 01:49 newfile.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newfile$ chmod a+x newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwxr-x  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$ 
username@Technoscience:~/newfile$ chmod a+rw newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwxrwx  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$
username@Technoscience:~/newfile$ chmod o-rwx newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwx---  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$
username@Technoscience:~/newfile$ chmod og-r newfile.txt
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:11 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwx-wx---  1 username username    0 Feb  2 02:11 newfile.txt
username@Technoscience:~/newfile$
username@Technoscience:~/newfile$ touch file.sh
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:26 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rw-rw-r--  1 username username    0 Feb  2 02:26 file.sh
username@Technoscience:~/newfile$ chmod 777 file.sh
username@Technoscience:~/newfile$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 02:26 .
drwxr-xr-x 19 username username 4096 Feb  2 01:46 ..
-rwxrwxrwx  1 username username    0 Feb  2 02:26 file.sh
username@Technoscience:~/newfile$
chown[OPTIONS] OWNER[:GROUP] FILE
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 03:39 .
drwxr-xr-x 18 username username 4096 Feb  2 03:39 ..
-rw-r--r--  1root     root       0 Feb  2 03:39 testfile.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$sudo chown username testfile.txt     # Change the Owner of the file "testfile.txt" to user "username"
[sudo] password for username:
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 03:39 .
drwxr-xr-x 18 username username 4096 Feb  2 03:39 ..
-rw-r--r--  1 username root        0 Feb  2 03:39 testfile.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$ sudo chown username:username testfile.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 03:39 .
drwxr-xr-x 18 username username 4096 Feb  2 03:39 ..
-rw-r--r--  1username root       0 Feb  2 03:39 testfile.txt
username@Technoscience:~/newdir$
username@Technoscience:~$ chown -R username:group newdir
username@Technoscience:~$ 
username@Technoscience:~$ chgrp groupname newdir
username@Technoscience:~$ 
ln[OPTIONS] TARGET NEW_LINK
username@Technoscience:~/newdir$ touch file.txt
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:13 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  1 username username    0 Feb  2 11:13 file.txt
username@Technoscience:~/newdir$ln file.txt link_file.txt    # Create a Hard link named "link_file.txt" that points to the original file "file.txt"
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:16 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  2 username username    0 Feb  2 11:13 file.txt
-rw-rw-r--  2 username username    0 Feb  2 11:13 link_file.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$ touch file.txt
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:26 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  1 username username    0 Feb  2 11:26 file.txt
username@Technoscience:~/newdir$ ln -s file.txt sym_link_file.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$ ls -al
total 8
drwxrwxr-x  2 username username 4096 Feb  2 11:26 .
drwxr-xr-x 18 username username 4096 Feb  2 11:11 ..
-rw-rw-r--  1 username username    0 Feb  2 11:26 file.txt
lrwxrwxrwx  1 username username    8 Feb  2 11:26sym_link_file.txt-> file.txt
username@Technoscience:~/newdir$
username@Technoscience:~/newdir$ rm file.txt
username@Technoscience:~/newdir$ cat sym_link_file.txt
cat: sym_link_file.txt: No such file or directory
username@Technoscience:~/newdir$
find[OPTIONS] [LOCATION] [EXPRESSION]
username@Technoscience:~$ find . -name '*.js'
username@Technoscience:~$ find . -type d -name src
username@Technoscience:~$ find folder1 folder2 -name filename.txt
username@Technoscience:~$ find . -type d -name node_modules -or -name public
username@Technoscience:~$ find . -type d -name '*.md' -not -path 'node_modules'
username@Technoscience:~$ find . -type f -size +100c
username@Technoscience:~$ find . -type f -size +100k -size -1M
username@Technoscience:~$ find . -type f -mtime +3
username@Technoscience:~$ find . -type f -mtime -1
username@Technoscience:~$ find . -type f -mtime -1 -delete
username@Technoscience:~$ find . -type f -exec cat {} \;
locate[OPTIONS] [PATTERN]
username@Technoscience:~$locate file.txt    # Finds all files and directories named file "file.txt"
username@Technoscience:~$locate -i file.txt
username@Technoscience:~$ locate-cfile.txt
which[COMMAND]
username@Technoscience:~$which ls    # Displays the full path to the ls executable file on the system
/bin/ls
username@Technoscience:~$
whereis [OPTIONS] filename
username@Technoscience:~$ whereis gunzip
gunzip: /bin/gunzip /usr/share/man/man1/gunzip.1.gz
username@Technoscience:~$
username@Technoscience:~$ whereis -b gunzip
gunzip: /bin/gunzip
username@Technoscience:~$

Where '[OPTIONS]' are optional flags that modify the behavior of the 'ls' command and '[DIRECTORY]' is the name of the directory to be listed (the current directory by default).

Here are some common options for "ls":

  • -a: List all files, including hidden files and directories (those starting with a dot .).
  • -l: Display the contents in a long format, showing detailed information about each file or directory.
  • -h: Display file sizes in human-readable format (e.g. KB, MB, GB, etc.).
  • -r: Reverse the order of the listing.
  • -t: Sort the contents by modification time (newest first).

Example usage:

Inside a folder you can list all the files that the folder contains using the ls command:

If you add a folder name or path, it will print that folder's contents:

This command lists the contents of the directory /bin/ in a long format with human-readable file sizes.

"ls" accepts a lot of options. One of my favorite options combinations is -al. Try it:

compared to the plain ls, this returns much more information. 

You have, from left to right:

  • the file permissions (and if your system supports ACLs, you get an ACL flag as well)
  • the number of links to that file
  • the owner of the file
  • the group of the file
  • the file size in bytes
  • the file modified datetime
  • the file name

This set of data is generated by the l option. The “a” option instead also shows the hidden files. 

Note: Hidden files are files that start with a dot ( . )

cd

The cd (change directory) command is used to change the current working directory in Linux. The basic syntax for cd is as follows:

Where [DIRECTORY] is the name of the directory to change to.

Here are some special directory references that can be used with cd:
  • ".": The current directory.
  • "..": The parent directory of the current directory.
  • "~": The home directory of the current user.

Example usage:

Once you have a folder, you can move into it using the cd command. “cd” means to change the directory. You invoke it by specifying a folder to move into. You can specify a folder name or an entire path.

Now you are in the "newdir" folder.

You can use the ".." special path to indicate the parent folder:

The # character indicates the start of the comment, which lasts for the entire line after it's found.

You can use it to form a path:

There is another special path indicator which is “.”, and indicates the current folder. You can also use absolute paths, which start from the root folder “/”:

This command changes the current working.

cp

The cp (copy) command is used to copy files and directories in Linux. The basic syntax for cp is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the cp command, SOURCE is the file or directory to be copied, and DESTINATION is the location to copy the SOURCE to.

Here are some standard options for "cp":

  • -a or --archive: Copy the contents of a directory recursively and preserve the original file attributes (such as permissions, timestamps, etc.).
  • -r or -R: Recursively copy a directory and its contents.
  • -v: Verbose, display progress, and other information during the copy operation.
  • -i: Interactive, prompt for confirmation before overwriting an existing file.

Example usage:

You can copy a file using the cp command:

To copy folders you need to add the -r option to recursively copy the whole folder contents:

mv

The mv (move) command is used to move or rename files and directories in Linux. The basic syntax for mv is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the mv command, SOURCE is the file or directory to be moved, and DESTINATION is the location to move the SOURCE to or the new name for the SOURCE if it is being renamed.

Here are some standard options for mv:

  • -v: Verbose, display progress, and other information during the move operation.
  • -i: Interactive, prompt for confirmation before overwriting an existing file.
  • -f: Force move, overwrite existing files without confirmation.

Example usage:

Once you have a file, you can move it around using the "mv" command. You specify the file's current path and its new path:

This is how you rename files and folders. If you have two directories, you want to move, use the following command:

If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files and they will all be moved to the folder path identified by the last parameter:

rm

The rm (remove) command is used to delete files or directories in Linux. The basic syntax for rm is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the rm command and [FILE] is the name of the file or directory to be deleted.

Here are some common options for rm:

  • -f: Force deletion, ignoring any prompts or errors.
  • -r or -R: Recursively delete a directory and its contents.
  • -v: Verbose, display progress and other information during deletion.
  • --interactive: Prompt for confirmation before deleting each file.

It is important to use "rm" with caution, as once a file or directory is deleted, it cannot be recovered without a backup or data recovery tool.

Example usage:

Once you have a file, you can remove it using the "rm" command:

You can also remove a directory using the "rm" command:

mkdir

The mkdir (make directory) command is used to create new directories in Linux. The basic syntax for mkdir is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the mkdir command and DIRECTORY is the name of the directory to be created.

Here are some common options for mkdir:
  • -p: Create intermediate directories as necessary.
  • -v: Verbose, and display progress and other information during the directory creation.

Example usage:

You create folders using the "mkdircommand:

You can create multiple folders with one command:

You can also create multiple nested folders by adding the-poption:

This command creates a directory structure a/b/c with intermediate directories "a" and "b" if they do not already exist.

Options in UNIX commands commonly take this form. You add them right after the command name, and they change how the command behaves. You can often combine multiple options, too.

You can find which options a command supports by typingman <command-name>. Try now withman mkdir for example (press theqkey to esc the man page). Man pages are the amazing built-in help for UNIX.


rmdir

The rmdir (remove directory) command is used to remove empty directories in Linux. The basic syntax for rmdir is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the rmdir command and DIRECTORY is the name of the directory to be removed.

Here are some common options for rmdir:
  • -v: Verbose, display progress, and other information during the directory removal.

Example usage:

Just as you can create a folder usingmkdir, you can delete a folder usingrmdir:

You can also delete multiple folders at once:

Note: The folder you delete must be empty

To delete folders with files in them, we'll use the more generic "rmcommand which deletes files and folders, using the "-rfoptions:

Be careful as this command does not ask for confirmation and it will immediately remove anything
you ask it to remove. 

There is nobinwhen removing files from the command line, and recovering lost files can be hard.

Note: Directories that contain files cannot be removed with "rmdir". To remove a non-empty directory, use the "rm" command with the "-r" option to remove the directory and its contents recursively.

touch

The touch command is used to create new empty files or update the modification timestamps of existing files in Linux. The basic syntax for touch is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the touch command and FILE is the name of the file to be created or updated.

Here are some common options for touch:

  • -a: Only update the access time of the file.
  • -m: Only update the modification time of the file.
  • -c: Do not create a file if it does not exist (no-create).

Example usage:

You can create an empty file using the touch command:

This command creates a new empty file named "newfile.txt" if it does not already exist, or updates its modification timestamp if it exists.

This command updates the access timestamp of the file "file.txt" without modifying its modification timestamp.

chmod

The chmod (change mode) command is used to change the permissions of files and directories in Linux. The basic syntax for chmod is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the chmod command, MODE is the new permission set for the file or directory, and FILE is the name of the file or directory whose permissions will be changed.

The permissions on a file or directory in Linux can be represented as a three-digit octal number, where each digit represents the permissions for the owner, group, and others, respectively. Each digit is a combination of read (r), write (w), and execute (x) permissions. 

For example, rwx means to read, write, and execute permissions, while r-x means to read and execute but not write permission.

The weird strings you see on each file line, likedrwxr-xr-x, define the permissions of the file or folder. 

Let's dissect it. 

The first letter indicates the type of file:

  • "-" means it's a normal file
  • "d" means it's a directory
  • "l" means it's a link

Then you have 3 sets of values:

  • The first set represents the permissions of the owner of the file
  • The second set represents the permissions of the members of the group the file is associated to
  • The third set represents the permissions of everyone else
Those sets are composed of 3 values. rwx means that a specific persona has read, write, and execution access.

Anything that is removed is swapped with a "-", which lets you form various combinations of values and relative permissions: rw-, r--, r-x, and so on.

You can change the permissions given to a file using the chmod command. chmod can be used in 2 ways. The first uses symbolic arguments, and the second is using numeric arguments. Let's start with symbols first, which is more intuitive. 

You type chmod followed by a space and a letter:
  • "a" stands for all
  • "u" stands for user
  • "g" stands for the group
  • "o" stands for others

Then you type either '+' or '-' to add permission or to remove it. Then you enter one or more permissions symbols ( r, w, x ). All are followed by the file or folder name. 

Example Usage:

We have a file named "newfile.txt" which has read and write permission for users and groups and only readable permission for others.

To add executable permission for everyone use the following command:

To add read and write to everyone use the following command:

To remove read, write, and Executable permissions from other permissions use the following command:

You can apply the same permissions to multiple permissions by adding multiple letters before the+/-:

In case you are editing a folder, you can apply the permissions to every file contained in that folder using the -r (recursive) flag.

Numeric arguments are faster but I find them hard to remember when you are not using them day to day. You use a digit that represents the permissions of the persona. This number value can be a maximum of 7, and it's calculated in this way:

  • 1 if has execution permission
  • 2 if has to write permission
  • 4 if has read permission
This gives us 4 combinations:
  • 0 no permissions
  • 1 can execute
  • 2 can write
  • 3 can write, execute
  • 4 can read
  • 5 can read, execute
  • 6 can read, write
  • 7 can read, write and execute

We use them in pairs of 3, to set the permissions of all the 3 groups altogether:

This command adds execute permission for all users for the file "file.sh".

chown

The chown (change owner) command is used to change the owner and/or group of files and directories in Linux. The basic syntax for chown is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the chown command, OWNER is the username or numeric user ID of the new owner of the file or directory, GROUP is the name or numeric group ID of the new group for the file or directory, and FILE is the name of the file or directory whose owner and/or group will be changed.

Here are some standard options for chown:

  • -R: Recursively change the owner and/or group of files and directories in a directory and its subdirectories.

Example Usage: 

If you have a file that's owned by root, you can't write to it as another user:

You can use chown to transfer the ownership to you:

Files/directories don't just have an owner, they also have a group. Through this command, you can change that simultaneously while you change the owner:

This command changes both the owner and group of the file "testfile.txt" to user and group respectively.

It's rather common to have the need to change the ownership of a directory, and recursively all the files contained, plus all the subdirectories and the files contained in them, too. 

You can do so using the -R flag:

You can also just change the group of a file using the chgrp command:

ln

The ln (link) command is used to create hard links or symbolic links in Linux. The basic syntax for creating a hard link is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the ln command, TARGET is the name of the original file or directory, and NEW_LINK is the name of the new hard link to be created.

Here are some standard options for ln:

  • -s: Create a symbolic link instead of a hard link.

The ln command is part of the Linux file system commands.

It's used to create links. 

What is a link?

It's like a pointer to another file. A file that points to another file. You might be familiar with Windows shortcuts. 

We have 2 types of links: 

  1. Hard links
  2. Soft links

1. Hard links

Hard links are rarely used. They have a few limitations: you can't link to directories, and you can't
link to external filesystems (disks).

A hard link is created using the following command:

Now any time you edit any of those files, the content will be updated for both. 

If you delete the original file, the link will still contain the original file content, as that's not removed until there is one hard link pointing to it.

2. Soft links

Soft links are different. They are more powerful as you can link to other filesystems and to directories, but when the original is removed, the link will be broken. You create soft links using the-soption of "ln" :

Example Usage:

Say you have a file called file.txt. You can create a soft link to it using the following command:

In this case, you can see there's a special “l” flag when you list the file using ls -al, and the file name has a @ at the end, and it's colored differently if you have colors enabled:

Now if you delete the original file, the links will be broken, and the shell will tell you "No such file or
directory
" if you try to access it:

find

The "find" command is used to search for files and directories in a specified location. The basic syntax for the "find" command is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the find command, [LOCATION] is the starting directory for the search, and [EXPRESSION] is a set of conditions that specify the files and directories to be found.

Here are some standard options for "find":

  • -name: Search for files and directories with a specific name.
  • -type: Search for files and directories of a specific type (e.g. f for files, d for directories).
  • -mtime: Search for files and directories modified within a certain number of days ago.
  • -print: Display the names of the found files and directories.

Example usage:

Find all the files under the current tree that have the " .jsextension and print the relative path of each file matching:

It's important to use quotes around special characters like '*to avoid the shell interpreting them.

Find directories under the current tree matching the name "src":

Use-type fto search only files, or -type lto only search symbolic links. 

-nameis case sensitive. use-inameto perform a case-insensitive search.

You can search under multiple root trees:

Find directories under the current tree matching the name "node_modules" or 'public':

You can also exclude a path, using -not -path:

You can search files that have more than 100 characters (bytes) in them:

Search files bigger than 100KB but smaller than 1MB:

Search files edited more than 3 days ago:

Search files edited in the last 24 hours:

You can delete all the files matching a search by adding the-deleteoption. This deletes all the files
edited in the last 24 hours:

You can execute a command on each result of the search. In this example, we runcatto print the file
content:

notice the terminating " \; ".{}is filled with the file name at execution time.

locate

The 'locate' command is used to find files and directories quickly on a Linux system. It works by searching an index of filenames, which is generated by the updatedb command, rather than by scanning the file system itself. The basic syntax for the locate command is as follows:

Where [OPTIONS] are optional flags that modify the behavior of the locate command, and [PATTERN] is a string that specifies the name of the files and directories to be found.

Here are some standard options for "locate":

  • -i: Perform a case-insensitive search.
  • -c: Display the number of matching results, but not the results themselves.

Example usage:

If you want to search any file use the following command:

To finds all files and directories named file.txt or FILE.TXT on the system, ignoring the case:

To display, the number of files and directories named file.txt on the system, but not the names themselves.

which

The "which" command is used to determine the location of an executable file in the system's PATH. The basic syntax for the which command is as follows:

Where [COMMAND] is the name of the executable file to be found.

Example usage:

You can do so using 'which'. The command will return the path to the command specified:

whichwill only work for executables stored on disk, not aliases or built-in shell functions.

whereis

whereis command is used to find the location of the source/binary file of a command and manuals sections for a specified file in a Linux system. If we compare the "whereis" command with the find command they will appear similar to each other as both can be used for the same purposes but the "whereis" command produces the result more accurately by consuming less time comparatively.

Where [OPTIONS] are optional flags that modify the behavior of the locate command.

Here are some standard options for whereis:

  • -b : This option is used when we only want to search for binaries.
  • -m : This option is used when we only want to search for manual sections. 
  • -B: This option is used to change or otherwise limit the places where whereis searches for binaries.
  • -M: This option is used to change or otherwise limit the places where whereis searches for manual sections. 
  • -S: This option is used to change or otherwise limit the places where whereis searches for sources.

Example Usage:

 To find the location of the source/binary file of a command uses the following command:

To locate the binary of a Linux command let’s say gunzip:

Each of these commands has various options and flags that can be used to customize their behavior, so it is important to consult the manual pages for these commands for more information.

Reading Material
File & Directory Management Lesson 5 of 14
In Progress

Archive Compression Linux Commands

Archive compression is a process of compressing one or multiple files and directories into a single archive file with a reduced size. The archive file can then be compressed using various algorithms such as gzip, tar, bzip2, etc. to save disk space and to make it easier to transfer files over the network. The archive file can be later decompressed and the original files can be restored.

gzip

gzip is a widely used compression tool in Linux and Unix systems that compresses a single file into a smaller file with a .gz extension. It uses the DEFLATE algorithm to achieve lossless data compression. The compressed file is smaller in size than the original file, making it easier to transfer or store. The original file can be decompressed to its original state using the gzip -d command.

The syntax for the gzip command is:

gzip [OPTIONS] [FILE_NAME]


Options:
  •  -1 to -9: compression level, 1 is the fastest and less compression, 9 is slowest with the most compression 
  • -d: decompress the file 
  • -f: force compression/decompression, even if the file is compressed/decompressed already 
  • -h: display help/usage information 
  • -k: keep the original file, do not delete it 
  • -r: process directories recursively -t: test compressed file for integrity 
  • -v: display verbose information 
  • -#: display the compression ratio for each file
username@Technoscience:~$ gzip newfile2.txt
username@Technoscience:~$ ls
newfile2.txt.gz
username@Technoscience:~$
username@Technoscience:~$ gzip -c newfile.txt newfile2.txt > newfile.gz
username@Technoscience:~$ ls
newfile.gz    newfile.txt    newfile2.txt
username@Technoscience:~$
username@Technoscience:~$ gzip -k newfile4.txt
username@Technoscience:~$ ls
newfile4.txt    newfile4.txt.gz
username@Technoscience:~$
username@Technoscience:~$ gzip -4 newfile newfile4.txt newfile.txt > newfile.gz
username@Technoscience:~$

Example: 

You can compress a file using the gzip compression protocol namedLZ77using the gzipcommand. Here's the simplest usage:

This will create a compressed file "newfile.txt.gz" and delete the original file.

To prevent this, you can use the-coption and use output redirection to write the output to the "newfile.gzfile:

The-coption specifies that output will go to the standard output stream, leaving the original file intact, or you can use the -koption:

There are various levels of compression. The more the compression, the longer it will take to compress
(and decompress)
Levels range from 1 (fastest, worst compression) to 9 (slowest, better compression), and the default is 6.

You can choose a specific level with the -<NUMBER> option:

tar

"tar" is a file archiving utility in Linux used for packaging multiple files into a single archive file (tar file). The tar archive file format is commonly used for backup purposes and for the distribution of multiple files as a single archive. Tar files can be compressed using gzip, bzip2, or other compression utilities. The basic syntax to create a tar archive is:

username@Technoscience:~$ touch file1 file2 file3
username@Technoscience:~$ ls
file1  file2 file3
username@Technoscience:~$ tar -cvf archive.tar file1 file2 file3
file1
file2
file3
username@Technoscience:~$ ls
archive.tar  file1    file2    file3
username@Technoscience:~$ rm file1 file2 file3
username@Technoscience:~$

[OPTIONS] used in the above command:

username@Technoscience:~$ tar -xvf archive.tar
file1
file2
file3
username@Technoscience:~$ ls
file1      file2    file3
username@Technoscience:~$
  • -c: create an archive
  • -v: verbose output
  • -f: file, used to specify the name of the archive file

To extract the contents of a tar archive:

tar -xvf archive.tar

Options used in the above command:
username@Technoscience:~$ tar -xf archive.tar -C Desktop/
username@Technoscience:~$ ls Desktop/
file1  file2  file3
username@Technoscience:~$
username@Technoscience:~$ ls
archive.tar
username@Technoscience:~$ tar -tf archive.tar
file1
file2
file3
username@Technoscience:~$
username@Technoscience:~$ tar -czf archive.tar.gz file1 file2
username@Technoscience:~$ ls
file1    archive.tar.gz    file2
username@Technoscience:~$
username@Technoscience:~$ tar -xf archive.tar.gz
username@Technoscience:~$
bzip2 [options] filename
  • -x: extract files
  • -v: verbose output
  • -f: file, used to specify the name of the archive file

To extract them to a specific directory, use:

You can also just list the files contained in an archive:

'taris often used to create acompressed archive, gzipping the archive. This is done using the -zoption:

This is just like creating a tar archive and then running gzipon it. 

To unarchive a gzipped archive, you can usegunzip, orgzip -d, and then unarchive it, buttar -xfwill recognize it's a gzipped archive and do it for you:

bzip2

'bzip2' is a lossless compression tool in Linux used to compress and decompress files. The compression method used by bzip2 is known as the Burrows-Wheeler algorithm and it provides better compression ratios than gzip. To use bzip2, one can run the following command in the terminal:

Syntax: 

Options: 

  • -z: Compress the file 
  • -d: Decompress the file 
  • -k: Keep the original file after compression 
  • -v: Display the progress of compression 
  • -f: Force compression or decompression 
  • -t: Test the integrity of the compressed file

Example: 

username@Technoscience:~$ bzip2 -z file1
username@Technoscience:~$ ls
file1.bz2
username@Technoscience:~$
username@Technoscience:~$ bzip2 -d file1.bz2
username@Technoscience:~$ ls
file1 
username@Technoscience:~$
zip[-options] archive.zipfile1 [file2 ...]

This will compress the file "file.txt" and create "file.bz2".

This will decompress the file "file.txt.bz2" and create "file.txt".

zip

The zip command is used to compress and archive files and directories in a zip archive. It works by compressing each file individually, then combining all the compressed files into a single archive file. The syntax for using the zip command is:

Where:

  • options are various optional switches that can modify the behavior of the zip command.
  • archive.zip is the name of the archive file that will be created.
  • file1 [file2 ...] are the names of the files and/or directories to be included in the archive.

Some common options used with the zip command include:-r (or --recurse-paths)
username@Technoscience:~$ zip archive.zip file1 file2
  adding: file1 (stored 0%)
  adding: file2 (stored 0%)
username@Technoscience:~$ ls
archive.zip  file2    file1
username@Technoscience:~$
  • include all files and subdirectories within a specified directory.
  • -9 (or --best): use the highest level of compression.
  • -u (or --update): update an existing archive file, adding or replacing files as necessary.

For example:

To extract files from a zip archive, use the unzip command.

unzip

"unzip" is a command line utility in Unix-based systems that allows you to extract compressed archive files (with .zip extension) and restore the original files. The syntax is:

unzip [options] archive.zip [file(s) ...] [destination]
Some common options include:
username@Technoscience:~$ unzip -l archive.zip   # List the contents of an archive
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2023-02-02 22:54   file1
        0  2023-02-02 22:54   file2
---------                     -------
        0                     2 files
username@Technoscience:~$ unzip archive.zip -d Desktop/
Archive:  archive.zip
 extracting: Desktop/file2
username@Technoscience:~$
  • -l: List the contents of an archive without actually extracting the files.
  • -o: Overwrite files without asking for confirmation.
  • -d: Specify the destination directory for the extracted files.
  • -p: Extract files and preserve the original file permissions.
  • -q: Quiet mode, do not display any messages or error messages.

If the [file(s) ...] argument is not specified, "unzip" will extract all the files in the archive. If an [destination] argument is not specified, "unzip" will extract the files to the current working directory.

Reading Material
Text Editing & Text Processing Lesson 6 of 14
In Progress

Text Processing Linux Commands

Text processing in Linux refers to a set of commands used to manipulate and format text data in the command-line environment. These commands can perform operations such as reading and concatenating the contents of files, searching for specific patterns, sorting and filtering data and performing various text transformations

The text processing commands in Linux are widely used for tasks such as data processing, log analysis, and report generation. They can be combined with other Linux commands and pipelines to automate complex text-processing tasks.

cat [OPTION]...[FILE]...
username@Technoscience:~$ cat newfile.txt
The text 1
The text 2
The text 3
The text 4
username@Technoscience:~$
username@Technoscience:~$ cat newfile.txt newfile2.txt
The text 1
The text 2
The text 3
The text 4
The text 5
The text 6
The text 7
username@Technoscience:~$
username@Technoscience:~$ cat newfile.txt newfile2.txt > newfile3.txt
username@Technoscience:~$ cat newfile3.txt
The text 1
The text 2
The text 3
The text 4
The text 5
The text 6
The text 7
username@Technoscience:~$
username@Technoscience:~$ cat newfile.txt newfile2.txt >> newfile4.txt
username@Technoscience:~$ cat newfile4.txt
The text 1
The text 2
The text 3
The text 4
The text 5
The text 6
The text 7
username@Technoscience:~$
username@Technoscience:~$ cat -n newfile4.txt
     1The text 1
     2The text 2
     3The text 3
     4The text 4
     5The text 5
     6The text 6
     7The text 7
username@Technoscience:~$
cut [OPTIONS] [FILE]

Some common text-processing commands in Linux include:

cat

cat is a basic text processing command in Linux used to concatenate and display the contents of files. It is commonly used to view the contents of small text files or to combine multiple files into one. Here are some of its key features:

Syntax: 

Options:

  • -A, --show-all: equivalent to -vET
  • -b, --number-nonblank: number nonempty output lines, overrides -n
  • -E, --show-ends: display $ at end of each line
  • -n, --number: number all output lines
  • -s, --squeeze-blank: suppress repeated empty output lines
  • -T, --show-tabs: display TAB characters as ^I
  • -v, --show-nonprinting: use ^ and M- notation, except for LFD and TAB

In its simplest usage,catprints a file's content to the standard output:

You can print the content of multiple files:

and using the output redirection operator>you can concatenate the content of multiple files into a new file:

Using >>you can append the content of multiple files into a new file, creating it if it does not exist:

When watching source code files it's great to see the line numbers and you can havecatprint them using the-noption:

You can only add a number to non-blank lines using-b, or you can also remove all the multiple empty lines using-scat is often used in combination with the pipe operator|to feed a file content as input to another command.

cut

'cut' is a text processing command in Linux that is used to extract specific columns or fields from input data (i.e., from a file or from the standard input). It splits the input into lines and then cuts out specified fields from each line.

Here are some key features of the cut command:

Options:

  • -b, --bytes: selects the specified byte positions
  • -c, --characters: selects the specified character positions
  • -d, --delimiter=DELIM: specifies the delimiter that separates fields in the input data
  • -f, --fields=LIST: specifies the fields to be extracted (a comma-separated list of field numbers)

Usage:

grep[options]pattern [file_name(s)]
username@Technoscience:~$ grep 'text' newfile3.txt
Thetext1
Thetext2
Thetext3
Thetext4
Thetext5
Thetext6
Thetext7
username@Technoscience:~$
username@Technoscience:~$ grep -n 'text 2' newfile3.txt
2:Thetext 2
username@Technoscience:~$
username@Technoscience:~$ grep -nC 2 'text 2' newfile3.txt
1-The text 1
2:Thetext 2
3-The text 3
4-The text 4
username@Technoscience:~$
username@Technoscience:~$ less newfile3.txt | grep -n 'text 5'
5:Thetext 5
username@Technoscience:~$
username@Technoscience:~$ ls -al | grep 'newfile'
-rw-rw-r--  1 username username     33 Feb  2 19:39 newfile2.txt
-rw-rw-r--  1 username username     77 Feb  2 19:42 newfile3.txt
-rw-rw-r--  1 username username     77 Feb  2 19:44 newfile4.txt
-rw-rw-r--  1 username username     44 Feb  2 19:37 newfile.txt
username@Technoscience:~$
username@Technoscience:~$ ls -al | grep -v 'newfile'
sort [OPTION]...[FILE]...

cut -f1,3 -d: /etc/passwd: Extracts the first and third fields (separated by a colon) from the /etc/passwd file.


Note: By default, the delimiter is a tab character (\t).

grep

'grep' is a powerful utility in Linux for searching for a specific pattern of text in a file or input. The basic syntax of the command is:

where:
  • 'pattern' is the string of text you want to search for.
  • 'file_name(s)' is the name(s) of the file(s) you want to search. You can specify one or more file names, or use a wildcard pattern to search multiple files.
'grep' has several useful options, including:
  • -v: to invert the match, so that it returns lines that do not contain the pattern.
  • -i: to ignore the case of the pattern when searching.
  • -r: to search recursively through all subdirectories.
  • -c: to count the number of matches in each file.

For example, here's how we can find the occurrences of the 'text' line in the 'newfile3.txt' file:

Using the -noption will show the line numbers:

One very useful thing is to tell grep to print 2 lines before, and 2 lines after the matched line, to give us
more context. That's done using the
-Coption, which accepts a number of lines:

Search is case-sensitive by default. Use the '-iflag to make it insensitive. As mentioned, you can use grep to filter the output of another command. We can replicate the same functionality as above using:

The search string can be a regular expression, and this makesgrepvery powerful.

Another thing you might find very useful is to invert the result, excluding the lines that match a particular string, using the '-voption:

You can use 'grep' in a pipeline to search the output of another command. This makes it possible to search for patterns in the output of any command, making it a very versatile tool for text processing.

sort

sort is a Linux command used to sort the contents of a file or input stream in ascending or descending order. The sorted output can be redirected to a file or displayed on the terminal. By default, sort sorts the input lines in lexicographic order. Options can be provided to sort the input based on different criteria such as numeric value, reverse order, field-based sorting, etc.

Syntax: 

Suppose you have a text file that contains the names of dogs:

username@Technoscience:~$ cat dogs
golden retriever
labrador retriever
french bulldog
beagle
german shepherd dog
poodle
bulldog
most popular breeds
Great Danes
Miniature schnauzers
Cane corso
Boxers
Havanese
Spaniels
Brittanys
username@Technoscience:~$

This list is unorderedRun the sort command to sort them by name:

username@Technoscience:~$ sort dogs
beagle
Boxers
Brittanys
bulldog
Cane corso
french bulldog
german shepherd dog
golden retriever
Great Danes
Havanese
labrador retriever
Miniature schnauzers
most popular breeds
poodle
Spaniels
username@Technoscience:~$

Use the -r option to reverse the order:

username@Technoscience:~$ sort -r dogs
Spaniels
poodle
most popular breeds
Miniature schnauzers
labrador retriever
Havanese
Great Danes
golden retriever
german shepherd dog
french bulldog
Cane corso
bulldog
Brittanys
Boxers
beagle
username@Technoscience:~$

Sorting by default is case-sensitive, and alphabetic. Use the--ignore-caseoption to sort case insensitive, and the-noption to sort using a numeric order. If the file contains duplicate lines:

username@Technoscience:~$ sort --ignore-case -n dogs
beagle
Boxers
Brittanys
bulldog
Cane corso
french bulldog
german shepherd dog
golden retriever
Great Danes
Havanese
labrador retriever
Miniature schnauzers
most popular breeds
poodle
Spaniels
username@Technoscience:~$

You can use the-uoption to remove them:

username@Technoscience:~$ sort -u dogs
beagle
Boxers
Brittanys
bulldog
Cane corso
french bulldog
german shepherd dog
golden retriever
Great Danes
Havanese
labrador retriever
Miniature schnauzers
most popular breeds
poodle
Spaniels
username@Technoscience:~$

sortdoes not just works on files, as many UNIX commands it also works with pipes, so you can use it on the output of another command, for example, you can order the files returned bylswith:

username@Technoscience:~$ ls | sort
Desktop
Documents
dogs
Downloads
Music
newdir
newfile2.txt
newfile3.txt
newfile4.txt
newfile.txt
Pictures
Public
Templates
Videos
username@Technoscience:~$

sortis very powerful and has lots more options, which you can explore by callingman sort.

SORT(1)                             User Commands                            SORT(1)
NAME
       sort - sort lines of text files
SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F
DESCRIPTION
       Write sorted concatenation of all FILE(s) to standard output.
       With no FILE, or when FILE is -, read standard input.
       Mandatory  arguments  to  long  options  are mandatory for short options too.
       Ordering options:
       -b, --ignore-leading-blanks
              ignore leading blanks
       -d, --dictionary-order
              consider only blanks and alphanumeric characters
<SNIP>
uniq [OPTION]... [INPUT [OUTPUT]]
username@Technoscience:~$ uniq dogs
golden retriever
labrador retriever
french bulldog
<SNIP>
username@Technoscience:~$
username@Technoscience:~$ ls | uniq
Desktop
Documents
dogs
Downloads
<SNIP>
username@Technoscience:~$
username@Technoscience:~$ sort dogs | uniq
beagle
Boxers
Brittanys
bulldog
Cane corso
<SNIP>
username@Technoscience:~$
username@Technoscience:~$ sort dogs | uniq
beagle
Boxers
Brittanys
bulldog
Cane corso
french bulldog
<SNIP>
username@Technoscience:~$
username@Technoscience:~$ sort dogs | uniq -d
username@Technoscience:~$
username@Technoscience:~$ sort dogs | uniq -u
beagle
Boxers
Brittanys
bulldog
Cane corso
<SNIP>
username@Technoscience:~$
username@Technoscience:~$ sort dogs | uniq -c
      1 beagle
      1 Boxers
      1 Brittanys
      1 bulldog
      1 Cane corso
<SNIP>
      1 Spaniels
username@Technoscience:~$
username@Technoscience:~$ sort dogs | uniq -c | sort -nr
      1 Spaniels
      1 poodle
      1 most popular breeds
      1 Miniature schnauzers
      1 labrador retriever
      1 Havanese
      1 Great Danes
<SNIP>
username@Technoscience:~$

uniq

uniq is a Linux command used to remove duplicate lines from a sorted file or input stream. The output of uniq will contain only unique lines from the input. If multiple adjacent lines are identical, only one of them will be included in the output.

Syntax: 

You can get those lines from a file, or using pipes from the output of another command:

You can list duplicate lines from the directory:

You need to consider this key thing:uniqwill only detect adjacent duplicate lines. This implies that you will most likely use it along with sort:

Thesortcommand has its own way to remove duplicates with the-u(unique) option. Butuniqhas
more power. 

By default it removes duplicate lines:

You can tell it to only display duplicate lines, for example, with the -d option:

There are no duplicate lines. You can use the-uoption to only display nonduplicate lines:

You can count the occurrences of each line with the-coption:

Use the special combination to then sort those lines by most frequent:

sed

sed (Stream EDitor) is a powerful Linux command-line utility that performs text transformations on an input file or input stream. sed can perform operations such as text substitution, deletion, insertion, and search and replace. sed operates in a non-interactive manner and performs the transformations in batch mode. The output of sed can be redirected to a file or displayed on the terminal.

Syntax: 

sed [OPTION]... {script} [input-file]...
awk [OPTION]... 'program' [input-file]...
tr [OPTION]... SET1 [SET2]
tac [FILE]...
username@Technoscience:~$ tac dogs
Brittanys
Spaniels
Havanese
Boxers
<SNIP>
username@Technoscience:~$
head [OPTION]... [FILE]...

awk

'awk' is a programming language and a Linux command used for text processing and manipulation. It scans input text files line by line, performing actions based on patterns and rules specified by the user. awk is often used to extract information from log files, CSV files, and other types of text data.

'awk' is capable of performing operations such as text substitution, deletion, insertion, and search and replace, and it also supports advanced features such as pattern matching and conditional statements. The output of awk can be redirected to a file or displayed on the terminal.

Syntax: 

tr

'tr' is a Linux command used for text translation and manipulation. It translates or deletes specified characters in the input text and writes the result to standard output.

'tr' can be used to perform operations such as character substitution, deletion, and compression. For example, it can be used to convert lowercase characters to uppercase, remove duplicates, or squeeze repeated characters.

Syntax: 

tac

'tac' is a Linux command used to concatenate and print files in reverse order. It reads the input files in reverse order, line by line, and writes the result to the standard output.

The tac command is useful for viewing the last lines of a file, for example, the latest entries in a log file.

Syntax: 

Run the following command:

head

'head' is a Linux command that prints the first n lines (10 by default) of a file to the standard output. It can also be used to view the first part of a large file to see what it contains before reading the whole file.

Syntax: 

Options:

username@Technoscience:~$ head -n 5 dogs
golden retriever
labrador retriever
french bulldog
beagle
german shepherd dog
username@Technoscience:~$
tail [OPTION]...[FILE]...
username@Technoscience:~$ tail -f /var/log/dpkg.log
2023-02-02 15:43:15 status installed htop:amd64 2.1.0-3
2023-02-02 15:43:15 trigproc desktop-file-utils:amd64 0.23-1ubuntu3.18.04.2 <none>
2023-02-02 15:43:15 status half-configured desktop-file-utils:amd64 0.23-1ubuntu3.18.04.2
2023-02-02 15:43:15 status installed desktop-file-utils:amd64 0.23-1ubuntu3.18.04.2
2023-02-02 15:43:15 trigproc man-db:amd64 2.8.3-2ubuntu0.1 <none>
2023-02-02 15:43:15 status half-configured man-db:amd64 2.8.3-2ubuntu0.1
2023-02-02 15:43:17 status installed man-db:amd64 2.8.3-2ubuntu0.1
2023-02-02 15:43:17 trigproc mime-support:all 3.60ubuntu1 <none>
2023-02-02 15:43:17 status half-configured mime-support:all 3.60ubuntu1
2023-02-02 15:43:17 status installed mime-support:all 3.60ubuntu1
username@Technoscience:~$ tail -n 5 newfile3.txt
The text 3
The text 4
The text 5
The text 6
The text 7
username@Technoscience:~$
username@Technoscience:~$ tail -n+5 newfile3.txt
The text 5
The text 6
The text 7
username@Technoscience:~$
  • -n: Specify the number of lines to display.
  • -c: Specify the number of bytes to display.
  • -q: Quiet, never print headers giving file names.

Example: 

To print the first 5 lines of the file 'dogs' use the following command:

tail

"tail" is a Linux command used to display the last N number of lines in a file. By default, it shows the last 10 lines. It can be used to monitor logs, check for updates, etc.

Syntax:


Options:
  •  -f, --follow: Output appended data as the file grows 
  • -n, --lines=[+|-]NUM: Number of lines to output. A '+' symbol shows lines starting from NUM 
  • -q, --quiet, --silent: Don't print headers when multiple files are outputting 
  • -v, --verbose: Always print headers when multiple files are outputting

Example:

The best use case of the tail in my opinion is when called with the-foption. It opens the file at the end and watches for file changes. Any time there is new content in the file, it is printed in the window. This is great for watching log files, for example:

To exit, press Ctrl + C.

You can print the last 5 lines in a file:

You can print the whole file content starting from a specific line using+before the line number:

tail -n +10 <filename>

'tail'can do much more and as always my advice is to checkman tail.

These commands are used in combination with pipes (|) and redirection (> and >>) to create powerful text processing pipelines that can manipulate, filter, and format text data in various ways.

Reading Material
Text Editing & Text Processing Lesson 7 of 14
In Progress

vi Text-Editor with Commands in Linux/Unix

There are several Text-editors available in Linux to edit files. They are categorized into two parts that are GUI based and Screen-oriented Text-editors.

┌──(mrdev㉿kali)-[~/]
└─$ vi first_file

VI is a screen-oriented text editor originally designed for UNIX and UNIX-based operating systems. VI is the most popular and classic text editor in the Linux family. Below, are some reasons which make it a widely used editor?

  • It is available in almost all flavors of Linux Distributions,
  • It works the same across different platforms and Distributions,
  • It is user-friendly.

Hence, millions of Linux users love it and use it for their editing needs.

In recent times there be an advanced version of the VI editor available, and the most popular one is Vim which is VI Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn VI because it is feature-rich and offers endless possibilities to edit a file.

To work on the VI editor, you need to understand its operation modes. They can be divided into two main parts.

  • Command mode: In commands mode, VI only understands commands, you can only move the cursor and cut, copy, and paste the text. In the command, you can modify and change the file. Remember that Commands are always case Sensitive. Insert mode is always used to insert text into the file.
  • Insert Mode: To switch to the Insert mode from the command mode by pressing 'i' on the keyboard. Once you are in Insert mode, any key would be taken as input for the file on which you are currently working.

To return to the command mode and save the changes you have made you need to press the Esc key.

VI-Editor Usage

1. To launch the VI Text-Editor, Open the Terminal and type vi <file name>.

It will open a new interface:

As you see VI editor opens in command mode. 

Always remember that if you specify an existing file, then the editor would open it for you to edit, else, you can create a new file.

2. Press “I” to enter the insert mode.

Here I type Hello World.

3. Press ‘ESC’ to Enter Command mode. Type :wq to save and Quit.

To check the content of the file we use cat the file name here the file name is first_file.

VI editing Commands

There are several commands available in the VI text editor.

Note: The "command mode" to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter case.

Here are all the commands used in the VI Text editor.

Keystrokes

Action

I

Insert at cursor (goes into insert mode)

A

Write after the cursor (goes into insert mode)

A

Write at the end of the line (goes into insert mode)

ESC

Terminate insert mode

U

Undo last change

U

Undo all changes to the entire line

O

Open a new line (goes into insert mode)

dd


3dd

Delete line


Delete 3 lines.

D

Delete the contents of the line after the cursor

C

Delete the contents of a line after the cursor and insert new text. Press the ESC key to end insertion.

dw
4dw

Delete word
Delete 4 words

cw

Change word

x

Delete the character at the cursor

r

Replace character

R

Overwrite characters from cursor onward

s

Substitute one character under the cursor and continue to insert

S

Substitute the entire line and begin to insert at the beginning of the line

~

Change case of individual character

Make sure, you have pressed the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, or o, as required.

Moving within a file

You need to be in command mode to move within a file. The default keys for navigation are mentioned below else; you can also use the arrow keys on the keyboard.

Keystroke

Use

k

Move cursor up

j

Move cursor down

h

Move cursor left

l

Move cursor right

Saving and Closing the file

You should be in the command mode to exit the editor and save changes to the file.

Keystroke

Use

Shift+zz

Save the file and quit

:w

Save the file but keep it open

:q

Quit without saving

:wq

Save the file and quit

In a short time, I hope, I have completely described the whole thing, if there are any doubts then write them down in my comment session.

Reading Material
Search & Shell Operations Lesson 8 of 14
In Progress

Search Related Linux Commands

Search-related Linux commands are commands used for searching files, directories, and content within a file system

Some common search-related Linux commands include:

man [command]
username@TechnoScience:~$ man ls
username@TechnoScience:~$ ls --help
which [command]
username@TechnoScience:~$ which ls
/bin/ls
username@TechnoScience:~$
whereis [OPTIONS] name
username@TechnoScience:~$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
username@TechnoScience:~$ whereis -m ls
ls: /usr/share/man/man1/ls.1.gz
username@TechnoScience:~$ whereis -b -m ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
username@TechnoScience:~$
locate [OPTION]... PATTERN...
username@TechnoScience:~$ locate bash
find [path] [expression]
username@TechnoScience:~$ find . -name "*.txt"
./.local/share/Trash/files/newfile3.txt
./.local/share/Trash/files/newdir/sym_link_file.txt
username@TechnoScience:~$
username@TechnoScience:~$ find . -mtime -7
.
./Downloads
./Downloads/systemd-udevd.dll
./.vboxclient-vmsvga-session-tty7.pid
./file2
./.tldr
./.bashrc
./.dmrc
./Documents
./dogs
./.config
./.config/pcmanfm
<SNIP>

Man Pages

The man command is used to display the manual pages for a Linux command. It stands for manual.

Syntax: 

When used with a command name as an argument, it displays the manual page for that command. The manual pages contain information about the command's usage, options, return codes, etc.

The manual pages are organized into sections, each section providing specific information about the command:

  • Section 1: User commands
  • Section 2: System calls
  • Section 3: Library functions
  • Section 4: Devices and special files
  • Section 5: File formats and conventions
  • Section 6: Games
  • Section 7: Miscellaneous
  • Section 8: System administration commands and daemons

You can also use the option -s followed by a section number to display manual pages from a specific section.

For example, man ls displays the manual page for the ls command and man -s 2 fork displays the manual page for the fork system call.

This is a man (from the manual) page. Man pages are an essential tool to learn, as a developer. They contain so much information that sometimes it's almost too much.

Command Line Help

The "help" command is a shell built-in command in most Unix-like operating systems. It displays information about built-in shell commands and functions. The information provided by "help" varies by shell and system, but typically includes a brief description of the command and its options. 

The "help" command can be useful for quickly looking up information on shell commands without having to switch to a manual page or online documentation. For example, in a Bash shell, typing "help echo" will provide information on the "echo" command.

Which

The "which" command in Linux is used to find the location of a command in the system PATH. It displays the full path of the executable file associated with the given command. It is commonly used to determine the location of a binary file to ensure it is not a malicious file.

Usage:

The "which" command takes one argument: the name of the command for which the location is being searched.

Example:

In the above example, the "which" command was used to find the location of the ls command. The output indicates that the ls command is located at /bin/ls.

Whereis

"whereis" is a Linux command that can be used to locate the source, binary, and manual page files for a specified command. It is used to search the standard Linux directories for specific executables, libraries, and manual pages, and returns the path to these files.

Syntax:

Options: 

  • -b, --binary: Search only for executable files 
  • -m, --man: Search only for manual pages 
  • -s, --source: Search only for source files 
  • -B path, --block-size=path: Use a blocksize of path 
  • -M path, --manpath=path: Specify man search path 
  • -f, --fill-holes: Fill in any holes in the manual section with system manuals 
  • -u, --update: Update the database used by whereis

Examples:

Locate

locate is a command-line utility in Unix-like systems for finding files based on their names. It uses a pre-built database of file locations for faster search results compared to a full directory search with find.

Syntax:

Options:
  • -c or --count: Display only the total number of matches.
  • -d or --database=DB: Use the specified database.
  • -i or --ignore-case: Case-insensitive search.
  • -l or --limit=N: Limit the number of results to N.
  • -r or --regexp: Search using regular expressions.
  • -v or --version: Show version information and exit.
Example:

This will show all files with the name bash in the database. 

Note that the database must be updated regularly to ensure accuracy, usually through the updatedb command.

Find

The find command is used in Unix/Linux to search for files and directories based on various criteria like name, type, size, date, etc. It recursively searches the directory tree starting from a specified directory and returns a list of files that match the specified conditions.

The basic syntax of the find command is:

where "path" is the starting directory for the search and expression is a set of options and tests used to match the desired files.

Examples:

Find all the files under the current tree that have the ".txt" extension and print the relative path of each file matching:

It's important to use quotes around special characters like "*to avoid the shell interpreting them.

Find all files that were modified in the last 7 days:

For complete details and options available, refer to the find manual page by running man find in a terminal. 

These commands are used to search for files or directories based on name, content, or other attributes. They provide quick and efficient ways to search and locate information within a Linux system.

Reading Material
Search & Shell Operations Lesson 9 of 14
In Progress

Shell Linux Commands

Shell-based Linux commands are commands that are executed within a shell environment in Linux. The shell is a command-line interface that provides users with an interface to interact with the underlying operating system. The shell interprets commands entered by the user and performs actions based on the commands. 


These below commands are used to perform various system administration tasks, such as listing files, changing directories, displaying system information, and more.

echo

echo is a shell command that outputs the string passed as an argument to the standard output (i.e. the terminal). Some common options for echo are:

  • -n: suppresses the newline character at the end of the output
  • -e: enables the interpretation of backslash escapes in the string passed as an argument, such as \n for a newline or \t for a tab character. 
username@TechnoScience:~$ echo Hello World! Hello World!
Hello World! Hello World!
username@TechnoScience:~$
username@TechnoScience:~$ echo -n Hello World! Hello WorldWorld!
Hello World! Hello WorldWorld!username@TechnoScience:~$
username@TechnoScience:~$ echo -e "Hello\tWorld\n" Hello World
HelloWorld
 Hello World
username@TechnoScience:~$
env [OPTION]... [-] [NAME=VALUE]...
username@TechnoScience:~$ env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
XTERM_256_COLORS=1
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_MENU_PREFIX=moksha-
LANG=en_IN
GDM_LANG=en_US
DISPLAY=:0.0
QT_STYLE_OVERRIDE=gtk
E_DATA_DIR=/usr/share/enlightenment
OLDPWD=/home
<SNIP>
username@TechnoScience:~$ env | grep "SHELL"
SHELL=/bin/bash
username@TechnoScience:~$
username@TechnoScience:~$ env -u SHELL
set [option(s)] [variable(s)]
username@TechnoScience:~$ set VARNAME=value
username@TechnoScience:~$ set
username@TechnoScience:~$ set VARNAME=
username@TechnoScience:~$ set VARNAME=newvalue
export [variable=value]
mrdev@kali:~$ export VAR=value
mrdev@kali:~$ export -p
alias <alias_name>='<command>'
username@TechnoScience:~$ alias ll='ls -l'
username@TechnoScience:~$
username@TechnoScience:~$ ll
total 40
-rw-rw-r-- 1 username username  298 Feb  2 23:30 archive.zip
drwxr-xr-x 2 username username 4096 Feb  2 23:38 Desktop
drwxr-xr-x 2 username username 4096 Feb  1 18:22 Documents
-rw-rw-r-- 1 username username  192 Feb  2 20:40 dogs
drwxr-xr-x 2 username username 4096 Feb  2 15:43 Downloads
-rw-rw-r-- 1 username username    0 Feb  2 22:54 file1
-rw-rw-r-- 1 username username    0 Feb  2 22:54 file2
-rw-rw-r-- 1 username username    0 Feb  2 22:54 file3
drwxr-xr-x 2 username username 4096 Feb  1 18:22 Music
drwxr-xr-x 2 username username 4096 Feb  1 18:22 Pictures
drwxr-xr-x 2 username username 4096 Feb  1 18:22 Public
drwxr-xr-x 2 username username 4096 Feb  1 18:22 Templates
drwxr-xr-x 2 username username 4096 Feb  1 18:22 Videos
username@TechnoScience:~$
unalias [-a] name
username@TechnoScience:~$ unalias ll
username@TechnoScience:~$ ll
ll: command not found
username@TechnoScience:~$
username@TechnoScience:~$ unalias -a
source filename
. filename
username@TechnoScience:~$ cat script.sh
#!/bin/bash

echo "Hello World!"
username@TechnoScience:~$ source script.sh
Hello World!
username@TechnoScience:~$

Example usage:

Print "Hello World!"

Print "Hello World!" along with the shell:

Enable interpolation of a backslash:

env

env is a shell command used to display environment variables and their values.

Syntax: 

Options:

  • -i: run the utility with only specified environment variables, discarding all previously set variables.
  • -u: unset specified variables.
  • -P: use full pathnames in the output.
  • -0: end each output line with 0 bytes rather than a new line.

Examples:

Display all environment variables and their values:

Display the value of the SHELL environment variable.

unset the SHELL environment variable:

Note: The environment variables can be set, modified, or unset using the export, unset, and set commands respectively.

set

"set" is a shell built-in command used in various Unix-like operating systems including Linux to set or modify environment variables. An environment variable is a value stored in the system that can be accessed by various applications and scripts.

The syntax for the "set" command is:

Examples:

Set an environment variable: set VARNAME=value

Display all environment variables: set

Remove an environment variable: set VARNAME=

Change the value of an environment variable: set VARNAME=newvalue

export

export is a built-in shell command in Unix-like operating systems that sets or updates the values of environment variables. Environment variables are values that affect the behavior of the shell and programs started from the shell. export allows you to make a shell variable available to child processes of the shell.

Usage: 

Options:

  • -p: Displays a list of all exported variables.
  • -f: Forces the removal of read-only variables.
Examples:

export VAR=value: sets the value of the VAR environment variable to value.

export -p: displays a list of all exported variables and their values.

It is common to use the export command in shell scripts to set environment variables required by the script.

alias

The "alias" command in Linux is used to set a shorthand or alias name for a specified command. The format is:

For example, to set an alias for the "ls" command, you can run:

alias ll='ls -l'

This creates an alias named "ll" for the "ls -l" command, so you can run "ll" instead of "ls -l".

Aliases are temporary and only last for the current session. To make them permanent, add the alias command to your shell startup file, such as ".bashrc" or ".bash_profile".

unalias

"unalias" is a built-in shell command in most Unix-like operating systems (including Linux). It's used to remove an alias from the current shell environment. An alias is a nickname for a shell command or a set of commands, and it's created with the "alias" command.

Syntax: 

Options: 

  • -a: Remove all aliases. 
  • name: The name of the alias to be removed.

Examples:

To remove an alias "ll" for "ls -l":

To remove all aliases:

source

"source" is a shell command in Linux/Unix-like operating systems that reads and executes commands from a specified file in the current shell environment. It acts like running the commands in the current shell, instead of starting a new shell.

Syntax:

Example:

source command is commonly used to run shell scripts and configure shell environment variables or settings in the current shell session, instead of creating a new sub-shell or process.

.

"." is a shorthand for the source command in a Unix-style shell. The source command is used to run a shell script in the current shell environment, rather than creating a new sub-shell to run the script. The script is executed in the current shell environment, and any changes to environment variables or other configuration settings made within the script will persist after the script has been completed. The "." command is equivalent to a source and can be used interchangeably.

Reading Material
System Information & Process Management Lesson 10 of 14
In Progress

System Information Linux Commands

System information Linux commands are commands that are used to gather information about the system, such as its hardware components, memory usage, disk space utilization, etc.


Here are some common Linux commands used to gather system information:

uname

The uname command is used to display information about the Linux operating system and its underlying system information, including the name, version, and architecture.

Here's the basic syntax of the uname command:

uname[OPTION]...

Where [OPTION] is one or more of the following options:

  • -a: Display all information about the system, including the name, version, release, and machine architecture.
  • -m: Display the machine hardware name.
  • -n: Display the hostname of the system.
  • -r: Display the Linux kernel release version.
  • -s: Display the name of the operating system.
  • -p: Display the processor type.
  • -v: Display the Linux kernel version.
username@Technoscience:~$ uname
Linux
username@Technoscience:~$
username@Technoscience:~$ uname -a
Linux Technoscience 4.15.0-88-generic #88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
username@Technoscience:~$
username@Technoscience:~$ uname -m
x86_64
username@Technoscience:~$
username@Technoscience:~$ uname -n
Technoscience
username@Technoscience:~$
lsb_release[OPTION]...

Example usage:

Callingunamewithout any options will return the Operating System codename:

To display all information about the system, including the name, version, release, and machine architecture.

To display the machine hardware name, e.g. x86_64 for a 64-bit system.

To display the hostname of the system.

lsb_release

The lsb_release command is used to display information about the Linux distribution and its version.

Here's the basic syntax of the lsb_release command:

username@Technoscience:~$ lsb_release -a
No LSB modules are available.
Distributor ID:Ubuntu
Description:Bodhi Linux
Release:18.04
Codename:bionic
username@Technoscience:~$
username@Technoscience:~$ lsb_release -d
Description:Bodhi Linux
username@Technoscience:~$
username@Technoscience:~$ lsb_release -s
No LSB modules are available.
username@Technoscience:~$

Where [OPTION] is one or more of the following options:

  • -a: Display all information about the distribution, including the name, description, and release version.
  • -d: Display the description of the distribution.
  • -i: Display the distribution ID.
  • -r: Display the release version of the distribution.
  • -s: Display a short description of the distribution.

Example usage:

To display all information about the distribution, including the name, description, and release version.

This command displays the description of the distribution:

This command displays a short description of the distribution:

cat /proc/version

The /proc/version file is a virtual file in the /proc file system that provides information about the Linux kernel version that is running on the system. The cat command can be used to display the contents of this file.

The output of the cat /proc/version displays the following information:

username@Technoscience:~$ cat /proc/version
Linux version4.15.0-88-generic(buildd@lgw01-amd64-036) (gcc version7.4.0(Ubuntu7.4.0-1ubuntu1~18.04.1))#88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020
username@Technoscience:~$
  • The Linux version string, which includes the Linux kernel version and the name of the distribution that is running the kernel.
  • The build time and date of the Linux kernel.
  • The name and email address of the person who built the Linux kernel.

Example usage:

To display the information about the Linux kernel version and builds information use the following command:

In this example, the Linux kernel version is 4.15.0-88-generic, and the distribution that is running the kernel is Ubuntu 18.04.1.

cat /proc/cpuinfo 

The /proc/cpuinfo file is a virtual file in the /proc file system that provides information about the central processing unit (CPU) in the system. The cat command can be used to display the contents of this file.

The output of cat /proc/cpuinfo displays information about each processor in the system, including:
username@Technoscience:~$ cat /proc/cpuinfo
processor: 0
vendor_id:GenuineIntel
cpu family: 6
model        : 69
model name:Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz
stepping: 1
cpu MHz: 2494.228
cache size:3072 KB
physical id: 0
siblings: 1
core id: 0
cpu cores: 1
apicid        : 0
initial apicid: 0
fpu        : yes
fpu_exception: yes
cpuid level: 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm invpcid_single pti fsgsbase bmi1 avx2 bmi2 invpcid md_clear flush_l1d
bugs        : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
bogomips: 4988.45
clflush size: 64
cache_alignment: 64
address sizes: 39 bits physical, 48 bits virtual
power management:

username@Technoscience:~$
username@Technoscience:~$ cat /proc/meminfo
MemTotal:       1511588 kB
MemFree:          439024 kB
MemAvailable:    1139848 kB
Buffers:           91660 kB
Cached:           699344 kB
SwapCached:            0 kB
Active:           435760 kB
Inactive:         481672 kB
Active(anon):     127208 kB
Inactive(anon):     2564 kB
Active(file):     308552 kB
Inactive(file):   479108 kB
Unevictable:          32 kB
Mlocked:              32 kB
SwapTotal:       1535360 kB
SwapFree:        1535360 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        126480 kB
Mapped:           126816 kB
Shmem:              3348 kB
Slab:             101160 kB
SReclaimable:      84968 kB
SUnreclaim:        16192 kB
KernelStack:        3084 kB
PageTables:        10960 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2291152 kB
Committed_AS:     916248 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       80832 kB
DirectMap2M:     1478656 kB
username@Technoscience:~$
  • Processor vendor and model name.
  • Processor clock speed.
  • Cache size.
  • Processor flags, which represent features that the processor supports.

Example usage:

This command displays information about the central processing unit (CPU) in the system.

Here's a sample output of cat /proc/cpuinfo:


In this example, the system has an Intel core i5 4th Gen processor. The processor clock speed is 1.90 GHz and the cache size is 3072 KB.

cat /proc/meminfo 

The /proc/meminfo file is a virtual file in the /proc file system that provides information about the memory usage and statistics of the system. The cat command can be used to display the contents of this file.

The output of cat /proc/meminfo displays information about the system's memory usage, including:

  • Total installed memory (MemTotal).
  • Free memory is available (MemFree).
  • Buffered memory is used by the kernel (Buffers).
  • Cached memory is used by the kernel (Cached).
  • Total swap space (SwapTotal).
  • Free swap space (SwapFree).

Example usage:

To display the information about the memory usage and statistics of the system. Here's a sample output of cat /proc/meminfo:

In this example, the system has a total of  1511588 KB of memory, with 439024 KB of free memory available. The system also has a total of 1139848 KB of swap space, with 699344 KB of cache space.

df 

The df command is used to display information about disk space usage on a file system.

By default, df displays the disk space usage information for all mounted file systems in the system. Each file system is displayed on a separate line, with columns indicating the following information:
username@Technoscience:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              722484       0    722484   0% /dev
tmpfs             151160    1000    150160   1% /run
/dev/sda1       32426780 4743504  26013032  16% /
tmpfs             755792       0    755792   0% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs             755792       0    755792   0% /sys/fs/cgroup
tmpfs             151156       8    151148   1% /run/user/1000
username@Technoscience:~$
username@Technoscience:~$ free
              total        used        free      shared  buff/cache   available
Mem:        1511588      197620      437308        3368      876660     1138800
Swap:       1535360           0     1535360
username@Technoscience:~$
  • Filesystem: the type of file system.
  • 1K-blocks: the total size of the file system in 1KB blocks.
  • Used: the amount of disk space used by the file system.
  • Available: the amount of disk space available on the file system.
  • Use%: the percentage of disk space used.
  • Mounted on: the mount point of the file system.

Example usage:

This command displays the disk space usage information for all mounted file systems in the system. Here's a sample output of df:

In this example, the system has two file systems, /dev/sda1 and /dev/sdb1, mounted at the root directory (/) and the /data directory, respectively.

free

The free command is used to display information about the total amount of physical memory and swap memory available, as well as the amount of memory being used and the amount of memory available for processes to allocate.

By default, free displays information in a table format, with columns indicating the following information:

  • total: the total amount of physical memory or swap memory available.
  • used: the amount of physical memory or swap memory being used.
  • free: the amount of physical memory or swap memory available for processes to allocate.
  • shared: the amount of physical memory shared between processes.
  • buffers: the amount of physical memory used as buffers.
  • cache: the amount of physical memory used as a cache.
  • available: the amount of physical memory available to allocate to running processes, taking into account cache and buffers.

Example usage:

This command displays the information about the total amount of physical memory and swap memory available, as well as the amount of memory being used and the amount of memory available for processes to allocate. Here's a sample output for free:

In this example, the system has 1511588 KB of physical memory, and 1535360 KB of swap memory. Of the physical memory, 197620 KB is being used, 0 is free, 3368 KB is shared, and 876660 KB is used as buffers/cache. The available column shows 1138800 KB of physical memory available to allocate to running processes, taking into account cache and buffers.

top

The top command is used to display the processes currently running on a Linux system, and information about the system resources being used. It is a dynamic, real-time view of the system's processes and resource usage.

By default, the top displays information about the following:

  • Current system uptime.
  • The number of users logged into the system.
  • Load average: an indicator of system activity, represented by three values representing the average number of system processes over the past 1, 5, and 15 minutes.
  • The total number of tasks (processes) running and the number of tasks in each process state (running, sleeping, stopped, etc.).
  • Physical memory usage and swap memory usage.
  • The list of processes is sorted by CPU usage. For each process, the top displays the following information:
    • Process ID (PID).
    • The user running the process.
    • CPU usage percentage.
    • Memory usage.
    • Command name.
username@Technoscience:~$ top
username@Technoscience:~$ lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              1
On-line CPU(s) list: 0
Thread(s) per core:  1
Core(s) per socket:  1
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               69
Model name:          Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz
Stepping:            1
CPU MHz:             2494.228
BogoMIPS:            4988.45
Hypervisor vendor:   KVM
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            3072K
NUMA node0 CPU(s):   0
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm invpcid_single pti fsgsbase bmi1 avx2 bmi2 invpcid md_clear flush_l1d
username@Technoscience:~$

Example usage:

To display the real-time view of the processes running on the system and information about the system resources being used.

Here's a sample output of the top:

In this example, the system has been up for 3:25 minutes. The load average is 0.10, 0.05, and 0.01 for the past 1, 5, and 15 minutes, respectively. There are 110 tasks running, with 1 task running and 77 tasks sleeping. 99.5% of the CPU is idle. The system has 1511588 KB of physical memory and 1535360 KB of swap memory. The first process listed, with PID 819, is using 0.3% of the CPU and 3.2% of the memory, and has been running for 0:24:15. The second process listed, with PID 1, is using 0.0% of the CPU and 0.6% of the memory, and has been running for 0:01:27.

htop

'htop' is a system monitoring tool for Linux that provides a real-time, interactive overview of the processes running on a system. It provides information on the system's performance, including CPU usage, memory usage, and other statistics. Here are some of the key features of 'htop':

  • User-friendly interface: 'htop' provides a visually appealing and easy-to-use interface compared to the standard 'top' command.
  • Color coding: Different process states, such as running, sleeping, or stopping, are color-coded to make it easier to distinguish between processes.
  • Sortable columns: 'htop' allows you to sort the process list by multiple criteria, such as CPU usage, memory usage, and process ID.
  • Detailed process information: 'htop' provides a detailed process information screen that can be accessed by selecting a process with the cursor. This screen provides information on the process's status, memory usage, and other statistics.
  • Process tree view: 'htop' displays a process tree view that shows the relationships between processes and the parent-child relationships between processes.
  • Real-time updates: 'htop' updates its display in real-time, allowing you to see changes in system performance as they occur.
  • Interactive controls: 'htop' provides interactive controls that allow you to perform actions, such as killing a process, directly from the 'htop' interface.

In summary, 'htop' is a powerful and user-friendly system monitoring tool that provides a real-time, interactive overview of the performance of a Linux system.

lscpu 

The command 'iscpu' is not a standard Linux command. It appears to be a typo or a custom command that may not exist on your system.

If you were looking for information about the 'lscpu' command, it is a Linux utility that provides information about the system's CPU architecture. The information provided by 'lscpu' includes the processor type, number of cores, clock speed, and other information. 

To run 'lscpu', open a terminal window and type 'lscpu', and press enter.

These commands are commonly used to gather information about the system, monitor its performance, and diagnose problems.

Reading Material
System Information & Process Management Lesson 11 of 14
In Progress

Process Management Linux Commands

Process management is the process of managing and controlling the execution of processes in a computer system. A process is a program in execution, which consists of a series of instructions and data that are executed by the CPU. In a multi-tasking operating system, multiple processes can be executed simultaneously, and process management is responsible for allocating system resources such as CPU time, memory, and I/O devices to these processes.

The main goals of process management are to ensure that processes are executed efficiently, that they do not interfere with each other, and that they are terminated gracefully when they are no longer needed. To achieve these goals, process management provides various services such as process creation, process scheduling, process synchronization, and process termination.

The following commands allow you to view information about running processes, change their priority, and terminate them if necessary.

ps

ps[OPTIONS]
username@Technoscience:~$ ps
  PID TTY          TIME CMD
 1724 pts/0    00:00:00 bash
 3927 pts/0    00:00:00 ps
username@Technoscience:~$

The 'ps' command is used to display information about the currently running processes on a Linux system.

The basic syntax for using 'ps' is:

Some common options for the 'ps' command include:

  • '-e' or '--every': display information about all processes, not just those started by the current user.
  • '-f' or '--forest': display the process hierarchy as a tree.
  • '-u' or '--user': display information about processes for a specific user.
  • '-A' or '--all': display information about all processes, not just those that are running.

By default, 'ps' displays information about the process ID (PID), the terminal associated with the process, the process owner, the process status, and the command used to start the process.

The 'ps' command is a basic and powerful tool for investigating and managing processes on a Linux system and is commonly used in combination with other commands like 'kill' or 'top' to control the behavior of processes and the allocation of system resources.

top

top

The 'top' command is a real-time system monitoring utility in Linux that displays information about the system's running processes.

The basic syntax for using 'top' is:

When run, 'top' displays a continuously updated list of processes, showing their current status, the amount of CPU and memory resources they are using, and other information such as the user running the process and the process ID (PID).

username@Technoscience:~$ top

Some common options for the 'top' command include:

  • '-d' or '--delay': specify the delay between updates in seconds.
  • '-p' or '--pid': display information about a specific process ID.
  • '-u' or '--user': display information about processes for a specific user.

By default, 'top' sorts the process list by the number of CPU resources being used, but you can sort by other criteria, such as memory usage or process ID, by pressing certain keys while 'top' is running.

Its usage is simple, you just typetop, and the terminal will be fully immersed in this new view:

The process is long-running. To quit, you can type the q letter orCtrl + C

There's a lot of information being given to us: the number of processes, how many are running or sleeping, the system load, the CPU usage, and a lot more.

Below, the list of processes taking the most memory and CPU is constantly updated. By default, as you can see from the%CPUcolumn highlighted, they are sorted by the CPU used

The 'top' command is a powerful tool for understanding how system resources are being used and for identifying processes that may be causing performance issues. It is often used by system administrators and developers to diagnose and resolve performance problems on Linux systems.

htop

htop
username@Technoscience:~$ htop

'htop' is an alternative to the 'top' command in Linux. It is a real-time system monitoring utility that provides a more user-friendly interface and additional features compared to 'top'.

The basic syntax for using 'htop' is:

When run, 'htop' displays a continuously updated list of processes, showing their current status, the amount of CPU and memory resources they are using, and other information such as the process ID (PID), the user running the process, and the command used to start the process.

Some of the key features of 'htop' include:

  • A color-coded interface that makes it easier to quickly identify processes and system resources.
  • The ability to sort the process list by multiple criteria, such as CPU usage, memory usage, process ID, and more.
  • A detailed process view that shows information about a specific process, including its full command line, environment variables, and more.
  • A system status bar that provides information about system load, memory usage, and other key metrics.
  • Support for mouse-based navigation and process management, making it easier to interact with the utility.
Use the following command to list of processes, showing their current status, and the amount of CPU and memory resources.

'htop' is a useful tool for system administrators and developers who need to monitor the performance of Linux systems and manage processes in real time. It provides a more user-friendly interface and additional features compared to 'top', making it a popular alternative for system monitoring on Linux.

kill

kill[signal] PID
username@Technoscience:~$ kill 4238
kill-HUP<PID>
kill-INT<PID>
kill-KILL<PID>
kill-TERM<PID>
kill-CONT<PID>
kill-STOP<PID>

The 'kill' command is a Linux utility that is used to send a signal to a process. It can be used to terminate a process, stop a process, or resume a stopped process. The syntax for using the 'kill' command is as follows:

Where PID is the process ID of the process you want to signal, and the signal is an optional argument that specifies the type of signal to be sent. If no signal is specified, the default signal SIGTERM (terminate) is sent.

Some common signals that can be sent using the 'kill' command include:

  • SIGTERM (terminate): This is the default signal sent by 'kill' and is used to request that a process terminate gracefully.
  • SIGKILL (kill): This signal cannot be ignored or caught by the process, and is used to force a process to terminate immediately.
  • SIGSTOP (stop): This signal stops a process, and it cannot be resumed until a SIGCONT signal is sent.
  • SIGCONT (continue): This signal resumes a stopped process.

It's not just used to terminate a program like a name would suggest, but that's its main job. We use it in this way:

By default, this sends theTERMsignal to the process id specified. We can use flags to send other signals, including:

  • HUPmeans to hang up. It's sent automatically when a terminal window that started a process is closed before terminating the process.
  • INTmeans to interrupt, and it sends the same signal used when we pressctrl-Cin the terminal, which usually terminates the process.
  • KILLis not sent to the process, but to the operating system kernel, which immediately stops and
    terminates the process.
  • TERMmeansterminate. The process will receive it and terminate itself. It's the default signal sent by kill.
  • CONTmeanscontinue. It can be used to resume a stopped process.
  • STOP is not sent to the process, but to the operating system kernel, which immediately stops (but does not terminate) the process.

You might see numbers used instead, likekill -1 <PID>

In this case, 

  • 1corresponds toHUP
  • 2corresponds toINT
  • 9 corresponds toKILL
  • 15corresponds toTERM
  • 18corresponds toCONT
  • 15corresponds to STOP

It's important to note that the 'kill' command should be used with caution, as it can cause data loss or corruption if used improperly. Before using the 'kill' command to terminate a process, it's a good idea to try and determine what the process is doing, and if possible, to try and terminate it gracefully using SIGTERM first.

pkill

pkill[options]pattern

The 'pkill' command is a Linux utility that is used to send a signal to a process based on its name, rather than its process ID. It is similar to the 'killall' command, but with some key differences. 

The syntax for using the 'pkill' command is as follows:

Where "pattern" is a string that matches the name of the process you want to signal, and options are optional arguments that specify the type of signal to be sent and other behavior of the 'pkill' command.

Some common options that can be used with 'pkill' include:

  • -signal: Specifies the type of signal to be sent. If no signal is specified, the default signal SIGTERM (terminate) is sent.
  • -f: Matches the process name against the full command line, rather than just the process name.
  • -u: username: Limits the search for processes to those owned by the specified user.
  • -n: Matches the most recently started process.
Like the 'kill' command, it's important to use 'pkill' with caution, as it can cause data loss or corruption if used improperly. Before using 'pkill' to terminate a process, it's a good idea to try and determine what the process is doing, and if possible, to try and terminate it gracefully using SIGTERM first.

killall

killall[options]name

The 'killall' command is a Linux utility that is used to send a signal to multiple processes based on their names. It is similar to the 'pkill' command, but with some key differences. The syntax for using the 'killall' command is as follows:

Where name is the name of the process you want to signal, and options are optional arguments that specify the type of signal to be sent and other behavior of the 'killall' command.

Some common options that can be used with 'killall' include:
  • -signal: Specifies the type of signal to be sent. If no signal is specified, the default signal SIGTERM (terminate) is sent.
  • -e: Matches the exact process name, rather than a partial match.
  • -I: Matches the process name exactly, ignoring the case.
  • -u username: Limits the search for processes to those owned by the specified user.
Like the 'kill' and 'pkill' commands, it's important to use 'killall' with caution, as it can cause data loss or corruption if used improperly. Before using 'killall' to terminate multiple processes, it's a good idea to try and determine what the processes are doing, and if possible, to try and terminate them gracefully using SIGTERM first.

nice

nice[options] [command[arguments]]
nice-n [priority level] [command[arguments]]
username@Technoscience:~$ nice -n 5 ls
Desktop  Documents  Downloads  Music  newdir  PicturesPublicTemplates  Videos
username@Technoscience:~$

The 'nice' command is a Linux utility used to set the priority of a process. By default, processes run with a priority of 0, but the 'nice' command allows you to specify a different priority level, which can range from -20 (highest priority) to 19 (lowest priority). The higher the priority of a process, the more CPU time it will receive.

The syntax for using the 'nice' command is as follows:

Where options are optional arguments that specify the priority level and other behavior of the 'nice' command, and command [arguments] is the command you want to run with the specified priority.

If no priority level is specified, the default priority level of 10 is used. To specify a different priority level, use the -n option followed by the priority level:

For example, to run the 'ls' command with a priority level of 5, you would use the following command:

Note that the 'nice' command only affects the priority of a process, not the amount of memory or other resources it can access. Additionally, only processes run by the superuser can increase their priority, while all users can decrease the priority of their own processes.

renice

renice priority[-p]processID[processID ...]
username@Technoscience:~$ renice 5 -p 12345

The 'renice' command is a Linux utility used to change the priority of an already running process. It allows you to adjust the priority of a process, which determines how much CPU time the process will receive. By default, processes run with a priority of 0, but the 'renice' command allows you to specify a different priority level, which can range from -20 (highest priority) to 19 (lowest priority).

The syntax for using the 'renice' command is as follows:

Where priority is the new priority level you want to set for the process, processID is the ID of the process you want to change the priority of, and -p is an optional argument that specifies that you want to change the priority of a process by its process ID, rather than by its name.

For example, to change the priority of the process with ID 12345 to a priority level of 5, you would use the following command:

Note that the 'renice' command only affects the priority of a process, not the amount of memory or other resources it can access. Additionally, only processes run by the superuser can increase the priority of other processes, while all users can decrease the priority of their own processes.

These are some of the basic process management commands in Linux. These commands provide a way to control and monitor the processes running on a Linux system and are an important tool for system administrators and power users.
Reading Material
User Administration & System Control Lesson 12 of 14
In Progress

User Management Linux Commands

User Management Linux Command is a set of commands and utilities that are used to manage user accounts and permissions on a Linux system. 

These commands allow system administrators to create, modify, delete, and manage user accounts, as well as control access to system resources and perform password authentication. 

adduser

"adduser" is a command used in Linux to create a new user account. It prompts for information such as the new user's name, password, and default settings, and then creates the new user account. This command can only be run by a user with administrative privileges, such as root. The complete syntax for the adduser command is:

adduser [Options] username

Options include --home to specify a custom home directory for the new user, --shell to specify a custom shell, --no-create-home to not create a home directory and others. 

username@Technoscience:~$ sudo adduser myuser
[sudo] password for username:
Adding user `myuser' ...
Adding new group `myuser' (1001) ...
Adding new user `myuser' (1001) with group `myuser' ...
Creating home directory `/home/myuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for myuser
Enter the new value, or press ENTER for the default
Full Name []: My User
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
username@Technoscience:~$
useradd [OPTIONS] USERNAME
usermod [options] username
username@TechnoScience:~$ usermod -g admin myuser
userdel [options] username
username@TechnoScience:~$ sudo userdel newuser
username@TechnoScience:~$
username@TechnoScience:~$ sudo userdel -r myuser
passwd [OPTION]...[USER]
username@TechnoScience:~$ sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
username@TechnoScience:~$
username@TechnoScience:~$ sudo passwd newuser
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
username@TechnoScience:~$
chage [-l] [-d LAST_DAY] [-E EXPIRE_DATE] [-I INACTIVE_DAYS] [-m MIN_DAYS] [-M MAX_DAYS] [-W WARN_DAYS] username
id [OPTIONS][USERNAME]
username@TechnoScience:~$ id
uid=1000(username) gid=1000(username) groups=1000(username),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),111(lpadmin),112(sambashare)
username@TechnoScience:~$
groups [OPTION] [USERNAME]
username@TechnoScience:~$ groups
username adm cdrom sudo dip plugdev lpadmin sambashare
username@TechnoScience:~$
su [OPTION][USERNAME]
username@TechnoScience:~$ su
Password:
root@TechnoScience:/home/username$
username@TechnoScience:~$ su myuser
Password:
myuser@TechnoScience:/home/username$
myuser@TechnoScience:/home/username$ su -c pwd username
Password:
/home/username
myuser@TechnoScience:/home/username$
sudo [command]
username@TechnoScience:~$ sudo apt-get update

The created user can be further configured using other user management commands such as usermod and userdel.

For Examaple:


useradd

useradd is a Linux command used to add new users to the system. It creates a new account with the specified username, password, and other options, such as the default home directory, shell, and group membership. 

Usage:

Options:

  • -d, --home-dir: Specifies the user's home directory.
  • -m, --create-home: Creates the user's home directory.
  • -s, --shell: Specifies the user's default shell.
  • -g, --gid: Specifies the user's primary group.
  • -G, --groups: Specifies the user's additional groups.
  • -c, --comment: Specifies the user's full name or other information.
For a complete list of options and more details on using useradd, refer to the man pages.

usermod

usermod is a Linux command used to modify the attributes of an existing user account. Some of the attributes that can be modified using usermod include:
  • Login name
  • User ID (UID)
  • Group ID (GID)
  • Home Directory
  • Login shell
  • Comment field (GECOS)

The basic syntax for usermod is:

Where username is the name of the user account you want to modify, and options specify the specific changes you want to make.

For example, to change the primary group of the user john to admin, you would use the following command:

For more information and usage options, refer to the man page for usermod by typing man usermod in the terminal.

userdel

The userdel command is used to delete a user account in a Linux system. This command removes the user's home directory, mail spool, and other files associated with the user, and also updates the system databases.

Syntax: userdel [options] username

Options: 

  • -r: removes the user's home directory and all its contents 
  • -f: force to delete user account even if it is in use or has files

Example:

Deletes the user account "newuser".

Deletes the user account "myusr" along with its home directory and its contents.

Note: It is advisable to use userdel with caution, as all user data will be permanently deleted and cannot be recovered.

passwd

passwd is a Linux command used to change a user's password.

Usage:

Options:

  • -a, --all: report password status on all accounts 
  • -d, --delete: delete the password for the named account 
  • -e, --expire: immediately expire an account's password 
  • -h, --help: display this help message and exit 
  • -k, --keep-tokens: change password only if user token count is less than the minimum 
  • -l, --lock: lock the password for the named account 
  • -n, --minimum=MIN: set minimum number of days before password change to MIN 
  • -q, --quiet: quiet mode 
  • -r, --root: CHROOT_DIR directory to chroot into 
  • -u, --unlock: unlock the password for the named account 
  • -x, --maximum=MAX: set the maximum number of days before password change to MAX

Examples:

To change your own password: passwd

To change another user's password (as root): passwd <username>

Note: The behavior of passwd may vary depending on the system and distribution you are using.

chage

"chage" is a Linux command for changing user account aging information. It allows you to modify the password aging information for a user account, such as the number of days between password changes, the number of days after which the password must change, the minimum number of days between password changes, and the maximum number of days a password is valid. 

Usage: 

id

The "id" command is used to display the user and group information of a specified user or the current user if no username is provided. It displays the user ID (UID) and group ID (GID) numbers, as well as the user and group names, the number of supplementary groups the user is a member of, and the user's default security context. 

The command has the following syntax:

Some common options are:

  • -a: display all information about the user, including group and supplementary group information.
  • -g: display only the group ID number.
  • -u: display only the user ID number.

Example:

In this example, the command was executed without a specified username, so it displayed information about the current user. The output shows the UID and GID numbers, along with the names of the user and primary group, and the names of the supplementary groups of the user is a member of.

groups

The "groups" command displays the names of the groups a user is a member of. It takes the username as an argument and by default shows the group of the current user. The output is a list of group names separated by spaces.

Syntax:

Options:
  • -a, --all: display groups of all users in the system, not just the current user
  • -n, --numeric-id: display the numeric IDs of the groups instead of the group names

Example: 

groups to display the groups of the current user. groups [username] to display the groups of a specific user.

su

"su" is a Linux command that stands for "switch user." It allows you to temporarily switch to another user account, either by specifying the username or by default to the root user. The "su" command is used to switch user context from the current user to another user.

Syntax: 

Options: 
  • -c, --command=COMMAND: Run a command with specified arguments as the target user. 
  • -l, --login: Start a login shell with an environment similar to a real login.
  • -m, --preserve-environment: Preserve the environment.

Example:

Switch to the root user account:

switch to the specified user account:

su -c '[command]' [username]: run a command as the specified user.

sudo

sudo is a Linux/Unix command that allows users to run programs with the security privileges of another user, usually the superuser. It is commonly used to perform administrative tasks.

The basic syntax for sudo is:

For example:

Here, the sudo command allows the user to run the apt-get update command with superuser privileges, which is necessary for updating the system packages.

Note that sudo requires a password for authentication, usually the password of the user who runs the sudo command. Also, the users who are allowed to use sudo are specified in the /etc/sudoers file, which can be edited using the visudo command.

Reading Material
User Administration & System Control Lesson 13 of 14
In Progress

System Administration Linux Commands

System administration Linux commands are used to manage and monitor the operating system and its resources. 

These commands include:

reboot

"reboot" is a Linux command used to restart the system. It is executed with superuser privileges and it causes the system to go down, restart, and go back up again. 

The basic syntax of the command is:

reboot [Options]

Options available for reboot command include:

  • -f or --force: Forces the system to go down and restart immediately, bypassing the normal shutdown procedures.
  • -n or --no-sync: Skips the step of writing all unwritten data to disk before rebooting, which can make the reboot faster but may cause data loss.
  • -w or --wtmp-only: Only updates the wtmp (login accounting) file, without actually rebooting the system.
  • -h or --halt: Halts the system after shutting it down.

Before using the reboot command, it's important to make sure all running processes have been saved and all open files have been closed to prevent data loss.

shutdown

"shutdown" is a Linux command used to shut down or reboot the system.

Usage: 

shutdown [OPTION]... [TIME] [WALL...] 

Options: 

  • -r, --reboot: reboot the system 
  • -h, --halt: halt the system 
  • -P, --poweroff: power-off the system 
  • -H, --halt, --poweroff: halt or power-off the system 
  • -c, --cancel: cancel a running shutdown 
  • -k, --kill: don't halt or power off, only send warnings 
  • -q, --quiet: don't display messages Time: 
  • +m, --time=minutes: time to wait before shutting down 
  • -t, --timeout=seconds: time to wait before shutting down 

Wall message: 

[message] message to be sent to all users

Note: Only the root or a user with the proper permissions can execute this command.

init

"init" is the first process started by the kernel when the system boots and is responsible for starting other processes and setting up the system environment. The init process has a process ID (PID) of 1 and is executed by the kernel as the last step of the boot process.

"init" can take different forms, with the most common being sysvinit, upstart, and systemd. Each form provides a different set of features and management of the boot process and system processes.

"init" is used to:
  • Start system services and daemons
  • Control the runlevels of the system
  • Reap and restart orphaned processes
  • Configure the system environment, such as setting up the PATH variable
  • Manage system shutdown and reboot.

systemctl

"systemctl" is a system management command in Linux that allows controlling the state of the system and services. It is a front-end for the "systemd" system and service manager.

systemctl can be used to:
  • Start, stop, restart, reload, or status of a service
  • Enable or disable service to start at boot time
  • List all services and their status
  • Show system status, including system uptime, load average, and logged-in users
  • Control the state of the system, including rebooting, shutting down, hibernating, and suspending.

"systemctl" has a variety of options and sub-commands, a few common ones include:

  • systemctl start <service> - starts a specified service
  • systemctl stop <service> - stops a specified service
  • systemctl status <service> - displays the status of a specified service
  • systemctl list-units - lists all units and their status
  • systemctl is-active <service> - shows if a service is running or inactive
  • systemctl enable <service> - enables a specified service to start at boot time.

"systemctl" is a powerful tool that should be used with caution as improper usage can result in system instability.

chkconfig

"chkconfig" is a Linux command-line utility used to manage the system startup scripts in System V-based operating systems such as Red Hat and Fedora. It provides a way to enable or disable specific services from starting during the boot process, and also to manage the levels at which they start (for example, whether a service starts in runlevel 3, runlevel 5, or both). 
chkconfig [options] service_name [runlevels]
mrdev@kali:~$ chkconfig httpd off
mrdev@kali:~$ chkconfig --list httpd
service <service_name> <command>

The syntax for using chkconfig is:

For example, to disable the httpd service from starting during the boot process, the command would be:

To check the current runlevel settings for a service, the following command can be used:

The --list option displays the runlevel status for all services, and the runlevel status for a specific service can be displayed by specifying the service name.

service

The service command is a tool to control system services on Linux systems. It provides an interface to manage system services using the init system.

Syntax: 

Commands:

  • start: starts the specified service
  • stop: stops the specified service
  • restart: restarts the specified service
  • status: displays the status of the specified service
  • reload: reloads the configuration of the specified service without restarting it

Note: This command may not be available in all distributions as some have transitioned to using the systemctl command.

top

"top" is a real-time system monitoring tool in Linux that displays system statistics such as CPU usage, and memory usage, and processes information in a dynamic and interactive fashion. By default, it refreshes the display every few seconds, providing an ongoing display of the most resource-intensive processes on the system.

Some common options/flags include:
  • -d DELAY: Specifies the refresh rate (in seconds) for the display
  • -n NUMBER: Specifies the number of iterations (updates) the command should run for
  • -p PID: Show statistics for the specified process ID (PID) only
  • -u USER: Show statistics for the specified username only
  • -h: Displays the help screen for the top.

Example usage: 

username@TechnoScience:~$ top
username@TechnoScience:~$ ps
username@TechnoScience:~$ ps -ef
username@TechnoScience:~$ ps -aux
username@TechnoScience:~$ ps -eLf
username@TechnoScience:~$ ps -p 1021
free [Options]
username@TechnoScience:~$ free
              total        used        free      shared  buff/cache   available
Mem:        1511588      209896      337320        3292      964372     1121920
Swap:       1535360           0     1535360
username@TechnoScience:~$

top (Displays the system statistics)

ps

The ps command is a Linux utility that shows information about the currently running processes and their status. Some of its basic usage and options are:

Display information about the current terminal's processes.

Display information about all processes running on the system, along with their parent process IDs, start time, and CPU usage statistics.

Display information about all processes running on the system, along with the process owner, CPU, memory usage statistics, and process start time.

Display information about all processes running on the system, along with the process ID, parent process ID, CPU usage statistics, and thread information.

ps -p [pid]: Display information about the process with the specified process ID.

The "ps" command is a powerful tool for monitoring the performance and status of processes running on a Linux system. By using the appropriate options, you can filter the output to display only the information that you're interested in.

free

"free" is a Linux command that is used to display the total amount of free and used physical memory and swap space in the system.

Usage:

Options: 

  • -b, --bytes: Display the amount of memory in bytes. 
  • -k, --kilo: Display the amount of memory in kilobytes (default). 
  • -m, --mega: Display the amount of memory in megabytes. 
  • -g, --giga: Display the amount of memory in gigabytes. 
  • -h, --human: Display the amount of memory in a human-readable format. 
  • -c, --count=N: Repeat the display N times. 
  • -s, --seconds=N: Wait N seconds between each display. 
  • -t, --total: Show the total sum of memory. 
  • -V, --version: Show version information.

Example:

username@TechnoScience:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              722484       0    722484   0% /dev
tmpfs             151160    1024    150136   1% /run
/dev/sda1       32426780 4791320  25965216  16% /
tmpfs             755792       0    755792   0% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs             755792       0    755792   0% /sys/fs/cgroup
tmpfs             151156      12    151144   1% /run/user/1000
/dev/sr0           51806   51806         0 100% /media/username/VBox_GAs_7.0.6
username@TechnoScience:~$
username@TechnoScience:~$ du
576./Downloads
4./.tldr
4./Documents
8./.config/pcmanfm/default
12./.config/pcmanfm
8./.config/featherpad
<SNIP>
username@TechnoScience:~$ uptime
 04:14:15 up 15:20,  1 user,  load average: 0.06, 0.03, 0.00
username@TechnoScience:~$
username@TechnoScience:~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 336816 103980 860716    0    0     8    32  117  316  0  0 97  3  0
username@TechnoScience:~$
username@TechnoScience:~$ iostat
Linux 4.15.0-88-generic (TechnoScience) 02/03/2023 _x86_64_(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.15    0.32    0.25    2.58    0.00   96.70

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.01         0.00        516          0
sda               0.87         8.68        32.81     477533    1804364

username@TechnoScience:~$
username@TechnoScience:~$ lsof
username@TechnoScience:~$ lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: VMware SVGA II Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
00:06.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0b.0 USB controller: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) USB2 EHCI Controller
00:0d.0 SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)
username@TechnoScience:~$
username@TechnoScience:~$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
username@TechnoScience:~$
username@TechnoScience:~$ systemctl
username@TechnoScience:~$ journalctl

The output shows the total memory, used memory, free memory, shared memory, buffer/cache memory, and available memory in the system.

df

display information about disk usage and available space

du 

display information about disk space usage of files and directories

uptime

display the system uptime and load average

vmstat

 display information about system processes, memory, paging, block I/O, and CPU activity.


iostat

display statistics about input/output operations

lsof

 list open files and the processes that have them open

lspci

display information about PCI buses and devices on the system


lsusb

display information about USB buses and devices on the system

systemctl

manage system services and daemons

journalctl

view system logs.

These commands provide insight into the system's resources and performance and are essential for effective system administration.
Reading Material
Networking Commands in Linux Lesson 14 of 14
In Progress

Networking Linux Commands

Networking Linux commands are used to manage, configure and troubleshoot network connections on a Linux system. 

Some common networking Linux commands are:

ifconfig

"ifconfig" is a command line tool used to configure a network interface on Linux-based systems. It displays the configuration of the network interface(s), such as IP address, netmask, broadcast address, etc. You can also use it to bring up or down an interface or to set the IP address, netmask, etc. for an interface. 

The command syntax is:

ifconfig [interface [addess]]

The "ifconfig" command in Linux is used to configure network interfaces. It can be used to display the status of current network interfaces, assign an IP address, configure the subnet mask, and broadcast address, and many other network-related tasks. Some common usage examples include: 

Displaying all interfaces and their details: ifconfig

username@Technoscience:~$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::f334:f0f:6e7f:e4ac  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:7c:13:96  txqueuelen 1000  (Ethernet)
        RX packets 86589  bytes 121679140 (121.6 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 39729  bytes 2460671 (2.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 306  bytes 28551 (28.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 306  bytes 28551 (28.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

username@Technoscience:~$

Displaying a specific interface: ifconfig <interface>

username@Technoscience:~$ ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::f334:f0f:6e7f:e4ac  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:7c:13:96  txqueuelen 1000  (Ethernet)
        RX packets 86589  bytes 121679140 (121.6 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 39729  bytes 2460671 (2.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

username@Technoscience:~$

Assigning an IP address to an interface: ifconfig <interface> <ip_address> netmask <subnet_mask>

username@Technoscience:~$ ifconfig enp0s3 10.0.2.33 netmask 255.255.255.0

Enabling or disabling an interface: ifconfig <interface> down

username@Technoscience:~$ ifconfig enp0s3 down

or ifconfig <interface> up

username@Technoscience:~$ ifconfig enp0s3 up

Note that the exact usage and options of ifconfig may vary depending on the distribution of Linux you are using.

username@Technoscience:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:7c:13:96 brd ff:ff:ff:ff:ff:ff
    inet10.0.2.15/24brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
       valid_lft 82081sec preferred_lft 82081sec
    inet6 fe80::f334:f0f:6e7f:e4ac/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
username@Technoscience:~$
username@Technoscience:~$ ip route show
default via 10.0.2.2 dev enp0s3 proto dhcp metric 100
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100
username@Technoscience:~$
username@Technoscience:~$ ip addr add 192.168.1.100/24 dev eth0
ping [OPTION] [HOSTNAME/IPADDRESS]
username@Technoscience:~$ ping google.com
PING google.com (216.58.203.46) 56(84) bytes of data.
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=1 ttl=53 time=113 ms
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=2 ttl=53 time=106 ms
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=3 ttl=53 time=154 ms
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=4 ttl=53 time=110 ms
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=5 ttl=53 time=144 ms
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=6 ttl=53 time=108 ms
64 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=7 ttl=53 time=118 ms
^C
--- google.com ping statistics ---
8 packets transmitted, 7 received, 12% packet loss, time 7025ms
rtt min/avg/max/mdev = 106.357/122.401/154.978/17.864 ms
username@Technoscience:~$
username@Technoscience:~$ ping -c 2 -W 2 google.com
PING google.com (216.58.203.46) 56(84) bytes of data.
64 bytes from hkg12s10-in-f46.1e100.net (216.58.203.46): icmp_seq=1 ttl=53 time=110 ms
64 bytes from hkg12s10-in-f46.1e100.net (216.58.203.46): icmp_seq=2 ttl=53 time=123 ms

--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 4017ms
rtt min/avg/max/mdev = 103.961/115.574/123.322/7.476 ms
username@Technoscience:~$
traceroute [options] hostname/IP address
username@Technoscience:~$ traceroute google.com
traceroute to google.com (216.58.203.46), 30 hops max, 60 byte packets
 1  _gateway (10.0.2.2)  0.856 ms  0.821 ms  0.804 ms
 2  * * *
 3  * * *
 4  * * *
 5  * 
username@Technoscience:~$
username@Technoscience:~$ traceroute -q google.com
Cannot handle `-q' option with arg `google.com' (argc 2)
username@Technoscience:~$ traceroute -q 1 google.com
traceroute to google.com (216.58.203.46), 30 hops max, 60 byte packets
 1  _gateway (10.0.2.2)  1.008 ms
 2  *
 3  *
 4  *
<SNIP>
17  *^C
username@Technoscience:~$
username@Technoscience:~$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    20813    /run/user/1000/systemd/notify
unix  3      [ ]         DGRAM                    13544    /run/systemd/notify
unix  7      [ ]         DGRAM                    13556    /run/systemd/journal/socket
<SNIP>
username@Technoscience:~$
nslookup [options] [hostname/IP address]
username@Technoscience:~$ nslookup google.com
Server:127.0.0.53
Address:127.0.0.53#53

Non-authoritative answer:
Name:google.com
Address: 172.217.166.174
Name:google.com
Address: 2404:6800:4009:80f::200e

username@Technoscience:~$
username@Technoscience:~$ nslookup -t ns google.com
;; Connection to 2404:6800:4009:80f::200e#53(2404:6800:4009:80f::200e) for ns failed: network unreachable.

username@Technoscience:~$
username@Technoscience:~$ nslookup -x 8.8.8.8
8.8.8.8.in-addr.arpaname = dns.google.

Authoritative answers can be found from:

username@Technoscience:~$
hostname [OPTION] [HOSTNAME]
username@Technoscience:~$ hostname
Technoscience
username@Technoscience:~$
username@Technoscience:~$ sudo hostname newhost
username@Technoscience:~$ hostname
newhost
username@Technoscience:~$
dig [options] [domain-name | host-name] [@server]
username@Technoscience:~$ dig google.com

; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53668
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;google.com.                INA

;; ANSWER SECTION:
google.com.        258INA216.58.203.14

;; Query time: 179 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Feb 03 00:51:19 IST 2023
;; MSG SIZE  rcvd: 55

username@Technoscience:~$
mrdev@kali:~$ ssh user@hostname
mrdev@kali:~$ scp file user@hostname:path

ip

The "ip" command is used to configure and manage network interfaces in Linux. With "ip", you can:

  • Display network interface information and configuration
  • Set network interface IP addresses and routes
  • Add/remove network addresses from interfaces
  • Display/manage the ARP table
  • Display/manage routing table information
  • Display/manage TCP/IP statistics

Example usage:

Display network interface information: "ip addr show"

Display routing table information: "ip route show"

Set IP address for an interface: "ip addr add 192.168.1.100/24 dev eth0"

Note: The "ip" command is more recent than "ifconfig" and provides more advanced features, so it is recommended to use "ip" instead of "ifconfig" in most cases.

ping

The ping command is a networking utility tool used to test the reachability of a network host and measure the round-trip time for packets to travel from the source host to the target host and back. 

The basic syntax for using ping is 

The following are some of the commonly used options with ping:

  • -c count: specifies the number of echo request packets to be sent.
  • -W timeout: sets the time-out value in seconds for waiting for a response.
  • -I interface: specifies the network interface to be used.
  • -s packetsize: sets the packet size in bytes.
  • -v: verbose output.

Example usage:

Here's an example of pinginggoogle.com:

The commands send a request to the server, and the server returns a response. "pingkeeps sending the request every second, by default, and will keep running until you stop it with ctrl + C unless you pass the number of times you want to try with the-coption: 

Once the "pingis stopped, it will print some statistics about the results: the percentage of packages lost, and statistics about the network performance.

As you can see the screen prints the host IP address, and the time that it took to get the response back. Not all servers support pinging, in case of the requests time out.

Sometimes this is done on purpose, to "hide" the server, or just to reduce the load. The ping packets
can also be filtered by firewalls.

pingworks using theICMP protocol(Internet Control Message Protocol), a network layer protocol
just like TCP or UDP. The request sends a packet to the server with the 
ECHO_REQUESTmessage and the server returns an ECHO_REPLY message. I won't go into details, but this is the basic concept.

Pinging a host is useful to know if the host is reachable (supposing it implements ping), and how
distant it is in terms of how long it takes to get back to you. Usually the nearest the server is geographic, the less time it will take to return back to you, for simple physical laws that cause a longer distance introduces more delay in the cables.

traceroute

"traceroute" is a network diagnostic tool for displaying the route packets take to the network host or destination. It shows the intermediate hops and their IP addresses, RTT (Round Trip Time), and packet loss along the path to the destination. The command provides information about the path that data follows from source to destination, identifying any network congestion, slowdowns, or faults along the way.

Syntax: 


Options:

  • -I: use ICMP packets instead of UDP
  • -m max_ttl: maximum number of hops to search for target
  • -q nqueries: number of probes per hop
  • -w wait_time: waiting time in seconds for the response
  • -n: display hostnames and IP addresses as numbers, not hostnames.

Example: 

This will show the hops, RTT, and IP addresses along the route to reach the destination google.com.

Not every router traveled returns our information. In this case,tracerouteprints* * *. Otherwise, we can see the hostname, the IP address, and some performance indicators.

For every router, we can see 3 samples, which means the traceroute tries by default 3 times to get you a good indication of the time needed to reach it. This is why it takes this long to execute traceroutecompared to simply doing apingto that host.

You can customize this number with the -qoption:

netstat

netstat is a Linux command used to display information about network connections, routing tables, and interface statistics. The command shows active TCP and UDP connections, open sockets, and information about the interfaces and protocols in use. The options available for netstat allow for detailed information about the network status to be displayed, including the IP addresses and ports in use, and the state of the connections. Some common options for netstat include:
  • -a: Display all active connections, including listening sockets.
  • -t: Display only TCP connections.
  • -u: Display only UDP connections.
  • -n: Display IP addresses and port numbers in numerical form, instead of resolving them to hostnames and service names.
  • -l: Display only listening sockets, which are sockets waiting for incoming connections.
  • -r: Display the system routing table.

By default, netstat only displays established connections. 

The output of netstat is useful for troubleshooting network issues, such as slow network performance, or for monitoring network usage.

nslookup

nslookup (name server lookup) is a command-line tool that helps to find the IP address of a domain name. It can be used to resolve the hostname to the IP address or vice versa. The complete usage of nslookup is as follows:

Options: 

  • -a: Display all information about the host. 
  • -d: Print debugging information. 
  • -r: Disable recursion. 
  • -v: Display verbose output. 
  • -t type: Specify the query type (e.g. -t MX, -t A, -t NS, etc.)
  • -x address: Reverse lookup of an IP address.

Examples:

To resolve a domain name to an IP address: nslookup google.com

To find the name server of a domain: nslookup -t ns google.com

To perform a reverse lookup of an IP address: nslookup -x 8.8.8.8

route

the "route" is a Linux command used to view and manipulate the IP routing table of a system. The IP routing table is a data structure used by the kernel to determine the path that network packets should take when they leave the local host. The route command is used to view the current routing table, add or delete routing table entries, and modify existing entries.

Here are some standard options and usage of the route command:

  • route add: Adds a new entry to the routing table
  • route delete: Deletes an entry from the routing table
  • route show: Displays the current routing table
  • route -n: Shows the routing table without performing hostname resolution
  • route -net: Shows the routing table for a specific network

The route command can also be used to view and set the default gateway for a system. The default gateway is the IP address of the router that connects the local network to the Internet.

For more information and usage examples of the route command, refer to the man pages or online documentation for your specific operating system.


hostname 

The "hostname" command in Linux is used to display or set the hostname of the system.

Syntax: 

Options: 

  • -a, --alias: Display the alias name (if used) 
  • -d, --domain: Display the name of the DNS domain 
  • -f, --fqdn, --long: Display the fully qualified domain name 
  • -A, --all-fqdns: Display all fully qualified domain names 
  • -i, --ip-address: Display the IP address(es) associated with the hostname 
  • -s, --short: Display the short hostname

Example usage:

To display the current hostname: hostname

To set a new hostname: hostname new_host_name

Note: Changing the hostname requires superuser privileges.

dig

"dig" is a network administration command-line tool used for querying the Domain Name System (DNS). It performs DNS lookups and displays the answers that are returned from the DNS server(s) that were queried.

Syntax: 

Options: 

  • -a, --address: display the IP address(es) of the host(s) 
  • -A, --any: set query type to ANY 
  • -c, --class=CLASS: query for a specific class (IN, CH, HS) 
  • -C, --tcp: use TCP instead of UDP 
  • -d, --debug enable debug output 
  • -e, --edns=0|1: set EDNS version 0 or 1 
  • -f, --file=FILE: specify a file containing the query 
  • -h, --help: display usage and exit
  • -i, --ignore: ignore truncation of responses 
  • -k, --checknames: check names in the reply 
  • -m, --msgarray: print out an array of raw message packets 
  • -M, --dnssec: request DNSSEC records 
  • -n, --ndots=NUMBER: set search NDOTS value 
  • -p, --port=PORT: set destination port number 
  • -q, --query=QUERY: set query type or class 
  • -Q, --quote: quote names in the reply 
  • -r, --recurse: set recursion desired 
  • -t, --type=TYPE: query for a specific type (A, MX, NS, ...) 
  • -u, --user=USER: set the username for TSIG 
  • -v, --verbose: enable verbose output 
  • -x, --reverse: reverse the lookups (PTR) 
  • -y, --key=KEY: set TSIG key -4, 
  • --ipv4: use IPv4 only -6, 
  • --ipv6: use IPv6 only

Examples: 

dig example.com perform a lookup for example.com 

  • dig example.com MX perform an MX record lookup for example.com 
  • dig @8.8.8.8 example.com perform a lookup for example.com using the specified DNS server 
  • dig -x IP_ADDRESS performs a reverse lookup for an IP address

ssh

Secure Shell (SSH) is a protocol used for secure communication over the internet. It provides secure encrypted communications between two untrusted hosts over an insecure network. SSH is used to log into a remote machine and execute commands, transfer files, or act as a secure SOCKS-based VPN. SSH typically uses port 22 and is based on the client-server architecture.

The client software establishes a secure connection with the server, authenticates the user, and then runs a shell session. SSH uses public-key cryptography to authenticate the remote computer and to secure the data that is being transmitted.

The ssh command is used to initiate an SSH connection in the terminal. You can connect to a remote host by specifying its hostname or IP address:

The scp command is used to securely copy files between hosts:

There are many other options and features available with SSH, including key-based authentication, port forwarding, X11 forwarding, and much more.

Getting Started with Linux & Shells Quiz

Getting Started with Linux & Shells Quiz

3 questions • Test your knowledge

Question 1
What is the primary role of a Linux shell?
Question 2
Under the Linux Filesystem Hierarchy Standard (FHS), which directory holds system-wide configuration files?
Question 3
Which shell is typically the default command shell across most modern Linux distributions?
File & Directory Management Quiz

File & Directory Management Quiz

3 questions • Test your knowledge

Question 1
Which command is used to remove a directory along with all of its files and subdirectories recursively without prompt?
Question 2
Which tar command flags are used to create a new gzipped archive named backup.tar.gz from a directory?
Question 3
What command displays the contents of a text file in reverse order (from bottom to top)?
Text Editing & Text Processing Quiz

Text Editing & Text Processing Quiz

3 questions • Test your knowledge

Question 1
Which command is used to search for lines matching a regular expression pattern inside files?
Question 2
In the vi/vim text editor, how do you save changes and exit back to the terminal from Normal mode?
Question 3
Which stream editor is commonly used in Linux pipelines for automated find-and-replace text substitution?
Search & Shell Operations Quiz

Search & Shell Operations Quiz

3 questions • Test your knowledge

Question 1
Which command queries a pre-built indexed database for rapid file searching across the entire system?
Question 2
How do you create a temporary shortcut command "la" for "ls -la" in the current Bash session?
Question 3
Which command locates the binary executable path in the user's $PATH environment variable?
System Information & Process Management Quiz

System Information & Process Management Quiz

3 questions • Test your knowledge

Question 1
Which signal is sent by default when you run "kill <PID>" without specifying a signal number?
Question 2
Which command displays disk space usage and availability across all mounted filesystems in human-readable units?
Question 3
What keystroke shortcut pauses a running foreground command and places it in the background as a stopped job in Linux?
User Administration & System Control Quiz

User Administration & System Control Quiz

3 questions • Test your knowledge

Question 1
What command changes the owner and group ownership of a file in Linux?
Question 2
In Linux numeric octal permissions, what does the permission 755 represent on a directory?
Question 3
Which command is used to inspect systemd service status and daemon control in modern Linux systems?
Networking Commands in Linux Quiz

Networking Commands in Linux Quiz

3 questions • Test your knowledge

Question 1
Which modern iproute2 command replaces the deprecated "ifconfig" to display all network interfaces and IP addresses?
Question 2
Which command displays active TCP/UDP listening sockets and ports faster and more reliably than netstat?
Question 3
Which command is used to download files non-interactively over HTTP, HTTPS, and FTP in the terminal?