01 - Working with Shell - I
Table of contents
What is Linux?
Linux is a powerful, open-source operating system that serves as the backbone of countless technologies we use every day.
It was created by Linus Torvalds in 1991 and is based on the Unix operating system.
Unlike proprietary operating systems, Linux is freely available for anyone to use, modify, and share.
At its core, Linux is a kernel - the fundamental part of an OS responsible for managing hardware and system resources.
Surrounding this kernel are various distributions (or "distros"), such as Ubuntu, Fedora, and CentOS, which package the kernel with user-friendly tools and applications.
Linux is renowned for its stability, security, and flexibility, making it a favorite among developers, system administrators, and organizations for servers, cloud computing, embedded systems, and more.
Working with THE SHELL
The Linux Shell is a program that allows text-based interaction between the user and the operating system. It is a command-line interface (CLI) that acts as an interpreter between the user and the operating system.
This interaction is carried out by typing commands into the interface and receiving response in the same way.
The HOME Directory
When you log into the shell, the very first directory you are taken to is the home directory.
The home directory in Linux is a personal space on the file system which allows users to store their personal data in the form of files and directories.
Each user in the system gets their own unique home directory with complete access to it, to be able to save, retrieve and delete data.
The home directory is typically located under the
/home
directory (/home
us system create directory). For example:If your username is
rohit
, your home directory will be/home/rohit
.System administrators (root users) usually have their home directory as
/root
.
The
~
(tilde)symbol is shorthand for the home directory, so you can reference it like this:$ ls ~
Command and Arguments
A command in Linux is an instruction given to the shell to perform a specific action.
- For example: The
echo
command is used to print a line of text on the screen.
- For example: The
$ echo
An argument acts as an input to the command.
- For example: To print a
hello
message typeecho hello
command.
- For example: To print a
$ echo hello
Many of the commands needs arguments to work, though it is not mandatory. There are several commands that work without arguments too.
- For example: Type in the command called
uptime
, this is used to print information about how long a system has been running for since the last reboot along with the other information
- For example: Type in the command called
$ uptime
A command can also have options to modify its behavior in some predetermined way.
The option, also sometimes referred to as a switch or a flag, is usually start with a
-
(single character) or--
(full word).- For example: To print a same word
hello
but without a trailing line, useecho
command with-n
flag.
- For example: To print a same word
$ echo -n hello
Command Types
Commands in Linux can be generally categorized into two types:
Internal or Build-in Commands:
This are part of the shell itself and come bundled with it.
There are total of 30 such commands.
For example:
echo
,cd
,pwd
,set
, etc.
External Commands:
This are binary programs or scripts which are usually located in distinct files in the system.
They either come pre-installed with the distributions package manager or can be created or installed by the user.
For Example:
mv
,date
,uptime
,cp
, etc.
To determine whether the command is internal or external, use the
type
command.type echo # command echo is a shell build-in # output type mv # command mv is hashed (/bin/mv) # output
Basic Linux Commands
Commands:
Directory Navigation Commands
- Directory navigation commands provide shortcuts to navigate to the desired location quickly.
Commands | Description |
ls | List files and directories in the current directory. |
ls -a | List all files and directories in the current directory (shows hidden files). |
ls -l | List files and directories in long format. |
pwd | Show the directory you are currently working in. |
cd | |
cd ~ | Change directory to $HOME . |
cd .. | Move up one directory level. |
cd - | Change to the previous directory. |
cd [directory_path] | Change location to a specified directory. |
File Commands
- File commands help with file and directory management on the system. Create, delete, move, and modify files and directories from the terminal
Command | Description |
mkdir [directory_name] | Create a new directory. |
rm [file_name] | Remove a file. |
rm -r [directory_name] | Remove a directory recursively. |
rm -rf [directory_name] | Recursively remove a directory without requiring confirmation. |
cp [source_file] [destination_file] | Copy the contents of one file to another file. |
cp -r [source_directory] [destination_directory] | Recursively copy a directory to a second directory. |
mv [source_file] [destination_file] | Move or rename files or directories. |
ln -s [path]/[file_name] [link_name] | Create a symbolic link to a file. |
touch [file_name] | Create a new file.. |
cat [file_name] | Show the contents of a file. |
cat [source_file] >> [destination_file] | Append file contents to another file. |
head [file_name] | Show the first ten lines of a file. |
tail [file_name] | Show the last ten lines of a file. |
more [file_name] | Display contents of a file page by page. |
less [file_name] | Show the contents of a file with navigation. |
nano [file_name] | Open or create a file using the nano text editor. |
vi [file_name] | |
vim [file_name] | Open or create a file using the Vi/Vim text editor. |
wc -w [file_name] | Show the number of words, lines, and bytes in a file. |
diff [first_file] [second_file] | Compare two files and display differences. |
System Management and Information Commands
- Use the terminal to manage the system directly. The commands show how to view basic system information, change options, and reboot or restart the system.
Command | Description |
uname -r | Show system information. |
uname -a | See kernel release information. |
uptime | Display system uptime, including the load average. |
hostname | View system hostname. |
hostname -i | Show the IP address of the system. |
last reboot | List system reboot history. |
date | See current time and date. |
timedatectl | Query and change the system clock. |
cal | Show current calendar (month and day). |
whoami | See which user you are using. |
finger [user_name] | Show information about a particular user. |
shutdown [hh:mm] | Schedule a system shutdown. |
shutdown now | Shut down the system immediately. |
Absolute Path and Relative Path
Absolute Path : An absolute path is defined as specifying the location of a file or directory from the root directory(/).
Relative Path : Relative path is defined as the path related to the present working directly(pwd).
Alternatives to the cd
command
Alternative to the
cd
is thepushd\popd
command.The pushd command is used to save the current directory into a stack and move to a new directory. Furthermore, popd can be used to return back to the previous directory that is on top of the stack.
To change directory using pushd, run
pushd <directory_name>
$ pushd /etc
- You can change to subdirecties under /etc as many times as you wish
$ pushd /var
$ pushd /tmp
$ pwd
/etc/var/tmp
- To return back to origin directory(say your home directory), use the
popd
command
$ popd
Pagers
In Linux, a pager is a utility that displays the output of commands or the contents of a file one screen (or page) at a time.
It’s especially useful when dealing with long outputs that don’t fit entirely on your terminal screen.
Common Pagers in Linux
more
:With
more
command, we can view the text file in a scrollable manner.This command loads the entire file at once, and is not the best choice when dealing with large files
more largefile.txt
less
:Less command, allows you to view the contents of a file and navigate through the file.
less largefile.txt
Key Controls in Pagers:
Key | Action |
Space | Move forward one page. |
Enter | Move forward one line. |
b | Move backward one page (less ). |
/pattern | Search for pattern in the text. |
n | Jump to the next search result. |
q | Exit the pager. |
Command-line help
whatis
: This command displays a one line description of what a command does.$ whatis date
man
: Most of the commands internal or external come bundled withman pages
which provides information about the command in detail (with examples, usecases and with command options).$ man date
--help
: Several commands will provide-h
or--help
to provide users with the options and use cases available in a command.$ date -h $ date --help
apropos
: To search through the man page names and descriptions for instances of the keyword useapropos
. This is useful if you want to look for all commands within the system that contains the specific keyword.$ apropos directories
Bash Shell
Types of Shell
Bourne Again Shell (
bash
)The most widely used shell on Linux systems which supports command history, functions and auto-completion.
Bash supports command history and autocompletion.
history # to get a list of previously run commands
Can set aliases for the actual commands.
alias dt=date # setting dt as an alias for date command
An improved version of the original Bourne Shell (
sh
).Bash is often the default shell on many Linux distributions.
Bourne Shell (
sh
)C Shell (
csh
ortcsh
)Korn Shell (
ksh
)Z Shell (
zsh
)
To Check Your Current Shell
- You can find out which shell you are using by running:
echo $SHELL
How to Switch Between Shells
- To switch to a different shell, type its name in the terminal (if installed). For example:
zsh # Switch to zsh
bash # Switch to bash
- To make a shell your default, you can use the
chsh
command:
chsh -s /bin/sh rohit # change shell from bash to sh for user rohit
Bash Environment Variables
Environment variables, specifically stores the information about the user’s login session, which is use by the shell when executing commands.
To see the list of all environment variables.
env # to see the list of all environment variables
To set the environment variable (this set the variable for the current shell and any other process or programs started by the shell)
export NAME=Rohit # to set the environment variable (this set the variable for the current shell and any other process or programs started by the shell)
To set the environment variable (this only apply the variable within the shell, the value is not carried forward by any other process)
NAME=Rohit # to set the environment variable (this only apply the variable within the shell, the value is not carried forward by any other process)
To make the environment variables persistent over persistent login or reboots (add them to
~/.profile
or~/.pam_environment
file.
Path Variable
When a user issues an external command into the shell, the shell uses path variable to search for these external commands.
To see the directories defined in path variable. Use the command
echo $PATH
.$ echo $PATH
To check if the location of the command can be identified. Use the
which
command:$ which obs-studio
To define a command in the
PATH
variable, we can use theexport
command.$ export PATH=$PATH:/opt/obs/bin $ which obs-studio
Customize Bash Prompt
Once you login into the shell, the line you see is the bash prompt.
[~]$ # ~ = present working directory # $ = user prompt symbol
The bash prompt is control by a special shell environment variables:
PS1
[~]$ echo $PS1 [\W]$ # \W = ~ = present working directory # $ = user prompt symbol
To change the PS1 to only display the word
ubuntu-server:
(To customize further, have a look at the below special character).