UNIT 9 (Commands Set1)
UNIT 9 (Commands Set1)
Unix Commands
▪ Syntax :
cd [-Options] [Directory]
▪ Example :
Option Use
cd .. Change Current directory to parent directory
cd ~ Move to users home directory from anywhere
cd lab_1 Change from current working directory to lab_1
cd ../downloads If we are currently in /home/username/documents then
we would be placed in /home/username/downloads.
cd Command Example
cd .. Change Current directory to parent directory
3
cd Command Example
cd ~ Move to users home directory from anywhere
cd Command Example
cd lab_1 Change from current working directory to lab_1
5
cd Command Example
cd ../downloads If we are currently in /home/student/documents then
we would be placed in /home/student/downloads
6
ls Command
• List directory contents.
▪ Syntax :
ls [Options] [file|dir]
▪ Example :
Option Use
ls -l To show long listing information about the file/directory
ls -a List all files including hidden file starting with '.'
ls -r List in reverse order
ls -t Sort by time & date
ls -s Sort by file size
7
name of the file/dir
ls Command $ ls -l
specifies owner of the
/etc/hosts
Output:
file/dir
r
date and time modification
specifies the of the last of the file
re
g
sr
h
w
o
specifies group of the indicates the file/dir size in
8
file/dir bytes.
ls Command Example
ls -l To show long listing information about the file/directory
10
ls Command Example
ls -a List all files including hidden file starting with '.'
11
ls Command Example
ls -r List in reverse order
12
man Command
• It is the interface used to view the system's reference manuals.
▪ Syntax :
▪ Example
13
echo Command
• Display a line of text/string on standard output or a file.
▪ Syntax :
▪ Example :
Option Use
echo -n Do not output a trailing newline
echo -e Enable interpretation of backslash escape sequences
Option Use
\b It removes all the spaces in between the text
\n It creates new line from where it is used
\t It create horizontal tab spaces
14
cal Command
• Displays a simple, formatted calendar in your terminal.
▪ Syntax :
▪ Example :
Option Use
cal -1 Display single month output. (This is the default.)
cal -3 Display three months spanning the date.
17
date Command
• Print or set the system date and time.
▪ Syntax :
date [OPTION]... [+FORMAT]
▪ Example :
Option Use
date +%a The abbreviated weekday name (e.g., Sun)
date +%A The full weekday name (e.g., Sunday)
date +%b The abbreviated month name (e.g., Jan)
date +%B Locale's full month name (e.g., January)
date +%C The current century; like %Y, except omit last two digits
(e.g., 20)
date +%w day of week (0..6); 0 is Sunday
24
date Command
Option Use
date +%d Display the day of the month
date +%m Displays the month of year (01 to 12)
date +%y Displays last two digits of the year(00 to 99)
date +%Y Display four-digit year.
date +%T Display the time in 24 hour format as HH:MM:SS
date +%H Display the hour
date +%M Display the minute
date +%S Display the seconds
date +%V ISO week number, with Monday as first day of week
(01..53)
date +%P locale's equivalent of either AM or PM
25
26
27
31
date Command Example
date +%V ISO week number, with Monday as first day of week
(01..53)
32
clear Command
• Clear the terminal screen.
• If you take a detailed look after running the clear command, you'll
find that it doesn't really clear the terminal. The tool just shifts the
text upwards, out of the viewable area.
▪ Syntax :
clear
34
35
cat Command
• It is used to create, display and concatenate file contents.
▪ Syntax :
▪ Example :
Option Use
cat -b Omits line numbers for blank space in the output
cat -E Displays a $ (dollar sign) at the end of each line
cat -n Line numbers for all the output lines
cat -s Suppress repeated empty output lines
cat -T Displays the tab characters as ^I in the output
36
cat Command
▪ Example :
$ cat > file1.txt
• It creates file1.txt and allow us to insert content for this file.
• After inserting content you can use ctrl+c to exit the file.
$cat file.txt > newfile.txt
• Read the contents of file.txt and write them to newfile.txt,
overwriting anything newfile.txt previously contained. If
newfile.txt does not exist, it will be created.
$cat file.txt >> newfile.txt
• Read the contents of file.txt and append them to the end of
newfile.txt. If newfile.txt does not exist, it will be created.
37
cat Command
▪ Example :
cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the
result in the terminal.
cat file1.txt file2.txt > combinedfile.txt
• It will concatenate the contents of file1.txt and file2.txt and write
them to a new file combinedfile.txt using the (>) operator. • If the
combinedfile.txt file doesn’t exist the command will create it.
Otherwise it will overwrite the file.
38
40
4
1
▪ Syntax :
pwd [-OPTION]
▪ Example :
45
who Command
• It display the users that are currently logged into your Unix
computer system.
▪ Syntax :
who [-options] [filename]
▪ Example :
Optio Use
n who Display the time of the last system boot
-b
who -H Print a line of column headings
who -q Displays all login names, and a count of all logged-on users
who -a Display all details of current logged in user
46
48
49
whoami Command
• This command prints the username associated with the current
effective user ID.
▪ Syntax :
whoami [-OPTION]
▪ Example :
Option Use
whoami --help Display a help message, and exit
whoami --version Display version information, and exit
51
▪ Syntax :
uname [-OPTION]
▪ Example :
Option Use
uname -s Print the kernel name
uname -n Print the network node hostname
uname -v Print the kernel version
uname -m Print the machine hardware name
uname -o Print the operating system
54
56
uname Command Example
uname -v Print the kernel version
57
59
passwd Command
• The passwd command is used to change the password of a user
account.
▪ Syntax :
passwd [-options] [username]
▪ Example :
mkdir Command
• This command is used to make Directories.
▪ Syntax :
mkdir [-OPTION] DIRECTORY
▪ Example :
Option Use
mkdir -v Print a message for each created directory
mkdir -p No error if existing, make parent directories as needed
mkdir -m To control the permissions of new directories
61
64
rmdir Command
• This command removes empty directories from your filesystem.
▪ Syntax :
▪ Example :
Option Use
Remove directory and its ancestors… e.g.,‘rmdir -p a/b/c’ is
rmdir -p
similar to ‘rmdir a/b/c a/b a’
65
cp(copy) Command
• This command is used to copy files and directories.
▪ Syntax :
cp [option] source destination/directory
▪ Example :
Option Use
cp -i Interactive - ask before overwrite
cp -f Force copy by removing the destination file if needed
cp -n Do not overwrite an existing file
cp -u Update - copy when source is newer than destination
cp -s Make symbolic links instead of copying
cp -R Copy directories recursively
cp -v Print informative messages
67
cp Command Example
68
cp Command Example
cp -i Interactive - ask before overwrite
69
cp Command Example
cp -v Print informative messages
70
cp Command Example
cp -s Make symbolic links instead of copying
71
mv(move) Command
• mv command is used to move files and directories.
▪ Syntax :
▪ Example :
Optio Use
n mv - Interactive prompt before overwrite
i
mv -f Force move by overwriting destination file without prompt
mv -n Never overwrite any existing file
mv -u Update - move when source is newer than destination
mv -v Print informative messages
72
mv Command Example
73
rm(remove) Command
• The ‘rm’ command is used to delete files and directories.
▪ Syntax :
rm [-OPTION] Filename
▪ Example :
Optio Use
n rm - Prompt before every removal
i
rm -d Delete a empty directory
rm -r Remove directories and their contents recursively
rm -f To remove the file forcefully
74
rm Command Example
rm -i Prompt before every removal
75
cut Command
• The cut command extracts a given number of characters or
columns from a file.
▪ Syntax :
cut [-options] [file]
▪ Example :
Optio Use
n cut - Select only the characters from each line as specified in LIST
c
cut -b Select only the bytes from each line as specified in LIST
cut -f Cuts the input file using list of field. The default field to be used
TAB. The default behavior can be overwritten by use of -d option
cut -d Specifies a delimiter to by used as a field. Default field is TAB and
this option overwrites this default behavior
76
paste Command
• The paste command displays the corresponding lines of multiple
files side-by-side.
▪ Syntax :
paste [-options] [file]
▪ Example :
Optio Use
n Reuse characters from LIST instead of tabs
paste -
d
paste -s Paste one file at a time instead of in parallel
80