0% found this document useful (0 votes)
6 views52 pages

Basic SHELL Commands

The document provides an overview of basic shell commands in Linux and Unix, highlighting differences, usage, and architecture. It covers various commands such as 'cd', 'mkdir', 'ls', 'echo', 'cp', and 'rm', along with their syntax and examples. Additionally, it explains shell types, permissions, and the concept of pipes for command output manipulation.

Uploaded by

Prachi Nilekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views52 pages

Basic SHELL Commands

The document provides an overview of basic shell commands in Linux and Unix, highlighting differences, usage, and architecture. It covers various commands such as 'cd', 'mkdir', 'ls', 'echo', 'cp', and 'rm', along with their syntax and examples. Additionally, it explains shell types, permissions, and the concept of pipes for command output manipulation.

Uploaded by

Prachi Nilekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Basic shell commands

Basis of Linux Unix


Difference
Linux is freely distributed. There Different flavors of Unix have
Cost are paid versions also available for different pricing depending upon
Linux. the type of vendor.
Linux is Open Source, and
Development thousands of programmer Unix systems have different
collaborate online and contribute to versions.
its development.
Everyone. From home users to
User The UNIX can be used in internet
developers and computer
servers, workstations, and PCs.
enthusiasts alike.
Linux OS can be installed on The UNIX operating system is
Usage various types of devices like used for internet servers,
mobile, tablet computers. workstations & PCs.
Different Versions of Linux are
Versions Different Versions of Unix are HP-
Redhat, Ubuntu, OpenSuse,
UX, AIS, BSD, etc.
Solaris, etc.

Portability Linux is portable and is booted


Unix is not portable
from a USB Stick
The source is available to the The source code is not available to
Source Code
• What is Unix ?
• The Unix operating system is a set of programs that act as a link
between the computer and the user.

• The computer programs that allocate the system resources and


coordinate all the details of the computer's internals is called
the operating system or the kernel.

• Users communicate with the kernel through a program known


as the shell. The shell is a command line interpreter; it translates
commands entered by the user and converts them into a
language that is understood by the kernel.
Unix Architecture
Here is a basic block diagram of a Unix system −
https://cocalc.com/projects/322d15ed-0c8d-43f1-8866-c401f71fb7db/files/
• Kernel − The kernel is the heart of the operating system. It
interacts with the hardware and most of the tasks like memory
management, task scheduling and file management. A system call
is a request by a process to the kernel. The kernel manages these
system calls and other resources.
• Shell −
– The shell is the utility that processes your requests.
– Shell is the interface between user and kernel.
– It is a command line interpreter and is the interface between the
user and the kernel.
– The user can enter commands to the shell. Then it interprets the
commands to perform the required task.
– Furthermore, it executes programs and shell scripts. A shell
script is a set of commands. The user should follow the
standard syntax to write commands to the shell.
– C Shell, Bourne Shell and Korn Shell
Types of Shell

Bourne shell (sh)


C shell (csh)
TC shell (tcsh)
Korn shell (ksh)
Bourne Again SHell (bash)

For Bourne-type shell, the default prompt is the $ character.


For C-type shell, the default prompt is the % character.

To display shell
echo $SHELL
To change your shell with chsh:

1.cat /etc/shells
At the shell prompt, list the available shells on your system with
cat /etc/shells.
2.chsh
Enter chsh (for "change shell").
Some systems prompt for a password, and some don't.
3./bin/zsh
Type in the path and name of your new shell.
4.su - yourid
Type in su - and your userid to relog in to verify that everything
works correctly.
If it doesn't, use chsh again and change back to the original shell or
to a different one.
General command format

Command -options arguments

• options/flags generally identify some optional capabilities


• some parts of a command are optional. These are indicated
in the man pages with [ ]
• case sensitive
NAME
cal - displays a calendar

SYNOPSIS
cal [-smjy13] [[[day] month] year]

DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the cur-
rent month is displayed. The options are as follows:

-1 Display single month output. (This is the default.)

-3 Display prev/current/next month output.

-s Display Sunday as the first day of the week.

-m Display Monday as the first day of the week.

-y Display a calendar for the current year.


