Linux Commands Cheat Sheet

Do you want to learn the Linux commands? If yes, then you are in the right place. There are uncountable commands in Linux. Generally, we use some of the Linux commands from the Linux commands cheat sheet regularly to perform the common tasks.

pythonarray.com is a free Python tutorial website for people who want to learn Python, fast.

You can also learn Google Sheets Tips – New Google Spreadsheet Hacks, Tricks with Examples

Types of Linux Commands

All the Linux commands are categorized into 7 different types. They are Basic Linux commands, File permission commands, Environment variables commands, User management commands, Networking commands, Process commands, VI Editing commands, Other Commands.

We are giving a detailed explanation for each and every Linux command with their examples for easy learning in the below sections. Have a look at them and follow them. Check the actual description of each Linux command in their manual page.

Basic Linux Commands

  • ls command:

It is used to list all files and directories of a directory. The ‘-l’ option gives the long listing format. Its syntax is $ ls [options] [file|dir]

Example: $ ls -l samplefile

  • ls-R command:

It gives the lists files in subdirectories. List recursively directory tree and syntax is $ ls -R [options] [file|dir]

Example: $ ls -R samplefile

  • ls -a Command:

It lists all files including hidden files starting with’.’ Its syntax is $ ls -a [options] [file|dir]

  • ls -al Command:

It lists all files details and directories with detailed information like permissions, size, owner, etc. Syntax is $ ls -al [options] [file|dir]

  • cd or cd ~ Command:

This command will navigate to the home directory or change the directory. Its syntax is $ cd [directory]

Example: $ cd Directory1

  • cd .. Command:

It moves one level up. Syntax is $ cd . . [directory]

  • cd Command:

Use this command to change to a particular directory. Its syntax is $ cd [directory]

Example: $ cd directory1

  • cd / Command:

This command will move to the root directory. Syntax is $ cd /

  • cat filename Command:

Create a new file using this command in Linux. Its syntax is $ cat filename.

Example: $ cat filename1

  • cat filename Command:

It displays the content in that particular file. The syntax is $ cat filename.

Example: $ cat filename1.

  • cat file1 file2 file3 Command:

It combines two files namely file1 and file2 and stores the output in a new file called file3. Its syntax is $ cat file1 file2 file3.

Example: $ cat add multiply arithmetic.

  • mv file “new file path” Command:

It moves the files to the new location. Syntax is $ mv file “new file path”.

Example: $ mv add “E:/Arithmeticopeartions/”.

  • mv filename new_file_name Command:

It renames or changes the file name to a new file name. Syntax is $ mv filename new_file_name.

Example: $ mv add sum

  • sudo Command:

sudo command allows a permitted user to run a command as root or another user. It runs commands in superuser mode. The syntax is $ sudo filename option

Example: $ sudo apt update

  • rm Command:

It is used to remove the files or directories. Syntax is $ rm filename or $ rm directory

Examples: $ rm sum and $ rm mydrive

  • man Command:

It is used to view the online reference manual pages for the commands or programs. It provides the help information of any commands. Its syntax is $ man commandname

Example: $ man cd

  • history Command:

It produces the list of previously used commands and gets the all past commands types in the current terminal session. The syntax is $ history

  • clear Command:

clear command is helpful to clear the terminal screen. The syntax is $ clear

  • mkdir directoryname Command:

It creates a new directory in the present working directory or at any specific path. Syntax is $ mkdir directoryname

Example: $ mkdir new_directory

$ mkdir -p new_directory: Here -p option is used to override the already available directory.

  • rmdir Command:

It helps to remove/ delete the empty directories. Syntax is $ rmdir [directory_name]

Example: $ rmdir new_directory

  • mv Command:

It is helpful to rename files or directories. mv command also moves a file or a directory to another location in the directory structure. Its syntax is $ mv file1 file2

Example: $ mv add sum

  • pr -x Command:

It divides the file into x columns. Syntax is $ pr -x filename.

Example: $ pr -5 add

  • pr -h Command:

It is useful to assign a header to the file. Syntax is $ pr -h filename

Example: $ pr -h sum

  • pr -n Command:

This command denotes the file with the line numbers. Its syntax is $ pr -n file

Example: $ pr -n sum

  • lp -nc , lpr c Command:

It prints the c number of copies of a file. lp stands for a line printer. Its syntax is $ lpr c filename

Example: $ lpr 5 sum

  • lp -d lp -P Command:

It specifies the name of the printer. The syntax is $ lp -P

  • apt-get Command:

It is a powerful and free front end package manager for Debian/Ubuntu systems. It is used to install new software packages, remove available software packages, upgrade existing software packages, and upgrade the entire operating system. Its syntax is $ apt-get package

Example: $ sudo apt-get update

  • mail -s ‘subject’ -c ‘cc-address’ -b ‘bcc-address’ ‘to-address’ Command:

It is a linux command used to send mail. Syntax is $ mail -s ‘subject’ -c ‘cc-address’ -b ‘bcc-address’ ‘to-address’

Example: $ mail -s ‘regarding_job’ -c ‘add@gmail.com’ -b ‘sum@gmail.com’ ‘arithmetic@gmail.com’

  • mail -s “Subject” to-address < Filename Command:

It is used to send an email with an attachment. Its syntax is $ mail -s “Subject” to-address < Filename

Example: $ mail -s “Addition of Numbers” sum@gmail.com < Addition

File Permission Commands

  • ls-l Command:

Use this command to show file type and access permission of any particular file. Syntax is $ ls-l filename

Example: $ ls-l sum

  • r Command:

It is used to give read permission to a file. The syntax is $ r filename.

Example: $ r sum

  • w Command:

It gives write permission to the file. The syntax is $ w filename

Example: $ w sum

  • x Command:

It is useful to provide execute permission to the file. Its syntax is $ x filename

Example: $ x sum

  • -= Command:

This command is used when you want to give no permission to a file. Syntax is $ -= filename

Example: $ -= file1

  • Chown user Command:

It is helpful to change the ownership of a file/directory. The syntax is $ chown user filename

Example: $ chown user file1

  • Chown user:group filename Command:

It changes the user and group for a file or directory. Syntax is $ chown user:group filename

Example: $ chown user:group sum

Environment Variables Command

  • echo $VARIABLE Command:

It displays the value of a variable or prints a text of the line. Its syntax is echo [option] [string]

Example: $ echo “Print this line” or $ echo $VARIABLE var1

  • env Command:

It lists all environment variables. The syntax is $ env

  • VARIABLE_NAME= variable_value Command:

It creates a new variable and assigns it a value. Syntax is $ VARIABLE_NAME= variable_value

Example: $ My_Variable= 10

  • Unset Command:

It removes a variable. Syntax is $ Unset VARIABLE_NAME

Example: $ Unset My_Variable

  • export Variable=value Command:

It is helpful to set the value of an environment variable. Syntax is $ export Variable=value

Example: $ export Variable1=90

User Management Commands of Linux

  • sudo adduser username Command:

It is used to create a username. The syntax is $ sudo adduser username

Example: $ sudo dduser user1

  • sudo passwd -l ‘username’ Command:

It is helpful to change the user password. Syntax is $ sudo passwd -l ‘username’

Example: $ sudo passwd -l ‘user1’

  • sudo userdel -r ‘username’ Command:

This command is used to add and delete users on Linux. Syntax is $ sudo userdel -r ‘username’

Example: $ sudo userdel -r ‘user1’

  • finger Command:

It is a user information lookup command which provides details of all users logged in. The Syntax $ finger

  • finger username Command:

It gives the login details of that particular user. Syntax $ finger username

Example: $ finger user1

Networking Commands

  • SSH username@ip-address or hostname Command:

You can log in to a remote Linux machine using SSH. The syntax is $ SSH username@ip-address or hostname

Example: $ SSH user1@https:11//1001

  • Ping hostname=”” or =”” Command:

It is used for analyzing the network and host connections. Syntax is $ Ping hostname=”” or =””

Example: $ Ping hostname=”host1″

  • dir Command:

It displays all files in the current directory of a remote computer. The syntax is $ dir

  • cd “dirname” Command:

It changes the current directory to “dirname” on a remote computer. Syntax is $ cd “dirname”

Example: $ cd “E”

  • put file Command:

It is helpful to upload the file from local to the remote computer. The syntax is $ put file

