Table of contents
Why Linux?
You know what? Linux is an Open source and 91% of applications are using different flavors of Linux. we have different types of Operating Systems for our devices which help in doing various amount of tasks on the system by interacting with applications and Hardware. And one of the Operating systems is Linux which allows multi-user and multi-tasking facilities. Not only that it provides high security without any anti-virus software.
Flavours of Linux are:
Kali Linux
Ubuntu
CentOs
SUSE
Red Hat etc.
Shell Types
Linux systems have both CLI and GUI. Shell is a command line interface to interact with the system. We give the commands via shell to the kernel so the desired task is performed.
We have different types of Shells
Bourne Shell (Sh Shell)
C shell (csh or tcsh)
Z Shell (zsh)
Bourne Again Shell (Bash)
To Check in which shell we are in we can use the command
echo $SHELL
Here the $ symbol is used to represent the environmental variable
Basic Linux Commands
lsb_release -a
This command helps us to know about the Linux Standard Base(LSB).
lsb_release -a
# Output
No LSB are Available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04 LTS
Release: 22.04
codename: jammy
# for More information checkout
man lsb_release
this command is cool but why? because we need to have at least information about the system that we are using. we can check this out there.
Who and whoami
who the command gives the Login name of the user, the Date and Time of login and the remote hostname of the user.
whoami it is displaying the username of the user
who
whoami
id
Id command displays user identification.
id
id -g # print only the effective group ID
id -G # print all group IDs
ls
ls # List all the files and directories.
ls -a # List all the files and directories, including hidden ones.
ls -l # List all the files and directories for long listing format
ls -al
ls -R # Lists all the subdirectories
Whereis
To locate the location on the system where the program is installed.
whereis docker # locates the docker application path
whereis ubuntu
whereis java
mkdir
To create the directory [folder]
mkdir new_dir # new_dir is created
mkdir -p new_dir/new_dir1/new_dir2 # Subdirectories are created.
cd
To change the directory
cd new_dir # changed to new_dir directory
cd . # changed to present working directory
cd .. # cchanges to the previous directory of the present directory
cd # changes to Home directory
Environment Variable
By setting environment variables at the system level, administrators can ensure that all programs running on the system behave consistently, even if they were developed by different teams or for different purposes.
echo $PATH # PATH is a environment variable
echo $SHELL
echo $HOME
echo $USER
okay, these all are cool commands but are these commands magical to print all this information? No behind-the-scenes of these command files are running to get the desired output.
Cat
Hey, cool down. You might be wondering why suddenly animals came into the picture. Just kidding, this is a command that helps in creating files and displaying data of files etc. Let's look into that.
cat > Text.txt # Create Text.txt, add data, exit with Ctrl+D.
cat Text.txt # Displays the data of Text.txt file
cat > Text.txt # Creating same file name overwrites previous file contents.
cat Text1.txt Text2.txt # Display first file, then show next file's data.
cat Text1.txt Text2.txt > Text3.txt # Combine text1.txt and text2.txt data into text3.txt file.
Echo
echo "Hello World!" # Displays Hello World!.
echo "Hello world" > Text.txt # Overwrite Text.txt with "Hello world" data.
echo "Hello World" >> Text.txt # Appends Text.Txt with "Hello World" data.
Man
Gives the manual of a command.
man grep
man whereis
man ls
man echo
Tr
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
cat Text.txt |tr a-z A-Z
# DATA IN Text.txt "hello" coverted to "HELLO"
Touch
To create a file we use the touch command
touch main.go
touch Example1.txt
touch Test.py
touch Text.txt
touch new_dir/Text1.txt # Text1.txt is created in new_dir folder
Cp (copy)
cp text.txt copy_text.txt # copy_text.txt copies the text.txt
Mv (move)
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY
mv names.txt New_dir # names.txt is moved to New_dir
mv file2.txt new_file.txt # renamed file
mv New_dir Random_dir # renamed directory if Random_dir doesn't exist
mv new_dir random_dir # random_dir directory exist then it moves target
Rm (Remove)
To remove a file or a directory we have the command rm
rm file.txt # Removes file.txt if exists
rm -R new_dir # Removes directory if exists
rmdir new_dir # Removes new_dir directory
rm --help # For more Information
Sudo (Super User do)
This command gives the administrative or root use privileges just like in windows we open the command line prompt as administrator.
Using sudo is more secure than logging into the system as sudo user. But we can log in via
sudo -i
sudo -s
sudo su -
su -root
su -
df (disk space)
Show information about the file system on which each FILE resides, or all file systems by default.
df # This command gives disk space
du(disk usage)
du
Head
This command helps to get data from the file from top to bottom. By default it gives 10 lines of the file we can also mention how many lines we want.
head file.txt
head -n 3 file.txt # Gives top 3 line of code in file.txt
Tail
tail file.txt
tail -n 100 file.txt # This command give 100 line from bottom of the file
Diff
When we copy some files and made some changes in the file how to find what changes are made then here you go.
diff text.txt text_copy.txt
mlocate
To locate the file we use mlocate.
mlocate "*.txt" # Locates all the files with .txt file extentions
# if you are not finding mlocate in your system then run this command
sudo apt install mlocate
Find
This command helps us to find the files and directories.
find .
find ..
fid random_dir
find rand_dir/test
find . -type d # Finds all directories in the present directory.
find . -type f # Finds all files in the present directory.
find . -type f -iname "file.txt" # finds file.txt file in present directory.
find . -perm 777 # Finds all files with permissions read, write, and execute.
find . -empty # Finds the files which are empty
File Permissions
In Linux, we can secure private files by granting and revoking access. This access can be read, written, and executed. Read has a value of 4, write has a value of 2, and execute has a value of 1. If we want to provide all access to a file or directory, then we can use 7. And one more thing we can give access to the user, group, and others. If we want them to have all the permissions, we can give them 777.
U =rwx (read, write, execute) User
G= rwx (read, write, execute) Group
O=rwx (read, write, execute) Others
To change permissions of these three we can use chmod
chmod 777 New_Dir
let's grant all permissions to the user, grant read permissions to groups, and grant write and execute permissions to others
chmod 743 Random_dir
Grep
Search for PATTERNS in each FILE.
grep --v # Gives Version of grep
grep "Linux" os.txt # Highlights the word and displays the whole line.
grep -w "linux" os.txt # Finds word pattern which is linux
grep -i "linux" os.txt # To remove case sensitivity
grep -n "linux" os.txt # It shows the line number of pattern
grep -r "linux" os.txt # It recursively searches for the pattern mentioned.
grep -win "linux" os.txt
history | grep "ls" # lists every ls command that we have ever used.
grep --help # for more infomation
clear
To clear the commands on the shell
clear
we can also use ctrl + l