cal --displays current moth with highlighted current day
Cal –y 2011 -------shows year 2011
• cd command- change current working directory
• If directory is given, changes the shell's working
directory to directory. If not, changes to HOME (shell
variable).
• The return status is zero if the directory is successfully
changed, non-zero otherwise.
• Move to the sample folder
– cd /usr/local/sample
• Move up one folder
– cd ..
• To move to old current directory
– cd
• The mkdir command ----allows users to create directories or
folders
• To create a directory in UNIX or Linux using the mkdir command
pass the name of directory to the mkdir command.
mkdir mydirectory
• How to create multiple directories
mkdir foo bar baz
• How to create parent directories
mkdir -p foo/bar/baz
tree foo
foo
└── bar
└── baz
• How to set permissions when creating a directory
• To set permissions when creating a directory pass the
-m option.
• This accepts a number value to set the file mode.
• mkdir -m 777 foo

– Read (4)
– Write (2)
– Execute (1)
• rmdir
• rmdir command will delete the empty directories.
• i.e directory without any sub-directories or files:
• You can’t remove a subdirectory unless you are placed in
a directory which is hierarchically above the one you have
chosen to remove.
• $ rmdir test
• Example-2:
• To Delete Nested Empty Directories in Linux:
• $ rmdir -p dir1/dir2/dir
touch command-
Touch options expression filename(s)

• touch abc – creates file abc if does not exist. Both times
are set to current time
• touch abc pqr ----creates two files abc and pqr
• stat filename to view complete timestamps of file
cat command-
It has three related functions with regard to text files:
displaying them, combining copies of them and creating new ones.
cat's general syntax is
cat [options] [filenames]
Reading Files
• The most common use of cat is to read the contents of files
cat file1
• In the following example, the standard output of cat is redirected
using the output redirection operator to file2:
cat file1 > file2
That is, the output from cat is written to file2 instead of being
displayed on the monitor screen.
• Following command will concatenate copies of the contents of the
three files file1, file2 and file3:
cat file1 file2 file3
• The output can be redirected using the output redirection
operator to another file, such as file4, using the following:
cat file1 file2 file3 > file4
• In the next example, the output of cat is piped to the sort filter
in order to alphabetize the lines of text after concatenation and
prior to writing to file4:
cat file1 file2 file3 | sort > file4
• >> operator is used to append contents to file.
cat >> file1
• cat –n file1 ---- displays line numbers with text
• ls – list directory contents.
• ls [options] [file|dir]
• ls –l shows file or directory, size, modified date and
time, file or folder name and owner of file and it’s
permission.
• # ls -l
• drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
• drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
description

ls –a list all files including hidden file starting with '.'


ls -l list with long format - show permissions
ls -la list long format including hidden files
ls -lh list long format with readable file size
ls -S sort by file size
ls -t sort by time & date
• echo - display a line of text.
Example-1:
To print string "Hello, World!" on console
$ echo "Hello, World!"
output:
Hello, World!

Example-2:
To print value of x, where x=10.
$ echo $x
output:
10
echo the STRING(s) to standard output.

-e enable interpretation of backslash escapes

-E disable interpretation of backslash escapes (default)


Example-:
To print all the files/folder using echo command (ls command
alternative).
ubuntu@ubuntu:/usr$ echo *
output:
bin games include lib local sbin share src

Example-:
To echo output to a file and not standard output. in below example
output is redirected to test file.
$ echo "Hello World!" > test
output:
$cat test
Hello World!
cp-Copy from source to dest
cp [options] src dest
cp stands for copy. This command is used to copy files or group of
files or directory. It creates an exact image of a file on a disk with
different file name. cp command require at least two filenames in its
arguments.

Two file names : If the command contains two file names, then it
copy the contents of 1st file to the 2nd file. If the 2nd file doesn’t
exist, then first it creates one and content is copied to it. But if it
existed then it is simply overwritten without any warning.
cp abc.txt pqr.txt
cp –i abc.txt pqr.txt
One or more arguments : If the command has one or more
arguments, specifying file names and following those arguments, an
argument specifying directory name then this command copies each
source file to the destination directory with the same name, created if
not existed but if already existed then it will be overwritten.

cp abc.txt pqr.txt demo


Two directory names : If the command contains two directory
names, cp copies all files of the source directory to the destination
directory, creating any files or directories needed. This mode of
operation requires an additional option, typically R, to indicate the
recursive copying of directories.
cp –R Src_directory Dest_directory

In the above command, cp behavior depend upon