Example: $ put sum

  • get file Command:

It is used to download the file from remote to the local computer. The syntax is $ get file

Example: $ get add

  • quit Command:

It is used to logout from that network. The syntax is $ quit

Process Commands

  • bg Command:

You can send a process to the background with this command. The syntax is $ bg processname

Example: $ bg process1

  • fg Command:

To run a stopped process in the foreground. The syntax is $ fg processname.

Example: $ fg process1

  • top Command:

It gives the details of all active processes in the directory. The syntax is $ top

  • ps Command:

It produces the status of processes running for a user. The syntax is $ ps username

Example: $ ps user1

  • ps PID Command:

This command gives the status of a particular process. The syntax is $ ps PID processname

Example: $ ps PID process1

  • pidof Command:

It gives the process id of a process. The syntax is $ pidof processname.

Example: $ pidof process1

  • kill PID Command:

It kills a process. The syntax is $ kill PID processname

Example: $ kill PID process1

  • nice Command:

It is used to start a process with the given priority. The syntax is $ nice processname.

Example: $ nice process

  • renice Command:

It is used to change the priority of an already running process. The syntax is $ renice processname

Example: $ renice process2

  • df Command:

It provides free hard disk space on your system. The syntax is $ df

  • free Command:

It gives free RAM space on your system. The syntax is $ free

VI Editing Commands

  • i Command:

It inserts at the cursor i.e goes to insert mode. The syntax is $ i

  • a Command:

It is used to write after the cursor in insert mode. The syntax is $ a

  • A Command:

This command is used to write at the end of the line in insert mode. The syntax is $ A

  • ESC Command:

It is helpful to terminate insert mode. The syntax is $ ESC.

  • u Command:

It undoes the last change. The syntax is $ u.

  • U Command:

It undoes all changes to the entire line. The syntax is $ U

  • o Command:

It opens a new line in insert mode. The syntax is $ o

  • dd Command:

It deletes that particular line. The syntax is $ dd line number

Example: $ dd 25

  • 3dd Command:

This command deletes 3 lines. The syntax is $ 3dd line number

Example: $ 3dd 25

  • D Command:

It deletes the contents of a line after the cursor. The syntax is $ D

  • C Command:

It is used to delete the contents of a line after the cursor and insert new text. Press the ESC key to end the insertion. Syntax is $ C

  • dw Command:

Delete a word using this command. The syntax is $ dw

  • 4dw Command:

It is used to delete 4 words. The syntax is $ 4dw

  • cw Command:

It is used to change a word. Syntax is $ cw

  • x Command:

It deletes the character at the cursor. Syntax is $ x

  • r Command:

Replace the character. The syntax is $ r

  • R Command:

It overwrites characters from cursor onward. The syntax is $ R

  • s Command:

Substitute one character under the cursor and continue to insert. The syntax is $ s

  • S Command:

Substitutes the entire line and starts to insert at the beginning of the line. The syntax is $ S

  • ~ Command:

It is used to change the case of the individual character. Syntax is $ ~

Other Linux Commands

  • adduser/addgroup Command:

adduser adds a user and addgroup adds groups to the system. The syntax is $ adduser username

Example: $ sudo adduser tech

  • agetty Command:

agetty is a program that manages physical or virtual terminals and is invoked by init. It is the substitute for Linux getty.

Example: $ agetty -L 9600 ttyS1 vt100

  • alias Command:

It is a useful shell built-in command for creating shortcuts to a Linux command on a system.

Example: $ alias home’cd /home/drive/file1′

  • apropos Command:

This is used to search and display a short man page description of a program/ command. The syntax is $ apropos commandname

Example: $ apropos rm

  • aptitude Command:

It is a powerful text-based interface to the Debian Linux/GNU package management system. The syntax is $ aptitude package

Example: $ sudo aptitude update

  • arch Command:

This command is used to display the machine architecture or hardware name. The syntax is $ arch

  • arp Command:

ARP stands for Address Resolution Protocol that maps IP network addresses of a network neighbor with the hardware (MAC) addresses in an IPv4 network.

Example: $ sudo arp-scan –interface=enp2s0 –localnet

  • at Command:

It is used to schedule tasks to run in the forthcoming time. Syntax is $ at

Example: $ sudo echo “shutdown -h now” | at -m 15:20

  • atq Command:

This command is used to view jobs in at command queue. Syntax is $ atq

  • atrm Command:

It is helpful to delete/remove jobs from the queue. $ atrm 2

  • awk Command:

Awk is a programming language created for text processing and used as a data extraction and reporting tool.

Example: $ awk ‘//{print}’/etc/hosts

  • batch Command:

It is similar to at command which is useful to schedule tasks to run in a future time. Syntax is $ batch

  • basename Command:

It helps to print the name of a file stripping of directories in absolute path. Syntax is $ basename filename

Example: $ basename samplefile

  • bzip2 Command:

It is useful to compress or decompress the files. Syntax is $ bzip2 filename

Example: $ bzip2 -z filename #Compress

$ bzip2 -d filename.bz2 #Decompress

  • cal Command:

This command is used to print a calendar on the standard output. Syntax is $ cal

  • chgrp Command:

It is used to change the group ownership of a file. Syntax is $ chgrp new_group_name filename

Example: $ chgrp Product Multiplication.txt

  • chmod Command:

It is used to change/ update file access permissions. Syntax is $ chmod permission filename

Example: $ chmod +x product.txt

  • cksum Command:

This command is used to provide the checksum and byte count of the given input file. Syntax is $ cksum filename

Example: $ cksum product.txt

  • cmp Command:

It compares byte by byte comparison of two files. Syntax is $ cmp file1 file2

Example: $ cmp sum add

  • comm Command:

It is used to compare two sorted files line by line. Syntax is $ comm file1 file2

Example: $ comm product multiplication

  • cp Command:

It copies files and directories from one location to another. Syntax is $ cp location1 location2

Example: $ cp /home/arithmetic/add /home/arithmetic/product/

  • date Command:

It displays/ sets the system date and time. Syntax is $ date

Example: $ date –set=”20 Oct 2020 10:35:05″

  • diff Command:

The double advantage of this command is used to compare two files line by line and used to find the difference between two directories. Syntax is $ diff file1 file2

Example: $ diff sum add

  • dmidecode Command:

It retrieves the hardware information of any linux system. Syntax is $ dmidecode

Example: $ sudo dmidecode –type system

  • du Command:

It shows the disk space usage of files present in the directory as well as its subdirectories. The syntax is $ du directory

Example: $ du /home/arithmetic

  • eject Command:

eject command is used to eject removable media such as CD/DVD/ROM/floppy disk from the system. The syntax is $ eject disklocation

Example: $ eject /dev/cdrom

  • exit Command:

It is used to exit a shell. The syntax is $ exit

  • expr Command:

It is used to calculate an expression. The syntax is $ expr expression

Example: $ expr 15 * 5

  • factor Command:

It is used to display the prime factors of a number. The syntax is $ factor number

Example: $ factor 18

  • find Command:

It searches for files in the directory and its subdirectories. The syntax is $ find directory filename

Example: $ find /home/add/ -name product.txt

  • grep Command:

It searches for a specified pattern in the file and displays the output as lines containing that pattern. The syntax is $ grep

Example: $ grep ‘linuxcent’ domain-list.txt

  • groups Command:

Shows all names of groups of a user. The syntax is $ groups

  • gzip Command:

It helps to compress a file and replaces it with one having a .gz extension. The syntax is gzip filename

Example: $ gzip passwds.txt

  • gunzip Command:

It expands or restores files compressed with the gzip command. The syntax is $ gunzip gzfilename

Example: $ gunzip sum.gz

  • head Command:

It is helpful to display the first 10 lines of the specified file or stdin to the screen. The syntax is $ head

  • hostname Command:

This command is useful to print or set the system hostname. The syntax is $ hostname

Example: $ hostname New_hostname

  • id Command:

It shows user & group information for the current user or specified user. The syntax is $ id username

Example: $ id linuxcent

  • ifconfig Command:

It is used to configure Linux system network interfaces. The syntax is $ ifconfig

  • ionice Command:

This command is used to set or view process I/O scheduling class and priority of the specified process. The syntax is $ ionice process

