Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Introduction to LINUX/UNIX Commands and UNIX-Shell Basic Operators

Basic Commands
Linux Command Description
The date command tells the system to print the date and time:
date $ date
Sat Jul 20 14:42:56 EDT 2002
cal Displays calendar
show who is logged on
who Usage: who [OPTION]
eg. who , who b, who q
Display text on the screen. Mostly useful when writing shell scripts. For
example: echo “Hello World”
echo
1. echo filename : New file creation
2. echo msg>file1 : file1 will be created whose content is msg
bc Basic calculator

Commands for Navigating the Linux File systems

Linux Command DOS Command Description

“Print Working Directory”. Shows the


pwd cd
current location in the directory tree.

“Change Directory”. When typed all by


cd cd, chdir itself, it returns you to your home
directory.
Change into the specified directory
cd directory cd directory name.
Example: cd /usr/src/linux
“~” is an alias for your home directory.
It can be used as a shortcut to your
cd ~
“home”, or other directories relative to
your home.
Move up one directory. For example, if
you are in /home/vic and you type “cd
cd .. cd..
..”, you will end
up in /home.
Return to previous directory. An easy
cd - way to get back to your previous
location!

List all files in the current directory, in


ls dir /w
column format.

List the files in the specified directory.


ls directory dir directory
Example: ls /var/log

List files in “long” format, one file per


line. This also shows you additional info
about the file, such as ownership,
permissions, date, and size.
The first field could be
- for File, d for Directory, l for Link
The second, third, fourth fields
Those are permissions that means read,
write and execute, and comes in three
ls –l dir different fields that belongs to the
permission the:
• second: The owner has over the
file
• third: The group has over the file
• fourth: Everybody else has over
the file
The fifth field
This field specifies the number of links
or directories inside this directory.

2 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


The sixth field is the user
The user that owns the file, or directory
The seventh field is the group
The group that file belongs to, and any
user in that group will have the
permissions given in the third field over
that file.
The eighth field
The size in bytes
The ninth field
The date of last modification
The tenth field
The name of the file

List all files, including “hidden” files.


Hidden files are those files that begin
ls –a dir /a with a “.”, e.g. The
.bash_history file in your home
directory.

A “long” list of “directory”, but


instead of showing the directory
contents, show the directory's detailed
information. For example, compare
ls –ld
the output of the following two
commands:
ls -l /usr/bin
ls -ld /usr/bin

3 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


List all files whose names begin with
ls /usr/bin/d* dir d*.* the letter “d” in the /usr/bin
directory.

Piping
The pipe character, “|”, is used to chain two or more commands together. The output of the
first command is “piped” into the next program, and if there is a second pipe, the output is sent
to the third program, etc. For example:
ls -la /usr/bin | less
In this example, we run the command “ls -la /usr/bin”, which gives us a long listing of all of
the files in /usr/bin. Because the output of this command is typically very long, we pipe the
output to a program called “less”, which displays the output for us one screen at a time.

Redirecting Program Output to Files


