0% found this document useful (0 votes)
21 views18 pages

LOS Unit 2-1

Uploaded by

hrajaa16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

LOS Unit 2-1

Uploaded by

hrajaa16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT 2.

Linux Commands and Linux file system


Internal Command
Internal commands are the commands that are executed directly by the shell. These
commands will not have a separate process. You can’t find these commands in PATH
directories in system because these are built in shell command. echo is an external
command and if you try to find its path it will show shell built in.

$ 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

What is the difference between internal and external commands?


Internal commands are commands that are already loaded in the system. They can be executed any time
and are independent. On the other hand, external commands are loaded when the user requests for them.
Internal commands don’t require a separate process to execute them. External commands will have an
individual process. Internal commands are a part of the shell while external commands require a Path. If
the files for the command are not present in the path, the external command won’t execute.

What is the difference between internal and external commands?


- The commands that are directly executed by the shell are known as internal commands. No separate
process is there to run these commands.
- The commands that are executed by the kernel are knows as external commands. Each command has its
unique process id.

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

echo [options..] [string]

OPTIONS:

-n do not output the trailing newline


-e enable interpretation of the backslash-
escaped characters listed below
-E disable interpretation of those
sequences in STRINGs
Without -E, the following sequences the character whose ASCII code is
are recognized and interpolated: NNN (octal)
\NNN
\a alert (BEL)
\\ backslash
\b backspace
\c suppress trailing newline
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
EXAMPLE:
echo command :

echo "hscripts Hiox India"

The above command will print as hscripts Hiox India

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"

The above command will print as hscripts Hiox India

➢ 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.

The syntax for the type command is as follows:

type [OPTIONS] FILE_NAME...

For example, to find the type of the wc command, you would type the following:

type wc

The output will be something like this:

wc is /usr/bin/wc

You can also provide more than one arguments to the type command:

type sleep head

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

mv [-f] [-i] oldname newname

mv <file name> <directory path>

OPTIONS:

-f This will not prompt before overwriting


(equivalent to --reply=yes). mv -f will
move the file(s) without prompting even if
it is writing over an existing target.
-i Prompts before overwriting another file.
EXAMPLE:
1. To Rename / Move a file: mv file1.txt file2.txt
This command renames file1.txt as 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.
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:

-f Remove all files in a directory without


prompting the user.
-i Interactive. With this option, rm prompts
for confirmation before removing any
files.
-r (or) -R Recursively remove directories and
subdirectories in the argument list. The
directory will be emptied of files and
removed. The user is normally prompted
for removal of any write-protected files
which the directory contains.
EXAMPLE:
1. To Remove / Delete a file:
rm file1.txt
Here rm command will remove/delete the file file1.txt.

2. To delete a directory tree:


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.

3. To remove more files at once


rm file1.txt file2.txt
rm command removes file1.txt and file2.txt files at the same time.

➢ 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:

1. cat [OPTION]... [FILE]..

To create a file, execute it as follows:


1. cat > <file name>
2. // Enter file content

Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as follows:

1. cat <file name>

Command Explanation

cat -b This is used to add line numbers to non-blank lines

cat -n This is used to add line numbers to all lines

cat -s This is used to squeeze blank lines into one line

cat –E Show $ at the end of line

➢ useradd Command

The useradd command is used to add or remove a user on a Linux server.

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

who [options] [file]


OPTIONS:

am i Print the username of the invoking user,


The 'am' and 'i' must be space separated.
-b Prints time of last system boot.
-d print dead processes.
-H Print column headings above the output.
-i Include idle time as HOURS:MINUTES.
An idle time of . indicates 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:
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

The logname command prints the user-name of the current user.

$ 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

Example 1: Printing “1234567890” in large letters.


Example 2: Printing “GeeksforGeeks” in large letters. There are two things:

• 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

1. To use tab space:


date +"Date is %D %t Time is %T"
The above command will remove space and print as Date is 07/23/08 Time is 10:52:34

2. To know the week number of the year,


date -V
The above command will print 30

3. To set the date,


date -s "10/08/2008 11:37:23"
The above command will print Wed Oct 08 11:37:23 IST 2008

➢ bc Command(in detail refer separate pdf)

bc is a simple yet powerful and arbitrary precision CLI calculator language which can be
used like this:

$ echo 20.05 + 15.00 | bc

➢ 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:

1. less <file name>

➢ 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:

ENTER key: To scroll down page by line.

Space bar: To move to the next page.


b key: To move to the previous page.

