Command
Command
command
1
Index
2
3. Working with file Contents
3
4.8) last command 36
Display full login and logout time
Display specific username
Display reboot information
Display specific pts
4.9) ps command 39
List running process in full
List all running process
List process with specific username
List process with specific command
Listing process in hierarchy
Show root running process 43
4.10) Shutdown command
Shutting down system
Rebooting the system
Power-off or halt-off the system
Power-off the system
Printing a message during shutdown
Halt-off the system
Cancel shutdown
Sending warning and disabling logins
4.12) env command 46
4.12) du command 47
4.13) shred command 47
To delete file
4
5.11) Group command 55
groupadd command
groups command
usermod command
groupmod command
groupdel command
5
8. Linux Networking command
9. Extra Command
10. References 81
6
1) Working with Directories
1.2) cd command:
The "cd" stands for 'change directory' and this command is used to change the current directory i.e.;
the directory in which the user is currently working.
7
cd .. command (Brings you to the parent directory of current directory)
To go to the parent directory (the one just above your current directory in the directory tree)
8
1.3) ls command:
The ls are the list command in Linux. It will show the full list or content of your directory.
ls –a command
In Linux, hidden files start with .(dot) symbol and they are not visible in the regular directory. The (ls -a)
command will enlist the whole list of the current directory including the hidden files.
ls –l command
Many times you will be using options with ls to display the contents of the directory in different formats
or to display different parts of the directory. Typing just ls gives you a list of files in the directory. Typing
ls -l gives the list in a long list format
9
ls –lh command
This command will show you the file sizes in human readable format. Size of the file is very difficult to
read when displayed in terms of byte. The (ls -lh) command will give you the data in terms of Mb, Gb,
Tb, etc.
ls –lhS command
If you want to display your files in descending order (highest at the top) according to their size, then you
can use (ls -lhS) command.
ls –r command
It is used to print the list in reverse order.
10
ls –R command
It will display the content of the sub-directories also.
ls –lX command
It will group the files with same extensions together in the list.
11
ls –lt command
It will sort the list by displaying recently modified filed at top.
ls ~ command
It gives the contents of home directory.
ls ../ command
It gives the contents of parent directory.
12
1.4) mkdir command:
The mkdir stands for 'make directory'. With the help of mkdir command, you can create a new
directory wherever you want in your system.
If you will not provide a path then by default your file will be created in your current directory only.
If you want to create your directory somewhere else, then provide the path of your destination
directory and your file will be created there.
mkdir –p command
Using mkdir –p command you can also create multiple directories simultaneously.
13
1.5) rmdir command:
This command is used to remove a directory. But will not be able to remove a directory including a
sub-directory. It means, a directory has to be empty to be removed.
14
2) Working with Files
touch –r command
To update time of one file with reference to the other file.
15
touch –t command
To create a file by specifying the time.
2.3) rm command:
The 'rm' means remove. This command is used to remove a file. The command line doesn't have a
recycle bin or trash unlike other GUI's to recover the files. Hence, be very much careful while using
this command. Once you have deleted a file, it is removed permanently.
rm –i command
Remove a file interactively. To prevent yourself from accidentally removing a file, you can type rm -i.
16
rm –rf command
Remove a directory forcefully. By default, rm -r will not remove non-empty directories. The rm -rf
statement is famous because it will erase anything (providing that you have the permissions to do so).
When you are logged on as root, be very careful with rm -rf (the f means force and the r means recursive)
since being root implies that permissions don't apply to you. You can literally erase your entire file
system by accident.
2.4) cp command:
'cp' means copy. 'cp' command is used to copy a file or a directory.
cp –r command
To copy a directory along with its sub directories. (The -r option forces recursive copying of all files in all
subdirectories)
17
cp –i command
To prevent cp from overwriting existing files, use the -i (for interactive) option.
2.5) mv command:
Linux mv command is used to move existing file or directory from one location to another. It is also
used to rename a file or directory.
If you want to rename a single directory or file then 'mv' option will be better to use.
mv –i command
mv -i will ask permission to overwrite an existing file.
18
3) Working with File Contents
head –n command
The 'head -n' option displays specified number of lines.
head –c command
The 'head -c' command display the first n bytes of file
19
3.2) tail command:
The 'tail' command displays the last lines of a file. Its main purpose is to read the error message.
By default, it will also display the last ten lines of a file.
20
To Append the content of a file
The ‘cat’ command with double greater than sign (>>) append something in your already existing file.
cat –n command
The 'cat -n' option displays line numbers in front of each line in a file.
21
cat –b command
The 'cat -b' option add line number to non-blank lines.
cat –e command
The 'cat-e' option displays '$' sign at the end of every line.
Marker command
The 'cat << EOF ' option displays end marker at the end of a file. This is called here directive and file
content will be saved at the given end marker.
File can be also saved with the help of 'ctrl + d ' keys. It works same as end marker.
Note: Any word other than 'EOF' can be used for the end marker.
22
3.4) tac command:
The 'tac' command is the reverse of the 'cat' command. It is also known as 'cat' backwards. It will
display the file content in reverse order.
23
3.6) less command:
The 'less' command is same as 'more' command but include some more features. It automatically
adjust with the width and height of the terminal window, while 'more' command cuts the content as
the width of the terminal window get shorter.
24
4) Working with Linux Admin Command
Here, system load averages are the processes which are either in runnable or in uninterruptable state.
A runnable process may be either a running one using CPU or waiting to use CPU. An
uninterruptable process is waiting for some I/O access.
start command
To start a service use the following syntax,
Syntax: service script_name start
restart command
To restart a service use the following syntax,
Syntax: service script_name restart
25
status command
To get current status of a service use the following syntax,
Syntax: service script_name status
To use terminating commands you need to know different PIDs. PID for a process can be find out
with the following command,
Syntax: ps -A
kill command
The most common command to terminate a process is kill command. You need to know the PID of the
process you want to terminate.
kill command sends signal to the specified process. For sending signal either signal name or signal
number can be used.
26
kill -l command
To see a list of signal names in your system.
killall command
The killall command needs the process name instead of PID. It kills all the processes with the specified
name in the system.
Syntax: killall -<signal name or option> <name>
Command killall is case sensitive, to make it non case sensitive, use killall –I
27
4.4) pmap command:
The command pmap reports memory map of one process or multiple processes. It displays
information about memory usage and address space of a process. To check pmap of a process we
need PID of the process.
The main feature of wget is its robustness and recursiveness. Due to its robustness, it works even in
slow internet connection. It automatically starts downloading the file from where it was left in case of
network failure. Due to its recursiveness, it keeps trying until the file is retrieved completely.
In some systems wget may not be installed. To install wget use the following command,
sudo apt-get install wget
Single file can be downloaded with following syntax. By default this file is saved in the current
directory.
28
Save with different file name:
By default, downloaded file will be saved with the last name mentioned in the URL. To save file with a
different name option O can be used.
29
4.7) top command:
The top command displays all the running process within the environment of your system. It helps in
monitoring system usage and performances. It is mainly used to detect load on the server by system
administrators.
Line1
Time
how long system is running
how many users are logged in
and load average
Line2
Total number of tasks
number of running tasks
number of sleeping tasks
number of stopped tasks
and number of zombie tasks
Line3
It shows CPU usage in percentage for
users
system
low priority processes
idle processes
io wait
hardware interrupts
software interrupts
steal time
30
Line4
It shows memory usage in kilobytes for
total memory
used memory
free memory
buffered memory
Line5
It shows swap memory usage in kilobytes for
total memory
used memory
free memory
cached memory
Table explanation
process ID
user
priority
nice user
virtual memory
resident memory
shareable memory
CPU used percentage
memory used percentage
time a process has run
command
Look at the above snapshot, after pressing ctrl+z keys top command has stopped and we got our terminal
back.
31
Sorting top output
By default, top command always displays output in the order of CPU usage.
32
Killing a task without exiting from top
A task can be stopped without exiting from top command by pressing k key.
It will ask for task's PID number, if you'll have authority to kill that task, then task will be removed.
Otherwise, your command will fail.
Look at the above snapshot, after pressing k, we got a message asking for PID of task to be killed.
Renice a task
Renice is done to change the scheduling order. By pressing r, you can change the priority of a process
without killing it. It will also ask for PID of the process.
Look at the above snapshot, after pressing r, we got a message asking for PID of task to be reniced.
33
Display processes for selected user
In top command output you can display all the processes for a particular user only by two options. One
through command line and other without existing top.
Look at the above snapshot, it displays all the processes only for user prashant.
When top command is running, press u, it will ask for username. Type the username and press enter.
34
Updating top output
By default, top output is updated after every 3 seconds. When you want to update it in between 3 seconds
press space bar.
You can also change updating frequency by pressing d key while running top command.
Changing colors
Colors can be changed by pressing z key and text can be made bold by pressing b key.
Look at the above snapshot, by pressing b all running processes are highlighted in white.
Now, if you want to change the colors for different areas, press Z (capital Z). it will take you to the menu
where you can select different colors for different target.
35
Suppose we want to apply blue color in column heading and magenta color in the task information.
But you can define certain number of iterations after which top command will automatically quit from the
terminal.
36
Display full login and logout time
To display full login and logout information including date and time, option F is used.
Syntax: last -F
37
Display specific username
To know the log in details of a specific user, use the username as argument of last command.
38
Display specific pts
Make pts as an argument of the last command to print information about the specific pts.
4.9) ps command:
The ps command is used to view currently running processes on the system. It helps us to determine
which process is doing what in our system, how much memory it is using, how much CPU space it
occupies, user ID, command name, etc.
The ps command may display different results for different systems because it displays information
about the currently running process of a system.
Syntax: ps -ef
Look at the above snapshot, it display all the running processes on our system.
39
Syntax: ps -aux
Look at the above snapshot, this command filter the result by CPU or memory usage. It helps you in
determining how much memory is used or how much CPU space is used by a process.
Syntax: ps -ax
40
List process with specific username (ps –f –u)
Option u displays the process for a specific user. You can also specify multiple usernames separated by a
comma between them.
Syntax: ps -f -u <username>
Example: ps -f -u akki,sssit
Syntax: ps -C <command>
Example: ps -C firefox
41
Listing process in hierarchy (pstree / ps -axjf)
In normal ps command we have to look manually on PID and PPID number to know the relation between
processes. In hierarchical format, child processes are shown under the parent process which makes it easy
for us to look upon.
Syntax: pstree
Syntax: ps -axjf
42
Show root running process (ps -U root -u root u)
It displays all the process running by root user.
Signal SIGTERM notifies all the processes that the system is going down, so that processes can be
saved and exit properly.
Five minutes before shutdown sequence starts, file /etc/nologin is created when shutdown is
scheduled for future which does not allow new user logins.
If by any reason, command shutdown is stopped before signalling init, this file is removed. It is also
removed to change runlevel before signaling init.
To run shutdown command root user access is required.
43
Shutting down system
You can shutdown a system by passing a definite time (in minutes). System will automatically shutdown
after specified minute giving a message and time to save all work
.
Syntax: shutdown <time>
Example:shutdown 3
44
power-off the system (shutdown –P)
To power-off the system option P is used. You don't have to manually power-off your system; this option
will automatically power-off your system.
45
Cancel shutdown (shutdown –c)
If due to any reason you want to cancel the shutdown command within the specified time, c option can be
used.
Syntax: shutdown -c
With the help of env, commands can be added or removed; you can assign new values to the existing
variables.
46
4.12) du command:
Command du stands for Disk Usage. It is used to check the information of disk usage of files and
directories on a system.
Command du displays a list of all the files along with their respective sizes. By default, size given is
in kilobytes.
To delete file
Use of shred command without any argument only overwrites the file content, but file still remains in the
file system.
To delete the file from the file system, use option u.
47
5) Linux User’s management
48
5.4) w command:
This command tells about the users who are logged in and what are they doing.
5.5) id command:
This command tells about your user id, primary group id, and a list of groups that belongs to you.
5.6) su command:
The su command allows you to run a shell as another user.
Syntax: su <username>
49
su to root
You can change the user to root when you know the root password.
Syntax: su root
su as root:
The root user can become any existing user without knowing that user's password. Otherwise, password is
needed.
Example: su - sssit
su - $username command:
The su command maintains the same shell environment. To become another user and also get the target
user's environment, issue the su - command followed by the target username.
50
su – command:
If any user name is not mentioned then by default, it will assume root as the target user.
Look at the above snapshot, we have created a user xyz along with creating a home directory (-m), setting
the name of home directory (-d), and a description (-c).
51
5.9) usermod command:
The command usermod is used to modify the properties of an existing user.
Look at the above snapshot, user name paddy is replaced by the new user name paddy123
52
Encryption With passwd
Passwords are always stored in encrypted format. Encryption is done with crypt function. The simplest
way to add a user with a password is to add the user with the command useradd -m and then set the
user's password with command passwd.
Chage command
The chage command can be used by a user to know the information about their password. The -l option is
used to list the information.
53
Disabling A Password
Passwords in /etc/shadow are not saved starting with exclamation mark (!). If exclamation mark is present
in starting then password cannot be used.
This feature can be used to disable a password and the process is called locking,
disabling and suspending a user account. It can be done in vi or with usermod command.
Here, we'll disable the password of akki with usermod command.
Unlocking A Password
You can unlock your account with usermod -U.
54
5.11) Group command:
Users can be listed in different groups. Group allows us to set permission on the group level instead
of setting the permission on individual level.
groupadd command
groups command
The group command tells about the group where current user belongs to.
usermod command
The group members can be edited with usermod or useradd command. If a group is not listed then by
default, usermod command will remove the user from every group of which he is a member. Here,
-a (append) option is used to prevent this from happening.
55
groupmod command
With the help of groupmod command you can change the name of an already existing group.
groupdel command
The command groupdel will delete a group permanently from the system.
56
6) Linux File Security
Syntax: ls -lh
57
6.3) Chgrp command:
The chgrp command can be abbreviated as change group. You can change the group owner of the
file using chgrp command.
Command chown can also be used to change both user owner and group.
Syntax: chown <newOwner:newGroup> <fileName>
Example: chown jtp:php msg.txt
58
6.5) Setting Permissions With chmod
You can change the permissions with chmod command accordingly to your need.
59
To set explicit permission
mkdir –m
The 'mkdir -m' command can be used to set the mode.
60
7) Linux Filter Commands
The 'cut' command is useful in selecting a specific column of a file. After (-d), delimiter (from where
you want to separate the columns) comes. Delimiters can be a space (' '), a hyphen (-), a slash (/) or
anything else. After (-f), column number is mentioned.
61
Hyphen (-) As Delimiter
Syntax: cut -d- -f(columnNumber) <fileName>
Example: cut -d- -f2 marks.txt
cut -d- -f1 marks.txt
Space As Delimiter
If you want to use space as a delimiter then you have to quote the space (' ').
62
7.3) Grep command:
The 'grep' command stands for "global regular expression print". grep command filters the content
of a file which makes our search easy.
63
grep options
--- grep -vM: The 'grep -v' command displays lines not matching to the specified word.
--- grep -i: The 'grep -i' command filters output in a case-insensitive way.
64
7.4) comm command:
The 'comm' command compares two files or streams. By default, 'comm' will always display three
columns. First column indicates non-matching items of first file, second column indicates non-
matching items of second file, and third column indicates matching items of both the files. Both the
files has to be in sorted order for 'comm' command to be executed.
If you want to output a single column, you have to specify number of the columns which are not to be
displayed.
Syntax:
comm -23 (To display first column)
comm -13 (To display second column)
comm -12 (To display third column)
Example:
comm -23 file1.txt file2.txt
comm -13 file1.txt file2.txt
comm -12 file1.txt file2.txt
65
7.5) tr command:
The command 'tr' stands for 'translate'. It is used to translate, like from lowercase to uppercase and
vice versa or new lines into spaces.
Change Case
The 'tr' command can change case.
66
Remove New Lines
To write all the lines into a single line we have to translate all new lines into spaces.
tr Options
--- tr -s : The 'tr -s' command squeezes the occurrence of multiple characters into one.
67
--- tr rot13: This command encrypts the text. It is case-sensitive.
Example:
cat exm.txt | tr 'a-z' 'nopqrstuvwxyzabcdefghijklm'
cat exm.txt | tr 'a-z' 'n-za-m'
68
7.6) uniq command:
With the help of uniq command you can form a sorted list in which every word will occur only once.
uniq –c command
You can count the number of occurences of a word with 'uniq -c' command.
7.7) wc command:
The 'wc' command helps in counting the lines, words and characters in a file.
Syntax:
wc <fileName> (Counts words, lines and characters)
wc -l <fileName> (Counts only lines)
wc -w <fileName> (Counts only words)
wc -c <fileName> (Counts only characters)
69
Example:
wc exm.txt
wc -l exm.txt
wc -w exm.txt
wc -c exm.txt
70
To Sort A Column
If a file has more than one column, column number is used to sort a specific column.
Numeric Sorting
Numeric sorting is different from alphabetical sorting. For numeric sorting option 'n' is used along
with the column number if required.
71
7.9) gzip command:
gzip (GNU zip) is a compressing tool, which is used to truncate the file size. By default original file
will be replaced by the compressed file ending with extension (.gz).
To decompress a file you can use gunzip command and your original file will be back.
Syntax:
gzip <file1> <file2> <file3>. . .
gunzip <file1> <file2> <file3>. . .
Example:
gzip file1.txt file2.txt
gunzip file1.txt file2.txt
If you want to compress more than one file together, you can use 'cat' and gzip command with pipe
command.
72
gzip –l command
The 'gzip -l' command tells about the compression ratio or how much the original file has compressed.
To Compress A Directory
The gzip command will not be able to compress a directory because it can only compress a single file. To
compress a directory you have to use 'tar' command.
73
8) Linux Networking Commands
Every computer is connected to some other computer through a network whether internally or externally
to exchange some information. This network can be small as some computers connected in your home or
office, or can be large or complicated as in large University or the entire Internet.
74
Setting MTU size
By default MTU (Maximum Transmission Unit) size is 1500, you can change size as per your wish.
Example: Ifconfig eth0 mtu xxxx
8.2) ip command:
This is the newer version of ifconfig command.
Syntax: ip a or ip addr
75
8.4) host command:
This command displays domain name for given IP address and displays IP address for given domain
name. It also performs DNS lookups related to DNS query.
Example:
host javatpoint.com
host 144.76.11.18
76
9) Extra Command
Finding By Name
You can search all the files ending with extension '.txt'.
Example: find . -name "*.txt"
Finding By Type
The '-type' parameter is used to specify the file type.
Some of the file types are:
f: regular file
d: directory
l:symbolic links
c: character devices
b: block devices
77
9.2) locate command:
The command locate and find are used to search a file by their filename. But difference is that locate
command is a background process and searches the file in database whereas; find command searches in
file system. The locate command is much faster than find command.
If you are unable to find a file with locate command then it means that your database is out of date, and
you can update your database with the updatedb command.
Syntax: date
Date Format
You can display the date in a format of your choice.
78
9.4) cal command:
The 'cal' term stands for calendar. It displays current month's calendar with current day highlighted.
Syntax: cal
79
9.6) zcat command:
Compressed files or zipped files can be viewed with the help of 'zcat' command.
9.7) df command:
The 'df' command tells about the disk space used in the file system. It defines the number of blocks used,
number of blocks available and the directory where file system is mounted.
Syntax: df
80
10) References
1. https://www.mygov.in/sites/default/files/user_submission/920565a3edaa057078bcfc5b39665
68c.pdf
2. http://linux-training.be/linuxfun.pdf
81