Linux
Linux
Linux Bash
The Linux Bash is also known as 'Bourne-again Shell. written by Brian Fox
The Bash is a command language interpreter as well as a programming
language. It supports variables, functions, and flow control, like other
programming languages. It can also read and execute the commands from a file,
which is called a shell script.
What is Shell
The terminal contains the shell; it allows us to execute the commands to interact with
the system. We can perform various operations such as store and retrieve data,
process information, and various other simple as well as complex tasks.
Perform some basic operations such as date, cal, ls, and pwd
What is Scripting
Shell Scripts: It is a text file that has shell commands and executes them when it is
used. Bash reads and executes the commands then exits.
Linux Unix
It is an open-source operating system It is an operating system which can be only
which is freely available to everyone. used by its copyrighters.
different distros Ubuntu, Redhat, Fedora, IBM AIX, HP-UX and Sun Solaris.
etc
Linux is used everywhere from servers, It is used in servers, workstations and PCs.
PC, smartphones, tablets to mainframes
and supercomputers.
Linux kernel is developed by the Unix has three distributions IBM AIX, HP-UX
community of developers from different and Sun Solaris. Apple also uses Unix to
parts of the world. Although the father of make OSX operating system.
Linux, Linus Torvalds oversees things.
It provides higher security. Linux has Unix is also highly secured. It has about 85-
about 60-100 viruses listed till date. 120 viruses listed till date
As Linux is open-source,whenever a user In Unix, users have to wait for some time for
post any kind of threat, developers from the problem to be resolved.
all over the world start working on it. And
hence, it provides faster solution.
To change the directory by using an absolute path, we have to mention the whole
path starting from the root.
From the above output, we are changing our directory to 'certs' from 'cups.' So, we
have provided the whole path "/run/cups/certs" starting from the root (/). This is called
an absolute path.
we have changed our directory from 'cups' to 'certs,' but have not mentioned the
whole path. This is the relative path.
1. cd ~
cd ~username
7) Change up to multiple Sub Directories
cd Dir1/Dir2/Dir3....
ls option Description
ls -a ls -a shows whole list + hidden files.
ls -l long list .
ls -lh file sizes in human readable format. Size of the file is very
difficult to read when displayed in terms of byte. The (ls -
lh)command will give you the data in terms of Mb, Gb, Tb, etc.
ls -g or ls -lG With this you can exclude column of group information and
owner.
ls -li This command prints the index number if file is in the first
column.
ls -lX It will group the files with same extensions together in the list.
ls -lt It will sort the list by displaying recently modified filed at top.
Commands Description
exit Exits from the current user group to the last group.
groups Displays the group name to which the current user belongs to.
rm Removes a file.
sudo It allows a user to start a program with the credentials of another user.
>cd ../../..
rmdir directory
>rmdir pune ->delete single directory
>rm -r galaxy ->delete full directory for galaxy to …..
>cat hi
hello.txt
Ctrl s
Ctrl x
>bash hello.txt
Hello world Good Morning
>nano hello.txt
-e then use \n
nusta \n nhi chanlar use both \e and \n
Shell of
echo -e “Hello world \n Good Morning” hello.txt
Ctrl s
Ctrl x
>bash hello.txt
Hello world
Good Morning
$ : (same as printf)
>nano hello.txt
Shell of
a=10
echo a
>bash hello.txt
>nano hello.txt
2.Case-Insensitive Search:
>grep -i "MANGO" fruits.txt
mango -i : ignores case
4.
5.
>nano hello.txt
>nano hello.txt
>nano hello.txt
Process management
1. ps To display the currently working processes
2. top Display all running process
Unix/Linux Command Reference
3. kill pid Kill the process with given pid
4. killall proc Kill all the process named proc
5. pkill pattern Will kill all processes matching the pattern
6. bg List stopped or background jobs,resume a stopped
job in the background
7. fg Brings the most recent job to foreground
8. fg n Brings job n to the foreground
File permission
1. chmod octal file Change the permission of file to octal,which
can
be found separately for user,group,world by
adding,
• 4-read(r)
• 2-write(w)
• 1-execute(x)
Searching
1. grep pattern file Search for pattern in file
2. grep -r pattern dir Search recursively for pattern in dir
3. command | grep
pattern
Search pattern in the output of a command
4. locate file Find all instances of file
5. find . -name filename Searches in the current directory
(represented by
a period) and below it, for files and directories with
names starting with filename
6. pgrep pattern Searches for all the named processes , that
matches with the pattern and, by default, returns
their ID
System Info
1. date Show the current date and time
2. cal Show this month's calender
3. uptime Show current uptime
4. w Display who is on line
5. whoami Who you are logged in as
Unix/Linux Command Reference
6. finger user Display information about user
7. uname -a Show kernel information
8. cat /proc/cpuinfo Cpu information
9. cat proc/meminfo Memory information
10. man command Show the manual for command
11. df Show the disk usage
12. du Show directory space usage
13. free Show memory and swap usage
14. whereis app Show possible locations of app
15. which app Show which applications will be run by default
Compression
1. tar cf file.tar file Create tar named file.tar containing file
2. tar xf file.tar Extract the files from file.tar
3. tar czf file.tar.gz files Create a tar with Gzip compression
4. tar xzf file.tar.gz Extract a tar using Gzip
5. tar cjf file.tar.bz2 Create tar with Bzip2 compression
6. tar xjf file.tar.bz2 Extract a tar using Bzip2
7. gzip file Compresses file and renames it to file.gz
8. gzip -d file.gz Decompresses file.gz back to file
Network
1. ping host Ping host and output results
2. whois domain Get whois information for domains
3. dig domain Get DNS information for domain
4. dig -x host Reverse lookup host
5. wget file Download file
6. wget -c file Continue a stopped download
Unix/Linux Command Reference
Shortcuts
1. ctrl+c Halts the current command
2. ctrl+z Stops the current command, resume with fg in the
foreground or bg in the background
3. ctrl+d Logout the current session, similar to exit
4. ctrl+w Erases one word in the current line
5. ctrl+u Erases the whole line
6. ctrl+r Type to bring up a recent command
7. !! Repeats the last command
8. exit Logout the current session
Assignments
1).Write a shell script to create a menu driven program for adding,
deletion or finding a record in a database. Database should have the field
like rollno, name, semester and marks of three subjects. Last option of the
menu should be to exit the menu.
#!/bin/bash
# Initialize an associative array to act as a simple database
declare -A database
# Menu
while true; do
echo "Menu:"
echo "1. Add Record"
echo "2. Delete Record"
echo "3. Find Record"
echo "4. Exit"
read -p "Enter your choice: " choice
case $choice in
1)
add_record
;;
2)
delete_record
;;
3)
find_record
;;
4)
echo "Exiting the menu."
exit 0
;;
*)
echo "Invalid choice. Please enter a valid option."
;;
esac
done
2) Write a Linux shell script to accept 10 number and tell how many are
+tive, -tive and zero.
#!/bin/bash
*
**
***
****
#!/bin/bash
1
12
123
1234
11. Shell Script to make such a pattern like a right angle triangle with a
number which will repeat a number in a row.
1
22
333
4444
#!/bin/bash
12. Shell Script to make such a pattern like a right angle triangle with the
number increased by 1.
1
23
456
7 8 9 10