whether Dest_directory is exist or not. If the Dest_directory doesn’t
exist, cp creates it and copies content of Src_directory recursively as it
is. But if Dest_directory exists then copy of Src_directory becomes
sub-directory under Dest_directory.
Umask command-
On Linux and other Unix-like operating systems, new files are
created with a default set of permissions. Specifically, a new file's
permissions may be restricted in a specific way by applying a
permissions "mask" called the umask. The umask command is used
to set this mask, or to show you its current value.
Umask [-S] [mask]

-S Accept a symbolic representation of a mask, or


return one.
mask If a valid mask is specified, the umask is set to this
value. If no mask is specified, the current umask value
is returned.
Umask 044
touch test2 -----By default the mask is 022. Default permission for
file is 666 and for directory is 777
This will assign permission for file test2 (666-044=622)
chmod - To change access permissions, change mode.
read (4), write (2), and execute (1)

Absolute permissions:
Read by owner only
$ chmod 400 sample.txt
Write by group only
$ chmod 020 sample.txt
Execute by anyone
$ chmod 001 sample.txt
Allow everyone to read, write, and execute file.
$ chmod 777 sample.txt
• Relative permissions:
• user/owner (u), other users in the file's group (g), other users not in
the file's group (o), or all users (a).
• The operator '+' causes the permissions selected to be added to the
existing permissions of each file;
• '-' causes them to be removed;
• and '=' causes them to be the only permissions that the file has.
Deny execute permission to everyone.
$ chmod a-x sample.txt
Allow read permission to everyone.
$ chmod go+rw sample.txt
Make a shell script executable by the user/owner.
$ chmod u+x samplescript.sh
chmod ug+x,o-w a.txt
wc word count
rm - remove files or directories
rm [OPTION]... FILE...
rm removes each specified file. By default, it does not remove
directories.
Example-1:
Remove the file myfile.txt. If the file is write-protected, you will be
prompted to confirm that you really want to delete it:
$ rm myfile.txt
rm myfile.txt myfile1.txt
rm -i myfile.txt

Example-2:
Remove the file myfile.txt. You will not be prompted, even if the file
is write-protected; if rm can delete the file, it will:
$ rm -f myfile.txt
Example-3:
Remove all files in the working directory. If it is write-protected, you will be
prompted before rm removes it:
$ rm *

Example-4:
Remove all files in the working directory. rm will not prompt you for any
reason before deleting them:
$ rm -f *

Example-5:
Attempt to remove every file in the working directory, but prompt before
each file to confirm:
$ rm -i *
Example-7:
Remove the directory mydirectory, and any files and directories it
contains. If a file or directory that rm tries to delete is write-
protected, you will be prompted to make sure that you really want
to delete it:
$ rm -r mydirectory

Example-8:
Same as the above command, but you will never be prompted; if
rm can delete the files, it will:
$ rm -rf mydirectory

Pipes

• A pipe is a way to use the output from one command as the


input to another command without having to create
intermediary files.
• Example: want to see who is logged in, and you want the
result displayed alphabetically:
who > namelist
sort namelist
• Using a pipe:
who | sort
• Example: want to get a count of the users logged in to the
system:
who | wc -l
[it@localhost ~]$ cat >a.txt
hi
hello
how r u
nice
abye
[it@localhost ~]$ cat a.txt | sort |wc -l
5
[it@localhost ~]$ cat a.txt | sort
abye
hello
hi
how r u
nice
[it@localhost ~]$

Sort- sort lines of text files [it@localhost ~]$ sort -r a.txt
sort [option] [file] b.txt
om
[it@localhost ~]$ sort a.txt b.txt
nice
abye
how r u
baby
ho
fello hi
hello HELLO
HELLO hello
how r u fello
nice baby
om Abye
[it@localhost ~]$ sort –o c.txt a.txt b.txt
[it@localhost ~]$ cat c.txt [it@localhost ~]$ sort -k 2 c.txt
abye 6 aa 123
baby 2 bbb 671
fello 1 hh 345
hello [it@localhost ~]$ sort –k 3 c.txt
HELLO
6 aa 123
1 hh 345
how r u
2 bbb 671
nice
[it@localhost ~]$
om
cut - to extract data in vertical form
cut –c 1 file --- it will show column 1 details from file.
cut –c 5-9 file --range can also be used to specify column nos
cut –f 2 file ---- It will show field 2 from file for all tuples

[it@localhost ~]$ cat c.txt