Example: $ ionice -c 3 rm /var/logs/syslog

  • iostat Command:

iostat command is helpful to show the CPU and i/o statistics for devices and partitions. The syntax is $ iostat

  • ip Command:

It is used to display or manage devices, routing, policy routing, and tunnels. The syntax is $ ip addr

  • iw Command:

This is used to manage wireless devices and their configuration. The syntax is $ iw

Example: $ iw list

  • killall Command:

It kills a process with the help of its name. The syntax is $ killall processname

Example: $ killall process1

  • kmod Command:

It is useful to manage Linux kernel modules. To list all currently loaded modules, type. Syntax is $ kmod

Example: $ kmod list

  • last Command:

It displays a list of last logged in users. The syntax is $ last

  • ln Command:

This is used to create a soft link between files using -s flag. Syntax is $ ln -s file1 file2

Example: $ ln -s product multiplication

  • locate Command:

It is used to find a file with its name. The syntax is $ locate filename

Example: $ locate -b ‘add.txt’

  • lshw Command:

It gives detailed information on the hardware configuration of the machine. The syntax is $ lshw

  • lscpu Command:

This Linux command displays the system’s CPU architecture information. The syntax is $ lscpu

  • lsof Command:

It displays information related to files opened by processes. The syntax is $ lsof processname

Example: $ lsof -u add

  • lsusb Command:

It shows information: about USB buses in the system and devices connected to them. The syntax is $ lsusb

  • md5sum Command:

This command is used to compute and print the MD5 message digest of the file.

  • more Command:

It enables to view through relatively lengthy text files one screenful at a time. The syntax is $ more filename

Example: $ more add

  • nano Command:

You can open a file using this nano command. The syntax is $ nano filename

Example: $ nano sum

  • netstat Command:

It displays useful information such as routing tables, network connections, and others about Linux networking subsystems. Syntax is $ netstat

Example: $ netstat -a | more

  • nproc Command:

It shows the number of processing units present to the current process. The syntax is $ nproc

  • passwd Command:

This command is used to create/update passwords for user accounts. The syntax is $ passwd username

Example: $ passwd user1

  • pstree Command:

It displays running processes as a tree that is rooted at either PID or init if PID is omitted. The syntax is $ pstree

  • reboot Command:

This command is used to power-off, halt, or reboot a system. The syntax is $ reboot

  • scp Command:

It enables you to securely copy files between hosts on a network. Syntax is $ scp

Example: $ scp ~/names.txt root@192.168.56.10:/root/names.txt

  • shutdown Command:

It schedules a time for the system to be powered down. Its syntax is $ shutdown

Example: $ shutdown –poweroff

  • sleep Command:

This command is used to pause or delay for a specific amount of time. The syntax is $ sleep

Example: $ check.sh; sleep 5; sudo apt update

  • sort Command:

This is used to sort lines of text in the specified files from stdin. The syntax is $ sort

Example: $ sort add.txt

  • split Command:

It is used to split a large file into smaller pieces. The syntax is $ split filename

Example: $ split add

  • stat Command:

It is helpful to know the file system status. The syntax is $ state filename

Example: $ stat -f add.txt

  • su Command:

It is used to switch to another user ID or become root during a login session. The syntax is $ su

  • sum Command:

This command shows the checksum and blocks counts for all files on the command line. The syntax is $ sum output filename

Example: $ sum output add.txt

  • tac Command:

It concentrates and displays files in reverse. It prints each file to the standard output, shows the last line first. The syntax is $ tac filename

Example: $ tac sum.txt

  • tail Command:

It displays the last 10 lines of each file. The syntax is $ tail filename

Example: $ tail sum.txt

  • talk Command:

talk is used to talk to another network user/ system. Use a login name to talk to the user on the same machine and use ‘use@host’ to talk to a user on another machine. The syntax is $ talk

Example: $ talk person [ttyname] or $ talk ‘user@host’ [ttyname]

  • tee Command:

This is used to read from standard input & prints to standard output and files.

Example: $ echo “Testing how tee command works” | tee file1

  • time Command:

This command runs programs and summarizes system resource usage. The syntax is $ time

  • touch Command:

It changes file timestamps and it is used to create a file. The syntax is $ touch filename

  • uname Command:

It displays system information such as operating system, network node hostname kernel name, version, and release, etc. Syntax is $ uname

  • uniq Command:

It displays or omits repeated lines from input. The syntax is $ uniq

  • uptime Command:

It shows how long the system has been running, the number of logged on users, and system load averages. The syntax is $ uptime

  • wall Command:

This command is used to send/display a message to all users on the system. The syntax is $ wall “message”

Example: $ wall “This is Linux operating system”

  • wc Command:

This command displays the word, newline, and byte counts for each file. The syntax is $ wc filename

Example: $ wc add.txt

  • whatis Command:

It searches & shows a short or one-line manual page description of the provided command name. The syntax is $ whatis wget

  • who Command:

It shows details about users who are currently logged in. The syntax is $ who

  • which Command:

This command is used to get the absolute path of the file which would be executed in the current environment. The syntax is $ which who

  • whereis Command:

It helps to locate binary, source and manual files for commands. The syntax is $ whereis cat

  • yes Command:

This command is used to display a string repeatedly until when terminated or killed. The syntax is 4 yes

Example: $ yes “This is LinuxCent – Linux HowTos”

  • zcmp/zdiff Command:

zcmp and zdiff minimal utilities used to compare compressed files. Syntax is $ zcmp filesnames, $ zdiff filesnames

Example: $ zcmp domain-list.txt.zip basic_passwords.txt.zip

$ zdiff domain-list.txt.zip basic_passwords.txt.zip

  • zip Command:

It is simple & easy to use utility used to package & compress files. Syntax is $ zip packagename

Example: $ zip inarchive.zip foo.c bar.c –out outarchive.zip

We have mentioned all the Linux commands in the Linux Commands Cheatsheet. Make use of these countless commands that you might feel helpful.

BASH “switch case” in Linux with practical example

The switch case in BASH is more relevant and is widely used among the Linux admins/Devops folks to leverage the power of control flow in shell scripts.

As we have seen the if..elif..else..fi Control Structure: Bash If then Else. The switch case has a stronger case where it really simplifies out the control flow by running the specific block of bash code based on the user selection or the input parameters.

Let’s take a look at the simple Switch case as follows:

OPTION=$1
case $OPTION in
choice1)
Choice1 Statements
;;

choice2)
Choice2 Statements
;;

choiceN)
ChoiceN Statements
;;

*)
echo “User Selected Choice not present”
exit 1

esac

The OPTION is generally read from user input and upon this the specific choice case block is invoked.

Explanation:
In the switch command the control flow is forwarded to case keyword and stops here, it checks for the suitable match to pass over the control to relevant OPTION/CHOICE statement block. Upon the execution of the relevant CHOICE statements the case is exited once the control flow encounters esac keyword at the end.

Using the Pattern match
The control flow in bash identifies the case options and proceeds accordingly.
There can be cases where you can match the Here you might have observed that the user input the regular expression and the logical operators using the | for the input case

#! /bin/bash

echo -en "Enter your logins\nUsername: "
read user_name 
echo -en "Password: "
read user_pass 
while [ -n $user_name -a -n $user_pass ]
do

case $user_name in
    ro*|admin)
        if [ "$user_pass" = "Root" ];
        then
            echo -e "Authentication succeeded \ n You Own this Machine"
	    break
        else
            echo -e "Authentication failure"
            exit
        fi
    ;;
    jenk*)
	if [ "$user_pass" = "Jenkins" ];
	then
		echo "Your home directory is /var/lib/jenkins"
	    	break
	else
        	echo -e "Authentication failure"
	fi
        break
    ;;
    *)
        echo -e "An unexpected error has occurred."
        exit
    ;;
esac

done

You should kindly note that the regex used for the cases at ro*|admin and jenk*

We now have demonstrated by entering the username as jenkins and this will get matched with the jenkins case the control flow successfully enters into relevant block of code, checking the password match or not is not relevant for us as we are only concerned till the case choice selection.
We have named the switch case into a script switch-case.sh and run it, Here are the results.

OUTPUT :

[vamshi@node02 switch-case]$ sh switch-case.sh
Enter your logins
Username: jenkins
Password: Jenkins
Your home directory is /var/lib/jenkins

