LOS Unit 2-1
LOS Unit 2-1
$ type echo
echo is a shell builtin
when you run echo the system will not search in PATH location, it will execute it from
shell builtin commands that are stored in some separate file.
External Command
External commands are the commands that are executed by the kernel. These commands
will have a process id running for it. External Command existed in any PATH directory
like /bin, /usr/bin etc. like ls, sed, mv. These commands having independent existence in
/bin, /usr/bin, or any other directory. External commands paths can be found using type
command
$ type ls
ls is /bin/ls
2.1 Internal & external commands in Linux: Internal commands in Linux echo, type, etc.,
➢ echo COMMAND:
echo command prints the given input string to standard output.
SYNTAX:
The Syntax is
OPTIONS:
1. To use backspace:
echo -e "hscripts \bHiox \bIndia"
The above command will remove space and print as hscriptsHioxIndia
2. To use tab space in echo command
echo -e "hscripts\tHiox\tIndia"
➢ type command: The type command is used to display information about the command type. It will
show you how a given command would be interpreted if typed on the command line.
type is a shell builtin in Bash and other shells like Zsh and Ksh. Its behavior may be slightly different
from shell to shell. We will cover the Bash builtin version of type.
For example, to find the type of the wc command, you would type the following:
type wc
wc is /usr/bin/wc
You can also provide more than one arguments to the type command:
The output will include information about both sleep and head commands:
sleep is /bin/sleep
head is /usr/bin/head
External commands in Linux, ls, mv, rm, cat, useradd etc
➢ ls COMMAND:
ls command lists the files and directories under current working directory.
SYNTAX:
The Syntax is
ls [OPTIONS]... [FILE]
OPTIONS: -l Lists all the files, directories and their
mode, Number of links, owner of 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: Display root directory contents:
ls / lists the contents of root directory.
1. Display hidden files and directories:
ls -a
lists all entries including hidden files and directories.
2. Display inode information: ls -i
7373073 book.gif
7373074 clock.gif
7373082 globe.gif
➢ 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.
The mv command is used to move a file or a directory form one location to another location.
SYNTAX:
The Syntax is
OPTIONS:
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.
3. To Move multiple files/More files into another directory mv file1.txt tmp/file2.txt newdir
This command moves the files file1.txt from the current directory and file2.txt from the tmp folder/directory
to newdir.
➢ rm COMMAND:
rm linux command is used to remove/delete the file from the directory.
SYNTAX:
The Syntax is rm [options..] [file | directory]
OPTIONS:
➢ cat command
This command can read, modify or concatenate text files. It also displays file contents.
The cat command is a multi-purpose utility in the Linux system. It can be used to create a file, display
content of the file, copy the content of one file to another file, and more.
syntax:
Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as follows:
Command Explanation
➢ useradd Command
Syntax:
1. useradd username
Output:
2.2 Command line commands – who, log name, banner, cal, date, bc, man, info etc.
Linux provides a CLI (Command Line Interface) to communicate with the OS. Here are the most basic
of the Linux Commands.
➢ who COMMAND:
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
Output:
NAME LINE TIME IDLE PID COMMENT
hiox ttyp3 Jul 10 11:08 . 4578
This sample output was produced at 11 a.m.
The "." indiacates activity within the last minute.
2. who am i
who am i command prints the user name.
➢ Logname command
$ logname
himanshu
➢ banner command
This command in linux is used to print the ASCII character string in large letter to standard output.
Syntax:
banner text
• First, all the letter will be displayed in Capital letters in standard output.
• Second, only “GEEKSFORGE” will be printed as banner has a default capacity of 10 characters
in a word. After that, you have to give space which is shown in further examples.
➢ cal COMMAND:
cal command is used to display the calendar.
SYNTAX:
The Syntax is
cal [options] [month] [year]
OPTIONS:
-1 Displays single month as
output.
-3 Displays
prev/current/next month
output.
-s Displays sunday as the
first day of the week.
-m Displays Monday as the
first day of the week.
-j Displays Julian dates
(days one-based,
numbered from January
1).
-y Displays a calendar for
the current year.
EXAMPLE:
1. cal
cal command displays the current month calendar.
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:
April 2008 May 2008 June 2008
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
12345 1231234567
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
Here the cal command displays the calendar of April, May and June month of year 2008.
➢ date COMMAND:
date command prints the date and time.
SYNTAX:
The Syntax is
date [options] [+format] [date]
OPTIONS:
-a Slowly adjust the time by sss.fff seconds (fff represents
fractions of a second). This adjustment can be positive or
negative. Only system admin/ super user
can adjust the time.
- date - string Sets the time and date to the value specfied in the datestring.
The datestr may contain the month names, timezones, 'am',
'pm', etc.
-u Display (or set) the date in Greenwich Mean Time (GMT-
universal 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: date command
date
The above command will print
Wed Jul 23 10:52:34 IST 2008
bc is a simple yet powerful and arbitrary precision CLI calculator language which can be
used like this:
➢ man Command
man command is used to view the on-line reference manual pages for commands/programs
like so.
Syntax:
$ man du
$ man df
man command in Linux is used to display the user manual of any command that we can run
on
the terminal. It provides a detailed view of the command which includes NAME, SYNOPSIS,
DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES,
VERSIONS, EXAMPLES,
AUTHORS.
Every manual is divided into the following sections:
• Executable programs or shell commands
• System calls (functions provided by the kernel)
• Library calls (functions within program libraries
• Games
• Special files (usually found in /dev)
• File formats and conventions eg /etc/passwd
• Miscellaneous (including macro packages and conventions),
• System administration commands (usually only for root)
• Kernel routines [Non standard]
Syntax :
$man [OPTION]... [COMMAND NAME]...
Options and Examples
1. No Option: It displays the whole manual of the command.
Syntax :
$ man [COMMAND NAME]
2. Section-num: Since a manual is divided into multiple sections so this option is used to
display
only a specific section of a manual.
Syntax :
$ man [SECTION-NUM] [COMMAND NAME]
2.3 Files & directory commands: – cat, less, more, ls, comm, diff,tar
Cat:- refer above topic 2.1
➢ less Command
The less command is similar to the more command. It also includes some extra features such
as 'adjustment in width and height of the terminal.' Comparatively, the more command cuts
the output in the width of the terminal.
Syntax:
➢ more COMMAND:
more command is used to display text in the terminal screen. It allows only backward movement.
The more command is quite similar to the cat command, as it is used to display the file
content in the same way that the cat command does. The only difference between both
commands is that, in case of larger files, the more command displays screenful output at a
time.
In more command, the following keys are used to scroll the page:
SYNTAX:
The Syntax is
more [options] filename
OPTIONS:
-c Clear screen before displaying.
-e Exit immediately after writing the last line
of the last file in the argument list.
-n Specify how many lines are printed in the
screen for a given file.
+n Starts up the file from the given number.
EXAMPLE:
1. more -c index.php
➢ comm command
Select or reject lines common to two files.
Syntax
comm [-1] [-2] [-3 ] file1 file2
Examples
comm myfile1.txt myfile2.txt
The above example would compare the two files myfile1.txt and myfile2.txt.
➢ tar command
syntax:
➢ touch Command
The touch command is used to create empty files. We can create multiple empty files by
executing it once.
Syntax:
Output:
pwd - Print Working Directory. pwd command prints the full filename of the current working
directory.
SYNTAX:
The Syntax is
pwd [options]
OPTIONS: -P The pathname printed will not
contain symbolic links.
-L The pathname printed may
contain symbolic links.
EXAMPLE:
1. Displays the current working directory.
pwd
If you are working in home directory then, pwd command displays the current working directory
as /home.
➢ cd COMMAND:
cd command is used to change the directory.
SYNTAX:
The Syntax is
cd [directory | ~ | ./ | ../ | - ]
OPTIONS:
-L Use the physical
directory structure.
-P Forces symbolic
links.
EXAMPLE:
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 ~
This command will move to the user's home directory which is "/home/username".
➢ 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
OPTIONS:
-a same as -dpR.
--backup[=CONTROL] make a backup of each existing destination
file
-b like --backup but does not accept an
argument.
-f if an existing destination file cannot be
opened, remove it and try again.
-p same as --
preserve=mode,ownership,timestamps.
- preserve[=ATTR_LIST] preserve the specified attributes (default:
mode,ownership,timestamps) and security
contexts, if possible additional attributes:
links, all.
--no- preserve=ATTR_LIST don't preserve the specified attribute.
--parents append source path to DIRECTORY.
EXAMPLE:
Copy two files: cp file1 file2
The above cp command copies the content of file1.php to file2.php.
1. To backup the copied file: cp -b file1.php file2.php
The above cp command copy the folder and subfolders from scripts to scripts1.
➢ mkdir COMMAND:
mkdir command is used to create one or more directories.
SYNTAX:
The Syntax is
mkdir [options] directories
OPTIONS:
-m Set the access mode for the
new directories.
-p Create intervening parent
directories if they don't exist.
-v Print help message for each
directory created.
EXAMPLE:
1. Create directory:
mkdir test
The above command is used to create the directory 'test'.
2. Create directory and set permissions:
mkdir -m 666 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: -p Allow users to remove the directory
dirname and its parent directories 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.
2. To delete a directory tree:
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.
Syntax:
2.4 File link commands:- chmod,umask, file, type, wc, split, cmp, diff.
➢ 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,
Read r
Write w
Execute x
User
Group
Others
Permission 000
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
➢ wc Command
The wc command is used to count the lines, words, and characters in a file.
Syntax:
1. wc <file name>
Output:
➢ split Command
Split a file into pieces.
Syntax
split [-linecount | -l linecount ] [ -a suffixlength ] [file [name] ]
split -b n [k | m] [ -a suffixlength ] [ file [name]]
➢ cmp
➢ Diff
The diff command lets you compare two files line by line.