Professional Documents
Culture Documents
Basic Unix Commands
Basic Unix Commands
Misc commands
man,banner,cal, calendar,clear,nohup, tty .
man ls will explain about the ls command and how you can use it.
man -k pattern command will search for the pattern in given command.
Banner command.
banner prints characters in a sort of ascii art poster, for example to print wait in big letters. I
will type
banner wait at unix command line or in my script. This is how it will look.
#
#
#
#
#
#
# ## #
## ##
#
#
##
#
#
#
#
#
#
#
#
#
######
#
#
#
#
#####
#
#
#
#
#
Cal command
cal command will print the calander on current month by default. If you want to print
calander of august of 1965. That's eightht month of 1965.
cal 8 1965 will print following results.
August 1965
S M Tu W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Clear command
clear command clears the screen and puts cursor at beginning of first line.
Calendar command
calendar command reads your calendar file and displays only lines with current day.
For example in your calendar file if you have this
12/20
1/15
1/20
On dec 20th the first line will be displayed. you can use this command with your crontab file
or in your login files.
Nohup command.
nohup command if added in front of any command will continue running the command or
process even if you shut down your terminal or close your session to machine. For exmaple,
if I want to run a job that takes lot of time and must be run from terminal and is called
update_entries_tonight .
nohup update_entries_tonight will run the job even if terminal is shut down in middle of this
job.
Tty command
Tty command will display your terminal. Syntax is
tty options
Options
-l will print the synchronous line number.
-s will return only the codes: 0 (a terminal), 1 (not a terminal), 2 (invalid options)
(good for scripts)
Pwd command.
pwd command will print your home directory on screen, pwd means print working directory.
/u0/ssb/sandeep
Ls command
ls command is most widely used command and it displays the contents of directory.
options
ls will list all the files in your home directory, this command has many options.
ls -l will list all the file names, permissions, group, etc in long format.
ls -a will list all the files including hidden files that start with . .
ls -lt will list all files names based on the time of creation, newer files bring first.
ls -Rwill lists all the files and files in the all the directories, recursively.
ls -R | more will list all the files and files in all the directories, one page at a time.
Mkdir command.
mkdir sandeep will create new directory, i.e. here sandeep directory is created.
Cd command.
cd sandeep will change directory from current directory to sandeep directory.
Use pwd to check your current directory and ls to see if sandeep directory is there or not.
You can then use cd sandeep to change the directory to this new directory.
Cat command
cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Head command.
head filename by default will display the first 10 lines of a file.
If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename and
so forth.
Tail command.
tail filename by default will display the last 10 lines of a file.
If you want last 50 lines then you can use tail -50 filename.
More command. more command will display a page at a time and then wait for input
which is spacebar. For example if you have a file which is 500 lines and you want to read it
all. So you can use
more filename
Wc command
wc command counts the characters, words or lines in a file depending upon the option.
Options
File command.
File command displays about the contents of a given file, whether it is a text (Ascii) or binary
file. To use it type
file filename. For example I have cal.txt which has ascii characters about calander of current
month and I have resume1.doc file which is a binariy file in microsoft word. I will get
file resume.doc
resume1.doc:
data
file cal.txt
cal.txt:
ascii text
Cp command.
cp command copies a file. If I want to copy a file named oldfile in a current directory to a file
named newfile in a current directory.
cp oldfile newfile
If I want to copy oldfile to other directory for example /tmp then
cp oldfile /tmp/newfile. Useful options available with cp are -p and -r . -p options preserves
the modification time and permissions, -r recursively copy a directory and its files,
duplicating the tree structure.
Rcp command.
rcp command will copy files between two unix systems and works just like cp command (-p
and -i options too).
For example you are on a unix system that is called Cheetah and want to copy a file which is
in current directory to a system that is called lion in /usr/john/ directory then you can use rcp
command
rcp filename lion:/usr/john
You will also need permissions between the two machines. For more infor type man rcp at
command line.
Mv command.
mv command is used to move a file from one directory to another directory or to rename a
file.
Some examples:
mv -f oldfile newfile will force the rename even if target file exists.
mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa directory.
Ln command.
Instead of copying you can also make links to existing files using ln command.
If you want to create a link to a file called coolfile in /usr/local/bin directory then you can
enter this command.
ln mycoolfile /usr/local/bin/coolfile
Some examples:
ln -s fileone filetwo will create a symbolic link and can exist across machines.
ln -n option will not overwrite existing files.
Rm command.
To delete files use rm command.
Options:
rm -r option will delete the entire directory as well as all the subdirectories, very
dangerous command.
Rmdir command.
rmdir command will remove directory or directories if a directory is empty.
Options:
rmdir -p will remove directories and any parent directories that are empty.
Diff command.
diff command will compare the two files and print out the differences between.
Here I have two ascii text files. fileone and file two.
Contents of fileone are
This
this
this
this
this
is
is
is
is
is
first file
second line
third line
different
as;lkdjf
not different
filetwo contains
This
this
this
this
this
is
is
is
is
is
first file
second line
third line
different
xxxxxxxas;lkdjf
not different
as;lkdjf
xxxxxxxas;lkdjf
Cmp command.
cmp command compares the two files. For exmaple I have two different files fileone and
filetwo.
cmp fileone filetwo will give me
fileone filetwo differ: char 80, line 4
Dircmp Command.
dircmp command compares two directories. If i have two directories in my home directory
named
dirone and dirtwo and each has 5-10 files in it. Then
dircmp dirone dirtwo will return this
Dec
9 16:06 1997
./cal.txt
./dohazaar.txt
./four.txt
./junk.txt
./test.txt
./fourth.txt
./rmt.txt
./te.txt
./third.txt
Grep Command
grep command is the most useful search command. You can use it to find processes running
on system, to find a pattern in a file, etc. It can be used to search one or more files to match
an expression.
It can also be used in conjunction with other commands as in this following example, output
of ps command is passed to grep command, here it means search all processes in system and
find the pattern sleep.
ps -ef | grep sleep will display all the sleep processes running in the system as follows.
ops
dxi
ops
ops
ops
dxi
ops
dxi
ops
ops
ssb
pjk
12964
12974
12941
12847
12894
13067
13046
12956
12965
12989
13069
27049
25853
15640
25688
25812
25834
27253
25761
13078
25737
25778
26758
3353
0
0
0
0
0
2
0
0
0
0
2
0
16:12:24
16:12:25
16:12:21
16:11:59
16:12:12
16:12:48
16:12:44
16:12:23
16:12:24
16:12:28
16:12:49
15:20:23
ttyAE/AAES
ttyAH/AAHP
ttyAE/AAEt
ttyAH/AAH6
ttyAE/AAEX
ttyAE/ABEY
ttyAE/AAE0
ttyAG/AAG+
ttyAE/AAEp
ttyAH/AAHv
ttyAH/AAHs
?
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
sleep 60
sleep 60
sleep 60
sleep 60
sleep 60
sleep 1
sleep 60
sleep 60
sleep 60
sleep 60
grep sleep
sleep 3600
Options:
other associated commands with grep are egrep and fgrep. egrep typically runs faster. for
more information type man egrep or man fgrep in your system.
Find command.
Find command is a extremely useful command. you can search for any file anywhere using
this command provided that file and directory you are searching has read write attributes set
to you ,your, group or all. Find descends directory tree beginning at each pathname and finds
the files that meet the specified conditions. Here are some examples.
Some Examples:
find $HOME -print will lists all files in your home directory.
find /work -name chapter1 -print will list all files named chapter1 in /work directory.
find / -type d -name 'man*' -print will list all manpage directories.
find / -size 0 -ok rm {} \; will remove all empty files on system.
conditions of find
-atime +n |-n| n will find files that were last accessed more than n or less than -n days
or n days.
-ctime +n or -n will find that were changed +n -n or n days ago.
-depth descend the directory structure, working on actual files first and then
directories. You can use it with cpio command.
-exec commad {} \; run the Unix command on each file matched by find. Very useful
condition.
-perm nnnfind files whole permission flags match octal number nnn.
-type c Find file whole type is c. C could be b or block, c Character special file, d
directory, p fifo or named pipe, l symbolic link, or f plain file.
Text processing
cut,paste, sort, uniq,awk,sed,vi.
Cut command.
cut command selects a list of columns or fields from one or more files.
Option -c is for columns and -f for fields. It is entered as
cut options [files]
for example if a file named testfile contains
this is firstline
this is secondline
this is thirdline
Examples:
cut -c1,4 testfile will print this to standard output (screen)
ts
ts
ts
It is printing columns 1 and 4 of this file which contains t and s (part of this).
Options:
Paste Command.
paste command merge the lines of one or more files into vertical columns separated by a tab.
for example if a file named testfile contains
this is firstline
this is testfile2
Options:
Sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a
file named testfile with these contents
zzz
aaa
1234
yuer
wer
qww
wwe
Then running
sort testfile
will give us output of
1234
aaa
qww
wer
wwe
yuer
zzz
Options:
-d ignores punctuation.
+m[-m] skips n fields before sorting, and sort upto field position m.
Uniq command.
uniq command removes duplicate adjacent lines from sorted file while sending one copy of
each second file.
Examples
sort names | uniq -d will show which lines appear more than once in names file.
Options:
Examples:
df -t | awk 'BEGIN {tot=0} $2 == "total" {tot=tot+$1} END {print (tot*512)/1000000}'
Will give total space in your system in megabytes.
Here the output of command df -t is being passed into awk which is counting the field 1 after
pattern "total" appears. Same way if you change $1 to $4 it will accumulate and display the
addition of field 4
which is used space.
for more information about awk and nawk command in your system enter man awk or man
nawk.
Sed command.
sed command launches a stream line editor which you can use at command line.
you can enter your sed commands in a file and then using -f option edit your text file. It
works as
sed [options] files
options:
bg
at command line will put this job in background, entering
fg
will put this job in foreground. Entering
jobs
at command line will show me all my concurrent jobs that are running.
Other common features
> will redirect output from standard out (screen) to file or printer or whatever
you like.
>> filename will append at the end of a file called filename.
| pipe output, or redirect output, good for joining commands, i.e. find
command with cpio, etc.
* will match any characters in a file or directories. junk* will match all files
with first 4 letters
() execute in subshell.
bg background execution.
Examples:
cd; ls execute one after another.
(date;who;pwd)> logifile will redirect all the output from three commands to a
filenamed logfile.
sort file | lp will first sort a file and then print it.
alias [options] [name[='command']] will let you create your own commands. i.e.
o
alias ll="ls -la" will execute `ls -la` command whenever ll is entered.
let i=i+1 will work as a counter with i incrementing each time this statement is
encountered.
function name {commands;} is the syntax of a function which can be called from
anywhere in program.
if condition1 then commands1 elif condition2 then commands2 ... ... ... else
commands3 fi
Csh or C shell
csh is second most used shell.
Echo command
echo command in shell programming.
Line command.
line command in shell programming.
Sleep command.
sleep command in shell programming.
Test Command.
test command in shell programming.
Communications
cu,ftp,login, rlogin,talk,telnet, vacation and write .
Cu command.
cu command is used for communications over a modem or direct line with another Unix
system.
Syntax is
cu options destination
Options
-bn process lines using n-bit characters (7 or 8).
-cname Search UUCP's device file and select local area network that matches name.
-d Prints diagnostics.
Destination
telno is the telephone number of the modem to connect to.
system is call the system known to uucp.
options
-d enable debugging.
-g disable filename globbing.
ftp hostname by default will connect you to the system, you must have a login id to be able to
transfer the files. Two types of files can be transferred, ASCII or Binary. bin at ftp> prompt
will set the transfer to binary. Practice FTP by ftping to nic.funet.fi loggin in as anomymous
with password being your e-mail address.
Login command.
login command invokes a login session to a Unix system, which then authenticates the login
to a system. System prompts you to enter userid and password.
Rlogin command.
rlogin command is used to log on to remote Unix systems, user must have permissions on
both systems as well as same userid, or an id defined in .rhosts file. Syntax is
rlogin options host
options
-8 will allow 8 bit data to pass, instead of 7-bit data.
-e c will let you use escape character c.
-l user will let you to login as user to remote host, instead of same as local host.
Talk command.
talk command is used to invoke talk program available on all unix system which lets two
users exchange information back and forth in real time. Syntax is
talk userid@hostname
Telnet command.
Telnet command invokes a telnet protocol which lets you log on to different unix, vms or any
machine connected over TCP/IP protocol, IPx protocol or otherwise. Syntax is
telnet hostname
Vacation command.
vacation command is used when you are out of office. It returns a mail message to sender
announcing that you are on vacation. to disable this feature, type mail -F " " .
syntax is
vacation options
Options
-d will append the date to the logfile.
-F user will forward mail to user when unable to send mail to mailfile.
-l logfile will record in the logfile the names of senders who received automatic reply.
Storage commands
compress uncompress, cpio,dump,pack, tar, mt.
Compress command.
Compress command compresses a file and returns the original file with .z extension, to
uncompress this filename.Z file use uncompress filename command. syntax for compress
command is
compress options files
Options
-bn limit the number of bits in coding to n.
-c write to standard output (do not change files).
Uncompress command.
Uncompress file uncompresses a file and return it to its original form.
syntax is
uncompress filename.Z this uncompresses the compressed file to its original name.
Options
-c write to standard output without changing files
Cpio command.
cpio command is useful to backup the file systems. It copy file archives in from or out to tape
or disk, or to another location on the local machine. Its syntax is
cpio flags [options]
Copy out a list of files whose name are given on standard output.
cpio -p
o
o
-R ID reassign file ownership and group information to the user's login ID.
-s swap bytes.
find . -name "*.old" -print | cpio -ocvB > /dev/rst8 will backup all *.old files to
a tape in /dev/rst8
cpio -icdv "save"" < /dev/rst8 will restore all files whose name contain "save"
find . -depth -print | cpio -padm /mydir will move a directory tree.
Options
0-9 This number is dump level. 0 option causes entire filesystem to be dumped.
b blocking factor taken into argument.
Pack command.
pack command compacts each file and combine them together into a filename.z file. The
original file is replaced. Pcat and unpack will restore packed files to their original form.
Syntax is
Pack options files
Options
- Print number of times each byte is used, relative frequency and byte code.
-f Force the pack even when disk space isn't saved.
Tar command.
tar command creates an archive of files into a single file.
Tar copies and restore files to a tape or any storage media. Synopsis of tar is
tar [options] [file]
Examples:
tar cvf /dev/rmt/0 /bin /usr/bin creates an archive of /bin and /usr/bin, and store on the tape
in /dev/rmt0.
tar tvf /dev/rmt0 will list the tape's content in a /dev/rmt0 drive.
tar cvf - 'find . -print' > backup.tar will creates an archive of current directory and store it in
file backup.tar.
Functions:
Options:
v print function letter (x for extraction or a for archive) and name of files.
Mt command
Mt command is used for tape and other device functions like rewinding, ejecting, etc. It give
commands to tape device rather than tape itself. Mt command is BSD command and is
seldom found in system V unix versions.
syntax is
mt [-t tapename] command [count]
Examples
o
System Status
at, chmod,chgrp, chown,crontab,date, df,du, env, finger, ps,ruptime, shutdwon,stty, who.
At command.
at command along with crontab command is used to schedule jobs.
at options time [ddate] [+increment] is syntax of at command.
for example if I have a script named usersloggedin which contains.
#!/bin/ksh
who | wc -l
echo "are total number of people logged in at this time."
and I want to run this script at 8:00 AM. So I will first type at 8:00 %lt;enter>
usersloggedin %lt;enter>
I will get following output at 8:00 AM
30
are total number of people logged in at this time.
Options:
-f file will execute commands in a file.
-m will send mail to user after job is completed.
-l will report all jobs that are scheduled and their jobnumbers.
Chmod command.
chmod command is used to change permissions on a file.
for example if I have a text file with calender in it called cal.txt.
initially when this file will be created the permissions for this file depends upon umask set in
your profile files. As you can see this file has 666 or -rw-rw-rw attributes.
ls -la cal.txt
-rw-rw-rw-
1 ssb
dxidev
135 Dec
3 16:14 cal.txt
In this line above I have -rw-rw-rw- meaning respectively that owner can read and write
file, member of the owner's group can read and write this file and anyone else connected
to this system can read and write this file., next ssb is owner of this file dxidev is the
group of this file, there are 135 bytes in this file, this file was created on December 3 at
time16:14 and at the end there is name of this file. Learn to read these permissions in
binary, like this for example Decimal 644 which is 110 100 100 in binary meand rw-r--r-- or
user can read,write this file, group can read only, everyone else can read only. Similarly, if
permissions are 755 or 111 101 101 that means rwxr-xr-x or user can read, write and execute,
group can read and execute, everyone else can read and execute. All directories have d in
front of permissions. So if you don't want anyone to see your files or to do anything with it
use chmod command and make permissions so that only you can read and write to that file,
i.e.
chmod 600 filename.
Chgrp command.
chgrp command is used to change the group of a file or directory.
You must own the file or be a superuser.
chgrp [options] newgroup files is syntax of chgrp.
Newgroup is either a group Id or a group name located in /etc/group .
Options:
-h will change the group on symbolic links.
-R recursively descend through directory changing group of all files and
subdirectories.
Chown command.
chown command to change ownership of a file or directory to one or more users.
Syntax is
chown options newowner files
Options
-h will change the owner on symbolic links.
-R will recursively descend through the directory, including subdirectories and
symbolic links.
Crontab command.
crontab command is used to schedule jobs. You must have permission to run this command
by unix Administrator. Jobs are scheduled in five numbers, as follows.
Minutes
0-59
Hour
Day of month
month
Day of week
0-23
1-31
1-12
0-6 (0 is sunday)
so for example you want to schedule a job which runs from script named backup_jobs in
/usr/local/bin directory on sunday (day 0) at 11.25 (22:25) on 15th of month. The entry in
crontab file will be. * represents all values.
25 22
15 * 0 /usr/local/bin/backup_jobs
Date command.
Date displays todays date, to use it type date at prompt.
Sun Dec
Df command.
df command displays information about mounted filesystems. It reports the number of free
disk blocks. Typically a Disk block is 512 bytes (or 1/2 Kilobyte).
syntax is
df options name
Options
-b will print only the number of free blocks.
-e will print only the number of free files.
-n will print only the file system name type, with no arguments it lists type of all
filesystems
Du command.
du command displays disk usage.
Env command.
env command displays all the variables.
Finger command.
finger command.
PS command
ps command is probably the most useful command for systems administrators. It reports
information on active processes.
ps options
options.
-a Lists all processes in system except processes not attached to terminals.
-e Lists all processes in system.
Ruptime command.
ruptime command tells the status of local networked machines.
ruptime options
options.
-a include user even if they've been idle for more than one hour.
-l sort by load average.
-t sort by uptime.
Shutdown command.
Shutdown command can only be executed by root. To gracefully bring down a system,
shutdown command is used.
options.
-gn use a grace-period of n seconds (default is 60).
-ik tell the init command to place system in a state k.
o
Stty command
stty command sets terminal input output options for the current terminal. without options stty
reports terminal settings.
stty options modes < device
options
-a report all options.
-g report current settings.
Modes
0 hang up phone.
n set terminal baud.
Who command
who command displays information about the current status of system.
who options file
Who as default prints login names of users currently logged in.
Options
-a use all options.
-b Report information about last reboot.
-H print headings.