We have entered the correct password and successfully runs the jenkins case block statements

We shall also see the or ro*|admin case, demonstrated as follows.

[vamshi@node02 switch-case]$ sh switch-case.sh 
Enter your logins
Username: root
Password: Root
Authentication succeeded \ n You Own this Machine

We now test the admin username and see the results.

[vamshi@node02 switch-case]$ sh switch-case.sh 
Enter your logins
Username: admin
Password: Root
Authentication succeeded \ n You Own this Machine

Here is a more advanced script used to deploy a python application using the switch case..
Please refer to the Command line arguments section for user input

A complete functional Bash switch case can be seen at https://github.com/rrskris/python-deployment-script/blob/master/deploy-python.sh

Please feel free to share your experiences in comments.

create multiple files and Directories at once in Windows Command line

The windows commandline also known as cmd provides the cli interaction to the windows operating system.

The Windows powershell which has been recently made opensource provides cross platform compatibility and can also be installed on Linux OS.

The command to create a file in Powershell:

PS /home/vamshi> New-Item -ItemType file testfile.txt

There is another simpler way to create a file in windows and also used often in Linux/Unix

PS /home/vamshi> echo " " > testfile2.txt
PS /home/cloud_user> dir

Directory: /home/vamshi

Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 04/25/2020 07:40 2 testfile.txt
----- 04/25/2020 07:40 2 testfile2.txt

Command to create a Directory in Powershell.

PS /home/vamshi>New-Item -ItemType directory testDir.

How to create multiple files in Windows using Command-line?

Write down the list of all filenames in a file filenames.txt as below:

PS /home/vamshi> cat filenames.txt
file1
file2
file3
file4

To achieve this we have to create a for loop.

PS /home/vamshi> cat filenames.txt | foreach-object -process { echo "" > $_ }
PS /home/cloud_user> dir

Directory: /home/vamshi

Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 04/25/2020 07:49 1 file1
----- 04/25/2020 07:49 1 file2
----- 04/25/2020 07:49 1 file3
----- 04/25/2020 07:49 1 file4

The foreach command can be used along with New-Item -ItemType as below:

PS /home/vamshi> cat filenames.txt | foreach-object -process { New-Item -ItemType file $_ }

Create multiple Directories in windows:

PS /home/vamshi> cat ./dirs.txt
dir1
dir2
dir3
PS /home/vamshi> cat ./newdirs.txt | foreach-object -process { New-Item -ItemType directory $_ }
PS /home/vamshi> dir
Directory: /home/vamshi
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 04/25/2020 07:54 dir1
d---- 04/25/2020 07:54 dir2
d---- 04/25/2020 07:54 dir3

SVN commands

SVN is Version controlling system and predates git, It was one of the most widely used version control system and served the community and still going on.. It had its own share advantages and shortcomings.

In this tutorial we will shed some light on practical usecases of SVN and day-to-day activities.

How to take a dump of live svn repo by loading it to another Repo on the fly.

# svnadmin dump /opt/svn/ProjectCode | svnadmin load /opt/svn/ProjectCode

SVN commands to take the dump from repo to a .dump file

# svnadmin dump /opt/svn/PST > test.dump

Using svn comands svnadmin to take the dump for specific revision number range from commandline.

# svnadmin dump -r 41:3601 /opt/svn/ProjectCode > test.dump

SVN Commands to split svn directories to respective svn repositories:

We have the folder structure of the ProjectCode and want to create a individual repository of all the folders with their own SVN repos, We can use the svndumpfilter to achieve the task.

$ cat projectslist
./project1./project2
./project3
./project4
$ for i in `cat projectslist`; do sudo svndumpfilter --drop-empty-revs --renumber-revs exclude `cat projectslist | grep -v $i` < SVN.dump > `echo $i | sed -e 's/\///g'`.dump; done

How to setup up a SVN repo from linux commandline.

Creating Dir for new SVN

# mkdir /opt/svn/ProjectCode

Initialize a repo in the newly created directory

#svnadmin create /opt/svn/ProjectCode

Load the repo from dump

#svnadmin load --force-uuid /opt/svn/ProjectCode < newProjectCode.dump

Loading repo from dump and redirecting output to file

#svnadmin load --force-uuid /opt/svn/ProjectCode < newProjectCode.dump >> /root/pst.out &

Linux Commands To delete a repo; Make sure the svnserve process is in stopper state and not active while removing the repo.

# rm -rf /opt/svn/PST

While taking SVN repo dump How to Exclude some paths from existing dump and create a new dump
We will be using the exclude option and passing the exclude pattern

# svndumpfilter exclude `cat ProjectCode-exclude-somerolders` < ProjectCode.dump > new-exclude.dump

Below is a sample exclude file with specific file names and directory paths

# cat ProjectCode-excludelist
/path/to/code/not/required
/Some/more/paths/and/specific-filenames.css
/code/static/test/

How to check the logs from SVN repo from commandline

# svn log file:///opt/svn/Project

How to setup SVN repository with configuration ?

# svnadmin create /opt/svn

Below is the example snippet from svnserve.conf

[general]
anon-access = none
auth-access = write
password-db = /opt/svn/conf/passwd
authz-db = /opt/svn/conf/authz

Here is the sample config snipet from authz

[/]
svamshi = rw

How to start SVN server from linux commandline

# svnserve --listen-host ip-address/project-repo -r /opt/svn/my-projectcode/ -d

How to checkout a SVN repo from linux commandline

# svn co --username=svamshi --password=mypassword svn://ip-address/project-repo

Adding the new created files to svn and then commit to svn.

# svn add linuxcent.html
# svn commit -am "updated with linuxcent.html"

How to serve the SVN repository through Apache Httpd server.

Example [/code]/opt/svn[/code]The svn module has to be downloaded

Below is the sample configuration of virtualhost informantion

subversion-apache-httpd-set-up-configuration

Rename files in linux

The linux mv command has very featureset, It can be used to rename the file(s) and Directory names, also also used to relocate the contents and help better in organizing the files and directories on a linux OS.

Syntax of mv command:

$ mv [OPTIONS] </path/to/Source> </path/to/Destination>

How to rename a single file

The rename operation is linux is done using the mv command

[vamshi@linuxcent mv]$ ls
demo.txt
$ mv demo-today.txt demo-old.txt
[vamshi@linuxcent mv]$ ls
demo-old.txt

Here the file demo-today.txt has been renamed to demo-old.txt

How to move or relocate multiple files and directories at once into a Destination Directory

Out DemoProject Directory contains the following content

[vamshi@node02 DemoProject]$ ls
api LICENSE mvnw mvnw.cmd README.md

We are only interested to move out only selected directories core/ site/ admin/ and the file pom.xml to the target destination /tmp/Demo-test/, We can achieve this using the option -t --target-directory= Option

[vamshi@node02 DemoProject]$ mv -vi core/ site/ admin/ pom.xml -t /tmp/Demo-test/
‘core/’ -> ‘/tmp/Demo-test/core’
‘site/’ -> ‘/tmp/Demo-test/site’
‘admin/’ -> ‘/tmp/Demo-test/admin’
‘pom.xml’ -> ‘/tmp/Demo-test/pom.xml’

As a result we have successfully moved the selected content:

[vamshi@linuxcent DemoProject]$ ls /tmp/Demo-test/
admin core pom.xml site

Renaming multiple files with extensions

Here’s what we will be demonstrating in this tutorial, We will use a combination of tools like cut combining them with a for loop to accomplish our task in an iterative loop.

For simplicity sake let’s consider we have 10 files ending with .txt extension, as seen below

[vamshi@linuxcent ~]$ ls
file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt

We will now rename them and append an extension of .txt to all the files as demonstrated below:

[vamshi@node02 source]$ for i in *.txt; do sh -c "mv $i `echo $i| cut -d'.' -f1 `.html" ; done
[vamshi@linuxceent ~]$ ls
file10.html file1.html file2.html file3.html file4.html file5.html file6.html file7.html file8.html file9.html

Using the rename command to rename the file extensions.

The linux rename command takes the arguments

We have here 10 files with .html extension

[vamshi@linuxcent ~]$ rename .html .doc *
[vamshi@linuxcent ~]$ ls
file10.doc file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc

