Unix Basic Commands
Unix Basic Commands
UNIT-1
Introduction
It interacts with the hardware and most of the tasks like memory
management, task scheduling and file management, etc.
The shell uses standard syntax for all commands. C Shell, Bourne
Shell and Korn Shell are the most famous shells which are
available with most of the Unix variants.
ARCHITECTURE CONTD…
Commands − There are various commands which you can make
use of in your day to day activities.
cp, mv, cat and grep, etc. are few examples of commands
There are over 250 standard commands plus numerous others
provided through 3rd party software.
All the commands come along with various options.
Files and Directories − All the data of Unix is organized into
files.
All files are then organized into directories. These directories are
further organized into a tree-like structure called the file system.
UNIX KERNEL
ARCHITECTURE
KERNEL ARCHITECTURE
It prints the path of the working directory, starting from the root.
Example: $ pwd
cd: change directory , This command is used to change the
current working directory
ls -R: list all files recursively, descending down the directory tree
from the given path.
e.g.
dir1 dir2 file1 file2
./dir1:
file3
./dir2:
ls -l: list the files in long format i.e. with an index number, owner
name, group name, size, and permissions.
BASIC UNIX COMMANDS CONTD…
e.g.
ls -t: sort the list by time of modification, with the newest at the
top.
ls -S: sort the list by size, with the largest at the top.
INTRODUCTION CONTD…
help :
The new users of Unix generally use “help” command to get the
information about the command
This also describes the purpose of using the command along with
the options in a simple way
e.g. ls --help
man :
Arithmetic operators
Assignment operators
Math functions
Conditional statements
Iterative statements
GENERAL PURPOSE COMMANDS CONTD…
Arithmetic Operators
Example: $ echo "12+5" | bc Output : 17
$ echo "10^2" | bc Output : 100
How to store the result of complete operation in variable?
Example:
$ x=`echo "12+5" | bc`
$ echo $x Output:17
GENERAL PURPOSE COMMANDS CONTD…
Assignment Operators
The list of assignments operators supported are:
var = value : Assign the vale to the variable
var += value : similar to var = var + value
var -= value : similar to var = var – value
var *= value : similar to var = var * value
var /= value : similar to var = var / value
var ^= value : similar to var = var ^ value
var %= value : similar to var = var % value
Examples:
$ echo "var=10;var" | bc Output: 10
$ echo "var=10;var^=2;var" | bc Output: 100
GENERAL PURPOSE COMMANDS CONTD…
How to store the result of complete operation in variable?
Example: $ x=`echo "var=500;var%=7;var" | bc`
$ echo $x Output:3
Explanation: Stores the result of 500 modulo 7 i.e. remainder of
500/7 in variable x and then display variable x as $x.
Increment Operators
There are 2 kinds of increment operators:
Decrement Operators
There are 2 kinds of decrement operators:
Mathematical Functions
The built-in math functions supported are :
Output: m is greater
GENERAL PURPOSE COMMANDS CONTD…
Iterative statements
bc command supports the for loop and while loop for doing
iterations.
Syntax:
while(condition)
{
statements.....
}
GENERAL PURPOSE COMMANDS CONTD…
Examples:
$ echo "for(i=1; i<=5; i++) {i;}" | bc
Output:
1
2
3
4
5
$ echo "i=1;while(i<=5) {i; i+=1}" | bc
Output:
1
2
3
4
5
GENERAL PURPOSE COMMANDS CONTD…
script
script command in Unix is used to make typescript or record all
the terminal activities. After executing the script command it
starts recording everything printed on the screen including the
inputs and outputs until exit.
who:
who command is used to find out the information about logged
in users
whoami : your user name
stty :
shows the settings of teletype
PROCESS BASICS
Whenever you issue a command in Unix, it creates, or starts, a
new process. When you tried out the ls command to list the
directory contents, you started a process. A process, in simple
terms, is an instance of a running program.
Background Processes
A background process runs without being connected to your
keyboard. If the background process requires any keyboard
input, it waits.
GENERAL PURPOSE COMMANDS CONTD…
The advantage of running a process in the background is that
you can run other commands; you do not have to wait until it
completes to start another!
ch01-2.doc ch02-1.doc
GENERAL PURPOSE COMMANDS CONTD…
Listing Running Processes
It is easy to see your own processes by running the ps (process
status) command as follows −
$ps
One of the most commonly used flags for ps is the -f ( f for full)
option, which provides more information
$ps -f
GENERAL PURPOSE COMMANDS CONTD…
Stopping Processes
Ending a process can be done in several different ways. Often,
from a console-based command, sending a CTRL + C keystroke
(the default interrupt character) will exit the command. This
works when the process is running in the foreground mode.
If a process is running in the background, you should get its Job
ID using the ps command. After that, you can use the kill
command to kill the process as follows −
$ps -f
$kill 6738
GENERAL PURPOSE COMMANDS CONTD…
Parent and Child Processes
Each unix process has two ID numbers assigned to it: The Process ID
(pid) and the Parent process ID (ppid). Each user process in the system
has a parent process.
Most of the commands that you run have the shell as their parent. Check
the ps -f example where this command listed both the process ID and the
parent process ID.
nice
To set the priority of a process we can use nice command
kill
To terminate a process, use kill command
kill 6567
6567 is the process id of the process
UNIX FILE SYSTEM
Unix file system is a logical method of organizing and
storing large amounts of information in a way that makes it
easy to manage. A file is a smallest unit in which the
information is stored. Unix file system has several important
features. All data in Unix is organized into files. All files are
organized into directories. These directories are organized into a
tree-like structure called the file system.
touch :
The touch command is a standard command used in
UNIX/Linux operating system which is used to create, change
and modify timestamps of a file. Basically, there are two
different commands to create a file in the Linux system which
is as follows:
touch command: It is used to create a file without any content.
The file created using touch command is empty. This command
can be used when the user doesn’t have data to store at the time
of file creation.
Syntax:
touch file_name
CONTD…
cp:
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.
$ cp Src_file Dest_file
CONTD…
rm:
rm stands for remove here. rm command is used to remove
objects such as files, directories, symbolic links and so on from
the file system like UNIX. To be more precise, rm removes
references to objects from the filesystem, where those objects
might have had multiple references (for example, a file with
two different names). By default, it does not remove
directories.
This command normally works silently and you should be very
careful while running rm command because once you delete
the files then you are not able to recover the contents of files
and directories.
$ rm a.txt
CONTD…
mv :
mv stands for move. mv is used to move one or more files or
directories from one place to another in file system like UNIX.
It has two distinct functions:
(i) It rename a file or folder. $ mv a.txt b.txt
less :
Less command is linux utility which can be used to read
contents of text file one page(one screen) per time. It has faster
access because if file is large, it don’t access complete file, but
access it page by page.
For example, if it’s a large file and you are reading it using any
text editor, then the complete file will be loaded to main
memory, but less command don’t load entire file, but load it
part by part, which makes it faster.
less filename
CONTD…
lp :
lp command arranges for the files specified by the Files parameter and their
associated information (called a request) to be printed by a line printer. If you do
not specify a value for the Files parameter, the lp command accepts standard
input. The file name - (dash) represents standard input and can be specified on
the command line in addition to files. The lp command sends the requests in the
order specified. If the job is submitted to a local print queue, the lp command
displays the following to standard output: Job number is: nnn where nnn is the
assigned job number. To suppress the job number use the -s flag.
To print the /etc/motd file on printer lp0 attached to device dlp0, enter:
$ lp /etc/motd
To print 30 copies of the /etc/motd file using a copy of the file, and to notify the user that the job is
completed using mail, enter:
$ lp -c -m -n30 -dlp0:lpd0 /etc/motd
CONTD…
cmp :
cmp command in Linux/UNIX is used to compare the two files
byte by byte and helps you to find out whether the two files are
identical or not.
When cmp is used for comparison between two files, it reports
the location of the first mismatch to the screen if difference is
found and if no difference is found i.e the files compared are
identical.
cmp displays no message and simply returns the prompt if the
the files compared are identical.
$ cmp file1.txt file2.txt
CONTD…
diff :
diff stands for difference. This command is used to display the
differences in the files by comparing the files line by line. Unlike
its fellow members, cmp and comm, it tells us which lines in one
file have is to be changed to make the two files identical.
The options -1, -2, and -3 suppress printing of the corresponding columns.
Unlike some other comparison utilities, 'comm' has an exit status that does
not depend on the result of the comparison. Upon normal completion 'comm'
produces an exit code of zero. If there is an error it exits with nonzero status.
CONTD…
OPTIONS
-1 suppress lines unique to file1.
-2 suppress lines unique to file2.
-3 suppress lines that appear in both files.
EXAMPLES
Show the lines unique to words.txt
$ cat words.txt
Apple
Banana
Orange
India
US
Canada
$ cat countries.txt
India
US
Canada
CONTD…
$ comm -23 < (sort words.txt | uniq) < (sort countries.txt | uniq)
Apple
Banana
Orange
$ comm -12 < (sort words.txt | uniq) < (sort countries.txt | uniq)
India
US
Canada
CONTD…
gzip :
gzip command compresses files. Each single file is compressed
into a single file. The compressed file consists of a GNU zip
header and deflated data.
If given a file as an argument, gzip compresses the file, adds a
“.gz” suffix, and deletes the original file. With no arguments,
gzip compresses the standard input and writes the compressed
file to standard output.
$ gzip mytext.txt
zip :
ZIP is a compression and file packaging utility for Unix. Each file is stored in
single .zip {.zip-filename} file with the extension .zip.
zip is used to compress the files to reduce file size and also used as file package
utility. zip is available in many operating systems like unix, linux, windows etc.
If you have a limited bandwidth between two servers and want to transfer the
files faster, then zip the files and transfer.
The zip program puts one or more compressed files into a single zip archive,
along with information about the files (name, path, date, time of last
modification, protection, and check information to verify file integrity). An entire
directory structure can be packed into a zip archive with a single command.
Compression ratios of 2:1 to 3:1 are common for text files. zip has one
compression method (deflation) and can also store files without compression. zip
automatically chooses the better of the two for each file to be compressed.
The program is useful for packaging a set of files for distribution; for archiving
files; and for saving disk space by temporarily compressing unused files or
directories.
Difference between Gzip and zip command in Unix and when to use which
command
ZIP and GZIP are two very popular methods of compressing files, in order to save
space, or to reduce the amount of time needed to transmit the files across the network,
or internet.
In general, GZIP is much better compared to ZIP, in terms of compression, especially
when compressing a huge number of files.
The common practice with GZIP, is to archive all the files into a single tarball before
compression. In ZIP files, the individual files are compressed and then added to the
archive.
When you want to pull a single file from a ZIP, it is simply extracted, then
decompressed. With GZIP, the whole file needs to be decompressed before you can
extract the file you want from the archive.
When pulling a 1MB file from a 10GB archive, it is quite clear that it would take a lot
longer in GZIP, than in ZIP.
GZIP’s disadvantage in how it operates, is also responsible for GZIP’s advantage.
Since the compression algorithm in GZIP compresses one large file instead of
multiple smaller ones, it can take advantage of the redundancy in the files to reduce
the file size even further.
If you archive and compress 10 identical files with ZIP and GZIP, the ZIP file would
be over 10 times bigger than the resulting GZIP file.
gunzip :
gunzip takes a list of files on its command line and replaces each file whose
name ends with .gz, -gz, .z, -z, _z or .Z and which begins with the correct magic
number with an uncompressed file without the original extension. gunzip also
recognizes the special extensions .tgz and .taz as shorthands
for .tar.gz and .tar.Z respectively. When compressing, gzip uses
the .tgz extension if necessary instead of truncating a file with a .tar extension.
unzip :
unzip will list, test, or extract files from a ZIP archive,
commonly found on MS-DOS systems. The default behavior
(with no options) is to extract into the current directory (and
subdirectories below it) all files from the specified ZIP archive.
A companion program, zip(1L), creates ZIP archives; both
programs are compatible with archives created by
PKWARE’s PKZIP and PKUNZIP for MS-DOS, but in many
cases the program options or default behaviors differ.
CONTD…
tar :
The Linux ‘tar’ stands for tape archive, is used to create
Archive and extract the Archive files. tar command in Linux is
one of the important command which provides archiving
functionality in Linux. We can use Linux tar command to create
compressed or uncompressed Archive files and also maintain
and modify them.
END OF UNIT-1
k
CONTD…
k
CONTD…
k
CONTD…
k
F
k
F
k
F
k
F
k
F
k
F
k
F
k
F
k
F
k
F
k
F
k
F
k