Linux System Admin Journey - Day 1 {Basic Stuff}

Table of contents
- Prerequisites
- What are OS, Kernel, and Shell, and How do they interact with each other?
- If Linux is a Kernel, then why it is termed as an OS nowadays?
- What is the GNU Project?
- Is Linux the same as Unix?
- Why does the Industry prefer Linux over other OS?
- Where is Linux used?
- What are the minimum directories required to boot the system?
- What’s Inside /proc?
- Types of Files in Linux:
- Everything about Path:
- Everything about Shell and Login Script files:
- Basic Commands in Linux:
- What is the "anaconda-ks.cfg" file for?
- #1 Shell Expansion in Linux (commands and arguments):
- #2 Shell Expansion in Linux (control operators):
- #3 Shell Expansion in Linux (shell variables):
- #4 Shell Expansion in Linux (shell embedding, and shell history):
- #5 Shell Expansion in Linux (file globbing):

Prerequisites
Checkout Day 0 BLOG → HERE
What are OS, Kernel, and Shell, and How do they interact with each other?
Linux is a modified Kernel. It was replaced in place of Unix that's why it's called "Unix-like Kernel", not completely Unix kernel.
Now, the question is how the interaction works. As shown in the above block diagram, the user is on the top hierarchy which runs the command on the shell.
Commands are the utilities performed on the shell and give the desired output. Let's say the user runs the command 'ls' (this will list all the directories (folders) and files on the shell, of your current directory). But, Internally, the shell sends the request to the Kernel, which reads the Storage driver of that particular path and then goes to the I/O driver via signals, → which then gives the output to the kernel, and finally displays it on the shell.
What is a Kernel? => Kernel is the heart of the OS. It is a low-level system program (or system program inside the OS) responsible for communicating with the system's hardware. Let's say we want to increase the volume (sound) of the system, so, we as a user, generally deals with the OS [i.e. dragging button to increase the volume (sound) with the mouse], but internally, the request goes to the kernel which sends it to the amplifier inside the system, and, as the result -> the volume is increased.
Kernel talks to the Hardware, with the help of drivers.
What is a Shell? => Shell is the command line interface (or a program) used to interact between the user and the kernel. We generally work on the terminal in Linux (using different shells), because we don't get the GUI environment. It is a black screen where a user fires commands to check files or other stuff. There are multiple types of shells (programs) like: [bash, ksh, zsh, sh, etc] but the most used is the bash shell. To list what shells you have in your system, run the command: [chsh -l
].
We can even change the type of the shell of the user with chsh <username>
command or usermod -s
command or by directly going inside the file (/etc/passwd
).
What is OS? => OS comprises both Shell and Kernel. It is a higher-level system program that can interact with both the user and the kernel. But remember, we don’t directly interact with the OS, we interact with the OS via the interface (i.e. Shell {command-line based} or GUI ).
If Linux is a Kernel, then why it is termed as an OS nowadays?
This is because, nowadays various distributions of Linux like [Ubuntu, Fedora, Debian, etc], package the Linux Kernel with various utilities, libraries, and applications to form a complete OS. When people refer to Linux, they often mean these distributions, that provide full user GUI experience.
What is the GNU Project?
GNU means "GNUs Not Unix", which means it is a project that worked for the development of the Linux kernel. This project's goal was not to replace the Unix kernel, instead, its mission was to develop a free and open-source "Unix-like OS". Therefore Linux is called a "Unix-like kernel", not a completely Unix-based kernel.
NOTE: GNU is also termed as "Linux", therefore in some places, the term is written as "GNU/Linux".
This term -> "GNU/Linux", typically refers to the combination of the GNU operating system components [developed by the Free Software Foundation (FSF)] and the Linux kernel [originally developed by Linus Torvalds]. The GNU Project provided many essential components of a Unix-like operating system, such as the GNU C Compiler (GCC), and core utilities like ls, mv, and bash shell.
Therefore, "GNU/Linux" is the collaborative effort of the GNU Project's software combined with the Linux kernel. It's often used to refer to a variety of distributions that package these components together, such as Ubuntu, Fedora, Debian, and many others.
Is Linux the same as Unix?
The answer to this question is both "YES" and "NO".
YES because:
It has the same look and feel as the Unix OS.
It offers to run any program that runs on Unix systems (through API conventions like POSIX, etc)
NO because:
- The heart of the system (Kernel) has a lot of new features that go beyond the classical design philosophy of Unix kernels.
Therefore Linux is a “Unix-like” OS, not Unix.
Why does the Industry prefer Linux over other OS?
FAST
FLEXIBLE
Linux Kernel is flexible, as we can modify the kernel depending on the Hardware. Every Smart Hardware we see has Linux inside it, even Android phones. We can make a Linux distribution as low as 25MB of storage with graphics 🙂. (SOUNDS SHOCKING), but this is possible by modifying the kernel.
SECURE
Linux is secure because the virus cannot come inside the system.
One of the questions is how different types of viruses penetrate the system.
This happens because the user executes the virus-enriched file in his/her system, and this file can come into his/her system either via any communication medium (Public Wifi, Pendrive) or via downloading any file/folder from any suspicious link.
But, still, Viruses can't affect Linux systems, This is because of the Authentication and Privileges feature in Linux. Suppose the user tries to execute the virus file. In that case, he/she might not be authenticated or privileged to execute the file, as execute permissions for a user are by default disabled in Linux.
However, the Linux Kernel can have bugs inside it, which the open-source developers debug and fix as soon as possible.
OPEN SOURCE
Linux has its own huge and vast open-source community, where developers from all over the globe contribute to the Linux kernel's source code, making the kernel more advanced, stable, fast, bug-free, etc.
The source code for the Linux kernel is open source, but enterprise Linux like RHEL, their code is not open source, because they provide advanced features on top of their flavor of Linux. Apart from that, we get dedicated community support in enterprise-based operating systems.
STABLE
Multi-User OS
Linux is a Multi-User OS, as we can open multiple virtual terminals (VT) with different users logging into them.
To open multiple VT, we need to use either the shortcut → [ctrl + alt + (Functions keys (from 1-6))], or we can directly use the command → [chvt 1
,chvt 2
,chvt 3
, etc.]
Where is Linux used?
It is used from a small smartwatch to the large Super Computers. Wherever we see a powerful machine, 99% chances are that machine has a Linux core inside it. Every Android phone, server, supercomputer, smartwatch/band, smart TV, Space robot, Gaming console, submarine, cloud computing, NASA, Space Station, US defense system, Aircrafts, and even the Large Hadron Collider at CERN (where God particle {Higgs Boson} was created) has used Linux inside it.
So basically, Linux is used everywhere.
What are the minimum directories required to boot the system?
As shown in the above diagram, the following directories exist in the root (/) directory. But, the minimum directories that are used to boot the system are:
[ etc, bin (binaries), sbin (system binaries), root, home, boot, lib/lib64, var, usr, tmp, proc, sys]
NOTE: Apart from the root and home directory, if any other directory's permission is changed or, any directory is removed, the Kernel and OS will not boot.
etc
contains "Configuration files".bin
contains "General user commands".sbin
contains "System Administrator related commands".root
is the "super user home directory".home
is the "general user's home directory".boot
contains "Kernel and its supportive files".lib
contains "shared libraries and modules".The files kept inside the shared libraries have the extension of ".so" (shared objects) and the files kept inside the modules directory have the extension of ".ko" (kernel objects). Kernel objects are called "Drivers" in Windows.
var
contains "log files, cache files, queue files, and system data".What are log files? => Log files are the "Activity Report" of any software boot.
What are cache files? => Cache files are "Repeated temporary files".
usr
contains "user application files and documents".dev
contains "device-related files" (like SSD, HDD, Pendrive).NOTE: Drivers (Sound-card driver, Storage driver, Memory driver, and others) are kept in the
lib
directory, which communicates with the hardware in thedev
directory.
Q. How do drivers and hardware communicate with each other? —> [This we’ll see in the Booting Process of Linux System].tmp
contains "temporary files".proc and sys
contain "Virtual File System". It is called Virtual FS because it does not have anything inside by default. When the system boots, then this directory gets the files. So basically it has running process-related data.Now, as proc has running process-related data, therefore most people confuse
/proc
directory as RAM. The/proc
directory in RHEL 9 (or any Linux system) is not literally the RAM, but rather a virtual filesystem (procfs) that provides information about processes and system resources. It dynamically represents system and process-related information stored in memory, but the directory itself does not consume RAM in the same way as application data does.
What’s Inside /proc
?
The /proc
directory contains:
Numerical Directories – Each running process has a directory named after its Process ID (PID).
System Information Files – These include:
/proc/cpuinfo
→ CPU details/proc/meminfo
→ Memory usage/proc/uptime
→ System uptime/proc/loadavg
→ System load average/proc/filesystems
→ Supported filesystems/proc/net/
→ Networking-related details/proc/mounts
→ Mounted filesystems/proc/cmdline
→ Kernel boot parameters
How Many Numbered Directories Are in
/proc
?
The number changes dynamically as processes start and terminate.
Types of Files in Linux:
There are 3 types of files in Linux, which are further categorized:-
General files
d → directory files
- → Normal ASCII files
l → Softlink files
Device files
c → character files
b → block files
System files
s → socket files
p → pipe files
Note:
Communication with the Hardware (keyboard, mouse, console (terminal), etc…) is done via character files. It makes serial communication (i.e. character files transmit the stream of data). Some of the examples are: /dev/null, /dev/zero, /dev/random, /dev/tty, etc.
All the devices like RAM, Harddisk, Pendrive, etc. come under block device files. Data can be randomly accessed from these devices. Some of the examples are: /dev/sda, /dev/sdb, etc.
System files are present in
/var/run/
directory.Socket files are the communication endpoint that applications use to exchange data. If an application wants to communicate with another application, it connects with the socket of that application.
Each socket has an associated IP address and Port number [ IP:PORT-NUM ] that allow it to accept connections from the clients.
Socket files use the sendmsg() and recvmsg() system calls to enable inter-process communication between local applications. We’ll understand system calls and processes in detail, in the Process management blog.c,b,s,p type of files are advanced files that we can use for Kernel or OS modification.
Everything about Path:
There are 2 types of paths in Linux:-
Absolute Path
It is a path that starts with slash (/).
Here, we don’t have to worry about the target file or directory, because the path starts with the slash.
According to the above example, If we are currently present in the /abc directory and want to go inside the rhce directory, then we can use cd (change directory) command and easily go inside the rhce directory →
cd linux/rhcsa/rhce
Relative Path
It is the path that does not start with slash (/). Here, we change the directory from the current path to the target path.
Here, we move around the directories starting from the current directory.
Now, according to the above example: the power directory is the child of the job directory, and accordingly, the other directories are the parent and child of each other. If we are currently present in the power directory, and we want to go back to script directory, then we can easily do this with cd command →cd ../..
current path is denoted with → “ . “
child name is denoted by it’s own name
and parent name is denoted by → “ .. “
Everything about Shell and Login Script files:
The program where a user interacts via commands with the OS is called the Shell.
There are 2 types of shell:
Interactive shell
This has multiple shells inside it, like:
/bin/sh
/bin/bash (This shell is mostly used in all the flavours of the Linux).
/bin/ksh
/bin/zsh
/bin/csh
/bin/fsh, etc…
Interactive shell can be accessed in 2 ways:
login
login is the time, when the user logs in for the first time into his/her shell.non-login
non-login is the time, when the user logs into another shell (sh, bash, zsh, etc.) from its currently present shell. Just type the command “bash”, or “zsh“ and we’ll land into another shell. To check the number of shells opened, or, to check the shell level, there is a variable →echo $SHLVL
.
Non-Interactive Shell
This has only one shell → (/sbin/nologin), and if it is given to the user, the user will not be able to access the shell.
This shell is mainly given to the Non-interactive System Users, like the services that run in the background and do not require any interactive shell.
Login Script files are also of 2 types:
System profile file / Global Login Script file
/etc/bashrc
/etc/bash_profile
Individual user profile file / Individual Login Script file
~/.bashrc
~/.bash_profile
When the user logs in to the shell, then all the 4 files are executed or loaded, and when the user non-login into the shell, then only 2 files are executed or loaded. The files are: /etc/bashrc
and ~/.bashrc
.
This is the reason, people mainly prefer to put the content (declaring any variable, alias, umask, etc) in the bashrc files to make the particular content permanent.
Basic Commands in Linux:
ls
To list the contents of the directories. By default, we use color codes to display the content.
- Blue → directory
- Black/White → Normal File (depend on the background)
- Cyan → Softlink
- Green → Executable files
Here, we get a lot of options that we use, to display or understand the content in different ways:
Ex:ls -l /etc
→ To long list the directory contents and to know each content’s permissions, size, inode number, ownership, modified time, etc.ls -ld /etc
→ To know the permissions, size, ownership, etc of the directory.ls -R /etc
→ To list the subdirectories/files in the /etc/ directory. I would recommend that, instead of this command, use thetree
command.ls -i /etc
→ To list the inode numbers of the contents present in the /etc/ directory.ls -a
→ To list all the files (including the hidden files) in the current working directory.ls -Z /etc/passwd
→ To list the SeLinux properties of the passed file.ls -lt /etc
→ To list all the files based on the latest modified time.ls -ltr /etc
→ To list all the files based on oldest to latest modified time (r means reverse order). Generally, we write this command asls -ltrh /etc/
. (h means to read the file size in human-readable form).
Note: As we know, it is not compulsory that we get full OS environment (i.e. integrated with colors) in our live production work terminal, so at that time, How we’ll recognize that the contents displayed via ls
command are files/directories/soft links/executable files? → because every file has a specified color to represent.
Therefore, to recognize the contents, we either type the command: ls -l
or ls -F
.
ls -F
will display some special characters at the end of the file names.
/ (slash) → represents directory file.
‘ ‘ (empty space) → represents normal ASCII files.
* → represents executable files.
@ → represents Softlink files.
cat / tac
The cat command displays the file's content on the screen, but its primary use or name tells us about the “concatenation of the files“. We can use the cat command in a lot of different scenarios.
To display the content of the file: [
cat /etc/passwd
]To display the content of the file with the numbers: [
cat -n /etc/passwd
]In the above command, it will also give the numbers to the blank lines, if we don’t want to display the o/p with the numbers to blank lines, then: [
cat -b /etc/passwd
]To display the content of the file, in the reverse order: [
tac /etc/passwd
]To concatenate 2 files and display the data of both files on the screen : [
cat file1.txt file2.txt
]If there are multiple of blank lines in the row, then, to remove those blank lines and merge it to one:
cat -s /etc/passwd
To overwrite the content present in the existing file, we use redirection symbols: [
cat > file.txt
]To add the content in the existing file: [
cat » file.txt
]NOTE: If the file does not exist, then, it will be created using redirection symbols, and whatever content (or input) we’ll give, it will be stored in the file.
Another method to take the input of the file is:
cat « EOF > customdata.txt
#####ENTER THE CONTENT#####
EOF
EOF is just a keyword, we can use any keyword to stop the file from taking the input.
While taking the input of the data in the file via redirection symbols, we use CTRL + D to stop the file from taking the input.
Now, what if I say that, the cat command can do neither of these stuff that I have written above?
Sounds weird!!! These are just the use cases of the cat command, but internally, the cat command does not read the content, add the content in the file, concatenate, etc… Then what is the internal working of the cat command? → that we'll see in the I/O redirection topic {Part -2 Blog}.
bat
This command is an advanced version of the
cat
command. It displays the content of the file beautifully.
If we givecat /etc/passwd
→ This just displays the contents of the file on the terminal screen, but the bat command → [bat /etc/passwd
] displays the contents of the file on the screen with the line numbers and theless command
in-built in its output.
To use this command we have to install it.paste
This command is used to concatenate the data of 2 files and is used to represent the data in tabular form.
Ex:paste file1.txt file2.txt
cmp
This command is designed to compare the binary files, but it can also compare plain text files.
This command displays the byte by byte of the text information (i.e. when we compare 2 files withcmp
command, it internally converts the text into bytes, and gives the o/p accordingly).diff
This command is designed to compare plain text files and tell the difference between the files.
There are few operators/characters that we need to understand indiff
command’s output.< → This means first file
\> → This means second file
c → change
a → add
d → delete
The point to note in this command is that if we give the first file followed by the second file with diff
command → [diff file1.txt file2.txt
], then obviously, it will compare the first file to the second file.
Some of the diff
command options that can be helpful:
To get the parallel view of both files:
diff -y ~/.bashrc /etc/bashrc
To get a unified view of the files (i.e. to see both the files together, displayed as concatenated files, with + and - in front of each line):
diff -u ~/.bashrc /etc/bashrc
+ means the data of the first file is present in the second file, and
- means the data of the first file is not present in the second file.To recursively search through the directories, and find, if the same files are present in both directories or not:
diff -r /boot /var
sort
This command is used to display sorted characters in the file. If there are any duplicate data in the file, this command will sort that as well and then display the sorted file on the terminal.
Ex:sort fruits.txt
There are many options in this command, which are really helpful:To remove the duplicate data and display it on the terminal
sort -u fruits.txt
To put the output of the sorted file into another file
sort fruits.txt > backup.txt
sort fruits.txt -o back.txt
To display the sorted file in the reverse order
sort -r fruits.txt
wc
This command displays line number, words, and number of characters inside the file.
To display everything:
wc file.txt
To display only line number:
wc -l file.txt
To display only words:
wc -w file.txt
To display only characters:
wc -c file.txt
This command is generally used with the pipe (|), to fetch the line number or words in the file.
ex: cat file.txt | wc -l
comm
This command is also used to compare the 2 files, but its output is kind of different from the
diff
command.
The only condition while using this command is that, both the files should be sorted. If the files are not sorted the output will be incorrect or unpredictable.
Thecomm
command assumes that the input files are already sorted, and it compares them line by line in order.Ex:
comm file1 file2
→ This command gives the output in 3 columns.The first column displays the unique lines of the first file.
The second column displays the unique lines of the second file.
The third column displays the common lines of both files.
echo
echo command prints the value of any given message or variable on the terminal's screen.
Ex:
echo “Linux is best“
orecho $HOSTNAME
,echo $HISTFILE
,echo $SHLVL
.touch
touch
command is used to create the empty files. But, its primary use case is not to create the empty file, it's use case is to modify the timestamps of the files. Hackers use them to manipulate the timestamps of the file. This can be used for security purposes as well.touch -t YY-MM-DD-hh-mm[.ss] (ss -> seconds are optional)
touch -t 2310131413
[touch file.txt
] -> The access time will be updated. If we want to update the date and time of the file manually, then we have to use the flag -t
with the touch
command and write the date and time in the reverse order. Ex: touch -t 202105210315.30 file.txt
Let's break it down:
20 -> century (CC)
21 -> year (YY)
05 -> month (MM)
21 -> date (DD)
03 -> hours (HH)
15 -> minutes (MM)
.30 -> seconds (.ss)
If you omit the century part (CC), the current century is assumed. If you omit the seconds (.ss), they are assumed to be 00.
stat
To check the time stamps of the file (i.e. birth time of the file, access time, change time, modified time, and other stuff (like: blocks, file size, inodes, permissions, etc…)
Ex:
stat file.txt
mkdir
This command is used to make directory files (i.e. folders). Ex:
mkdir test
Here, test directory will be created in the present working directory.If we want to create the directory inside the directory, then we can use
-p
flag.
Ex:mkdir -p test/demo/dump
.If we want to create the directory with our dedicated permissions →
mkdir -m 700 /fuse
cp
This command is used to copy the files from the source path to the destination path. If the destination path already contains some data, then this command will prompt to override the content of the file.
Ex:
cp file.txt customfile.txt
# To copy the file contents.cp -r /etc /tmp
# To copy the /etc/ directory to the /tmp directory.NOTE: If the <destination file> already has the content, then, the
cp
command is set with an aliascp -i
that internally prompts (asks) to override the content.Whenever we take the backup of any directory or file with
cp
command, it usually changes its selinux attributes and the ownership. If we want hardcoded backup of any directory/file then, we can use the-p
option. Ex:cp -rp /var/spool/mail/ /hard-backup
If we want to copy the parent directory (with specific content) or the parent directory (with all contents) to the <destination directory>, then we can use
—parent
option.Ex:
cp —parent /etc/passwd /storage/
orcp -r —parent /etc/ /storage/
mv
This command is used to shift the files from one location to another. Basically doing (Cut and Paste). If we are moving the files from the<source file>
to the<destination file>
, and the destination file already has content in it then,mv command
will also prompt to override the content of the file. This means, internally,mv command
is also set with an alias ofmv -i
.- Ex:
mv file.txt /root/dump
- Ex:
This command also acts as renaming the file, but the actual command, → to rename the file name or directory name is the rename
command.
- Ex:
mv topup.txt backup.txt
rename
This is the main command used for renaming the file or directory. But, in the real world, no one uses it. Its syntax is :rename <source file> [target file] <source file>
.
Ex:rename /data /secure-data /data
rm
/rm -r
/rm -rf
By default
rm
andrm -r
command is set with an alias ofrm -i
, which is a prompt, that asks to delete the file (only if the file is not empty). It is a good practice to use the prompt else there is no way to recover the file once it's deleted, and never userm -rf
command until you are not sure about deleting any content.head
By default, this command shows the top 10 lines of the file.
If we want to see more/less number of lines, then, we can specify the number of lines we want.
[ Ex:head -15 /etc/passwd
]tail
By default, this command shows the bottom 10 lines of the file.
If we want to see more/less number of lines, then, we can specify the number of lines we want.
[ Ex:tail -15 /etc/passwd
]This command is very useful because it also helps in the monitoring of the logs or other things.
[ Ex:tail -f /var/log/messages
]more
This command displays the content of the file “pagewise“ and also shows the % of the file open (at the last), which tells about the % of pages left of the opened file.
# To go to the next line, if the page is opened → Press “Enter“.
# To go to the next page, if the page is opened → Press “Spacebar“.
# To go to the back page → Press “b“.
# To quit and come out → Press “q“.
### If we don’t remember these options, then type:more -d /etc/passwd
To define particular number of lines (ex: 10) in a single page:
more -10 /etc/passwd
If we want our page should open from the 10th line of the file:
more +10 /etc/passwd
If there are multiple blank lines in the file and we want to merge them into one, then we can type:
more -s /etc/passwd
. This command is similar tocat -s /etc/passwd
less
This command displays the content of the files with scrolling pages. This command is majorly used with one of the redirection symbols called PIPE → “ | ”.
cat /etc/passwd | less
# To go to the next line → Press “Enter“.
# To go single line up and down → Press “Up Down Arrow keys“.
# To go at the end of the page or at the top of the page → Press “Page Down or Page Up“.
# To quit and come out → Press “q“.
# To search any word in the file → Enter →/(searching-keyword)
strings
The binary files present in the
bin or sbin
directory cannot be opened by the cat command. If we try to open the binary file with the cat command it will show the encoded text. So, therefore, we usestrings
command to open these types of binary files or programs.Ex:
strings ostree
cd
This command is used to change the directory by giving specific paths of the location. The path can be either Absolute or Relative.
To go back to the parent directory, we use the command:
cd ..
,cd ../../..
Let’s say, you are in the directory (
/var/log/
) and now, you went to (/root
) with the help of cd command, Now, you want to go back again to/var/log/
directory, so usecd -
to directly jump to the previous directory.
man
(manual)We can easily get help, for the Linux commands, with either the "
man
" command or, by using "--help
" option, at the last of the command.man command
gives us the complete manual (book) of a particular command, with all the flags and other things.Ex:
man lsblk
.
When we check the manual of any command, it internally runs →man 1 lslbk
. ‘1’ is the signal to open the manual of the command. If we want to open the manual of any file (ex: /etc/passwd or /etc/shadow), then, the signal used is ‘5’. Ex:man 5 passwd
man 5 shadow
.
Now as we know, “passwd“ is also a command and a file, so, accordingly with the help of signals we can open the manual of either a command or a file.
############# 👇🏻👇🏻 #############If we want a short and single-line description from the manual, we can use the "
whatis
" command.Ex:
whatis lsblk
If for some reason the "
whatis
" command does not work, then, we can update the manual page database with the command "mandb
", becausewhatis command
uses the mandb database at the backend, to fetch the single line description of the command.
The database is kept in/var/cache/man/index.db
.apropos
To find the command for the particular usage (like: compression, copy, cut, adding user, etc..), then we can use
apropos
command.Ex:
apropos compress
,apropos copy
which
If we want to quickly check, whether, the command is installed in our system or not, then we can use
which
command. This command tells the path of the command (where the command is stored).Ex:
which lsblk
,which httpd
pwd
To check the present working directory (i.e. currently in which directory we are there).
tar
This command is used for achieving and compressing of the larger files/directories. The syntax of the command is:
tar -cf <archieve-file.tar> <directory/file>
c → create and f → filesTo create the archive of the “directory (/etc) contents” in the current location:
tar -cf etc.tar /etc
(We can use absolute path or relative path, to change the location of the archiving file →
tar -cf /tmp/etc.tar /etc
To view the contents of the directory while achieving, we can use “v“ (verbose) option with the command. →
tar -cvf /tmp/etc.tar /etc
To list the contents of a “.tar file”, we use the option “t“. →
tar -tf etc.tar
,tar -tvf etc.tar
To create the archive of the “directory (/etc)”:
tar -cf /tmp/etc.tar -C /etc .
etc.tar will be created in /tmp folder.
/etc is the directory to be achieved.
. (The dot) specifies, tar to include all the sub-directories or sub-files present in the /etc/ directory.
To extract the .tar file in the current location →
tar -xf etc.tar
To extract the .tar file in another location →
tar xf etc.tar -C /tmp
Till now, it was archiving of the file (.tar), but now we’ll see “compression of the file” (.tar.gz or .tar.bz2). In archiving of the file, the "new object is created", but In Compression of the file the "object is changed".
For compression, we either use “gzip“ or “bzip2“. There are many compression tools, but majorly we use is these two.
After doing compression with gzip, to uncompress/decompress the file, we use “gunzip“, and similarly, After doing compression with bzip2, to uncompress/decompress the file, we use “bunzip2“.
Why do we use compressions? → To compress (or zip) the large-size unused files/directories, so that, they can be stored in the hard disk by taking minimum space, therefore optimizing the storage.
Nowadays, we can also use zip/unzip package, to compress/decompress large-size files.
To compress the archived file with gzip:
gzip etc.tar
,
and to uncompress the file, we usegunzip etc.tar.gz
.
After the compression with the gzip, the archived file gets the extension with.gz
, hence making it a compressed file.To compress the archived file with bzip2:
bzip2 etc.tar
,"
and to uncompress the file, we usebunzip2 etc.tar.bz2
Note: bzip2 compresses the file more than gzip, so the question is, If we have a higher utility of compression (i.e. bzip2), then, why do we use gzip? → Because the bzip2 utility uses more CPU utilization, and in the industry, we require less CPU utilization on servers.
If we want to directly compress the file with the tar command, without using any gzip or bzip2 utility, for this, we can use the option “a“. (a means autodetect the utility (gzip or bzip2)).
Its syntax will betar -caf <compressed-file.tar.gz> <directory/file to be compressed>
Ex:tar -caf etc.tar.gz /etc
,tar -caf /tmp/etc.tar.gz /etc
To uncompress the file with the tar command:
tar -xaf etc.tar.gz
,tar -xaf etc.tar.gz -C /tmp
.
If we don’t want this tar mess, then we can directly install the package “zip”, and compress the large-size file/directory. Ex: zip etc.zip /etc/
.
To uncompress → unzip etc.zip
grep
grep is the most underrated and powerful command. Its definition is that, its used to display the line containing the “searched string or the word”. Basically, it is used to search inside the files.
## Now, there is a difference between “string and word“……👇🏻
“string” is the collection of characters and can be part of the word, but the “word” is the collection of defined characters.Ex: we want to search the word called “root” from the file “/etc/passwd“. Now, if the file also contains the words like: “rooter“, “preroot”, “rootboss”, and we searched the word root like this:
grep root /etc/passwd
orgrep “root“ /etc/passwd
orgrep ‘root‘ /etc/passwd
, its basically “searching the string”, and we’ll get all the words highlighted, which has root string in it.
If we want to search the word, “root“, then, we’ll give the option-w
, which is:grep -w root /etc/passwd
.Q. Now, what makes grep such a powerful command?
\=> The way of searching the words by using its options.To ignore the case sensitivity of the searching word →
grep -i root /etc/passwd
To show the line number in the Output →
grep -n root /etc/passwd
To display the number (count) of searched strings in the particular file, without displaying the string on the terminal →
grep -c root /etc/passwd
If we don’t want to display the output of grep on the terminal →
grep -q root /etc/passwd
orgrep root /etc/passwd » /dev/null
(Majorly used in scripting (in conditions)).To see those lines which contain the word root at the start of their line →
grep ^root /etc/passwd
To see those lines which contain the word root at the end of their line →
grep root$ /etc/passwd
To search a string (ex: root) through the directories recursively directly from the grep command →
grep -r root /etc
orgrep -R root /etc
To search a word (root), with its line number, recursively from the directories →
grep -rwn root /etc
To search multiple strings or patterns in different lines of the file:
egrep ‘root|bash’ /etc/passwd
grep -E ‘root|bash’ /etc/passwd
To search a particular string or pattern in multiple files:
fgrep root /etc/passwd /etc/group /etc/shadow
grep -F root /etc/passwd /etc/group /etc/shadow
grep root /etc/passwd /etc/group /etc/shadow
→ Nowadays, this feature is built into grep command, so we don’t need to usefgrep
command or ‘-F
‘ option with grep command.Even
egrep
command or grep with-E
option can do this stuff easily. So, therefore, nowadays, we don’t usefgrep
or-F
option for this stuff. 👇🏻👇🏻
Then, for what things
fgrep
command is used? 👇🏻👇🏻fgrep
command is used to search a particular string or pattern that is assigned with a wildcard (. or * or ?)
Ex: There is a file → (shop.txt) which contains the strings like: grover.shop, groverjuice shop, grover?shop, groverrrshop, grover**shop.
Now, if we want to fetch the exact word “grover.shop“, then, with the normal grep command it is not possible, therefore we usefgrep or -F option with grep command
.
To search a particular string or pattern in a zipped (compressed) file (.gz or .bz2 or .zip):
zgrep Sara users.csv.gz
→ By default, the zipped (compressed) file cannot be read, and if it cannot be read, then neither any contents can be grepped from the file, therefore, we can usezgrep
command.
find
This is also the most underrated and powerful command, just like
grep
. This command is used to search inside the directories.
It has multiple search parameters that are useful in real life, to search through the directories.
Some of the search parameters are:-name
- To search all the files/directories of name → “passwd“ from the whole system(/) or from a particular directory (ex: /etc):
find / -name passwd
orfind /etc -name passwd
- To search all the files/directories of name → “passwd“ from the whole system(/) or from a particular directory (ex: /etc):
-iname
If we want to search the files or directories without case sensitivity:
find / -iname passwd
find / -iname “*.conf“
-type
- If we want to search only files or only directories inside a particular directory:
find /etc -type f -name passwd
orfind /etc -type d -name passwd
find / -name passwd ! -type d
=> ! (exclamation) means NOT.
The above command means only files (of name passwd) will be displayed.
- If we want to search only files or only directories inside a particular directory:
-user
- If we want to search any file or directory, created by a particular user:
find /etc -type f -user sorav
orfind / -type d -user dhruv
- If we want to search any file or directory, created by a particular user:
-group
- If we want to search any file or directory created by the users of the particular common group:
find / -group production
- If we want to search any file or directory created by the users of the particular common group:
-uid
- If we want to search any file or directory from the user’s uid:
find / -uid 1001
- If we want to search any file or directory from the user’s uid:
-gid
- If we want to search any file or directory from the user’s gid:
find / -gid 2001
- If we want to search any file or directory from the user’s gid:
-empty
- To search the empty files or directories in the current location:
find . -type d -empty
orfind . -type f -empty
- To search the empty files or directories in the current location:
-size
- To search only files below 100kb:
find / -type f ! -size +100k
- To search only files below 100kb:
-perm
To search the files based on the permissions:
find / -perm 777
{By default, this command shows only files, if we want to see the directories, we can use-type d
}To search the files with 777 permission in long listing:
find / -perm 777 -ls
To see the files which does not have 777 permission:
find / ! -perm 777
To see all the files which have execute permissions:
find / -perm /a=x -ls
orfind / -perm /u=x
To see the files with the SUID bit applied:
find / -perm /u=s
Similarly, we can use other options like: /u=r, /g=x, /g=s, /o=rw, /a=rx, etc
-inum
: This option is used to search the files/directories based on the inode numbers.-o
: This option acts as an OR gate or logical OR operator. The logic of-o
is such that both conditions will be evaluated for every file. It doesn't stop evaluating the second condition once the first one matches.Ex:
find / -name passwd -o -size +30M
If the find command finds the files with the name “passwd” in the / directory, then it will also run the second condition, which is finding the files which has the size greater than 30 MB.
Therefore, if both the conditions are true, then the command will run both the conditions.
-exec
To execute any commands after searching the files or the directories:
find /etc -name fstab -exec cp {} /home \;
find /etc -type d -exec cp -r {} /tmp \;
find /etc -name passwd -exec cp —parent {} /root/testing \;
find /home -name fstab -exec du -sh {} \;
find /home -name fstab -exec rm -rf {} \;
find / -perm /g=s -exec chmod g-s {} \;
-ctime
or+ctime
:
These search parameters find the files based on their status change time (i.e., when metadata such as permissions, ownership, or other attributes were last modified). The unit is days.To find the files whose status was last changed exactly 10 days ago:
find / -ctime 10
To find the files whose status was last changed more than 10 days ago:
find / +ctime 10
To find the files whose status was last changed less than 10 days ago:
find / -ctime -10
-mtime
or+mtime
:
These search parameters are used to find the files based on their modification time (when the content of the file was last changed). The unit is days.To check those files, whose last modified time is exactly 50 days:
find / -mtime 50
To check those files, whose last modified time is more than 50 days:
find / +mtime 50
To check those files, whose last modified time is less than 50 days:
find / -mtime -50
To check the files which were modified between 50 to 100 days:
find / +mtime 50 -mtime 100
-atime
or+atime
:
These search parameters find the files based on their access time (i.e., when the file was last read or accessed). The unit is days.To check those files, which have been last accessed exactly within 5 days:
find / -atime 5
To check those files, which have been last accessed more than 5 days:
find / +atime 5
To check those files, which have been last accessed less than 5 days:
find / -atime -5
-cmin
or +cmin
:
The-cmin
and+cmin
options are related to the status change time of the file.
This includes changes like → “file metadata modifications” (such as changes in permissions, ownership, or even renaming the file), but not the contents of the file itself.To find the files whose status was last changed exactly 60 min or 1 hr ago:
find / -cmin 60
To find the files whose status was last changed more than 60 min or 1 hr:
find / +cmin 60
To find the files whose status was last changed less than 60 min or 1 hr:
find / -cmin -60
-mmin
or+mmin
:
The-mmin
and+mmin
options are related to the modification time of the file, which refers to → when the contents of the file were last changed. This includes operations like “editing the file” or “overwriting its contents”.To find the files modified exactly 5 minutes ago:
find / -mmin 5
To find the files modified more than 5 minutes ago:
find / +mmin 5
To find the files modified less than 5 minutes ago:
find / -mmin -5
-amin
or+amin
:
The-amin
and+amin
options in the find command are used to filter files based on the last access time (i.e. the time when the file was last read or accessed (opened)), specifically in terms of minutes.To find the files that were accessed exactly 5 minutes ago:
find / -amin 5
To find the files that were accessed more than 5 minutes ago:
find / +amin 5
To find the files that were accessed less than 5 minutes ago:
find / -amin -5
NOTE: If you need more granularity for modification, access, or status change times, you might need to use the command, such as stat
, which can provide detailed timestamps of the file: stat file.txt
.
This command will give us a specific timestamp of the file, but they won't support the same search filtering capabilities as find
for finding multiple files based on time.
locate
The
locate
command is straightforward. It’s not likefind
command. With this command, we can directly locate the files and the directories, without specifying any path, and it looks that, this command will search from the whole system.
Now, here’s a catch, the locate command doesn’t directly search from the whole system, It searches from its own database (called mlocate db), which means, if the database is not updated, it will not search those files/directories.To install the
locate
command, in the RHEL systems, the package is: “mlocate”, and in the Ubuntu system, the package is: “plocate”, and accordingly their databases are termed “mlocate db“ and “plocate db”.Once the locate command is installed, search for any file/directory. Ex:
locate passwd
. Initially, we’ll get an error message → [locate: cannot stat() ‘/var/lib/mlocate/mlocate.db].
If this error comes, then, as a root user, we have to run the command → [updatdb
], because as I have said, locate command searches data from its own database, so to update the database with all the files and directories of the filesystem, we run this command.Every time we make any change in the file system (create/delete files/directories), we have to run the command →
updatedb
.locate
command is faster thanfind
command and consumes less CPU utilization, because it searches from its own database, but we can create the database searched from the find command as well, to reduce the CPU utilization:find / > find-db
. Now, grep any stuff from find-db file, and enjoy!!! 😎
alias
/unalias
alias command is used to set the short name of the command. It is used as a shortcut for any command. Ex:
alias ld='ls -ld'
. Now, when we typeld
, we'll get the result of thels -ld
command. The aliases that we set explicitly will only be present in the current shell. Once the terminal is closed and launched again, the aliases will be removed.To make the aliases permanent for a specific user,** we have to write the aliases in its login script file (~/.bashrc).
If we want to make the aliases permanent for all the users, then we’ll write the aliases in the (/etc/bashrc) file.To list down all the aliases on the terminal, the command is
alias
.To remove the alias, the command is
unalias
.
Ex:unalias ls
tee
'
tee
' command can store the intermediate information while redirecting the output of the commands via | (pipe).
Basically, it takes the Standard input and sends it to the Standard output and the files.
Ex:date | tee f.txt
(Display the output on the screen and store it in the file at the same time. This is not possible with redirection operators as they don’t show the output on the screen while storing the content in the file).NOTE: This command only works with | (pipe symbol), and is an alternative to the redirection operators like:
>
(overwrite) and»
(append).tee
command is an alternative to>
andtee -a
command is an alternative to»
.uptime
To check the uptime of the system (i.e. how long the system has been running, since the last reboot).
This command also provides additional details like:
- Number of users who are currently logged in.
- System load average.date
This command is used to check the date and time of the server.
This command has some options, that are mostly used in scripting or while taking backups:date +%F
→ To display the current date.date +%T
→ To display the current time.date +%r
→ To display the 12 hr clock.date +%R
→ To display the 24 hr clock.
cal
This command is used to check the calendar of the current year with the current month. If we want to see the calendar with all the months, then type → [
cal 2025
].
If we want to see the calendar with a specific month, then type → [cal aug 2020
].hostnamectl
This command displays a lot of stuff like: Static hostname, Icon name, Chassis, Machine ID, Boot ID, Virtualization, Operating System, CPE OS Name, Kernel, Architecture, Vendor, Hardware Model, and Firmware Version.
This command we generally use to set the hostname (name of the host/system) permanently. The command is:hostnamectl set-hostname mymailserver.org
timedatectl
This command displays a lot of stuff like: Local time, Universal time, RTC time, Time zone, System clock synchronized, NTP service, RTC in local TZ.
This command we generally use to change the date and time manually and change the timezone.
We can set the date and time manually, or we can sync it with the NTP (Network time protocol) server. Usually, the date and time of the server is synced with the NTP server.
We can do a lot of stuff with this command:To list all the timezones (IST, EDT, UTC,…):
timedatectl list-timezones
To apply the particular timezone:
timedatectl set-timezone Asia/Kolkata
To set/change the date and time manually:
timedatectl set-time “2025-10-12 13:30:20“
To make ntp service on/off:
timedatectl set-ntp true
ortimedatectl set-ntp false
file
If you want to know the type of the files, then we use this command. In Linux, we can't rely on extensions like (
.txt, .sql, .json, .py
, etc) to know the type of the file, because extensions have no meaning in Linux. So, to know the file type, either we use the ls -l command to see the files in the expanded format, or we use thefile
command.Ex:
file anaconda-ks.cfg
|file /etc/passwd
|file /etc/yum.conf
type
Now, there are multiple commands in Linux, that are executed inside the shell, and there are multiple commands that are executed with the help of their binaries. Any command defined inside the shell is called a “shell built-in command”, and any command that is being executed by their binaries is called an “external command”.
External commands are majorly seen in the bin or sbin directory, and shell built-in commands remain inside the shell.
Technically, each command is a file, so every command will have its binary file in the bin directory, but the built-in command means (they come with the shell, therefore, no need to install them).To check that the commands are shell built-in or external, we use
type
command.Ex:
type cd
,type ls
,type lsblk
,type fdisk
NOTE: LINUX is a case-sensitive OS, If we create 2 files with the same name but, one with Upper case and one with Lower case then both the files will be treated differently by the system, as their Inode number will be different. We’ll see the concept of inode numbers in a different blog.
Also, everything inside the Linux OS is a file, whether it is a command, directory, or any other file.
If we want to check the type of the file, then we can't judge it via its extension (Ex: .txt, .jpg, .png, etc...
) because the extension doesn't matter in Linux. So, to check the type of the file we can either use ls -l
command (i.e. o/p given in extended format) or ls -F command
or via file
command.
[Ex: file linux.txt
, file /dev/sda
, file /etc/updatedb.conf
]
What is the "anaconda-ks.cfg" file for?
This file comes along with the installation of RHEL OS. The file is kept inside the home directory of the root user. This is a special kickstart installation file, which can create 1000s of similar types of machines. During the installation time, whatever the person does manually like: (creating the user, setting the password, setting the time zone, creating partitions in the root, setting the size of the partitions, etc), all the stuff is stored in this file, and if we want to create the same machine, then this file does that automatically.
So, this file acts as a machine's template, to launch the other multiple machines.
This file was heavily used to launch the OS, of the same configuration, on the Bare Metal Machines before the era of Virtualization, and back in that time there were HDDs, so it used to take many hours to launch the machine. Now with the help of virtualization, we can just have the template of the machine, and create multiple VMs within a few minutes.
#1 Shell Expansion in Linux (commands and arguments):
NOTE: Whenever we type any command on the shell, the shell doesn't give that command directly to the Kernel. Firstly the command is manipulated (i.e. understood and broken into different arguments) by the shell itself and then, it is sent to the Kernel.
The process of shell expansion is:
Scanning of the command given by the user.
Understanding of the command.
Manipulating of the command.
Processing of the command.
Once the command is processed by the shell, it passes the command to the Kernel, and then we get the result on the terminal. So, the output of the command that we see on the terminal is not directly shown. The shell modifies the command and then the result is displayed.
#Examples:
echo Welcome to the world of Linux
# the echo command will print the above line as it is on the terminal.
# If we do this -> [ echo Welcome to the world of Linux ]
# Here also it will give the same o/p as above because shell is using shell expansion.
# It's scanning the command and then manipulating it and then giving the o/p.
# If we don't want the shell expansion to happen, then we can use quotes:
echo 'Welcome to the world of Linux'
# Here the o/p will be with the whitespace.
# Now, echo command is also used for printing the value of the variables...👇🏻
In Linux, we have two types of variables: “system variables” and “user-defined variables”.
Ex: SHELL, HOSTNAME, etc (There are many types of system variables like these, which are called "Environment variables"). We'll see more of these variables further.
To print the variable, we can use the command echo
.
Ex: echo $SHELL
Q. Now, what is the difference between ' ' (single quotes) and " " (double quotes)?
\=> The main difference between both the quotes are that single quote prints the variable itself, but the double quote prints the value of the variable.
Ex: echo '$SHELL'
-> $SHELLecho "$SHELL"
-> /bin/bash
Now, there are other options as well, which we can use with the echo
command like, new line variable (\n), horizontal tab key variable(\t), vertical tab key variable (\v) etc.
To use these options we have to enable them by adding -e
option with the echo
command.
These \n, \t, \v are called "special escaping characters".
Ex: echo -e "I live in \nChandigarh city."
, echo -e "I love to eat \tcitrus fruits"
NOTE: If we want to display both, “the command, and its o/p”, while firing the command on the shell, then we type: set -x
.
Ex: If we run date
command on the shell, it returns only the o/p of that command, but if we type set -x
and then run the date
command on the shell, it will return both, "the command" and "its o/p".
To unset this, type set +x
.
#2 Shell Expansion in Linux (control operators):
Control Operators are those operators, that are used to execute multiple commands on the terminal. There are many control operators in Linux:
; (semicolon)
To execute multiple commands serially in the row (i.e. one after another), then we use the semicolon operator to separate the commands.
Ex:
date ; cal ; pwd
,yum install tree -y ; cal
,echo 'Hello' ; echo 'World'
& (ampersand)
To execute multiple commands in parallel, we use the ampersand operator. The command that will have an ampersand after it, will run in the background, and the command after the ampersand, will run parallel on the terminal.
Ex:
sleep 30 & date
Here, the
sleep
command will execute in the background, and thedate
command will execute on the terminal.? (question mark)
This is a very important control operator (or, I would say a variable), because it tells whether the previous command was successfully executed or not. To know if the previous command is successfully executed, we just have to type
echo $?
.
Now, if the result comes '0' that means the previous executed command is perfect, but if any random number comes up, that means the previous executed command is wrong or has some error. It is heavily used in Shell Scripting.
So, if the command is perfectly executed, '?' will store a '0' code in it, but if the command is wrong, then, '?' will store a particular code, based on the error.Now, what are these codes coming up? These codes are called "exit codes or return codes or exit status" in bash.
The successful exit code is 0, and the error exit codes range between 1-255.
&& (logical AND)
This operator works when both of the conditions are true.
Ex:
echo 'Hello' && echo 'World'
✅zecho 'Hello' && echo 'World'
❌|| (logical OR)
This operator works when either of any conditions is true (i.e. if the first condition is true, then it will not go to the second condition and print the o/p of the first condition).
Ex:
-echo 'first' || echo 'second'
-> o/p: first
-zecho 'first' || echo 'second'
-> o/p: second.Now, we can combine the above 2 operators (&& and ||) to form the "if then else" structure.
Let's say there is a file called "cars.txt". Now, we'll perform if-then-else structure on the file and print the statement with this.
rm cars.txt && echo "It worked!" || rm cars.txt && echo "It failed!"
Now, if the cars.txt file is there, then according to the above command, the file will be deleted and the message will be printed on the screen “It worked!“. But, if the file is not present, then, the second command will work and the message will be printed on the screen “It failed!“
# (pound sign)
This is used as comments in the shell. If we write any command after the pound sign, the shell will ignore that command. But, it is useful when we want to bookmark something in our shell's history. It is generally useful for longer commands.
Ex:
# pwd
,echo # hello world
(escaping special character)
Whenever we use '\ (backslash)' between the commands,
Ex:
echo hello \; world
,echo hello\ \ \world
In the above example, the semicolon operator will execute the 2nd command which will give an error, but if we provide a backslash there, it will ignore the semicolon.
The other use case of this backslash is to divide the printing statement or a long command. 👇🏻👇🏻
echo This is a demo usecase \ for the above statement \ to execute in multiple lines
#3 Shell Expansion in Linux (shell variables):
Variables are those that do not have any fixed value.
There are 2 types of variables in Linux:-
System-defined variables:
These are those types of variables that are defined by the developer while developing the shell.
System-defined variables, also known as environment variables, and are always showcased in uppercase letters like: (
HOSTNAME, PWD, PATH, SHELL, HOME, HISTSIZE, HISTFILE, HISTFILESIZE, LANG, LOGNAME, TERM, etc
)Ex:
echo HOSTNAME
,echo SHELL
,echo HOME
,echo $LANG
We use these variables in shell scripting, to print with other statements, like:
echo This is the $SHELL shell on the computer $HOSTNAME
.User-defined variables:
These are those types of variables that are defined by the users, according to their need.
These variables are always showcased in lowercase letters.
Ex:
myvar=555
,user=admin
=> Now, to check the value of the variable, we give the command:
echo $myvar
,echo $user
Now, the variables that we defined are not permanent.
To make these variables permanent for a specific user, we can enter the variables into the specific user’s login script file → (~/.bashrc).
To make these variables permanent for all the users, we can enter the variables into (/etc/bashrc).
After adding the variable into the file, we have to reload the file by either “restarting the system” or, we can use the
source
command. →source /etc/bashrc
If we want the variable to be exported in all the shells, then we can use the command
export
.
Ex:export user
.Now, the value of the $user variable will be seen in all the shells (bash, sh, zsh, etc).
To uncheck the variable, if it is not stored in the login script file, we use the command
unset
.
Ex:unset myvar
The variables which are not defined are called "unbound variables". By default, when we echo any unbound variable (ex: echo gogins
), it shows the blank space.
If we want the system to prompt us: "variable unbound", (on echoing unbound variable) then we can enable this feature by using the command, set -u
.
To unset the feature, type set +u
.
NOTE: The variable can't start with the *number*.
* To check all the variables (bash + environment) of the system, we use the command set
.
* To check only the environment variables of the system: env
or printenv
.
$PATH variable is really important. Let’s understand this variable with the help of example: 👇🏻👇🏻
Q. If we run any command (Ex: ls
, cat
, cd
, etc), How does the system know, where to fetch the command from?
\=> For this, the system checks the binaries of that command in the (/bin directory).
Q. Now, how does the system know that the commands we use are in the /bin directory?
\=> because of the variable → $PATH
, which fetches the binary file's path.
NOTE: If we empty the PATH variable, then neither of the commands will work.
To add the Paths: [PATH = $PATH=/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin]
Now, there is a variable called "PS1" which determines the shell prompt. If we compare the bash shell with the zsh shell, the prompt will be different.
- To change the shell prompt, type this on the terminal ->
PS1=" press here: "
.
Now, if we checkecho $PS1
, it will show “press here:” as the prompt. To reset it, just re-login back to the terminal.
echo $PS1
=> [\u@\h \W]\$
\u -> username
\h -> hostname
\W -> Working dir
$ -> Privilege mode
There is another variable to check the shell level (i.e. how many non-login shells we have opened) called $SHLVL
. Ex: echo $SHLVL
.
To enter into non-login shells, just enter the name of the shell in which you want to go, Ex: bash
, zsh
Now, there are variables known as "Delineate variables in Linux". Let us understand with an example.
To cure these types of issues, where the system considers the variable as one variable ($prefixgirl and $prefixboy), we use { } curly braces. These are called delineate variables.
NOTE: Always remember that each user has its unique environment variables.
#4 Shell Expansion in Linux (shell embedding, and shell history):
Q. What is shell embedding?
\=> Shell embedding is the process of embedding a particular command or a variable inside the temporary sub-shell. Let’s understand with an example:
We can even embed a shell in an embedded shell, and, this is called “Nested embedding“.
So, we can embed the variables inside the command in a temporary sub-shell, and always remember that these embedded variables will not be permanent.
$A is a user-defined variable, in the main shell, that’s why its value is printed, but $B and $C are the unbound variables in the main shell, they were defined in the temporary sub-shell.
For shell embedding we either use → { $() or the backticks `` }.
So, the above example can also be done something like this → echo `var1=5 ; echo $var1`
.
Shell embedding is mainly used in “Scripting“. We embed the commands in the variable and then it is echoed when the script runs.
SHELL HISTORY:
The history
command displays all the commands executed in the past.
When we work in the shell environment, whatever command that we fire on the terminal, in the current shell session is present in the history cache. Once the session is logged out, the history, cache commands are cleared and are saved to the particular user’s history file → (~/.bash_history).
Q. Why are the history, cache commands cleared, when we logout from the current shell session, and how does it restore when we login again?
\=> Because in the current shell session, the history commands are kept in the RAM, and, when we log out from the shell, the commands in the RAM automatically get cleared and saved to the user’s history file. Now when we log in into the session again, the commands from the user’s history file are restored to the RAM or the history cache.
Note: By default, only 1000 commands in the particular shell session can be saved to the history cache and the user’s history file. Once the history cache or the user’s history file is filled with 1000 commands, the next commands start overwriting from the 1st command. Why?
\=> Because of the value associated with their environment variables of history cache and user’s history file.
history cache commands environment variable is →
echo $HISTSIZE
~/.bash_history environment variable is →
echo $HISTFILE
capacity of the .bash_history file to store the commands →
echo $HISTFILESIZE
We can increase the capacity of the history cache and the .bash_history file.
To fetch all the commands of the history, cache the command is:
history
To run any previous command, the command is:
!(history-number)
In the real world, we don’t type the number, we give the starting characters of the command, for getting the result quickly →!sys
,!ca
,!vi
If we by chance forget to type sudo in front of the last typed command, then the command is:
sudo !!
To clear a particular command from the history cache:
history -d (history-number)
Ex:history -d 23
,history -d 20 30 40
To clear the whole history cache, the command is:
history -c
If we want to see the history cache with the date and time, we need to set the variable called “HISTTIMEFORMAT“. By default, this is an unbound variable. We can set the variable in the shell → (this will set the variable in temporary form):
HISTTIMEFORMAT=”%d%m%y %T “
To set the variable in permanent form, we need to enter this variable in~/.bashrc
(for particular user) or/etc/bashrc
(for all the users).If we want to stop/pause the cache history, the command is:
set +o history
=> After this, whatever command will be executed, it will not be shown in the history cache.
To restart the cache history again:set -o history
In Ubuntu, if we don’t want any command to go in the history cache or the user’s history file, then we can type →
(space)(Type-command)
.
Ex:date
To hide the commands on the terminal from other person:
Ctrl +s
To unhide and check the commands that you ran:
Ctrl + q
#5 Shell Expansion in Linux (file globbing):
A glob is a “string of literal” or “wildcard characters” that matches file paths.
Globbing is the act of locating files/directories on a filesystem using one or more globs.
File globbing is the process of using wildcard characters to search files/directories with the same name pattern.
Q. What are wildcards in Linux?
\=> A wildcard in Linux is a symbol or a set of symbols that is used to search files/directories of the same pattern.
Ex: we have multiple files starting with the letter ‘b’ kept in the /tmp directory. If we want to fetch all the files, starting with the letter ‘b’, then we can type: ls b* /tmp
There are mainly 3 types of wildcard characters:
Asterisk (*) → **This represents all or everything.Ex: If we want to fetch the files that end with .png, then we can give this wildcard character to fetch those particular files:
ls *.png
Question mark (?) → This represents a single character (either number or alphabet). Ex:
ls b??t
Now, any word that starts with b and ends with t and has 2 characters in between, that will be fetched, like: (boat, best, boot)Bracketed characters ([]) → This matches any occurrence of character enclosed in the square brackets. Ex:
In the above example, we have the filename starting with “File“ and “file“. If we want to fetch only those files with Uppercase letters, then we can use → ls [A-Z]ile??
❌ BUT,
This will give us both the files, starting with Lowercase and Uppercase letters. But why is this happening? 👇🏻
Because of the “LANG” environment variable, which is set to en_IN.UTF-8. (echo $LANG
).
If we set the LANG variable to C, then we can easily fetch the files starting with Uppercase and Lowercase (separately), with this wildcard → ls [A-Z]ile??
, ls [a-z]ile??
Real World Example:
Let’s say there is a directory → (public_html/) which contains the files like: (.gif, .jpeg, .png, .jpg), and we want to move only those files which ends with ‘g’ and contains only 2 characters infront of ‘g’ like: (.png, .jpg) in another directory → (public_html/images/)
mv public_html/* .??g public_html/images/
To create multiple files in a single go:
touch file{1..20}.txt
To create multiple directories inside the directories (technically called “Nested directories“), in a single go:
mkdir -p firstdir/{sub1,sub2,sub3} seconddir/{pub1,pub2,pub3} thirddir/{cub1,cub2,cub3}
Subscribe to my newsletter
Read articles from Archit Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