While we also might have many other files in another extension format and we can change thrir extension format in the following method.
Suppose have 3 files with .txt extension as file11.txt file12.txt file13.txt and remaining files with .doc extension, they all can be renamed to .html as per the following format.

[vamshi@linuxcent source]$ ls
file10.doc file11.txt file12.txt file13.txt file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
[vamshi@linuxcent ~]$ ls
file10.doc file11.txt file12.txt file13.txt file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
[vamshi@linuxcent ~]$ rename .doc .txt .html *
[vamshi@node02 source]$ ls
file10.html file11.html file12.html file13.html file1.html file2.html file3.html file4.html file5.html file6.html file7.html file8.html file9.html

Linux cut command with practical examples

The Linux cut command, extracts the sections of text from the input lines of files.

The general syntax

cut [OPTION] [FILE]

The cut command offers special characters like - and , to extract the range and selection of data from input line or stream of lines read from a file.

The cut command has some most useful and quick options to extract data from the input lines..

First such option is -b, --bytes=LIST from the Specified LIST

Using the -b option we shall be able to extract the specific byte characters.

$ echo -e "Exploring the practical use of cut command"

For getting the specific byte index from input stream list:

echo -e "Exploring the practical use of cut command"  | cut -b 3
p

Extracting the range of bytes from input stream list:

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -b 3-6
plor

Extracting the bytes from starting till the given index range

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -b -6
Explor

Extracting the bytes from given index range till the end of the input line

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -b 3-
ploring the practical use of cut command

Combining the , and – operations and extracting the bytes from the input lines:

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -b 1,2-6,9
Explorg

First such option is -c, --characters=LIST from the Specified LIST

Extracting the specific character from the input string:

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -c 11
t

Extracting the range of bytes from input stream list:

[vamshi@linuxcent ~]$ echo -e "Exploring the practical use of cut command"  | cut -c 11-24
the practical

Getting the range of the characters from given index till the end of the input line using -

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -c 11-

the practical use of cut command

Using - to get the characters from starting till the given index range

[vamshi@node02 Linux-blog]$ echo -e "Exploring the practical use of cut command"  | cut -c -9

Exploring

We have a Text file called README with the following content.

The following are Most popular Linux server Distributions and their curent versions:

Distribution Versions
----------- --------
Redhat : 8.0
Ubuntu : 18.04
Centos : 8.0
Debian : 10.1
SUSE : 15.1

Extracting the fields from the given data file using -f, separated by the tabs which is a default delimited.

Lets try and extract the field from README datafile.

$ cat README | cut -f1
[vamshi@linuxcent ~]$ cut -f1 README
The following are Most popular Linux server Distributions and their current versions:

Distribution
-----------
Redhat
Ubuntu
Centos
Debian
SUSE

The options and ranges can also be mentioned in the field option to extract relevant data.
Extracting the fields indexed by numbers from given file separated by tabs

$ cat README | cut -f1,2,3
$ cut -f1,2,3 README

Extracting the first field through 3rd field from the given file separated by tabs

$ cat README | cut -f1-3 
$ cut -f1-3 README

A lot of combinations can be used to extract the ranges as shows below:

$ cat README | cut -f 3- 

We will also like to shed some light on the --complement option, As the option name hints, it will print only the inverse of the operation, It can be used in conjunction with -b , -c and -f respectively:

For example

[vamshi@linuxcent Linux-blog]$ echo -e "Exploring the practical use of cut command" | cut -c1 --complement
xploring the practical use of cut command

The first character E is excluded and then remaining characters are printed because of –complement option being used in conjunction.

Delimiter Option -d, --bytes=DELIM from the Specified LIST
Unlike the field extractor option which only set to default single tab space as Delimiter, The Delimiter option provides the flexibility to setup a custom Delimiter to any character. Widely used for setting a Delimiter other than a Tab.

Lets take an example of the line following input line:

dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin

The delimit option -dcan be set to :  and have the combination of fields and characters/bytes to highly customize and refine the extract text.

[vamshi@linuxcent Linux-blog]$ cat /etc/passwd | grep docker | cut -d":" -f1
dockerroot
[vamshi@linuxcent Linux-blog]$ cat /etc/passwd | grep docker | cut -d"/" -f1-5
dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin

The same logic of obtaining and extracting the ranges and specific field extractions is applied commonly in cut command.

A very special mentioning of the operator -s or --only-delimited, Which prints only the lines modified by the cut command and delimited by the delimit operator.

[vamshi@linuxcent Linux-blog]$ cat README | cut -d":" -f1 -s
The following are Most popular Linux server Distributions and their curent versions
Redhat 
Ubuntu 
Centos 
Debian 
SUSE

The option --output-delimiter="STRING" takes a string as its input and then substitutes it with the actual delimiter:

Lets look at the below practical example:

[vamshi@node02 Linux-blog]$ cat /etc/passwd | grep docker | cut -d":" -f1- --output-delimiter=' '
dockerroot x 996 992 Docker User /var/lib/docker /sbin/nologin

Format the Output to print newline using cut command:

[vamshi@linuxcent  ~]$ cat /etc/passwd | grep docker | cut -d":" -f1- --output-delimiter=$'\n'
dockerroot
x
996
992
Docker User
/var/lib/docker
/sbin/nologin

The output-delimiter formatting the output by replacing the delimiter with a newline character using $'\n' thus printing the output separated by the newline

chown and chgrp commands to change ownership of files and directories in Linux

The commands chown and chgrp are extensively used in linux systems to update the ownership and organize the file structure.

TL;DR:
The chown command can be implemented in the following two types of notations.
1) User and group owner Symbolic notation
2) Reference operator

The options/filters will be common to them for the two kinds on notations, We will look at the practical and important options.
The chown command syntax:

chown <username> <data-directory> <files>
chown <username>:<groupname> <data-directory>
chown --reference <Source-reference-file| Source-reference-Directory> <data-directory>

The ownership of the files is a privileged operation and it requires a sudo access to the system
Lets start by running the chown command on the files you want to change the ownership
We have a file called jdk-8u141-linux-x64.rpm and its owned by root user.