There are times when it is useful to save the output of a command to a file, instead of displaying
it to the screen. For example, if we want to create a file that lists all of the MP3 files in a
directory, we can do something like this, using the “>” redirection character:
ls -l /home/vic/MP3/*.mp3 > mp3files.txt
A similar command can be written so that instead of creating a new file called mp3files.txt, we
can append to the end of the original file:
ls -l /home/vic/extraMP3s/*.mp3 >> mp3files.txt

Working With Files and Directories


Linux Command DOS Command Description
Creates a blank file
touch edit
eg. touch sample
Find out what kind of file it is.
file For example, “file /bin/ls” tells us that it is a
Linux executable file.
Display the contents of a text file on the screen.
For example: cat mp3files.txt would display the
cat type
file we created in the previous section.
1. cat > filename : Creating file (Stop entering

4 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


data by Ctrl+d
2. cat file1>>file2 : Content of file1 appended to
file2
Display the first few lines of a text file.
head
Example: head /etc/services
Display the last few lines of a text file.
tail
Example: tail /etc/services
Display the last few lines of a text file, and then
output appended data as the file grows (very
tail –f
useful for following log files!).
Example: tail -f /var/log/messages
Copies a file from one location to another.
Example: cp mp3files.txt /tmp
(copies the mp3files.txt file to the /tmp directory)
cp copy 1. cp file1 file2 : Contents of file1 copied to file2
2. cp file1 file2 dir1 : Copy both file1 and file2
into dir1
3. cp –r dir1 dir2 : Copy all files of dir1 to dir2
Moves a file to a new location, or renames it.
For example: mv mp3files.txt /tmp (copy the file
to /tmp, and delete it from the original location)
mv rename, ren, move 1. mv dir1 dir2 : Rename dir1 as dir2
2. mv data1 /home/abc/pgm/data1 : data1
directory is moved to /home/abc/pgm
directory
Delete a file. Example: rm /tmp/mp3files.txt
1. rm file1 file2 : Remove file1 & file2 from
current directory
rm del
2. rm –r dir1 : Removes dir1 directory along
with all its sub-directories
3. rm –i file1 : Interactively removes file1

5 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


mkdir md Make Directory. Example: mkdir /tmp/myfiles/
Remove Directory.
rmdir rd, rmdir
Example: rmdir /tmp/myfiles/
Lists the contents of directories in a tree-like
tree
format.

Finding Things
The following commands are used to find files. “ls” is good for finding files if you already know
approximately where they are, but sometimes you need more powerful tools such as these:

Linux Command Description


Shows the full path of shell commands found in your path. For
example, if you want to know exactly where the “grep” command is
which located on the file system, you can type “which grep”. The output
should be something
like: /bin/grep
Locates the program, source code, and manual page for a command (if
all information is available). For example, to find out where “ls” and
whereis its man
page are, type: “whereis ls” The output will look something like:
ls: /bin/ls /usr/share/man/man1/ls.1.gz
A quick way to search for files anywhere on the file system. For
example, you can find all files and directories that contain the name
locate
“mozilla” by typing:
locate mozilla
A very powerful command, but sometimes tricky to use. It can be
used to search for files matching certain patterns, as well as many
other types of searches. A simple example is:
find
find . -name \*mp3
This example starts searching in the current directory “.” and all
subdirectories, looking for files with “mp3” at the end of their names.

6 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


Informational Commands
Linux Command Explanation
ps Lists currently running process (programs).
w Show who is logged on and what they are doing
id Print your user-id and group id's
df Report file system disk space usage (“Disk Free” is how I remember it)
Disk Usage in a particular directory. “du -s” provides a summary for
du
the current directory.
Displays CPU processes in a full-screen GUI. A great way to see the
top
activity on your computer in real-time. Type “Q” to quit.
free Displays amount of free and used memory in the system.
cat /proc/cpuinfo Displays information about your CPU.
cat
Display lots of information about current memory usage.
/proc/meminfo
Prints system information to the screen (kernel version, machine type,
uname –a
etc.)
Print the number of newlines, words, and bytes in files
Usage: wc [OPTION]... [FILE]...
eg. $ wc filename
2 19 103 filename
Here is the detail of all the four columns:
1. First Column: represents total number of lines in the file.
wc 2. Second Column: represents total number of words in the file.
3. Third Column: represents total number of bytes in the file. This is
actual size of the file.
4. Fourth Column: represents file name.
wc –l file1 : Counts no. of lines in file1
wc –w file1 : Counts no. of words in file1
wc –c file1 : Counts no. characters in file1

7 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


System Administration
Linux Command Description
“Switch User”. Allows you to switch to another user's account
temporarily. The default account to switch to is the root/superuser
account. Examples:
su
su - Switch the root account
su - - Switch to root, and log in with root's environment
su larry - Switch to Larry's account
Change permissions on a file
To add specific permission use chmod +
–To add write permission to all users use:
chmod a+w filename
–To add read permission to only the users in your group use:
chmod g+r filename
–To make a file executable and runnable by any user
chmod a+x myfile
To remove specific permission use chmod –
Add and remove permissions can be combined in a single step
– chmod u+x,g+r,o-rwx filename
chmod
Instead of using alphabets u, g, o for user, group, and others we can
use numbers to specify file permissions
rwx = 111 = 7
rw- = 110 = 6
r-x = 101 = 5
r-- = 100 = 4
-wx = 011 = 3
-w- = 010 = 2
--x = 001 = 1
--- = 000 = 0
Note that: chmod go+rx filename = chmod 755 filename

8 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


change file owner and group
chown Usage: chown [OPTION]... OWNER[:[GROUP]] FILE...
eg. chown remo myfile.txt

update a user’s authentication tokens(s)


passwd Usage: passwd [OPTION]
eg. Passwd

uptime Used to find the duration for which the system has been running

Other Utilities
Linux Command Description
clear Clear the screen
Display a file, or program output one page at a time. Examples:
more more mp3files.txt
ls -la | more
An improved replacement for the “more” command. Allows you to
less
scroll backwards as well as forwards.
Search for a pattern in a file or program output. For example, to find
out which TCP network port is used by the “nfs” service, you can do
this:
grep
grep “nfs” /etc/services
This looks for any line that contains the string “nfs” in the file
“/etc/services” and displays only those lines.
Print a file or program output. Examples:
lpr lpr mp3files.txt - Print the mp3files.txt file
ls -la | lpr - Print the output of the “ls -la” command.
sort Sort a file or program output. Example: sort mp3files.txt

9 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


Special Characters
Character Description
Escape character. If you want to reference a special character, you must
\ “escape” it with a backslash first.
Example: touch /tmp/filename\*
Directory separator, used to separate a string of directory names.
/
Example: /usr/src/linux
Current directory. Can also “hide” files when it is the first character in a
.
filename.
.. Parent directory
~ User's home directory
Represents 0 or more characters in a filename, or by itself, all files in a
directory.
*
Example: pic*2002 can represent the files pic2002, picJanuary2002,
picFeb292002, etc.
Represents a single character in a filename.
? Example: hello?.txt can represent hello1.txt, helloz.txt, but not
hello22.txt
Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.
[] Example: hello[0-2].txt represents the names hello0.txt,
hello1.txt, and hello2.txt
“Pipe”. Redirect the output of one command into another command.
|
Example: ls | more

Redirect output of a command into a new file. If the file already exists,
> over-write it.
Example: ls > myfiles.txt

Redirect the output of a command onto the end of an existing file.


>>
Example: echo “Mary 555-1234” >> phonenumbers.txt
Redirect a file as input to a program.
<
Example: more < phonenumbers.txt

10 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


Command separator. Allows you to execute multiple commands on a
; single line.
Example: cd /var/log ; less messages
Command separator as above, but only runs the second command if the
&& first one finished without errors.
Example: cd /var/logs && less messages
Execute a command in the background, and immediately get your shell
& back.
Example: find / -name core > /tmp/corefiles.txt &

Basic Operators
There are various operators supported by each shell. There are following operators which are
used mostly −
Arithmetic Operators
Relational Operators
Boolean Operators
String Operators
Arithmetic Operators
There are following arithmetic operators supported by Bourne Shell. Assume variable a holds
10 and variable b holds 20 then –
Operator Description Example

Addition - Adds values on either side of the


+ `expr a + b` will give 30
operator

Subtraction - Subtracts right hand operand


- `expr a − b` will give -10
from left hand operand

Multiplication - Multiplies values on either side


* `expr a\*b` will give 200
of the operator

Division - Divides left hand operand by right


/ `expr b/a` will give 2
hand operand

11 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


Modulus - Divides left hand operand by right
% `expr ba` will give 0
hand operand and returns remainder
Assignment - Assign right operand in left a=$b would assign value of b
=
operand into a
Equality - Compares two numbers, if both are
== [ a == b ] would return false.
same then returns true.
Not Equality - Compares two numbers, if both
!= [ a! = b ] would return true.
are different then returns true.

Relational Operators
Operator Description Example
Checks if the values of two operands are equal
-eq [ a − eqb ] is not true.
or not, if yes then condition becomes true.
Checks if the values of two operands are equal
-ne or not, if values are not equal then condition [ a − neb ] is true.
becomes true.
Checks if the value of left operand is greater
-gt than the value of right operand, if yes then [ a − gtb ] is not true.
condition becomes true.
Checks if the value of left operand is less than
-lt the value of right operand, if yes then condition [ a − ltb ] is true.
becomes true.
Checks if the value of left operand is greater
-ge than or equal to the value of right operand, if [ a − geb ] is not true.
yes then condition becomes true.
Checks if the value of left operand is less than
-le or equal to the value of right operand, if yes [ a − leb ] is true.
then condition becomes true.

12 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET


Boolean Operators
Operator Description Example
This is logical negation. This inverts a true
! [ ! false ] is true.
condition into false and vice versa.

This is logical OR. If one of the operands is true [ a − lt20 − ob -gt 100 ] is
-o
then condition would be true. true.

This is logical AND. If both the operands are


[ a − lt20 − ab -gt 100 ] is
-a true then condition would be true otherwise it
false.
would be false.

String Operators
There are following string operators supported by Bourne Shell. Assume variable a holds "abc"
and variable b holds "efg" then −
Operator Description Example
Checks if the values of two operands are equal
= [ a = b ] is not true.
or not, if yes then condition becomes true.
Checks if the values of two operands are equal
!= or not, if values are not equal then condition [ a! = b ] is true.
becomes true.

Checks if the given string operand size is zero.


-z [ -z $a ] is not true.
If it is zero length then it returns true.

Checks if the given string operand size is


-n nonzero. If it is non-zero length then it returns [ -z $a ] is not false.
true.

Check if str is not the empty string. If it is


str [ $a ] is not false.
empty then it returns false.

13 Prepared By Dr. A. Ghosal, Asst. Prof, IT Dept, STCET

You might also like