1 hh 345
6 aa 123
2 bbb 671
cut -f 2 c.txt
hh
aa
bbb
Let's have a sample file sample.txt
$ cat sample.txt
1;2;3;4;5;6;7;8;9
To Parse out column 2 from a semicolon (;) delimited file:

$ cut -f 2 -d \; sample.txt
2
head and tail
"head" will display information from the beginning of the file and "tail" will display
information from the end of the file.
By default both the "head" and "tail" program display the first 10 or last 10 lines
respectively:

head concatfile2.txt

head -n 5 concatfile2.txt
1 Linux Mint
2 Mageia
3 Ubuntu
4 Fedora
5 openSUSE
tail -n 5 concatfile2.txt
26 Debian
27 Arch Linux
28 PCLinuxOS
29 Zorin OS
30 CentOS
[it@localhost ~]$ cat try.txt uniq command
this is file
this is file
contents are
closing file
closing file
[it@localhost ~]$ uniq try.txt shows data without duplication
this is file
contents are
closing file
[it@localhost ~]$ uniq -d try.txt shows duplicate lines only
this is file
closing file
[it@localhost ~]$ uniq -u try.txt shows non duplicate lines only
contents are
[it@localhost ~]$ uniq -c try.txt shows count of duplication and contents of file
2 this is file
1 contents are
2 closing file
[it@localhost ~]$
Linux mv command.
mv command is used to move files and directories.
It renames a file or directory.
$ mv [options] source dest

Move main.c def.h files to /home/usr/rapid/ directory:


$ mv main.c def.h /home/usr/rapid/

Move all C files in current directory to subdirectory bak :


$ mv *.c bak
Rename file main.c to main.bak:
$ mv main.c main.bak

Rename directory bak to bak2:


$ mv bak bak2

Move main.c and prompt before overwrite bak/main.c:


$ mv -i main.c bak
grep- Global regular expression print
grep- print lines matching patterns
grep options pattern filename(s)
• Searching using grep
> grep music myfile
• To ignore upper/lower case distinctions, use the -i option
> grep -i music myfile
• To search for a phrase or pattern, you must enclose it in
single quotes. For example to search for the phrase operating
systems, type
> grep -i 'operating systems' myfile
> grep -i 'operating systems' *
Searching the contents of a file

Some of the other options of grep are:

-v display those lines that do NOT match expression


-n precede each matching line with the line number
-c print only the total count of matched lines
-l displays list of file names only
grep –l “director” emp.txt emp1.txt
-f takes pattern from a file, one per line
grep –f abc.txt pqr.txt
-e expr used to match multiple patterns
grep –e “Agrawal” –e “Aggarwal” emp.lst
tr command (Translating characters)
tr command doesn’t accept a file name as argument, the input has
to be redirected from a file or a pipe.

[it@localhost ~]$ cat try.txt


this is file
closing file
[it@localhost ~]$ cat try.txt|tr 'a-z' 'A-Z‘
or tr ‘a-z’ ‘A-Z’ < try.txt
THIS IS FILE
CLOSING FILE
[it@localhost ~]$
[it@localhost ~]$ cat >d.txt
aabbbbbbbboooooooof
[it@localhost ~]$ cat d.txt|tr -s b (squeeze option)
aaboooooooof
[it@localhost ~]$ cat d.txt|tr -s bo
aabof
[it@localhost ~]$ cat d.txt|tr -s abo
abof
[it@localhost ~]$ cat d.txt|tr -s [a-z]
abbbbbbbboooooooof
[it@localhost ~]$ cat d.txt|tr -s '[a-z]'
abof
[it@localhost ~]$ cat d.txt|tr -d o (deleting character)
aabbbbbbbbf
ps command
Ps shows which processes are running at any instant
ps
[it@localhost ~]$ ps
PID TTY TIME CMD
2723 pts/0 00:00:00 bash
2808 pts/0 00:00:00 ps
[it@localhost ~]$

ps –f ---- full listing of process


[it@localhost ~]$ ps -f
UID PID PPID C STIME TTY TIME CMD
it 2723 2721 0 Jun18 pts/0 00:00:00 bash
it 2803 2723 0 00:07 pts/0 00:00:00 ps -f
[it@localhost ~]$
ps –e ----shows details of every process running at that instant including user and
system processes
ps aux -----shows detailed information of every process running at that instant

You might also like