[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 root root 169980729 Jul 13 2017 jdk-8u141-linux-x64.rpm

We intend to change the ownership to user vamshi and we use chown command

[vamshi@linuxcent ~]$ sudo chown -v vamshi jdk-8u141-linux-x64.rpm 
changed ownership of ‘jdk-8u141-linux-x64.rpm’ from root to vamshi
[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 vamshi root 169980729 Jul 13  2017 jdk-8u141-linux-x64.rpm

By this only the user part of ownership was modified but the group was uneffected.

NOTE: We are using the option -v to print the command output information in verbose mode.

We can use chgrp command to modify the group ownership as follows:

[vamshi@linuxcent ~]$ sudo chgrp -v vamshi jdk-8u141-linux-x64.rpm 
changed group of ‘jdk-8u141-linux-x64.rpm’ from root to vamshi

To change both the user owner and group at once, chown command can be used, as demonstrated below:

[vamshi@linuxcent ~]$ sudo chown -v vamshi:vamshi jdk-8u141-linux-x64.rpm 
changed ownership of ‘jdk-8u141-linux-x64.rpm’ from root:root to vamshi:vamshi

[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 vamshi vamshi 169980729 Jul 13 2017 jdk-8u141-linux-x64.rpm

How do you recursively change the permissions within the destination sub-directories and files ?

chown command when used with -R applies the updated ownership recursively till the directory depth on destination.

[vamshi@linuxcent data]$ ls -ld redmine/
drwxr-xr-x. 3 root root 17 Apr 17 16:31 redmine/
[vamshi@linuxcent data]$ sudo chown vamshi:vamshi /mnt/data/redmine/ -R
[vamshi@linuxcent data]$ ls -ld /mnt/data/redmine/
drwxrwxr-x. 3 vamshi vamshi 17 Apr 17 16:31 redmine-4.0.6

How to apply reference permissions to chown command in Linux ?

We shall be using the option --reference which takes the reference from the existing file and applies the permission

Suppose we directory called djangoproject1.
The root user copied the contents from elsewhere and placed  them in the location /home/alice .
Now the user alice has to have the user ownership to them in-order to work with djangoproject1.

[root@node02 alice]# ls -ld djangoproject1
drwxrwxr-x. 3 root root 108 Apr 16 11:45 djangoproject1

We now file called requirements.txt and the file is owner by the user alice and has the owner following permissions

[root@node02 alice]# ls -lth alice-requirements.txt
-rw-rw-r--. 1 alice alice 130 Apr 17 15:54 alice-requirements.txt

So we can simply reference this file and apply the same ownership permissions to djangoproject1, Lets see the demonstration.

[root@node02 alice]# sudo chown --reference alice-requirements.txt djangoproject1/ -R

As a result we have successfully modified the user and group ownership to alice using the --reference operator; Applying the same ownership permissions as our reference file alice-requirements.txt.

[root@node02 alice]# ls -lthr djangoproject1/*
-rw-rw-r--. 1 alice alice 405 Apr 16 11:44 djangoproject1/wsgi.py
-rw-rw-r--. 1 alice alice 756 Apr 16 11:44 djangoproject1/urls.py
-rw-rw-r--. 1 alice alice 3.1K Apr 16 11:44 djangoproject1/settings.py
-rw-rw-r--. 1 alice alice 0 Apr 16 11:44 djangoproject1/__init__.py
-rw-rw-r--. 1 alice alice 405 Apr 16 11:44 djangoproject1/asgi.py

djangoproject1/__pycache__:
total 8.0K
-rw-rw-r--. 1 alice alice 2.3K Apr 16 11:45 settings.cpython-36.pyc
-rw-rw-r--. 1 alice alice 159 Apr 16 11:45 __init__.cpython-36.pyc

Thus we can use the chown command to update the ownership information to files and directories and also use the chgrp command as per requirement.

How to Shutdown or Reboot a remote Linux Host from commandline

The Shutdown process in a Linux system is an intelligent chain process where in the system ensures the dependent process have successfully terminated.

TL;DR:

Difference between the Halt and Poweroff in Linux?
What is a Cold Shutdown and Warm Shutdown?
Linux System System Halt : The Halt process instructs the hardware to Stop the functioning of the CPU. Can be referred as a Warm Shutdown.
Linux System Poweroff/Shutdown : The Poweroff function sends the ACPI(Advanced Configuration and Power Interface) to power down the system. Can be referred as a Cold Shutdown.

As you may be aware the Linux runtime environment is a duo combination of processes running in User space and the Kernel space, All the major system activities and resources are initiated and governed and terminated by Kernel space.
So we got the Kernel space and the User space, The kernel space is where all the reseurce related processes run, which follows a finite behaviour, and the the userspace where the processes are dependent on the user actions, most of the userspace programs depend on the kernel space and make a context switch to get the CPU scheduling and such..
So, In the sequence of Shutdown on a linux machine, the userspace processes are first terminated in a systematic fashion through the scripts triggered by the core systemd processes which ensures clean exit and termination all the processes.

The Linux system provides us quite a few commands to enforce fast shutdown or a graceful shutdown of the operating system, each having their own consequences.

Firstly the init or the systemd which is the pid 1 process is what controls the runlevel of the system and it determines which processes are launched and running in that runlevel

The init is a powerful command which executes the runlevel it is told to.
Here the init 0 proceeds to Power-off the machine

$ sudo init 0

Here the init 6 proceeds to Reboot the machine

$ sudo init 6

These commands are real quick as it triggers the kernel space shutdown invocation process.. most often resulting in unclean termination of processes resulting system recovery and journaling while booting.

The following commands Shutdown the machine in seconds after issuing the command But tend to follow the kill sequence and clean exit of the processed.

$ sudo shutdown
$ sudo poweroff
$ sudo systemctl poweroff

Prints a wall message to all users.
All the processes are killed and the volumes are unmounted or converted to be in Read-Only mode while system power off is in progress.
Puts the system into a complete poweroff mode cutting out power supply to the machine completely.

$ sudo halt
$ sudo systemctl halt

Prints a message of “System halted” and Puts the machine in halt mode
If the --force or -f when specified twice the operation is immediately executed without terminating any processes or unmounting any file systems and resulting in data loss

The servers can only be brought back online through physical poweron or Remote Power manager console such as IPMI or ILOM.

To reboot or [/code]systemctl kexec[/code] will to restart the operating system which is one power cycle or equivalent of shutdown followed by the startup.

$ sudo reboot

$ sudo systemctl kexec

$ sudo systemctl reboot

If the --force or -f when specified twice the operation is immediately executed without terminating any processes or unmounting any file systems and resulting in data loss

 

It is important to understand that the commands are all softlinks to systemctl shutdown command. and ensure in proper shutdown sequence process

[vamshi@linuxcent cp-command]$ ls -l /usr/sbin/halt
lrwxrwxrwx. 1 root root 16 Jan 13 14:41 /usr/sbin/halt -> ../bin/systemctl
[vamshi@linuxcent cp-command]$ ls -l /usr/sbin/reboot
lrwxrwxrwx. 1 root root 16 Jan 13 14:41 /usr/sbin/reboot -> ../bin/systemctl
[vamshi@linuxcent cp-command]$ ls -l /usr/sbin/poweroff
lrwxrwxrwx. 1 root root 16 Jan 13 14:41 /usr/sbin/poweroff -> ../bin/systemctl

It is important to observe that all the commands are softlink to the systemctl process, When issuing a shutdown or reboot

The best practice to poweroff the system by enabling broadcast the notification message to all the users connected actively either through the Pseudo remote connection terminal or TTY terminals, Demonstrated as follows:

$ sudo systemctl poweroff

# this writes an entry into the journal, the wtmp and broadcasts the shutdown message to all the users connected through PTS and TTY terminals

What is the difference between systemctl poweroff and systemctl halt ?

The Linux System when put to a Halt state, stops the all the applications and ensures they’re safely exited, filesystems and volumes are unmounted and it is taken into a halted state where in the power connection is still active. And Can only be brought  online with a power reset effectively with a simple reset.
The Halt process instructs the hardware to Stop the functioning of the CPU.
Commonly can be referred as a Warm Shutdown.

Below is the screenshot to demonstrate the same
systemctl halt command in linux

The Poweroff function sends the ACPI(Advanced Configuration and Power Interface) to power down the system.
The Linux System when put to a Poweroff state it becomes completely offline following the systematical clean termination of processes.. and power input is cut off to the external peripherals, which is also sometimes called as cold shutdown, and the startup cold start.
Commonly can be referred to as a Cold Shutdown.

If you found the article worth your time, Please share your inputs in the comments section and share your experiences with shutdown and reboot issues

Change file(s) and Directory/Folder ownership – chmod command in Linux

The Linux command utility chmod is used to set permissions on files and directories

The command chmod, or Change Mode, is widely used to modify the access permissions of files and directories, This facilitates the users to keep the data secure and properly organized.
There are three types the file permissions can be modified using chmod command:

1) Symblolic Notation

2) Octal Notation

3) Reference operator

The Owners, Groups and Others have different permissions to access a particular file.
Lets see the Sample syntax of chmod

$ chmod [option] [mode] < file | directory>

The Users categories are symbolically represented as

-u is used for the User/Owner

g is used for the Group

o is used for the Others

The Permission modes used to modify the Read, Write and Execute permissions respectively on a file or Directory and are symbolically represented as follows

r is for Read permission

w is for Write permission

x is for Execute permission

The Octal notation is simpler interms of representation and execution with r=4 w=2 x=1 total adding up to numerical aggregate value of 7 taking the place holders of Owner(-u) Group(-g) Others(-o) respectively.

Lets see the file permissions for a sample file with complete access to every user category i.e owner, group and others, Meaning that any person who has access to the file can modify,execute(run) or delete the newfile.txt

[vamshi@node02 linuxcent]$ ls -l file.txt
-rwxrwxrwx. 1 vamshi vamshi 3 Apr 10 19:13 file.txt

From the above listed permissions we would like to release the “others” from writing this file but only limit them to read it.

The command will be to delete the Write permissions from others:

[vamshi@linuxcent ~]$ $ chmod o-w file.txt
[vamshi@linuxcent ~]$ ls -l file.txt
-rwxrwxr-x. 1 vamshi vamshi 3 Apr 10 19:13 file.txt

We use the + and - operators to provide the Read,Write and Execute permissions to user categories, It’s Demonstrated as follows:

[vamshi@node02 linuxcent]$ chmod og-rw file.txt
[vamshi@node02 linuxcent]$ ls -l file.txt
-rwx------. 1 vamshi vamshi 3 Apr 10 20:23 file.txt

