Unix Commands
Unix Commands
Unix Commands
who command can list the names of users currently logged in, their terminal, the time
they have been logged in, and the name of the host from which they have logged in.
SYNTAX:
The Syntax is
who [options] [file]
OPTIONS:
Print the username of the invoking user, The 'am' and 'i' must be space
am i
separated.
-b Prints time of last system boot.
-d print dead processes.
-H Print column headings above the output.
Include idle time as HOURS:MINUTES. An idle time of . indicates
-i
activity within the last minute.
-m Same as who am i.
-q Prints only the usernames and the user count/total no of users logged in.
-T,-w Include user's message status in the output.
EXAMPLE:
1. who -uH
Output:
This sample output was produced at 11 a.m. The "." indiacates activity within the
last minute.
2. who am i
date COMMAND:
date command prints the date and time.
SYNTAX:
The Syntax is
date [options] [+format] [date]
OPTIONS:
Slowly adjust the time by sss.fff seconds (fff represents fractions of a
-a second). This adjustment can be positive or negative.Only system
admin/ super user can adjust the time.
-s date Sets the time and date to the value specfied in the datestring. The datestr
-string may contain the month names, timezones, 'am', 'pm', etc.
Display (or set) the date in Greenwich Mean Time (GMT-universal
-u
time).
Format:
%a Abbreviated weekday(Tue).
%A Full weekday(Tuesday).
%b Abbreviated month name(Jan).
%B Full month name(January).
%c Country-specific date and time format..
%D Date in the format %m/%d/%y.
%j Julian day of year (001-366).
%n Insert a new line.
%p String to indicate a.m. or p.m.
%T Time in the format %H:%M:%S.
%t Tab space.
%V Week number in year (01-52); start week on Monday.
EXAMPLE:
1. date command
date
The above command will print Wed Jul 23 10:52:34 IST 2008
The above command will print Wed Oct 08 11:37:23 IST 200
head COMMAND:
head command is used to display the first ten lines of a file, and also specifies how
many lines to display.
SYNTAX:
The Syntax is
head [options] filename
OPTIONS:
1. head index.php
2. head -5 index.php
3. head -c 5 index.php
tail COMMAND:
tail command is used to display the last or bottom part of the file. By default it
displays last 10 lines of a file.
SYNTAX:
The Syntax is
tail [options] filename
OPTIONS:
1. tail index.php
2. tail -2 index.php
3. tail -n 5 index.php
4. tail -c 5 index.php
tar COMMAND:
tar command is used to create archive and extract the archive files.
SYNTAX:
The Syntax is
tar [options] [archive-file] [File or directory to be archived]
OPTIONS:
-c Creates Archive
-x Extract the archive
-f creates archive with give filename
-t displays or lists files in archived file
-u archives and adds to an existing archive file
-v Displays Verbose Information
-A Concatenates the archive files
EXAMPLE:
1. To archive a directory or file :
This command creates a tarfile called backup.tar which is the archive of /etc
directory.
Where,
backup.tar - Is a tar file created
/etc - Is a directory archived
Where,
/dev/fd0 - Is a floppy-disk name where the archive is stored
/home/user1/HGB - Is a directory archived
The above command will display the files and directories that arc
ls COMMAND:
ls command lists the files and directories under current working directory.
SYNTAX:
The Syntax is
ls [OPTIONS]... [FILE]
OPTIONS:
Lists all the files, directories and their mode, Number of links, owner of
-l
the file, file size, Modified date and time and filename.
-t Lists in order of last modification time.
-a Lists all entries including hidden files.
-d Lists directory files instead of contents.
-p Puts slash at the end of each directories.
-u List in order of last access time.
-i Display inode information.
-ltr List files order by date.
-lSr List files order by file size.
EXAMPLE:
ls /
ls -a
ls -i
7373073 book.gif
7373074 clock.gif
7373082 globe.gif
7373078 pencil.gif
7373080 child.gif
7373081 email.gif
7373076 indigo.gif
pwd COMMAND:
pwd - Print Working Directory. pwd command prints the full filename of the current
working directory.
SYNTAX:
The Syntax is
pwd [options]
OPTIONS:
pwd
If you are working in home directory then, pwd command displays the current
working directory as /home.
mkdir COMMAND:
mkdir command is used to create one or more directories.
SYNTAX:
The Syntax is
mkdir [options] directories
OPTIONS:
1. Create directory:
mkdir test
The above command is used to create the directory 'test' and set the read and write
permission.
rmdir COMMAND:
rmdir command is used to delete/remove a directory and its subdirectories.
SYNTAX:
The Syntax is
rmdir [options..] Directory
OPTIONS:
Allow users to remove the directory dirname and its parent directories
-p
which become empty.
EXAMPLE:
1. To delete/remove a directory
rmdir tmp
rmdir command will remove/delete the directory tmp if the directory is empty.
rm -ir tmp
This command recursively removes the contents of all subdirectories of the tmp
directory, prompting you regarding the removal of each file, and then removes the
tmp directory itself.
rmdir COMMAND:
rmdir command is used to delete/remove a directory and its subdirectories.
SYNTAX:
The Syntax is
rmdir [options..] Directory
OPTIONS:
Allow users to remove the directory dirname and its parent directories
-p
which become empty.
EXAMPLE:
1. To delete/remove a directory
rmdir tmp
rmdir command will remove/delete the directory tmp if the directory is empty.
rm -ir tmp
This command recursively removes the contents of all subdirectories of the tmp
directory, prompting you regarding the removal of each file, and then removes the
tmp directory itself.
cd COMMAND:
cd command is used to change the directory.
SYNTAX:
The Syntax is
cd [directory | ~ | ./ | ../ | - ]
OPTIONS:
1. cd linux-command
This command will take you to the sub-directory(linux-command) from its parent
directory.
2. cd ..
This will change to the parent-directory from the current working directory/sub-
directory.
3. cd ~
cat COMMAND:
cat linux command concatenates files and print it on the standard output.
SYNTAX:
The Syntax is
cat [OPTIONS] [FILE]...
OPTIONS:
-A Show all.
-b Omits line numbers for blank space in the output.
-e A $ character will be printed at the end of each line prior to a new line.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.
-s If the output has multiple empty lines it replaces it with one empty line.
-T Displays the tab characters in the output.
Non-printing characters (with the exception of tabs, new-lines and form-
-v
feeds) are printed visibly.
EXAMPLE:
This command creates a new file file1.txt. After typing into the file press
control+d (^d) simultaneously to end the file.
To append data into the same file use append operator >> to write into the file,
else the file will be overwritten (i.e., all of its contents will be erased).
3. To display a file:
cat file1.txt
The above cat command will concatenate the two files (file1.txt and file2.txt) and
it will display the output in the screen. Some times the output may not fit the
monitor screen. In such situation you can print those files in a new file or display
the file using less command.
In the above example the output is redirected to new file file3.txt. The cat
command will create new file file3.txt and store the concatenated output into
file3.txt.
cp COMMAND:
cp command copy files from one location to another. If the destination is an existing
file, then the file is overwritten; if the destination is an existing directory, the file is
copied into the directory (the directory is not overwritten).
SYNTAX:
The Syntax is
cp [OPTIONS]... SOURCE DEST
cp [OPTIONS]... SOURCE... DIRECTORY
cp [OPTIONS]... --target-directory=DIRECTORY SOURCE...
OPTIONS:
-a same as -dpR.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument.
if an existing destination file cannot be opened, remove it
-f
and try again.
-p same as --preserve=mode,ownership,timestamps.
preserve the specified attributes (default:
--
mode,ownership,timestamps) and security contexts, if
preserve[=ATTR_LIST]
possible additional attributes: links, all.
--no-
don't preserve the specified attribute.
preserve=ATTR_LIST
--parents append source path to DIRECTORY.
cp COMMAND:
cp command copy files from one location to another. If the destination is an existing
file, then the file is overwritten; if the destination is an existing directory, the file is
copied into the directory (the directory is not overwritten).
SYNTAX:
The Syntax is
cp [OPTIONS]... SOURCE DEST
cp [OPTIONS]... SOURCE... DIRECTORY
cp [OPTIONS]... --target-directory=DIRECTORY SOURCE...
OPTIONS:
-a same as -dpR.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument.
if an existing destination file cannot be opened, remove it
-f
and try again.
-p same as --preserve=mode,ownership,timestamps.
preserve the specified attributes (default:
--
mode,ownership,timestamps) and security contexts, if
preserve[=ATTR_LIST]
possible additional attributes: links, all.
--no-
don't preserve the specified attribute.
preserve=ATTR_LIST
--parents append source path to DIRECTORY.
EXAMPLE:
cp file1 file2
cp -b file1.php file2.php
cp -R scripts scripts1
The above cp command copy the folder and subfolders from scripts to scripts1.
mv COMMAND:
mv command which is short for move. It is used to move/rename file from one
directory to another. mv command is different from cp command as it completely
removes the file from the source and moves to the directory specified, where cp
command just copies the content from one file to another.
SYNTAX:
The Syntax is
mv [-f] [-i] oldname newname
OPTIONS:
mv file1.txt file2.txt
2. To move a directory
mv hscripts tmp
In the above line mv command moves all the files, directories and sub-directories
from hscripts folder/directory to tmp directory if the tmp directory already exists.
If there is no tmp directory it rename's the hscripts directory as tmp directory.
This command moves the files file1.txt from the current directory and file2.txt
from the tmp folder/directory to newdir.
ln COMMAND:
ln command is used to create link to a file (or) directory. It helps to provide soft link
for desired files. Inode will be different for source and destination.
SYNTAX:
The Syntax is
ln [options] existingfile(or directory)name newfile(or directory)name
OPTIONS:
Link files without questioning the user, even if the mode of target
-f
forbids writing. This is the default if the standard input is not a terminal.
-n Does not overwrite existing files.
-s Used to create soft links.
EXAMPLE:
1. ln -s file1.txt file2.txt
Creates a symbolic link to 'file1.txt' with the name of 'file2.txt'. Here inode for
'file1.txt' and 'file2.txt' will be different.
2. ln -s nimi nimi1
rm COMMAND:
rm linux command is used to remove/delete the file from the directory.
SYNTAX:
The Syntax is
rm [options..] [file | directory]
OPTIONS:
rm file1.txt
rm -ir tmp
This rm command recursively removes the contents of all subdirectories of the
tmp directory, prompting you regarding the removal of each file, and then
removes the tmp directory itself.
rm file1.txt file2.txt
echo COMMAND:
echo command prints the given input string to standard output.
SYNTAX:
The Syntax is
echo [options..] [string]
OPTIONS:
1. echo command
echo -e "hscripts\tHiox\tIndia"
find COMMAND:
find command finds one or more files assuming that you know their approximate
filenames.
SYNTAX:
The Syntax is
find path [options]
OPTIONS:
It search for the given file, in the current directory or any other
-name
subdirectory.
True if the file was accessed n days ago. The access time of
-atime n
directories in path is changed by find itself.
-ctime n True if the file's status was changed n days ago.
True if the file belongs to the group gname. If gname is numeric
-group gname and does not appear in the /etc/group file, it is taken as a group
ID.
-mtime n True if the file's data was modified n days ago.
True if the file is n blocks long (512 bytes per block). If n is
-size n[c]
followed by a c, the size is in bytes.
EXAMPLE:
The system would search for any file named 'cal.txt' in the current directory and
any subdirectory.
The system would search for any file named 'cal.txt' on the root and all
subdirectories from the root.
bc COMMAND:
bc command is used for command line calculator. It is similar to basic calculator. By
using which we can do basic mathematical calculations.
SYNTAX:
The Syntax is
bc [options]
OPTIONS:
Compile only. The output is dc commands that are sent to the standard
-c
output.
Define the math functions and initialize scale to 20, instead of the
-l
default zero.
Name of the file that contains the basic calculator commands to be
filename
calculated this is not a necessary command.
EXAMPLE:
1. bc
Output:
bc 1.06
Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
9*2
18
2. bc -l
Output:
bc 1.06
Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2
3
3. bc calc.txt
Output:
bc 1.06
Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3
'calc.txt' file have the following code:1+2. Get the input from file and displays the
output.
kill COMMAND:
kill command is used to kill the background process.
SYNTAX:
The Syntax is
kill [-s] [-l] %pid
OPTIONS:
Specify the signal to send. The signal may be given as a signal name or
-s
number.
Write all values of signal supported by the implementation, if no
-l
operand is given.
-pid Process id or job id.
-9 Force to kill a process.
EXAMPLE:
chmod COMMAND:
chmod command allows you to alter / Change access rights to files and directories.
File Permission is given for users,group and others as,
Permission
Symbolic Mode
SYNTAX:
The Syntax is
chmod [options] [MODE] FileName
File Permission
# File Permission
0 none
1 execute only
2 write only
3 write and execute
4 read only
5 read and execute
6 read and write
7 set all permissions
OPTIONS:
-c Displays names of only those files whose permissions are being changed
-f Suppress most error messages
-R Change files and directories recursively
-v Output version information and exit.
EXAMPLE:
ls -alt
This command is used to view your files with what permission they are.
2. To make a file readable and writable by the group and others.
mp COMMAND:
cmp linux command compares two files and tells you which line numbers are
different.
SYNTAX:
The Syntax is
cmp [options..] file1 file2
OPTIONS:
The above cmp command compares file1.php with file2.php and results as
follows.
The above cmp command compares file1.php with file2.php and results as
follows.
cal COMMAND:
cal command is used to display the calendar.
SYNTAX:
The Syntax is
cal [options] [month] [year]
OPTIONS:
1. cal
Output:
September 2008
Su Mo Tu We Th Fr Sa
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
2. cal -3 5 2008
Output:
clear COMMAND:
This command clears the terminal screen.
SYNTAX:
The Syntax is
clear
OPTIONS:
There is no options for clearscreen command.
EXAMPLE:
1. clear
alias c='clear'
c is the alias name for clear command.