Linux Commands
Linux Commands
1
Unix Commands
clear command clears your screen if this is possible,
including its scroll back buffer.
It ignores any command-line parameters that may be present.
2
Unix Commands
Example:-
3
Unix Commands
Example:
5
Unix Commands
Field Explanation:
If first character is – then it is normal file
If it is d then it is directory
Field 1 – File Permissions: Next 9 character specifies the files
permission. Each 3 characters refers to the read, write, execute
permissions for user, group and world in this example, rwxr-xr-x
indicates read-write-execute permission for user, read-execute
permission for group, and read-execute permission for others.
Field 2 – Number of links: Second field specifies the number
of links for that file. In this example, 1 indicates only one link to
this file.
Field 3 – Owner: Third field specifies owner of the file. In this
example, this file is owned by username “dietstaff”.
Field 4 – Group: Fourth field specifies the group of the file. In
this example, this file belongs to “dietstaff” group.
Field 5 – Size: Fifth field specifies the size of file. In this
example, ’4096′ indicates the file size.
6
Unix Commands
Field 6 – Last modified date & time: Sixth field specifies the
date and time of the last modification of the file. In this
example, ‘Jul 7 08:57′ specifies the last modification time of the
file.
Field 7 – File or directory name: The last field is the name of
the file or directory. In this example, the file name is c1. c.
9) who :- who command can list the names of users currently logged
in, their terminal, the
time
7
Unix Commands
they have been logged in, and the name of the host from which they
have logged in.
Syntax: - who [options] [file]
Description: -
Optio Use
n
-b Prints time of last system boot
-H Print column headings above the output
-a Display all details of current logged in user
Prints only the usernames and the user count/total no of
-q
users logged in
Example: -
8
3140702 – Operating System | Unix
Commands
Description
:-
Optio Use
n
-m Set permission mode
-p No error if existing, make parent directories
as needed
-v Print a message for each created directory
Example: -
7
3140702 – Operating System | Unix
Commands
Description
: - bc is a language that supports arbitrary precision numbers with
interactive execution of statements.
bc starts by processing code from all the files listed on the
command line in the order listed. After all files have been
processed, bc reads from the standard input. All code is
executed as it is read.
Optio Use
n
-q To avoid bc welcome message
-l To include math library
functionalities
Example: -
8
3140702 – Operating System | Unix
Commands
Example:-
15) tty :- Print the file name of the terminal connected to standard input.
Syntax: - tty
Description: -
tty writes the name of the terminal that is connected to
standard input onto standard output.
Command is very simple and needs no arguments.
Example: -
9
3140702 – Operating System | Unix
Commands
Without arguments, it writes the settings of certain modes to
standard output.
Example: -
10
3140702 – Operating System | Unix
Commands
Optio Use
n
-i interactive - ask before overwrite
-f force copy by removing the destination file if
needed
-v print informative messages
-l link files instead of copy
-s follow symbolic links
-n no file overwrite
-u update - copy when source is newer than
destination
-R copy directories recursively
Example:-
11
3140702 – Operating System | Unix
Commands
12
3140702 – Operating System | Unix
Commands
Optio Use
n
-d Delete an empty directory
-r Remove directories and their contents recursively
-f Ignore non-existent files, and never prompt before
removing
-i Prompt before every removal
Example: -
14
3140702 – Operating System | Unix
Commands
22) cut :- cut command is used to cut out selected fields of each line of
a file. The cut command uses delimiters to determine where to split
fields.
Syntax :- cut [options] filename
Description: -
Optio Use
n
-c The list following -c specifies character positions
-d The character following -d is the field delimiter
-f Select only these fields on each line
-b Select only the bytes from each line as specified in
LIST
15
3140702 – Operating System | Unix
Commands
Example:
-
da
23) paste:- paste command is used to paste the content from one
file to another file. It is also used to set column format for each
line.
Syntax: - paste [option] file
Description: -
Paste prints lines consisting of sequentially corresponding lines
of each specified file. In the output the original lines are
separated by TABs. The output line is terminated with a
newline.
Optio Use
n
-d Specify of a list of delimiters
-s Paste one file at a time instead of in parallel
15
3140702 – Operating System | Unix
Commands
Example:
-
Option Use
-c Clear screen before displaying
-n Specify how many lines are printed in the screen for a
given file
+n Starts up the file from the given number
-s Doesn’t display extra blank lines
16
3140702 – Operating System | Unix
Commands
Example:
-
25) cmp :- It compares two files and tells you which line numbers are
different.
Syntax: - cmp [options...] file1 file2
Description: -
If a difference is found, it reports the byte and line number
where the first difference is found.
If no differences are found, by default, cmp returns no output.
Option Use
-b Print differing bytes
-i Skip a particular number of initial bytes from both the files
-n Compare at most LIMIT bytes
-l Print byte position and byte value for all differing bytes
17
3140702 – Operating System | Unix
Commands
Example:
-
1 Lines only in
file1. 2 Lines only
in file2. 3 Lines
in both files.
Option Use
-1 suppress lines unique to FILE1
-2 suppress lines unique to FILE2
-3 suppress lines that appear in both files
--check-order check that the input is correctly sorted, even if
all input
lines are pairable
--no check- do not check that the input is correctly sorted
order
18
3140702 – Operating System | Unix
Commands
Example:
-
In the above output we can see that first column contains two
lines unique to the first file and second column contains three
lines unique to the second file and the third column contains
two lines common to both the files. Comm. Can produce the
single column output using 3 options -1,-2 or -3. To drop a
particular column, simply use its column number as a prefix.
27) diff :- It is used to find differences between two files.
Syntax: - diff [options...] fileone filetwo
Description: -
Diff is the third command that can be used to display file
differences. Unlike its fellow members, cmp and comm, it tells
us which lines in one file have is to be changed to make the two
files identical.
Optio Use
n
-b Ignore any changes which only change the
amount of whitespace (such as spaces or tabs)
-B Ignore blank lines when calculating differences
-i Ignore changes in case. consider upper- and lower-case
letters
equivalent
19
3140702 – Operating System | Unix
Commands
Example
:-
File
#
Permission
0 none
1 execute only
2 write only
3 write and
execute
4 read only
Prof. Maulik Trivedi, CE Department | 3140702 – Operating System (2019-20) 20
3140702 – Operating System | Unix
Commands
5 read and
execute
6 read and write
set all
7
permissions
Example :
29) chown :- Command for system V that changes the owner of a file.
Syntax: - chown [options] newowner filename/directoryname
Example: -
30) chgrp :- chgrp command is used to change the group of the file
or directory. This is an admin command. Root user only can change
the group of the file or directory.
Syntax: - chgrp [options] newgroup filename/directoryname
21
3140702 – Operating System | Unix
Commands
Example: -
file :- file command tells you if the object you are looking at is
31 a file or a directory.
) Syntax: - file [options] directoryname/filename
Description: -
File command is used to determine the type of file, especially
of an ordinary file. We can use it with one or more filenames as
arguments. For example we can use file command to check the
type of the os1 file that we have created.
Optio Use
n
To view the mime type of a file rather than the human
-i
readable
format
Example: -
32) finger :- finger command displays the user's login name, real
name, terminal name and write status (as a ''*'' after the terminal
name if write permission is denied), idle time, login time, office
location and office phone number.
Syntax: - finger [username]
22
3140702 – Operating System | Unix
Commands
Description: -
Optio Use
n
-i Force long output format
-m Match arguments only on user name (not first or last
name)
Example: -
23
3140702 – Operating System | Unix
Commands
Example: -
Optio Use
n
-s send the specified signal to the process
-l list all the available signals.
-9 Force to kill a process.
Example: -
24
3140702 – Operating System | Unix
Commands
Syntax: - ln [options] existingfile(or directory)name newfile(or
directory)name
Description: -
What Is A Link?
A link is an entry in your file system which connects a filename
to the actual bytes of data on the disk. More than one filename
can "link" to the same data. Here's an example. Let's create a
file named file1.txt:
$ echo "This is a file." > file1.txt
This command echoes the string "This is a file". Normally this
would simply echo to our terminal, but the > operator redirects
the string's text to a file, in this case file1.txt
When this file was created, the operating system wrote the
bytes to a location on the disk and also linked that data to a
filename, file1.txt so that we can refer to the file in commands
and arguments.
If you rename the file, the contents of the file are not altered;
only the information that points to it.
The filename and the file's data are two separate entities.
File1.txt
“This is a file””
File1.txt File2.txt
“This is a file”
25
3140702 – Operating System | Unix
Commands
If we change the contents of the data pointed to by either one
of these files, the other file's contents are changed as well. Let's
append a line to one of them using the >>operator:
$ echo "Hello Linux" >> file1.txt
Now let's look at the contents of file1.txt:
$ cat
file1.txt
This is a file
Hello Linux
Now let's look at the second file, the one we created with the link
command.
$ cat
file2.txt
This is a file
Hello Linux
ln, by default, creates a hard link just like link does. So this ln
command:
$ ln file1.txt file2.txt
It is the same as the following link command. Because, both
commands create a hard link named file2.txt which links to the
data offile1.txt.
$ link file1.txt file2.txt
However, we can also use ln to create symbolic links with the -s
option. So the command:
$ ln -s file1.txt file2.txt
It will create a symbolic link to file1.txt named file2.txt. In
contrast to our hard link example, here's an illustration to help
you visualize our symbolic link:
File1.txt File2.txt
“This is a file”
You should also be aware that, unlike hard links, removing the
file (or directory) that a symlink(symbolic linkl) points to will
break the link. So if we create file1.txt:
$ echo "This is a file." > file1.txt
Now, create a symbolic link to it:
26
3140702 – Operating System | Unix
Commands
$ ln -s file1.txt file2.txt
27
3140702 – Operating System | Unix
Commands
we can cat either one of these to see the contents:
$ cat file1.txt
This is a file.
$ cat file2.txt
This is a file.
But, if we remove file1.txt:
$ rm file1.txt
we can no longer access the data it contained with our symlink:
$ cat file2.txt
cat: file2.txt: No such file or directory
Optio Use
n
-s Makes it so that it creates a symbolic link
-f If the destination file or files already exist,
overwrite
them
-i Prompt the user before overwriting destination
files
37) head :- head command is used to display the first ten lines of a
file, and also specifies how many lines to display.
Syntax: - head [options] filename
Description: -
Head command displays the top of the file. When used without
an option, it displays the first ten lines of the file.
Option Use
-n To specify how many lines you want to display
-n The number option-argument must be a decimal
number integer whose
sign affects the location in the file, measured in lines
-c The number option-argument must be a decimal
number integer whose
sign affects the location in the file, measured in bytes
28
3140702 – Operating System | Unix
Commands
Example: -
38) tail :- 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 :- tail [options] filename
Description: -
Option Use
The number option-argument must be a decimal
-c
number integer whose sign affects the location in the file,
measured in bytes
-n The number option-argument must be a decimal
numb integer whose sign affects the location in the file,
er measured in lines
Example : -
40) find :- Finds one or more files assuming that you know their approximate
path.
Syntax :- find [options] path
30
3140702 – Operating System | Unix
Commands
Description: -
Find is one of the powerful utility of Unix (or Linux) used for
searching the files in a directory hierarchy
Option Use
-name Search for files that are specified by ‘filename’
filename
31
3140702 – Operating System | Unix
Commands
-newer Search for files that were modified/created after
filename ‘filename’
-user Search for files owned by user name or ID ‘name’
filename
-size +N/-N Search for files of ‘N’ blocks; ‘N’ followed by ‘c’
can be
used to measure size in characters
-empty Search for empty files and directories
-perm octal Search for the file if permission is ‘octal’
Example:-
Optio Use
n
-c Precede each output line with a count of the number of times
the line
occurred in the input
-d Suppress the writing of lines that are not repeated in the input
-D Print all duplicate lines
32
3140702 – Operating System | Unix
Commands
-f Avoid comparing first N fields
-i Ignore case when comparing
-s Avoid comparing first N characters
33
3140702 – Operating System | Unix
Commands
-u Prints only unique lines
Example:-
34
3140702 – Operating System | Unix
Commands
Optio Use
n
-c Use the complement of SET1
-d Delete characters in SET1, do not translate
-s Replace each input sequence of a repeated character that
is listed in
SET1 with a single occurrence of that character
Example:-
35
Unix Commands
Example:
-
33
Unix Commands
Example:
-
grep :- It selects and prints the lines from a file which matches a given
45 string or pattern.
) Syntax: - grep [options] pattern [file]
Description: -
This command searches the specified input fully for a match
with the supplied pattern and displays it.
While forming the patterns to be searched we can use shell
match characters, or regular expressions.
grep stands for globally search for regular expression and print out.
Optio Use
n
-i Ignore case distinctions
-v Invert the sense of matching, to select non-
matching lines.
-w Select only those lines containing matches that form
whole
words
-x Select only matches that exactly match the whole
line.
-c Print a count of matching lines for each input file.
-n Display the matched lines and their line numbers
-o Print only the matched parts of a matching line
34
Unix Commands
Example:
-
46) pwd :-Displaying your current directory name (Print working directory).
Syntax: -pwd [options]
Description: -
At the time of logging in user is placed in the specific directory
of the file system. You can move around from one directory to
another, but any point of time, you are located in only one
directory. This directory is known as your current directory. pwd
command tells your current directory.
Example:-
47) wc :- Word Count (wc) command counts and displays the number of
lines, words, character and number of bytes enclosed in a file.
Syntax: - wc [options] [filename]
Description: -
This command counts lines, words and characters depending on
the options used. It takes one or more filenames as its
arguments and displays four- columnar output. For example
let’s read our os1 file. And we use wc command with that
filename.
35
Unix Commands
Optio Use
n
-l Print the newline counts
-w Print the word counts
-c Print the byte counts
-L Print the length of the longest
line
Example: -
36
Unix Commands
38
Unix Commands
39
Unix Commands
40