Combining options such read operation for other and group, write for group and execute permission being removed for owner.

[vamshi@node02 linuxcent]$ chmod og+r,g+w,u-x file.txt
[vamshi@node02 linuxcent]$ ls -l file.txt
-rw-rw-r--. 1 vamshi vamshi 3 Apr 10 20:23 file.txt

Creating a new file newfile.txt and working with it

[vamshi@node02 linuxcent]$ echo "Welcome to LinuxCent" > newfile.txt

Here are the file’s permissions when it’s newly created. It is resultant of the umask

Lets look at a Linux practical example to setup only execute permission for all the user categories

$ chmod +x  /etc/profile.d/java-path.sh

Listing out the System defined Default umask in Symbolic notation.

The Default Umask permissions can be demonstrated as below:

[vamshi@node02 linuxcent]$ umask -S
u=rwx,g=rwx,o=rx

Umask in Octal Notation as below:

[vamshi@node02 linuxcent]$ umask
0002

Now the new lets see the file permissions:

[vamshi@node02 linuxcent]$ ls -l newfile.txt
-rw-rw-r--. 1 vamshi vamshi 21 Apr 10 19:49 newfile.txt

This is the inverse notation of the umask Octal value(002) set at the system wide and our file has the 664 Octal Notation.

The Octal mode of Permissions

These permissions carry a numerical value all adding up to a sum of 4 and categorized as follows:

r=4 w=2 x=1

For Example chmod 754 file1.txt

[vamshi@node02 linuxcent]$ ls -lth newfile.txt
-rwxr-xr--. 1 vamshi vamshi 3 Apr 10 19:13 newfile.txt

How to apply reference permissions in Linux using chmod ?

We shall be using the option --reference which takes the reference from the existing file and applies the permission

Lets see the demonstration below:

vamshi@linuxcent ~]$ ls -l anotherfile.txt
-rwxrwx---. 1 vamshi vamshi  33 Apr 8 19:52 anotherfile.txt
vamshi@linuxcent ~]$ chmod --reference=anotherfile.txt newfile.txt
[vamshi@linuxcent ~ ]$ ls -l newfile.txt 
-rwxrwx---. 1 vamshi vamshi 21 Apr 10 19:49 newfile.txt

With this reference operator, the nexfile.txt permissions are modified identical to anotherfile.txt

 

Linux Find command – with Practical examples

The Linux find command utility is a powerful command in locating the files and directories at system wide level, the

The basic syntax of find command is very straight forward. The number of filters and types of options you choose while forming the find command determines and complexity of the operation and is directly proportional to the run time of the command.

Lets examine the general syntax of Linux find command line utility

$ find [path] [Options/filters] [expression]

How to Find all the files that are modified within 1 day.

$ find . -mtime -1

Use find command to find all the files that are modified within last x Minutes

$ find . -mmin -360

How to Find all the files that are created within given Minutes

$ find . -cmin -360

How to Find all the files under a specific size on the filesystem.

$ find . -type f -size -100G

NOTE: the - will find the files including and under the mentioned size

How to find really big size files with a given size using Linux Find Command.

$ find . -type f -size +100G

NOTE: the + will find the files exceeding the mentioned size

How to Find all the files with a pattern match using Find command?

vamshi.santhapuri@linux-pc:~/Linux/Programs> find *.py
my_program.py
myProgram.py
My_program.py

OutPut Demonstrated:

vamshi@linuxcent:~/Linux/Programs> ls -l
total 0
-rwxr-xr-x 1 vamshi users 0 Apr 8 16:33 my_program.py
-rwxr-xr-x 1 vamshi users 0 Apr 8 16:32 myProgram.py
-rwxr-xr-x 1 vamshi users 0 Apr 8 16:32 My_program.py ex

 

Find Files Based on file-type using option -type

Find only the regular file type

$ sudo find / -type f -print

Find only the Directory

$ sudo find / -type d -print

Lets see some special file formats like character files as in network level files and block files as in storage device file type.

$ sudo find /dev/ -type b -print
/dev/sdb8
/dev/sdb7
/dev/sdb6
/dev/sdb5
/dev/sdb4
/dev/sdb1
/dev/sdb
/dev/sda6
/dev/sda5

How to find empty files and Directories in Linux using Find command?

$ find . -empty # Finds all files and directories that are empty
$ find . -type f -empty #Find onlt empty files
$ find . -type d -empty #Find only empty Directories

How to Find all the hidden files and directories using Find command

You can also add the type flag as required

$ find . -name ".*" //Prints all the files and directories that are hidden

Find the character stream files and identify them yourselves.

$ sudo find / -type c -print

 

How to exclude some files or directories while using the find command?

The negation Operator ! in find command line

[vamshi@linuxcent ~]$ find / -type d  ! -path /mount ! -path /mnt/3TB/

How to find the files with .py extension and then setting the execute permission on them ?

-exec is a special builtin option which takes the specified command on the selected files

[vamshi@linuxcent ~]$ find *.py -exec chmod +x {} \;

A practical example of Finding and Deleting Big size files using Find Command

We Demonstrate a Big thread dump file that we generated in our Test environment by finding it on our server and then deleting it.

[vamshi@linuxcent ~]$ find /var -type f -size +100G -exec rm {} \;

 

To find the files with matching file permissions

[vamshi@linuxcent ~]$ sudo find / -type f -perm 0600 -print

More complex find operations can be performed by combining the negation operator and by excluding the certain mount points and using the logical AND OR operators.

For example:

sudo find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) -exec ls -ld {} \;

Ignore case using the find command

The filter -iname enables the find to perform case insesitive search.

[vamshi@node02 Linux-blog]$ find . -iname "rEadMe*" 
./Debian-Distro/README-Debian-Distro
./OpenSuse-Distro/README-Opensuse-Distro
./README
./Redhat-Distro/Centos/README-CentOS
./Redhat-Distro/README-Redhat-Distro

How to use Maxdepth Option in the find command to explore the directory depth?

[vamshi@node02 Linux-blog]$ find . -maxdepth 1 -name "README*" 
./README

By using find command filter option -maxdepth Now we can extract the results based on the one level deeper subdirectory, which is demonstrated as follows:

[vamshi@node02 Linux-blog]$ find . -maxdepth 2 -name "README*" 
./Debian-Distro/README-Debian-Distro
./OpenSuse-Distro/README-Opensuse-Distro
./README
./Redhat-Distro/README-Redhat-Distro

How to use mindepth Option in the find command to restrict the results based on the subdirectory depth?

The -mindepth filter option restricts the find command search to nth subdirectory level from the given path.

[vamshi@node02 Linux-blog]$ find . -mindepth 3 -name "README*" 
./Redhat-Distro/Centos/README-CentOS

 

How to use grep command in Linux and Unix

The grep command is one of the most essential commands in Unix/Linux ecosystem, It is used to extract match and find the occurrence of string literals and patterns among the text files.

In this section we will focus on the important scenarios and provide the realtime explanation about the Various Grep command options offered in linux command line.

We now have a text file with Following content.

Below is the content of the file called intro.txt.

[vamshi@linuxcent grep]$ cat intro.txt
the first line is in lower case
THE SECOND LINE IS IN UPPER CASE


There following are Most popular Linux server Distributions:
1) Redhat Enterprise Linux
2) Ubuntu
3) Centos
4) Debian
5) SUSE Linux Enterprise Server
This is a Demonstration of Linux grep command

Let’s start with search of a string pattern in the given file

The grep command sample search syntax is

$ grep “string pattern” <filename>
$ cat < filename | * > | grep "string pattern"

Lets see the Demonstration

[vamshi@linuxcent grep]$ grep "Linux" intro.txt
There following are Most popular Linux server Distributions:
1) Redhat Enterprise Linux
5) SUSE Linux Enterprise Server
This is a Demonstration of Linux grep command

Searching for a pattern on multiple files using grep command

Lets see the Demonstration

[vamshi@linuxcent grep]$ grep "Linux" intro.txt intro-demo.txt
intro.txt:There following are Most popular Linux server Distributions:
intro.txt:1) Redhat Enterprise Linux
intro.txt:5) SUSE Linux Enterprise Server
intro.txt:This is a Demonstration of Linux grep command
intro-demo.txt:There following are Most popular Linux server Distributions:
intro-demo.txt:1) Redhat Enterprise Linux
intro-demo.txt:5) SUSE Linux Enterprise Server
intro-demo.txt:This is a Demonstration of Linux grep command

 

