Basic/Common Special symbols/Special characters in Linux

Home Directory ~

# Back to home directory
cd ~
 
# Change to sub directory within home directory
cd ~/Desktop

Current Directory .

ls -al
ls -al

. in above image means current directory

# Run script.sh in current directory
./script.sh

Parent Directory ..

cd ..
cd ..
cd ..
# Change to /test/ folder in parent folder directory
cd ../test/

Path Separator, System root folder /

ls /etc
# Change to system root folder
cd /

Comment #

#Comments are ignored by Bash shell
Comment
Comment

We can also comment part of the string from variable

# Define variable
test_string="abc def"
 
# echo the variable with abc being commented out
echo ghi ${test_string#abc}
 
# echo full variable
echo $test_string
Comment part of the string/variable
Comment part of the string/variable

Single Character Wildcard ?

? Can be used for this purpose

ls file?.txt
ls file?.txt
ls file?.txt

Since file.txt doesn’t have any characters right after file, it is not in the result.

To match exactly how many characters with ?, we use corresponding number of ?

ls ????.txt
ls ????.txt
ls ????.txt

Wildcard *

Match any character including space

ls file*
ls file*
ls file*

Match any type of files

ls file.*

Character Set Wildcard []

Match at least one of the characters in the []

ls file0[123].txt
ls file0[123].txt
ls file0[123].txt
ls file[012][012].txt
ls file[012][012].txt

Shell Command Separator (Run till the last one anyway) ;

command 1; command 2; command 3
command 1; command 2; command 3
command 1; command 2; command 3

When using Shell Command Separator “;” no matter the previous command succeeded or not, it will run till the last command

Shell Command Separator (Stop if there is error/failed) &&

command 1 && command 2 && command 3
command 1 && command 2 && command 3
command 1 && command 2 && command 3

Background Process &

Add & behind the command to run the command in the background

command &
Run the command in the background
Run the command in the background

1709 is the process ID of this background task

Input Redirection <

command < file
sort test file
sort test file
wc test
 
wx < test

(< does not display source filename)

Output Redirection >

ls > ListOfFiles.txt
 
# To read the file again
cat ListOfFiles.txt

It can be used with stderr, stdin, stdout (See bottom of this page)

cat test.txt 2> err.txt
Use with stderr
Use with stderr

Pipe |

command 1 | command 2 | command 3
cat test | grep [Aa] | sort -r
Demo or pipe
Demo or pipe

1 Use cat to read test, pip to grep command

2 Use grep to filter the test, only left with text including A and a, pipe to sort command

3 Use sort to reverse sort the text

Pipeline logical NOT and History Operator !

[ ! -d ./testfolder ] && echo "Folder testfolder does not exist"

[ ! -d ./testfolder ]: Check if the folder named testfolder exists

If does not exist, echo the text Folder testfolder does not exist

Run history command

!number
 
# e.g.
!210
Run history command
Run history command
# Run last command
!!
Run last command
Run last command

Variable Expressions $

In Bash shell, $ usually means variables

We can use echo to view the values of the variable

echo $PATH
 
echo $USER
 
echo $HOME
Show values in variables
Show values in variables

We can define variables in following ways

year=2020
MyName=Fred
Define variable

We can use {} to perform other advanced manipulation of text

# Define variable
string=12345qwert
 
# Output the string
echo ${string}
 
# Output text begin from index of 3 (Begin from 0)
echo ${string:3}
 
# Output 3 characters begin from index 0
echo ${string:0:3}
 
# Output 2 characters begin from index 2
echo ${string:2:2}

Quoting Special Characters “” ”

Use “” to stop special character functions (But not $)

echo "$string"
Double quotes
Double quotes

Use single quotes ” to disable all special character’s function

echo '$string'
Single quotes
Single quotes

We can also use backslash \ to prevent the following character to be functioning as a special character

echo "\$string"
Backslash
Backslash

Extend Reading

More on redirection, pipeline,stdin,stdout,stderr | Linux – Basics, Useful Terminal commands and Basic File manipulation, (QuickStart)

More on flow control, if else, loop, variable Linux Bash/Shell simple, basic flow control


How to: Check Linux disk/partition information (Alternative to df)

To check Linux partition/disk usage, mount, file system types information etc. we can use df command which mentioned in “Useful commands to look up system information, install package etc. (Linux, Debian, Ubuntu, Kali Linux, RedHat, CentOS etc.)

We can also use another alternative to df which is di

1 Install

# Debian/Ubuntu/Kali Linux etc.
sudo apt install di
 
# CentOS/Fedora/RHEL
sudo dnf install di

2 Usage

2.1 Default output

[email protected]:~$ di
Filesystem         Mount               Size     Used    Avail %Used  fs Type
/dev/sda2          /                 196.3G    15.2G   171.1G   13%  ext4   
/dev/sda1          /boot/efi         511.0M     0.1M   510.8M    0%  vfat   
tmpfs              /dev/shm            1.9G     0.0G     1.9G    0%  tmpfs  
tmpfs              /run              391.3M    15.8M   375.4M    4%  tmpfs  
tmpfs              /run/lock           5.0M     0.0M     5.0M    0%  tmpfs  
tmpfs              /run/user/1000    391.3M     0.0M   391.3M    0%  tmpfs  
tmpfs              /sys/fs/cgroup      1.9G     0.0G     1.9G    0%  tmpfs
di
di

(By default di uses human readable units, with df we need to use -h switch “df -h”)

2.2 Show detailed mount point information, special device name etc.

di -A
[email protected]:~$ di -A
Mount          fs Type Filesystem 
        Options                                                                                               
            Size     Used     Free %Used  %Free 
            Size     Used    Avail %Used  %Free 
            Size     Used    Avail %Used  
           Inodes     IUsed     IFree %IUsed
/              ext4    /dev/sda2  
        rw,relatime,errors=remount-ro                                                                         
          196.3G    15.2G   181.2G    8%    92%  
          196.3G    25.2G   171.1G   13%    87%  
          186.3G    15.2G   171.1G    8%  
         13165632    570127  12595505    4% 
/boot/efi      vfat    /dev/sda1  
        rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 
          511.0M     0.1M   510.8M    0%   100%  
          511.0M     0.1M   510.8M    0%   100%  
          511.0M     0.1M   510.8M    0%  
                0         0         0    0% 
/dev/shm       tmpfs   tmpfs      
        rw,nosuid,nodev                                                                                       
            1.9G     0.0G     1.9G    0%   100%  
            1.9G     0.0G     1.9G    0%   100%  
            1.9G     0.0G     1.9G    0%  
           500837         1    500836    0% 
/run           tmpfs   tmpfs      
        rw,nosuid,nodev,noexec,relatime,size=400672k,mode=755                                                 
          391.3M    15.8M   375.4M    4%    96%  
          391.3M    15.8M   375.4M    4%    96%  
          391.3M    15.8M   375.4M    4%  
           500837       668    500169    0% 
/run/lock      tmpfs   tmpfs      
        rw,nosuid,nodev,noexec,relatime,size=5120k                                                            
            5.0M     0.0M     5.0M    0%   100%  
            5.0M     0.0M     5.0M    0%   100%  
            5.0M     0.0M     5.0M    0%  
           500837         4    500833    0% 
/run/user/1000 tmpfs   tmpfs      
        rw,nosuid,nodev,relatime,size=400668k,mode=700,uid=1000,gid=1000                                      
          391.3M     0.0M   391.3M    0%   100%  
          391.3M     0.0M   391.3M    0%   100%  
          391.3M     0.0M   391.3M    0%  
           500837        28    500809    0% 
/sys/fs/cgroup tmpfs   tmpfs      
        ro,nosuid,nodev,noexec,mode=755                                                                       
            1.9G     0.0G     1.9G    0%   100%  
            1.9G     0.0G     1.9G    0%   100%  
            1.9G     0.0G     1.9G    0%  
           500837        17    500820    0% 
di -A
di -A

2.3 Show all mounted devices

di -a
[email protected]:~$ di -a
Filesystem         Mount               Size     Used    Avail %Used  fs Type            
/dev/sda2          /                 196.3G    15.2G   171.1G   13%  ext4               
/dev/sda1          /boot/efi         511.0M     0.1M   510.8M    0%  vfat               
udev               /dev                1.9G     0.0G     1.9G    0%  devtmpfs           
hugetlbfs          /dev/hugepages      0.0M     0.0M     0.0M    0%  hugetlbfs          
mqueue             /dev/mqueue         0.0M     0.0M     0.0M    0%  mqueue             
devpts             /dev/pts            0.0M     0.0M     0.0M    0%  devpts             
tmpfs              /dev/shm            1.9G     0.0G     1.9G    0%  tmpfs              
proc               /proc               0.0M     0.0M     0.0M    0%  proc               
systemd-1          /proc/sys/fs/bi     0.0M     0.0M     0.0M    0%  autofs             
binfmt_misc        /proc/sys/fs/bi     0.0M     0.0M     0.0M    0%  binfmt_misc        
tmpfs              /run              391.3M    15.8M   375.4M    4%  tmpfs              
tmpfs              /run/lock           5.0M     0.0M     5.0M    0%  tmpfs              
sunrpc             /run/rpc_pipefs     0.0M     0.0M     0.0M    0%  rpc_pipefs         
tmpfs              /run/user/1000    391.3M     0.0M   391.3M    0%  tmpfs              
/dev/fuse          /run/user/1000/     0.0M     0.0M     0.0M    0%  fuse               
gvfsd-fuse         /run/user/1000/     0.0M     0.0M     0.0M    0%  fuse.gvfsd-fuse    
vmware-vmblock     /run/vmblock-fu     0.0M     0.0M     0.0M    0%  fuse.vmware-vmblock
sysfs              /sys                0.0M     0.0M     0.0M    0%  sysfs              
efivarfs           /sys/firmware/e     0.0M     0.0M     0.0M    0%  efivarfs           
none               /sys/fs/bpf         0.0M     0.0M     0.0M    0%  bpf                
tmpfs              /sys/fs/cgroup      1.9G     0.0G     1.9G    0%  tmpfs              
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup             /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup             
cgroup2            /sys/fs/cgroup/     0.0M     0.0M     0.0M    0%  cgroup2            
fusectl            /sys/fs/fuse/co     0.0M     0.0M     0.0M    0%  fusectl            
pstore             /sys/fs/pstore      0.0M     0.0M     0.0M    0%  pstore             
debugfs            /sys/kernel/deb     0.0M     0.0M     0.0M    0%  debugfs            
securityfs         /sys/kernel/sec     0.0M     0.0M     0.0M    0%  securityfs
di -a
di -a

2.4 Produce CSV style output

di -c
[email protected]:~$ di -c
s,m,b,u,v,p,T
"/dev/sda2","/","196.3G","15.2G","171.1G",13%,"ext4"
"/dev/sda1","/boot/efi","511.0M","0.1M","510.8M",0%,"vfat"
"tmpfs","/dev/shm","1.9G","0.0G","1.9G",0%,"tmpfs"
"tmpfs","/run","391.3M","15.8M","375.4M",4%,"tmpfs"
"tmpfs","/run/lock","5.0M","0.0M","5.0M",0%,"tmpfs"
"tmpfs","/run/user/1000","391.3M","0.0M","391.3M",0%,"tmpfs"
"tmpfs","/sys/fs/cgroup","1.9G","0.0G","1.9G",0%,"tmpfs"
di -c
di -c

2.5 Output size in Gigabyte

di -g

We can also use -k or -m to indicate output in K or M

[email protected]:~$ di -g
Filesystem         Mount              Gibis     Used    Avail %Used  fs Type
/dev/sda2          /                  196.3     15.2    171.1   13%  ext4   
/dev/sda1          /boot/efi            0.5      0.0      0.5    0%  vfat   
tmpfs              /dev/shm             1.9      0.0      1.9    0%  tmpfs  
tmpfs              /run                 0.4      0.0      0.4    4%  tmpfs  
tmpfs              /run/lock            0.0      0.0      0.0    0%  tmpfs  
tmpfs              /run/user/1000       0.4      0.0      0.4    0%  tmpfs  
tmpfs              /sys/fs/cgroup       1.9      0.0      1.9    0%  tmpfs
di -g
di -g

2.6 Display information for specific file system

di -I filesystem
 
e.g.
di -I tmpfs
[email protected]:~$ di -I tmpfs 
Filesystem         Mount               Size     Used    Avail %Used  fs Type
tmpfs              /dev/shm            1.9G     0.0G     1.9G    0%  tmpfs  
tmpfs              /run              391.3M    15.8M   375.4M    4%  tmpfs  
tmpfs              /run/lock           5.0M     0.0M     5.0M    0%  tmpfs  
tmpfs              /run/user/1000    391.3M     0.0M   391.3M    0%  tmpfs  
tmpfs              /sys/fs/cgroup      1.9G     0.0G     1.9G    0%  tmpfs
di -I tmpfs
di -I tmpfs

2.7 Skip title row

di -n
[email protected]:~$ di -n
/dev/sda2          /                 196.3G    15.2G   171.1G   13%  ext4   
/dev/sda1          /boot/efi         511.0M     0.1M   510.8M    0%  vfat   
tmpfs              /dev/shm            1.9G     0.0G     1.9G    0%  tmpfs  
tmpfs              /run              391.3M    15.8M   375.4M    4%  tmpfs  
tmpfs              /run/lock           5.0M     0.0M     5.0M    0%  tmpfs  
tmpfs              /run/user/1000    391.3M     0.0M   391.3M    0%  tmpfs  
tmpfs              /sys/fs/cgroup      1.9G     0.0G     1.9G    0%  tmpfs
di -n
di -n

2.8 Show total count at the bottom of the output

di -t
[email protected]:~$ di -t
Filesystem         Mount               Size     Used    Avail %Used  fs Type
/dev/sda2          /                 196.3G    15.2G   171.1G   13%  ext4   
/dev/sda1          /boot/efi         511.0M     0.1M   510.8M    0%  vfat   
tmpfs              /dev/shm            1.9G     0.0G     1.9G    0%  tmpfs  
tmpfs              /run              391.3M    15.8M   375.4M    4%  tmpfs  
tmpfs              /run/lock           5.0M     0.0M     5.0M    0%  tmpfs  
tmpfs              /run/user/1000    391.3M     0.0M   391.3M    0%  tmpfs  
tmpfs              /sys/fs/cgroup      1.9G     0.0G     1.9G    0%  tmpfs  
                   Total             201.4G    15.2G   176.2G   13%
di -t
di -t

2.9 Sort the output

# Sort by name of the mount point
di -sm
[email protected]:~$ di -sm
Filesystem         Mount               Size     Used    Avail %Used  fs Type
/dev/sda2          /                 196.3G    15.2G   171.1G   13%  ext4   
/dev/sda1          /boot/efi         511.0M     0.1M   510.8M    0%  vfat   
tmpfs              /dev/shm            1.9G     0.0G     1.9G    0%  tmpfs  
tmpfs              /run              391.3M    15.8M   375.4M    4%  tmpfs  
tmpfs              /run/lock           5.0M     0.0M     5.0M    0%  tmpfs  
tmpfs              /run/user/1000    391.3M     0.0M   391.3M    0%  tmpfs  
tmpfs              /sys/fs/cgroup      1.9G     0.0G     1.9G    0%  tmpfs
di -sm
di -sm

-s: sort

m: Order by mountpoint (Default)

n: Do not sort (Use the order from “/etc/fstab”)

s: Order by name of special device

t: Order by type of fil esystem

r: Reverse the order

Sort switch can be mixed, e.g. di -stsrm: Type, Device, Mount point in reverse order. di strsrm: Type, Device in reverse order, mount point.

2.10 Specify output format

# Output mount point name
di -fm
 
# Output file system type
di -ft
[email protected]:~$ di -fm
Mount          
/              
/boot/efi      
/dev/shm       
/run           
/run/lock      
/run/user/1000 
/sys/fs/cgroup
di -fm
di -fm
di -ft
di -ft

man page of di

di(1)                                                                                                  General Commands Manual                                                                                                  di(1)
Name
       di - disk information
Synopsis
       di [-AacghHklLmnPqRtZ] [-B block-size] [-d display-size] [-f format] [-I include-fstyp-list] [-s sort-type] [-w block-width] [-W inode-width] [-x exclude-fstyp-list] [-X debug-level] [-z zone-name] [file [...]]
       mi
Description
       di  Displays  usage information on mounted filesystems.  Block values are reported in a human readable format.  If the user or group has a disk quota, the values reported are adjusted according the quotas that apply to the
       user.
       If file is specified, the usage information for the partition on which file is located is printed.
       Unless the -a flag is specified, the following mounted filesystems will not normally be displayed: filesystems with total space <= 0; loopback filesystems that are duplicates of other normally mounted filesystems (filesys‐
       tem type of 'lofs' , 'none', or 'nullfs'); loopback filesystems that are part of a zone (Solaris).
       Filesystems that the user does not have permissions to access will not be displayed at all.
       mi Displays the mounted filesystem information.
       Several options may be specified to control the output of di and mi:
       -A     Print all fields (used for debugging).  Mount points and special device names are printed at full width.
       -a     (compatibility: --all)
              Prints all mounted devices (normally, those with 0 total space are not printed - e.g. /dev/proc, /dev/fd).
       -B     block-size (compatibility: --block-size, -b)
              Change the base block size from 1024 (default) to the size specified.  block-size may be one of: k - 1024 bytes, si - 1000 bytes, or a specific size.
       -c     (alias: --csv-output)
              Comma separated values are output.  The titles are output as the format string specifiers.  Totals are turned off. See also the -n flag.
       -C     (alias: --csv-tabs)
              Values are output with tab separators.  See also the -c option.
       -d     display-size (alias: --display-size)
              Display  the  usage  in  units  specified  by  display-size.   Note  that if the base block size is SI units of 1000 bytes, the display size is calculated using those units.  display-size may be one of: 512 - POSIX,
              k - kilobytes, m - megabytes, g - gigabytes, t - terabytes, p - petabytes, e - exabytes, z - zettabytes, y - yottabytes, h - Scaled alternative 1, H - Scaled alternative 2, or a specific value to use  as  the  block
              size.
              Block display sizes greater than 1024 bytes are displayed with a precision of one decimal place after the radix.
              The Scaled alternatives scale the sizes displayed and appends a suffix (e.g. 48.0k, 3.4M).
              With scaled alternative 1, sizes within a line may scale to different units.
              Scaled alternative 2 scales all the sizes in each individual line to the same unit size (the largest needed).
       -f     format Use the specified format string format.  See the Format Strings section.
       -g     (alias for: -dg)
              Display sizes in gigabytes.
       -h     (alias for: -dh)
              Display partition sizes in scaled alternative 1 format.
       --help
              Display some basic usage information.
       -H     (alias for: -dH; compatibility: --human-readable)
              Display partition sizes in scaled alternative 2 format.
       -I     include-fstype-list (compatibility: -F, --type)
              Include  only  the  file  system  types listed in include-fstyp-list.  The list is a comma separated list of file system types.  Multiple -I options may be specified.  If the 'fuse' filesystem type is specified, all
              fuse* filesystems will be included.
              e.g. -I nfs,tmpfs or -I nfs -I tmpfs.
       --inodes
              Ignored.  Use the -f option.
       -k     (alias for: -dk)
              Display sizes in Kbytes.
       -l     (compatibility: --local)
              Display only local file systems.
       -L     Turn off check for duplicate filesystems (loopback (lofs/none) mounts).
       -m     (alias for: -dm)
              Display sizes in megabytes.
       -n     Do not print a header line above the list of file systems.  Useful when parsing the output of di.
       --no-sync
              Ignored.
       -P     (compatibility: --portability)
              Output format is POSIX standard.  512 byte block size is the default.  The -k option may be used.
       --print-type
              Ignored.  Use the -f option.
       -q     Disable quota checks.
       -R     (also: --dont-resolve-symlinks)
              Do not resolve symlinks (for mount points that have a trailing UUID).
       -s     sort-type
              Use sort-type to sort the output.  The output of di is normally sorted by mount point.  The following sort flags may be used to change the sort order: m - by mount point (default); n - leave unsorted (as it  appears
              in  the  mount  table); s - by special device name; T - by total space; f - by free space; a - by available space; t - by filesystem type; r - reverse the sort order; This will apply to all sort flags following this
              sort flag.
              These sort options may be combined in any order.  e.g.:
              di -stsrm # by type, special, reversed mount;
              di -strsrm # by type, reversed special, mount.
       --si   An alias for -dH -Bsi.
       --sync Ignored.
       -t     (compatibility: --total)
              Print a totals line below the list of file systems.  Pooled filesystems (zfs, advfs) have only the main pool added to the total.  It is up to the user to exclude (using the -x option)  read-only  filesystems  (cdfs,
              iso9660), swap-based (memfs, mfs, tmpfs) filesystems and user (fuse*) filesystems.  Excluding the 'fuse' filesystem will exclude all fuse* filesystems.
       -w     block-width
              Set the print width for block values.  The default is eight.
       -v     Ignored.
       --version
              Display di's version and default format string.
       -W     inode-width
              Set the print width for inode values.  Default is seven.
       -x     exclude-fstype-list (compatibility: --exclude-type)
              Exclude  the  file  system  types  listed in exclude-fstyp-list.  The list is a comma separated list of file system types.  Multiple -x options may be specified.  If the 'fuse' filesystem type is excluded, all fuse*
              filesystems will be excluded.  e.g. -x nfs,tmpfs or -x nfs -x tmpfs.
       -X     level
              Set the program's debugging level to debug-level.
       -z     zone-name
              Display the filesystems for the specified zone.  The zone must be visible to the user.
       -Z     (alias for: -z all)
              Display the filesystems for all visible zones.
Format Strings
       The output of di may be specified via a format string.  This string may be given either via the -f command line option or as part of the DI_ARGS environment variable.  The format string may specify the following columns:
       m      Print the name of the mount point.
       M      Print the name of the mount point, at full length.  The mount point is formatted to the maximum width necessary for the longest mount point name.
       s      Print the file system name (special device or remote mount point).
       S      Print the file system name (special device or remote mount point), at full length.  The file system name is formatted to the maximum width necessary for the longest file system name.
       t      Print the file system type.
       T      Print the file system type at full length.  The file system type is formatted to the maximum width necessary for the longest file system type.
       Total Available
       b      Print the total number of megabytes on the file system.
       B      Print the total number of megabytes on the file system available for use by normal users.
       In Use
       u      Print the number of megabytes in use on the file system (actual number of megabytes used = total - free).
       c      Print the number of megabytes not available for use by normal users (total - available).
       Free
       f      Print the number of free (unused) megabytes on the file system.
       v      Print the number of megabytes available for use by normal users.
       Percentage Used
       p      Print the percentage of megabytes not available for use by normal users (number of megabytes not available for use / total disk space).
       1      Print the percentage of total megabytes in use (actual number of megabytes used / total disk space).
       2      Print the percentage of megabytes in use, BSD-style.  Represents the percentage of user-available space in use.  Note that values over 100% are possible (actual number of megabytes used /  disk  space  available  to
              non-root users).
       Percentage Free
       a      Print the percentage of megabytes available for use by normal users (number of megabytes available for use / total disk space).
       3      Print the percentage of total megabytes free (actual number of megabytes free / total disk space).
       Inodes
       i      Print the total number of file slots (inodes) that can be created on the file system.
       U      Print the number of file slots in use.
       F      Print the number of file slots available.
       P      Print the percentage of file slots in use.
       Mount Information
       I      Print the time the filesystem was mounted.  This column is not supported on all systems.
       O      Print the filesystem mount options.
       The default format string for di is smbuvpT.
       The default format string for mi is MSTIO.
       The format string may also contain any other character not listed above.  The character will be printed as is.  e.g. di -f 'mbuvp|iUFP' will print the character '|' between the disk usage and the file slot usage.  The com‐
       mand sequence:
              di -f 'mbuvp
              miUFP'
       will print two lines of data for each filesystem.
Examples
       Various df equivalent format strings for System V release 4 are:
              /usr/bin/df -v     di -P -f msbuf1
              /usr/bin/df -k     di -dk -f sbcvpm
              /usr/ucb/df        di -dk -f sbuv2m
       GNU df:
              df                 di -dk -f SbuvpM -w 10
              df -T              di -dk -f STbuvpM -w 10
       AIX df:
              df                 di -d 512 -f Sbf1UPM -w 10
              df -I              di -d 512 -f Sbuf1M
              df -I -M           di -d 512 -f SMbuf1 -w 10
       HP-UX bdf:
              bdf                di -d k -f Sbuv2M
              bdf -i             di -d k -f Sbuv2UFPM
       If you like your numbers to add up/calculate the percentage correctly, try one of the following format strings:
              di -f SMbuf1T
              di -f SMbcvpT
              di -f SMBuv2T
Environment Variables
       The DI_ARGS environment variable may be used to specify command line arguments.  e.g. If you always want gigabytes displayed, set DI_ARGS equal to "-dg".  Any command line arguments specified will override the DI_ARGS  en‐
       vironment variable.
       The DI_LOCALE_DIR environment variable may be used to specify the location of the di program's locale message files.
       The GNU df POSIXLY_CORRECT, and DF_BLOCK_SIZE and the BSD BLOCKSIZE environment variables are honored.
Note
       For filesystems that do not report available space (e.g. System V release 3), the number of available space is set to the free space.
WARNING
       Do not replace your system's df command with this program.  You will in all likelihood break your installation procedures.
See Also
       df(1), fstab(5), getmnt(2), getmntinfo(2), mnttab(4), mount(1M) statfs(2), statvfs(2)
Bugs
       Send bug reports to: brad.lanam.di_at_gmail.com
       Known Issues:
       di will probably not process a zettabyte or yottabyte sized filesystem properly due to overflow of a long long.
Website
       https://gentoo.com/di/
Author
       This program is Copyright 1994-2011 by Brad Lanam.
       Brad Lanam, Walnut Creek, CA (brad.lanam.di_at_gmail.com)
                                                                                                             17 Jan 2013                                                                                                        di(1)

Open source SSH/Telnet client

There are many open source and free SSH/Telnet clients, one of them is PuTTY.

PuTTY is very easy to use since it has GUI.

It can be downloaded from their official website

It has Microsoft Windows version, both in msi and exe format. It can be installed on the system or executed directly without installation.

It has Unix/Linux version as well, which can be downloaded from their official website too (source archive)

For Linux operating systems, we can also install directly from package managers as well.

Install on Linux

# Debian/Ubuntu/Kali Linux etc.
sudo apt install putty
 
# CentOS/RHEL/Fedora
sudo dnf install putty
or
sudo yum install putty
 
# Arch Linux
sudo pacman -S putty

Install on Linux from source

tar -xvf putty-0.73.tar.gz
cd putty-0.73/
./configure
sudo make && sudo make install

How to: Remove junk file from Linux easily

tmpwatch/tmpreaper is the tool we can use for this purpose. It can be used to remove files and folders which haven’t being used for a specific time. (find command can be used to achieve the same result as well)

By default, tmpwatch/tmpreaper can use access time, inode change time, modification time to remove files.

Usually, tmpwatch/tmpreaper is used to remove files from “/tmp” folder and other folders like old log files.

Warning: Do not run this command within root folder “/”!

1 Installation

# CentOS/RHEL/Fedora
sudo dnf install tmpwatch
 
# Older version of CentOS
sudo yum install tmpwatch
 
# Debian/Ubuntu Kali Linux etc.
sudo apt install tmpreaper
 
# OpenSUSE
sudo zypper install tmpwatch

2 Using tmpwatch/tmpreaper

tmpwatch and tmpreaper have identical commands/switches.

2.1 Remove files which haven’t being accessed over X days

tmpreaper 30d /var/log/

30d = 30 days

d: Days

h: hours

m: minutes

s: seconds

By default, without specifying the unit it is considered as hours (In following example, 24 hours)

# Remove files haven't being accessed over 10 hours
tmpreaper 24 /var/log/

2.2 Remove files which haven’t being modified over X days

tmpreaper -m 30d /var/log/

2.3 Remove symbolic links

tmpreaper -s 1d /var/log/

2.4 Remove all files (Include files, folders and symbolic files)

tmpreaper -a 1d /var/log/

2.5 Skip folder/directory when deleting

tmpreaper -am --nodirs /var/log/

2.6 Test deleting

tmpreaper -t 5d /var/log/

2.7 Force deleting

tmpreaper -f 3d /var/log/

2.8 Skip files when deleting

tmpreaper --protect '*.txt' 3d /var/log/

tmpreaper can be used with cron job, e.g. delete tmp or log file from 90 days ago every night at 2:00.


How to: Use shortcut keys/Key combinations in Linux Terminal

1 Tab

When entering command, enter beginning of the command, file name or folder name or command option then press “Tab” key, it will complete the rest for you automatically or show all possible results.

2 Ctrl + C

Terminate/Kill the command or process, it will terminate the running process immediately. (signal SIGINT). It can be intercepted by a program, thus the program can clean itself up before exiting or not exit at all.

3 Ctrl + Z

Suspending a process by sending the SIGSTOP signal, it cannot be intercepted by the program.

4 Ctrl + D

Exit the current terminal. If you are using SSH, it will close it. If you are using a terminal directly, it will close the terminal window.

5 Ctrl + L

Clear terminal screen, same effect as “clear” command

6 Ctrl + A

Move the type cursor to the beginning of the line (Same as pressing “Home” key on keyboard)

7 Ctrl + E

Move the type cursor to the end of the line (Same as pressing “End” key on keyboard)

8 Ctrl + U

Wipe the line and move the type cursor to the beginning of the line (Instead of use “Backspace” key to clear the line slowly)

9 Ctrl + K

Wipe the content from the type cursor to the end of the line

10 Ctrl + W

Clear a word

Before Ctrl + W

Before Ctrl + W
Before Ctrl + W

After Ctrl + W

After Ctrl + W
After Ctrl + W

11 Ctrl + Y

It will paste text removed by Ctrl + U, Ctrl + U and Ctrl + K. If you have deleted text by mistake, this will be helpful.

12 Ctrl + P

Review last command, use repetitively to go back further. Many Terminal provides this review function by PageUp key as well. Some provide the review function by using up arrow key as well (↑).

13 Ctrl + N

Similar usage as Ctrl + P but opposite direction, this command navigate to more recent commands. Many Terminal provides this review function by PageDown key as well. Some provide the review function by using down arrow key as well (↓).

14 Ctrl + R

Used for search history commands

Bonus:

Alternatively, we can use “history” command to show all history command

To search from history command, we can use “history | grep searchTerm”


What’s the difference between CentOS Linux vs CentOS Stream

1 If you just want to test CentOS or use it as server: Download CentOS Linux

2 If you want to know what’s difference, read on

In short, CentOS Stream is in-between Fedora and RHEL, it provides clearner vision of what the next version of RHEL will be, give developer time to plan and create next-generation applications, so that they are compatible with future RHEL version. Whereas the CentOS Linux is the normal version we should use if we just want to setup another server.

In short:

CentOS Linux is the normal CentOS or normal version of CentOS if that’s term you are after

Resources


Basics about Network configuration in Linux, IP commands, configuration files etc.

Table of Contents

1 Some useful basic IP commands

1.1 Use network/Interface configuration files to make permanent changes.

For CentOS/RHEL/Fedora etc.

File: /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE="eth0"
BOOTPROTO=static
ONBOOT=yes TYPE="Ethernet"
IPADDR=10.0.0.10
NAME="System eth0"
HWADDR=00:53:78:2C:7D:9E
GATEWAY=10.0.0.1

For Debian/Ubuntu/Kali Linux etc.

File: /etc/network/interfaces

auto eth0
iface eth0 inet static
address 10.0.0.10
netmask 255.255.255.0
gateway 10.0.0.1

Restart network services to make the changes take effect

sudo /etc/init.d/networking restart
 
OR
 
sudo service restart networking
 
OR
 
systemctl restart networking

1.2 Assign IP address to a specific interface (eth0 in this example) (nonpersistent, will be lost after system reboot)

sudo ip addr add 10.0.0.10 dev eth0

1.3 Remove IP address from a specific interface

sudo ip addr del 10.0.0.10/24 dev eth0

1.4 Check IP address

sudo ip addr
 
OR
 
sudo ip addr show
 
OR
 
sudo ifconfig

1.5 Enable Network interface

sudo ip link set eth0 up

1.6 Disable Network interface

sudo ip link set eth0 down

1.7 Check routing table

sudo ip route show

1.8 Add Static route

sudo ip route del 10.0.0.0/24

1.9 Add persistent static routes

For CentOS/RHEL/Fedora etc.

File: /etc/sysconfig/network-scripts/route-eth0

Add following

10.0.0.0/24 via 192.168.5.20 dev eth0

For Debian/Ubuntu/Kali Linux etc.

File: /etc/network/interfaces

Add following

up ip route add 10.0.0.0/24 via 192.168.5.20 dev eth0

Restart network services to make the changes take effect

sudo /etc/init.d/networking restart
 
OR
 
sudo service restart networking
 
OR
 
systemctl restart networking

1.10 Add default gateway

sudo ip route add default via 10.0.0.1

2 Network configuration file

For CentOS/RHEL/Fedora etc.

File: /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
   #Alias name for the NIC
BOOTPROTO={static|dhcp|none|bootp}
   #Boot protocol, static|none;dhcp
IPADDR=192.168.10.10
   #Set IP address
NETMASK=255.255.255.0
   #Netmask
GATEWAY=192.168.10.1
   #Gateway
ONBOOT=yes|no
   #Activate the network port or not, on boot
HWADDR=00:1E:0B:8F:B0:D0
   #MAC address, if same as the default MAC address of the hardware, this line can be omitted 
DNS1=202.106.0.20
   #Specifiy DNS server
USERCTL=yes|no
   #Users (non-admin/root) allowed to enable/disable this port or not
PEERDNS=yes|no
   #Accept/Reject the DNS server from DHCP while BOOTPROTO is dhcp

For Debian/Ubuntu/Kali Linux etc.

File: /etc/network/interfaces

auto eth1
     #Automatically connect to Ethernet on boot
iface eth1 inet static
     #Assign IP address by static/dhcp
address 192.168.72.8
     #IP address
netmask 255.255.255.0
     #Netmask
gateway 192.168.72.1
     #Default gateway
dns-nameservers 8.8.8.8 4.4.2.2
     #DNS server

3 Hosts configuration

File: /etc/hosts

192.168.0.10 internalserver.mynet

4 Network Interface Controller (NIC) Naming

lo: Localhost loop

ppp#: Point-to-Point Protocol

eth: Ethernet

5 Network management tool

network tool and NetworkManager tool.

network

Restart network

sudo /etc/init.d/network restart

NetworkManager

It can be used to manager network easily, when X Window is not available this GUI tool can be used to manage network without the need to edit configuration file manually.

nmtui
nmtui
nmtui
NetworkManager TUI - nmtui
NetworkManager TUI – mntui

6 NetworkManager cli

NetworkManager provides cli tools as well alongside nmtui

nmcli con show
     #Get UUID table
nmcli dev
     #Check network device status
nmcli r wifi off
     #Turn off wifi
nmcli - commands
nmcli – commands

Start NetworkManager on boot

chkconfig NetworkManager on
 
OR
 
 systemctl enable NetworkManager

Start NetworkManager immediately

service NetworkManager start
 
OR
 
 systemctl start NetworkManager