/ key: To search the string.

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

Clears the screen before printing the file .


2. more -3 index.php
Prints first three lines of the given file. Press Enter to display the file line by line.

➢ comm command
Select or reject lines common to two files.
Syntax
comm [-1] [-2] [-3 ] file1 file2

Suppress the output column of lines unique to


-1 file1.
-2 Suppress the output column of lines unique to
file2.
-3 Suppress the output column of lines duplicated
in file1 and file2.
file1 Name of the first file to compare.
file2 Name of the second file to compare.

Examples
comm myfile1.txt myfile2.txt
The above example would compare the two files myfile1.txt and myfile2.txt.

➢ tar command

The following command is used to zip files of .tar format.

syntax:

$ tar –cvf tar-filename source-folder-name

The following command is used to unzip files of .tar format.


syntax:

$ tar –xvf tar-file-name

➢ touch Command

The touch command is used to create empty files. We can create multiple empty files by
executing it once.

Syntax:

1. touch <file name>


2. touch <file1> <file2> ....

Output:

• Directory related commands: – pwd, cd, mkdir, rmdir,


➢ pwd COMMAND:

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

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.
-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

Backup of file1.php will be created with '~' symbol as file2.php~.


2. Copy folder and subfolders: cp -R scripts scripts1

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.

• Manipulating file commands: - cp, mv, rm


➢ cp Command

The cp command is used to copy a file or directory.

Syntax:

To copy in the same directory:

1. cp <existing file name> <new file name>

To copy in a different directory:


Output:

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

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:
1. To view your files with what permission they are:
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.
chmod 066 file1.txt
3. To allow everyone to read, write, and execute the file

chmod 777 file1.txt

➢ 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]]

-linecount | -l Number of lines in each piece. Defaults to 1000 lines.

-a Use suffixlength letters to form the suffix


suffixlength portion of the filenames of the split file. If -a is
not specified, the default suffix length is 2. If
the sum of the name operand and the
suffixlength option-argument would create a
filename exceeding NAME_MAX bytes, an
error will result; split will exit with a diagnostic
message
and no files will be created.

-b n Split a file into pieces n


bytes in size.
-b n k Split a file into pieces
n*1024 bytes in size.
-b n m Split a file into pieces
n*1048576 bytes in size.
File The path name of the
ordinary file to be split.
If no input file is given
or file is -, the standard
input will be used.
name The prefix to be used for
each of the files resulting
from the split operation.
If no name argument is
given, x will be used as
the prefix of the output
files. The combined
length of the basename
of prefix and
suffixlength cannot
exceed NAME_MAX
bytes; see OPTIONS.
Examples
split -b 22 newfile.txt new - would split the file "newfile.txt" into three separate files called
newaa, newab and newac each file the size of 22.
split -l 300 file.txt new - would split the file "file.txt" into files beginning with the name "new"
each containing 300 lines of text each

➢ cmp

The cmp command is used to perform byte-by-byte comparison of two files.

$ cmp file1 file2


file1 file2 differ: byte 1, line 1
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to
find out whether the two files are identical or not.
• When cmp is used for comparison between two files, it reports the location of the first
mismatch to the screen if difference is found and if no difference is found i.e the files
compared are identical.
• cmp displays no message and simply returns the prompt if the the files compared are
identical.
Syntax:
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

SKIP1 ,SKIP2 & OPTION are optional


and FILE1 & FILE2 refer to the filenames .
The syntax of cmp command is quite simple to understand. If we are comparing two files
then obviously we will need their names as arguments (i.e as FILE1 & FILE2 in syntax). In
addition to this, the optional SKIP1 and SKIP2 specify the number of bytes to skip at the
beginning of each file which is zero by default and OPTION refers to the options compatible
with this command about which we will discuss later on.
cmp Example : As explained that the cmp command reports the byte and line number if a
difference is found. Now let’s find out the same with the help of an example. Suppose there
are two files which you want to compare one is file1.txt and other is file2.txt :
$cmp file1.txt file2.txt
1. If the files are not identical : the output of the above command will be :
2. $cmp file1.txt file2.txt
3. file1.txt file2.txt differ: byte 9, line 2
4.
5. /*indicating that the first mismatch found in
two files at byte 20 in second line*/
6. If the files are identical : you will see something like this on your screen:
7. $cmp file1.txt file2.txt
8. $ _
/*indicating that the files are identical*/

➢ Diff

The diff command lets you compare two files line by line.

$ diff file1 file2


Refer above sections also

You might also like