How to print the sequence number of matched pattern in file using Grep command in Linux?

Print the sequence number of lines that contain the matched string pattern can be achieved using -n Option:

[vamshi@linuxcent grep]$ grep -n "Linux" intro.txt
4:There following are Most popular Linux server Distributions:
5:1) Redhat Enterprise Linux
9:5) SUSE Linux Enterprise Server
11:This is a Demonstration of Linux grep command

The -b Option prints the subsequent starting offset index of the matching word from the lines within the given filename.

[vamshi@linuxcent grep]$ grep -b "Linux" intro.txt
66:There following are Most popular Linux server Distributions:
127:1) Redhat Enterprise Linux
184:5) SUSE Linux Enterprise Server
217:This is a Demonstration of Linux grep command

So far we have seen only the matching string pattern, but if you want to find the occurance of particular word then use grep -w Option

Lets us see the Demonstration below:

[vamshi@linuxcent grep]$ grep -w "Cent" intro.txt


And As expected here we have not seen any occurrence of the word Cent from the file although the word Centos was present, But it didnt match because we used -w option to match the word Cent.

But when we grep for the Word Linux then we can see the exact occurrence of the word and the matching lines are printed.

[vamshi@linuxcent grep]$ grep -w "Linux" intro.txt
There following are Most popular Linux server Distributions:
1) Redhat Enterprise Linux
5) SUSE Linux Enterprise Server
This is a Demonstration of Linux grep command

This can be used in conjunction with -o prints only completely matched word string pattern.

[vamshi@linuxcent grep]$ grep -w -o "Linux" intro.txt
Linux
Linux
Linux
Linux

How to use the Invert Option in Linux grep command

The -v option prints all the lines not matching the given string pattern.

[vamshi@linuxcent grep]$ grep -v "Linux" intro.txt
the first line is in lower case
THE SECOND LINE IS IN UPPER CASE


2) Ubuntu
3) Centos
4) Debian

 

How to print the names of all the files matching the string pattern using Linux Grep command.

Using the -l Option of grep command will print all the file names containing the matching string pattern/string literal

[vamshi@node02 grep]$ grep -l Linux *
intro-demo.txt
intro.txt

How to get the number of Count Matches using Grep command ?

In Linux Grep command we can use the Option -c to get the total occurance Count of the matching pattern/string

Lets see the Demonstration below

[vamshi@linuxcent grep]$ grep -c "Linux" intro.txt
4

How to Exclude specific Directories while searching with grep command ?

The --exclude-dir="Some Directory name" options skips the contents of the mention directory Lets see the Demonstration:

$ grep -w -E "data" * -R --exclude-dir="backup" --exclude-dir="adb-fastboot" --exclude-dir="IntelliJ-IDEA"

How to use Linux Grep commmand to ignore the case  OR perform Case Insensitive search ?

Linux Grep Command offers the -i to skip the case and perform the search.

[vamshi@linuxcent grep]$ grep -i Cent intro.txt
3)Centos

Lets search for the Work Upper from the file with -i option

[vamshi@linuxcent grep]$ grep Upper -i intro-demo.txt
THE SECOND LINE IS IN UPPER CASE

How to Grep for the lines before and After the pattern occurrence in a file

We can use the below options in Linux Grep command to extract the N number of lines matching the containing pattern before/After from the given file.. Lets look at the Options in detail

Generic Syntax:

grep -<A|B|C> “string pattern” filename*

Lets see the Demonstration as follows:

-A: Prints N number of lines After the pattern match and the line containing the match

[vamshi@linuxcent grep]$  grep -A2 docker /etc/passwd
dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin

-B: Prints N number of lines Before the pattern match and the line containing the match

[vamshi@linuxcent grep]$  grep -B2 docker /etc/passwd
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
builduser1:x:1002:1002::/home/builduser1:/bin/bash
dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin

-C: Prints the line Containing the pattern and the N number of lines Before and After

[vamshi@linuxcent grep]$ grep -C2 docker /etc/passwd
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
builduser1:x:1002:1002::/home/builduser1:/bin/bash
dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin

Linux Grep command using the Regular Expression (regex) search pattern

How to use the Special Meta-characters in Grep Command

The Special Meta-Character ^ (caret) symbol to match expression at the start of a line.
Practical examples of grep command using meta-characters are as follows:

[vamshi@linuxcent grep]$ grep ^root /etc/services
rootd           1094/tcp                # ROOTD
rootd           1094/udp                # ROOTD

$ (dollar) symbol to match expression at the end of a line

[vamshi@linuxcent grep]$ grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
vagrant:x:1000:1000:vagrant:/home/vagrant:/bin/bash
vamshi:x:1001:1001::/home/vamshi:/bin/bash
jenkins:x:997:994:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
builduser1:x:1002:1002::/home/builduser1:/bin/bash
linuxcent:x:1003:1004::/home/linuxcent:/bin/bash

Another Bonus regex for evading Empty lines from a text file using the Meta-characters ^ Carat and $ Dollar Symbols.

[vamshi@node02 grep]$ grep -Ev "^$" intro.txt
the first line is in lower case
THE SECOND LINE IS IN UPPER CASE
There following are Most popular Linux server Distributions:
1) Redhat Enterprise Linux
2) Ubuntu
3) Centos
4) Debian
5) SUSE Linux Enterprise Server
This is a Demonstration of Linux grep command

As the result of the above command observed Empty lines in our text file are now excluded with the inverted matching of Empty line pattern expression.

Digit Matching operation using Linux Grep Command

The Grep command Option -P offers the perl-regex pattern matching to perform some of the tricky pattern matching conditions, Lets see the Demonstration.

[vamshi@linuxcent grep]$ grep -P "[\W][\d][\d]/" /etc/services

Now we Demonstrate Numerical pattern matching in the following ways to match the exact occurrence of IPv4 address

[vamshi@linuxcent grep]$ ip a |grep -E "[0-9].[0-9].[0-9].[0-9]"
inet 127.0.0.1/8 scope host lo
inet 10.100.0.20/24 brd 10.100.0.255 scope global noprefixroute eth1

The similar result can be extracted using -P, Demonstrated as below:

[vamshi@linuxcent grep]$ ip a  | grep -P "[\d].[\d].[\d].[\d]"
inet 127.0.0.1/8 scope host lo
inet 10.100.0.20/24 brd 10.100.0.255 scope global noprefixroute eth1

Highlighting the Search patterns with Color codes

If you want to highlight the search pattern from the output we can make use of Color code Option is grep

[vamshi@linuxcent ~]$ ps -ef |grep java --color=yes

 

grep command and the Meta-characters with practical examples

In the Extended Regular Expression the metacharacters ?, \+, \{,  \|, \(, and \) enhances and takes the search string to manifest into a rich pattern

The square brackets [/code][ ][/code] includes a list of characters and it matches a single character in the list. Within the bracket, we can specify a  range, like [a-z] [0-9] [abcd], But it only matches a single character in the give list range.
The brackets can contain the carat ^ or the $ symbols and can be globbed with the wildcard characters for finding the repetitive pattern.

Here we shall see the demonstration of a few metacharacters conjunction into a single grep command

Using the Infix Operator | with the meta-characters

$ dmesg -H | egrep '(s|h)d[a-z]'

A quick practical look at the dmesg to find about the errors and warnings using grep command.

$ dmesg -H | grep -Ei “error|warn”

How to find the exact work match for the word error or err with a blank white space at the start of the word from dmesg?
$ dmesg -H | grep -Ei “(er)[r]{1,2}(or)”
$ dmesg -H | grep -Ei “(\We)[r]+(or)|(\Werr)”

Explanation: The grep search metacharacter regular expression matches the occurring of the string literals “error” with the minimum 1 time occurrence (error) of literal r and maximum 2 times.

The output only prints lines containing the word “error“ in a case insensitive grep search.

 

Another great practical example here we apply the same logic to enhance the metacharacter regex pattern to search for the occurrence of error with but without a trailing colon “:” symbol

dmesg -H | grep -Ei “(error)[:]{0}”