Question :
Linux for beginners - Commands & ExplanationSubmitted on 19/4/2024 | Answered by Avi RanaAnswer :
Linux is a free & open source operating system that can be modified and disturbed by
anyone under the GNU General Public License. Linux can be found in the smallest
hardware platform to world’s largest supercomputers. It was developed by Linux Torvalds.
Linux heavily borrows heavily from the Unix operating system. Linux is a multi-user,
multiprogramming, multitasking operating system. Linux is developed by Linux Torvalds.
Now, Almost half of the world runs on linux.
As anyone can freely edit & distribute Linux, there are a number of linux distributions
available. Some of these are RedHat family systems which consist of CentOS & Fedora,
openSUSE and Debian family systems which consist of Ubuntu & Linux Mint.
Features
Linux also provides us with a lot of features, they are as follows:
1. Portable environment
2. Free & open source
3. Shell/Command-line interface
4. Lightweight infrastructure
5. End-to-end encryption
Booting Process
Booting is the process of initializing the system. It consists of everything that happens
when the system is powered on until the user interface is fully loaded. The booting
process is
BIOS>>MBR>>GRUB>>KERNEL>>INIT>>RUN-LEVEL
● BIOS: Basic Input/Output System
● MBR: Master Boot Record (MBR is of 512 bytes)
● GRUB: GRand Unified Bootloader
● KERNEL
● INIT: Initialization
● RUNLEVEL
RUN LEVELS
0 – System halt
1 – Single user mode
2 – Multiple user mode with no NFS(network file system).
3 – Multiple user mode under the command line interface and not under the graphical
user interface.
4 – User-definable
5 – Multiple user mode under GUI (graphical user interface) and this is the standard
runlevel for most of the LINUX based systems. (Default run-level)
6 – Reboot
File System
Linux uses a hierarchical system to organize files and directories in it. This structure is
often called a directory tree. The tree has a single node, the slash character (/), all
available directories are under this slash.
A directory is a special file that stores file & directories in it. These directories are referred
to as folders in Windows OS.
In Linux, we can create different disk partitions with different file systems such as ext2 &
ext3.
Normally each linux distribution implements a File Manager utility, which enables us to
locate and execute a file which either a program or opens an program which displays the
data.
FILE LOCATIONS
1. /etc/motd: motd stands for Message of the day
2. /etc/passwd: all user detail list
3. /home: see all users
4. /etc/shadow: all user password (encrypted)
5. /etc/group: all user group
6. /etc/rc.d: contains the start script for all runlevels
7. /etc/inittab: init test instruction (run levels) and data about default run level
8. mnt is used as default mount point
MORE FILE LOCATIONS
All these files are directly under /.
1. /boot - used to store files to boot the system
2. /dev - used to store special device files that the system uses to access hardware
3. /etc - used to store system configuration files
4. /home - home directory where regular users store their files & data
5. /root - directory for superuser
6. /usr - installed users programs and libraries
7. /var - stores system variables which don't get deleted b/w system reboots
8. /tmp - temporary storage for files
9. /run - runtime data for process such as processID
Mount & Unmount
Refer to Mounting and Unmounting File Systems
Backup
We can backup our system using commands such as cp or rsync.Although both can copy
the entire directory tree, rsync is much better & efficient as it checks if the copied file
already exists. Therefore, rsync is faster and more efficient. rsync can also be used to
move data from one computer to another.
RSYNC
rsync or Remote sync is used to create backup of the entire system. It is showing 7better
than cp as rsync does not copy files that already exist at the given location, it checks if
the file was modified. rsync is called remote sync as it can move data from one computer
to another.
Options
1. -dry-run: used to see the outcome of the command
2. ⁷2ẞ--progress: display move progress
File Compression ratio is
Files are compressed to reduce the disk Touch and hold a clip to pin it. Unpinned clips will
be deleted after 1 hour.Touch and hold a clip to pin it. Unpinned clips will be deleted after
1 hour.size that they occupy and reduce time taken to transmit the file over a network.
There are alot commands for file compression in linux, they are as follows:
1. GZIP (GNU zip)
2. BZIP2
3. XZ
4. ZIP (Zip archive file)
5. TAR (Tape archive)
GZIP (GNU zip)
Also stands for GNU zip, the most common compression method and is very fast. We use
gunzip to extract a file that was compressed with gzip. Compressed files will have .gz
extension. It will replace the original file with the zipped file.
Options
1. gzip * : compress all files in current directory
2. gzip <filename>
3. gzip -r <directory> : compress all files in a directory recursively
4. gunzip <filename.gz>: extract gz file
BZIP2
bzip2 has a similar syntax to gzip but uses a different algorithm than gzip and produces
files smaller than gzip but takes more time as well. Therefore it is only used to compress
very large files.r
XZ
xz is the best compression tool in terms of file size. The extension of the compressed file
is .xz
Options
1. xz * : compress each file in the current directory and replaces each one with a .xz
file
2. xz <filename>
3. xz -d <filename.xz>: to decompress
4. Xz -dk <filename.xz>: decompress .xz file and keeps it after successful
decompression
ZIP
Zip or zip archive file is used by WindowsOS, therefore best for sharing files on a different
OS.
1. zip <filename> <filename>
2. unzip <filename.zip>
TAR (Tap archive)
1. tar -cvf <filename.tar> <filename>
2. tar -xvf <filename>
Options
1. c=create
2. v=verbose (show name of files & folders)
3. f=file (output file)
4. x=extract
5. z=winzip (make compatible with windows)
6. t=display content for content of tar without extraction
GUNZIP
Gun zip is used to make a smaller zip than tar but is part of tar
1. tar -cvzf <filename.tar.gz> <filename>
2. tar -xvzf <filename.tar.gz>
We can also use the compress command to create an archive with the .z extension and
use uncompress command to extract the archive made with the compress command.
File Searching
We need to be able to find files on our system to save time & improve productivity.
LOCATE
This command searches for the requested file from a preconstructed database of files
and directory on your system. We need to use the updatedb command to update this
database. (GREP is a better option from this command)
FIND
This command uses a lot of the system resources to find the requested file. Even though
it is system expensive, it is a highly used command.
Its -exec option enables it to perform a command on the requested file.
Managing Users
On a linux system, each user is given an unique user id (uid), which is just an integer. For
normal users, the uid starts from 1000 and goes up. Linux uses groups to organize users.
Groups are collections of users with certain shared permissions. The control of group
membership is administered through /etc/group. All members of a group enjoy the same
privileges and access.
There are three types of users on a linux system:
1. root
2. system
3. users
ROOT
The root user has complete access and privilege over the system. It can control the whole
system, even completely destroy it. Root user has no restriction. When we are signed in
as root on the shell, it prompts the # symbol.
SYSTEM
These accounts are used to set up system servers like mail accounts and ssh accounts
etc. They are used to start & stop system services.
USERS
These accounts have very few privileges compared to root users.
There are also some commands to manage users.
COMMANDS
SU
su stands for switch user
1. su: change user
2. su -: to login superuser/root
3. su <username>: to login/access another user
CHMOD
It stands for change mode
- = file
d = directory or folder
l = link
rwx rwx rwx
r = read
w = write
x = execute
1 - owner
2 - group
3 - others
r = 4
w = 2
x = 1
- = denied
1. chmod -c <change in permission> <filename>
2. chmod <groups><+ or -><permission> <filename>
3. chmod 777 <filename>
CHAGE
This command is used to change & view password aging information for a user. This
command is useful when a user is to be provided with limited time access.
Example: chage <username>
Options
1. h: help for chage
2. d: set last password expiry date
3. m: set days before password is used
4. M: set max days before password expiry
5. E: set account expiry date
6. l: list account aging information
WALL
The wall command or write all command is used to send a message to all logged in users.
This command is very useful in servers where multiple users are logged in.
MESG
If we don't want to receive messages from other users, we can use the following
commands:
1. mesg y <username>: to allow messages
2. mesg n <username>: to block messages
WRITE
Message a user logged in on the same system without email
Example: write <username>
USERMOD
1. groupadd <groupname> : create a new group
2. useradd <username> : create a new user
3. usermod -G <groupname> <username>
Options
1. G:
2. g:
3. L: Lock a user/Make them unable to login
4. U: Unlock a user
Tips
1. Use cat /etc/group to see all groups and members
2. Use cat /etc/passwd or cat /home to see all users
pkill -KILL -u <username>: to logoff user
CHOWN
Change file ownership
1. chown -c <username> <filename>
Example: sudo chown -c user test
UMASK
This command is used to change the default permissions that a file is assigned on
creation. It takes a numerical argument as chmod
PASSWD
Change user password.
1. Passwd
User Creation
1. sudo adduser ashu
2. sudo passwrd ashu
Text Editors
Methods
1. cat>filename: Use CTRL+Z to save file or CTRL+C to kill file
2. vi filename
3. echo “data”>filename
Virtual Editor or vi
Modes
1. Command Mode
2. Insert Mode
3. Escape Mode
Commands
1. w: write
2. q: quit
3. !: override
● Press i key to enter insert mode
● Press ESC key to enter command mode and type
● Press d key two times to delete a whole line while hovering on it
● Press x key to delete a single character
● Press k key to move the cursor up one line
● Press j key to move the cursor down one line
● Press h key to move to the left
● Press l key to move to the right
● Press p key to paste
● Press H key to go top of the file
● Press L key to go to the bottom of the file
● Press ^ key to go to the start of the current line
● Press $ key to move to the end of the current line
● Press o key to create an empty line
● Press u key to undo
● Press yy key to copy entire line
Gedit
gedit works the same as notepad in Windows. All the shortcuts are the same.
Filter/Search
Content Comparison
1. diff: difference in file
2. comm: common data in file
3. cmp: short form for compare (files)
REV
rev command is used to reverse a string.
Example: echo “Hello” | rev
WC (Wordcount)
1. wc -l <filename>: no of lines
2. wc -c <filename>: no of char
3. wc -w <filename>: no of words
4. wc -L <filename>: longest line length
5. wc -m <filename>: no of bytes
SORT
1. sort <filename>
2. sort -d <filename>: sort in dictionary order
3. sort -g <filename>: sort file data numerically
4. sort -h <filename>: ascending order
5. sort -r <filename>: descending order
6. sort -n <filename>: ascending numerical order
7. sort -nr <filename>: descending numerical order
8. sort -k <column example 3n> <filename>: order according to column
9. sort -o <old file> <new file>: sort data and input in new file
10. sort -i <filename>: sort with case insensitive
UNIQ
1. uniq -d <filename>: displays unique lines & ignores duplicates
2. uniq -u <filename>: displays only non repeating lines
3. uniq -c <filename>: counts and displays all lines
CUT
View specific column of data from multiple columns, opposite of paste command
Here,
1. d option represent delimiter(or separator simply)
2. f option is used to specific the column number we wish to retrieve and view alone
1. cut -d " " -f l <filename>
2. cut -d " " -f 1,2,3 <filename>: To view multiple columns
3. cut -c 1-5 <filename>: To separate by char range (where 1-5 is range)
4. cut -c 1,2,3,7 <filename>: To separate by char position
5. cut -c 1-4,7-9 <filename>: for multiple range
Example: cut -d- -f1 sample
PASTE
Paste command is used to combine multiple columns from multiple files and display
them in one place
1. paste number <filename> <filename>: used to show multiple files in one place
2. paste -d " " number <filename> <filename>: used to change delemeter or
separator
3. paste -s number <filename> <filename>
Reference: Paste command in Linux with examples
TEE
The commands create a new file and displays its content automatically (which does not
happen in touch/echo/cat/vi method to create a file), usually used with pipe character
1. tee <filename>
Example: wc -l name | tee file10
TR
tr command is used to make changes in a file.
Example:
1. cat <filename> | tr “[:lower:]” “[:upper:]” : This commands changes all chars in
lowercase to uppercase
2. echo “Current time is 04:03 PM” | tr -d “[:digit:]”
OD (Octal dump)
Stands for octal dump, used to display contents in different encodings, e.g. octal, binary
and hexadecimal
NL
Used to number lines
Example: nl sample
Advance Filters/Search
GREP
Grep is used to search for a file.It stands for Global regular expression print and print
out, the searched term/pattern is called the regular expression.
1. -c: returns number of lines containing specific word
2. -h: shows all lines containing specified word (same as normal)
3. -n: display line with line number
4. -v: display lines not containing specified words (inverse of -h)
5. -i: ignore case of text(case insensitive)
6. -l: find word in all files in particular location and return file name
7. -e: used to find multiple words/patterns
8. -An: used to display nth line after found pattern
9. -Bn: used to display nth line before found pattern
10. -Cn: used to display n lines before & after found pattern
11. Example grep -e "search word" -e "second word" <filename>\
12. grep "search parameter" <filename>
13. grep "^search_parameter" <filename>: show lines with first char with specified
char
14. grep "search_parameter$" <filename>: show lines with last char with specified char
Note: There are two more types of grep which are egrep & fgrep
SED (Stream editor)
Also called the stream editor, can perform a lot of actions on a file like search, replace,
insertion & deletion. Main use of the command is to find and replace without opening a
file.
1. sed “s/unix/linux/” <filename>: replaces first occurrence unix with linux in the
given file
2. sed "s/unix/linux/2” <filename>: replaces nth occurrence, which in this this
example will be the second occurrence
3. sed “s/unix/linux/g” <filename>: replaces all occurrences of specified word.
Refer codequotient for more options
Note: This command does not make the change in the file, simply return the changed
text.
Redirecting errors
As we know that we can use the redirection symbol to redirect output of a file. In the
same way we can redirect errors, the bourne shell uses 0,1, 2 as descriptors for output
redirection.Here
0: stdin (standard input)
1: stdout (standard output)
2: stderr (standard error)
Networking
An interconnection of multiple devices, known as hosts connected together to exchange
data is known as Computer Network. A Computer network can also include multiple
devices that help in communication between two devices, these are known as Network
devices/hardware.
IP Address Basics
IP Address (Internet Protocol Address) is a unique ID given to each host on a network. If any
device joins a network, an IP address is assigned to it. There are two types of IP address,
i.e. IPv4 (IP Version 4) & IPv6 (IP Version 6).
IPv4 uses 32 bits whereas IPv6 uses 128 binary bits hence it is more accurate.
IP addresses can be either static or dynamic. A static IP address is a permanent address
assigned to a host. These are rarely assigned by an ISP (Internet Service Provider). We can
assign a static IP address to a host on our local network. Dynamic addresses are most
popular. They are assigned with the help of Dynamic Host Configuration Protocol (DHCP).
DHCP typically runs on network devices such as routers or dedicated DHCP servers.
Network Classes & Subnet Mask
IP addresses are divided into different classes of network by the organizations that
govern them. These classes are identified by their subnet mask
Network Ports
A network port is provided by the Transparent Layer Protocol of the internet protocol suite,
such as Transmission Control Protocol (TCP) and User Diagram Protocol (UDP) which serves
as an endpoint between two computers.
COMMANDS
IFCONFIG
It stands for Interface Configurator. It is the most fundamental command for networking
as it displays information about a system connected to a network.
1. ifconfig: Get system IP
PING
It stands as Packet Internet Groper. It is used to check the network connectivity between
two nodes. It sends ICMP (Internet Control Message Protocol) echo’s to check connectivity
until it is interrupted. We can use CTRL+C to interrupt the execution.
DIG
DIG stands for Domain Information Groper. It is used to perform DNS or Domain Name
System lookups & display results from primary & secondary nameservers.+
Example: dig www.google.com
NETSTATS
NETSTATS stands for Network Statistics. It is used to get information about open sockets,
routing tables & connection information. It can also display TCP & UDP sockets.
HOST
This command is used to get the IP address of a domain name & vice-versa.
SSH
SSH or secure shell or secure socket shell is a network communication protocol that enables
two computers to communicate.
1. ssh <username>@<ip>: connect to machine thru ip
2. ping <ip>: check if system is online
HOSTNAME
1. hostname: returns hostname
2. hostname <name>: change hostname
3. hostnamectl set-hostname <name>: change hostname (same as above)
Process Scheduling
Scheduling is used to execute commands without human intervention, automatically.
AT
This command is used to schedule a task. Executing it lets us write a script. We need to
CTRL+D to end the script.
1. at <time> : at executes a command single time
2. atq : view job execution queue
3. atqrm <jobId>: remove a scheduled job
Example: at 02:44 PM
cp /etc/passwd atfile
CRONTAB
Cron is a greek word meaning “time”. It is a time based scheduling utility command. It can
perform jobs in background. It has its configuration file called /etc/crontab
1. crontab -l: Used to view job
2. crontab -e: Used to edit job
* * * * *
1. First * denotes minutes
2. Second * denotes hour (in 24hr format)
3. Third * denotes day
4. Fourth * denotes month
5. Last * denotes week day (0-6)
Note: No need to specific week day when specifying day and vice-versa
Note: If no value is given and option contains *, then job will execute every hour, month
etc according to given option
Example: crontab -e 10 14 * * * echo “Daily Hello”>daily
SLEEP
Process Management
Commands
1. ps: displays all running process
2. bg (background)
3. fg (foreground)
4. top: displays all process in detail
5. nice: run a process after change in priority
6. renice: change priority of a process
7. kill <processID>: to end a process
Disk Management
df
Stands for “disk filesystem“, displays full summary of available and used disk space
Options
1. df: displays information about available and used disk space
2. df -a: displays information about all filesystems
3. df -h: display information in human readable form
4. df -k: display information in 1024 bytes (1 KB)
5. df -m: display information in mb
6. df -T: display information with file system type
fdisk
Wildcards
*
h*: return files which start from h
*h: return files which end with h
*h*: return files which contain the letter h
?
h??: return files which have h and 2 other chars (depends on number of ?)
Example: ls h????
return hello
[a-d]
It is known as range symbol, used to check from range of chars
1. [a-d]: return files range
2. [ad]: return for only the specified chars (in this case a & d)
Example: ls [a-d]*, rm file[235]
^
Not case or not symbol, return opposite of given condition
1. ^(condition)
Example: ls ^[a-d]*
>
output redirection symbol, used to redirect command output to a file
\
Used to write long commands in next line, used to ignore newline (enter)
Regular Expression
These are different from wildcards. See codequotient
More Symbols
1. * = all
2. | = run two commands in line and input in second command also known as Pipe
symbol
3. ~ = give home directory
4. ^ = start
5. $ = end
Package Management
Package management is a method of installing, removing, updating and keeping track of
software from different repos on a linux system.
YUM (Yellow Dog Updater, Modified)
YUM is the primary package management system for installing, removing & updating
packages on linux (RHEL). It can manage dependency resolution. Its config files are at
/etc/yum.conf and all repos are at /etc/yum.repos.d. Some of its basic commands are
1. yum install
2. yum remove
3. yum info
4. yum search
5. yum update
6. yum repolist
7. yum history or yum history undo <id>
RPM (RPM Package Manager)
It stands for RedHat Package Manager, used to install packages. It can’t manage
dependency resolution like YUM. An RPM package consists of metadata and an archive of
files.
RPM maintains a database of installed programs, which enables powerful and fast
queries. RPM stores this database at /var/lib and the file is name _db*
Options
1. i: installs a package
2. v: verbose for a nicer look
3. h: print hash (shows install progress)
4. U: upgrades a package
5. e: erasers/remove a package
6. V: verifies a package
Check dependencies
1. q: query a package/check is package is already installed
2. p: list capabilities this package provides
3. R: list capabilities this package depends on
4. nodeps: install package without any dependencies
Shell Scripting
Series of commands are called Script and these scripts are run by shells, hence shell
scripting. Files containing script have extension .sh. We need to make a file executable
before using it.
We write shell scripts for #!/bin/bash (specify in the header).
Variable
No need to declare or specify the type of a variable before using it. Use read to get input
and echo to print output.
Commands for Shells
1. echo $SHELL: see current shell
2. chsh --shell <new shell>: change current shell
3. cat /etc/shells: see all shells
References:-
expr command in Linux with examples