Linux Comands
Linux Comands
2. cal
Displays the calendar of the current month.
{ $cal
July 2012
Su Mo Tu We Th Fr Sa
1234567
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 }
‘cal ’ will display calendar for the specified month and year.
{ $cal 08 1991
August 1991
Su Mo Tu We Th Fr Sa
123
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 31 }
3. clear
This command clears the screen.
4. echo
This command will echo whatever provide it.
{ $ echo "linoxide.com"
linoxide.com }
The ‘echo’ command is used to display the values of a variable. One such variable is ‘HOME’. To check
the value of a variable precede the variable with a $ sign.
{ $ echo $HOME/home/Raghu }
5. date
Displays current time and date.
{ $ date
Fri Jul 6 01:07:09 IST 2012 }
If interested only in time, can use 'date +%T' (in hh:mm:ss):
{ $ date +%T
01:13:14 }
6. tty
Displays current terminal.
{ $tty
/dev/pts/0 }
7. whoami
This command reveals the user who is currently logged in.
{ $whoami
Raghu }
8. id
This command prints user and groups (UID and GID) of the current user.
{ $ id
uid=1000(raghu) gid=1000(raghu)
groups=1000(raghu),4(adm),20(dialout),24(cdrom),46(plugdev),112(lpadmin),120(admin),122(sambas
hare) }
By default, information about the current user is displayed. If another username is provided as an
argument, information about that user will be printed:
{ $ id root
uid=0(root) gid=0(root) groups=0(root) }
2. whatis
This command gives a one line description about the command. It can be used as a quick reference for
any command.
{$ whatis date
date (1) - print or set the system date and time }
{ $whatiswhatis
whatis (1) - display manual page descriptions }
3. man
‘--help’ option and ‘whatis’ command do not provide thorough information about the command. For more
detailed information, Linux provides man pages and info pages. To see a command's manual page, man
command is used.
{ $ man date }
The man pages are properly documented pages. They have following sections:
NAME: The name and one line description of the command.
SYNOPSIS: The command syntax.
DESCRIPTION: Detailed description about what a command does.
OPTIONS: A list and description of all of the command's options.
EXAMPLES: Examples of command usage.
FILES: Any file associated with the command.
AUTHOR: Author of the man page
REPORTING BUGS: Link of website or mail-id where can report any bug.
SEE ALSO: Any commands related to the command, for further reference.
With -k option, a search through man pages can be performed. This searches for a pattern in the name and
short description of a man page.
{ $ man -k gzip
gzip (1) - compress or expand files
lz (1) - gunzips and shows a listing of a gzip'dtar'd archive
tgz (1) - makes a gzip'd tar archive
uz (1) - gunzips and extracts a gzip'dtar'd archive
zforce (1) - force a '.gz' extension on all gzipfiles }
4. info
Info documents are sometimes more elaborate than the man pages. But for some commands, info pages are
just the same as man pages. These are like web pages. Internal links are present within the info pages. These
links are called nodes. Info pages can be navigated from one page to another through these nodes.
{ $ info date }
Linux Filesystem commands
Command:
{ $date
Output:
Tue Oct 10 22:55:01 PDT 2017 }
Note : Here unix system is configured in pacific daylight time.
3. –date or -d option: Displays the given date string in the format of date. But this will not affect the
system’s actual date and time value.Rather it uses the date and time given in the form of string.
Syntax:
{ $date --date=" string " }
Command:
{ $date --date="2/02/2010"
$date --date="Feb 2 2010" }
Output:
{ Tue Feb 2 00:00:00 PST 2010
Tue Feb 2 00:00:00 PST 2010 }
4. Using –date option for displaying past dates: Date and time of 2 years ago.
Command:
{ $date --date="2 year ago" }
Output:
{ Sat Oct 10 23:42:15 PDT 2015 }
*Date and time of 5 seconds ago.
Command:
{ $date --date="5 sec ago" }
Output:
{ Tue Oct 10 23:45:02 PDT 2017 }
*Date and time of previous day.
Command:
{ $date --date="yesterday" }
Output:
{ Mon Oct 9 23:48:00 PDT 2017 }
*Date and time of 2 months ago.
Command:
{ $date --date="2 month ago" }
Output:
{ Thu Aug 10 23:54:51 PDT 2017 }
*Date and time of 10 days ago.
Command:
{ $date --date="10 day ago" }
Output: { Sat Sep 30 23:56:55 PDT 2017 }
5. Using –date option for displaying future date: Date and time of upcoming particular week
day.
Command:
{ $date --date="next tue" }
Output:
{ Tue Oct 17 00:00:00 PDT 2017 }
*Date and time after two days.
Command:
{ $date --date="2 day" }
Output:
{ Fri Oct 13 00:05:52 PDT 2017 }
*Date and time of next day.
Command:
{ $date --date="tomorrow" }
Output:
{ Thu Oct 12 00:08:47 PDT 2017 }
*Date and time after 1 year on the current day.
Command:
{ $date --date="1 year" }
Output:
{ Thu Oct 11 00:11:38 PDT 2018 }
6. -s or –set option: To set the system date and time -s or –set option is used.
Syntax:
{ $date --set="date to be set" }
Command:
{ $date }
Output:
{ Wed Oct 11 15:23:26 PDT 2017 }
Command:
{ $date --set="Tue Nov 13 15:23:34 PDT 2018"
$date }
Output:
{ Tue Nov 13 15:23:34 PDT 2018 }
7. –file or -f option: This is used to display the date string present at each line of file in the date and
time format.This option is similar to –date option but the only difference is that in –date we can only
give one date string but in a file we can give multiple date strings at each line.
Syntax:
{ $date --file=file.txt
$cat >>datefile
Sep 23 2018
Nov 03 2019 }
Command:
{ $date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov 3 00:00:00 PDT 2019 }
Syntax:
{ $date +%[format-option] }
Examples:
Command:
{ $date "+%D" }
Output:
{ 10/11/17 }
Command:
{ $date "+%D %T" }
Output:
{ 10/11/17 16:13:27 }
Command:
{ $date "+%Y-%m-%d" }
Output:
{ 2017-10-11 }
Command:
{ $date "+%Y/%m/%d" }
Output:
{ 2017/10/11 }
Command:
{ $date "+%A %B %d %T %y" }
Output:
{ Thursday October 07:54:29 17 }
2. sed
sed is a powerful stream editor for filtering and transforming text. We’ve already written a two useful
articles on sed, that can go through it here:
How to use GNU ‘sed’ Command to Create, Edit, and Manipulate files in Linux
15 Useful ‘sed’ Command Tips and Tricks for Daily Linux System Administration Tasks
The sed man page has added control options and instructions:
{ $ man sed }
3. grep, egrep, fgrep, rgrep
These filters output lines matching a given pattern. They read lines from a file or standard input, and print
all matching lines by default to standard output.
Note: The main program is grep, the variations are simply the same as using specific grep options as
below (and they are still being used for backward compatibility):
{ $egrep = grep -E
$ fgrep = grep -F
$ rgrep = grep -r }
Below are some basic grep commands:
{ tecmint@TecMint ~ $ grep "aaronkilik" /etc/passwd
aaronkilik:x:1001:1001::/home/aaronkilik:
tecmint@TecMint ~ $ cat /etc/passwd | grep "aronkilik"
aaronkilik:x:1001:1001::/home/aaronkilik: }
4. head
head is used to display the first parts of a file, it outputs the first 10 lines by default. can use the -n num
flag to specify the number of lines to be displayed:
{ tecmint@TecMint ~ $ head /var/log/auth.log
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root
Jan 2 10:51:34 TecMintsudo: tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ;
COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py
Jan 2 10:51:34 TecMintsudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jan 2 10:51:39 TecMintsudo: pam_unix(sudo:session): session closed for user root
Jan 2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session closed for user root
Jan 2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session closed for user root
Jan 2 11:09:01 TecMint CRON[4146]: pam_unix(cron:session): session opened for user root by (uid=0)
}
{ tecmint@TecMint ~ $ head -n 5 /var/log/auth.log
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root
Jan 2 10:51:34 TecMintsudo: tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ;
COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py
Jan 2 10:51:34 TecMintsudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jan 2 10:51:39 TecMintsudo: pam_unix(sudo:session): session closed for user root }
5. tail :tail outputs the last parts (10 lines by default) of a file. Use the -n num switch to specify the
number of lines to be displayed.
The command below will output the last 5 lines of the specified file:
{ tecmint@TecMint ~ $ tail -n 5 /var/log/auth.log
Jan 6 13:01:27 TecMintsshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 13:01:27 TecMintsshd[1269]: Server listening on :: port 22.
Jan 6 13:01:27 TecMintsshd[1269]: Received SIGHUP; restarting.
Jan 6 13:01:27 TecMintsshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 13:01:27 TecMintsshd[1269]: Server listening on :: port 22. }
The following command will enable monitor changes in the specified file:
{ tecmint@TecMint ~ $ tail -f /var/log/auth.log
Jan 6 12:58:01 TecMintsshd[1269]: Server listening on :: port 22.
Jan 6 12:58:11 TecMintsshd[1269]: Received SIGHUP; restarting.
Jan 6 12:58:12 TecMintsshd[1269]: Server listening on 0.0.0.0 port 22.
Jan 6 12:58:12 TecMintsshd[1269]: Server listening on :: port 22.
Jan 6 13:01:27 TecMintsshd[1269]: Received SIGHUP; restarting.
Jan 6 13:01:27 TecMintsshd[1269]: Server listening on 0.0.0.0 port 22.
6. sort
sort is used to sort lines of a text file or from standard input.
Below is the content of a file named domains.list:
{ tecmint@TecMint ~ $ cat domains.list
tecmint.com
tecmint.com
news.tecmint.com
news.tecmint.com
linuxsay.com
linuxsay.com
windowsmint.com
windowsmint.com }
run a simple sort command to sort the file content like so:
{ tecmint@TecMint ~ $ sort domains.list
linuxsay.com
linuxsay.com
news.tecmint.com
news.tecmint.com
tecmint.com
tecmint.com
windowsmint.com
windowsmint.com }
7. uniq
uniq command is used to report or omit repeated lines, it filters lines from standard input and writes the
outcome to standard output.
After running sort on an input stream, can remove repeated lines with uniq as in the example below.
To indicate the number of occurrences of a line, use the -c option and ignore differences in case
{ tecmint@TecMint ~ $ cat domains.list
tecmint.com
tecmint.com
news.tecmint.com
news.tecmint.com
linuxsay.com
linuxsay.com
windowsmint.com }
tecmint@TecMint ~ $ sort domains.list | uniq -c
2 linuxsay.com
2 news.tecmint.com
2 tecmint.com
1 windowsmint.com e comparing by including the -i option: }
8. fmt
fmt simple optimal text formatter, it reformats paragraphs in specified file and prints results to the
standard output.
The following is the content extracted from the file domain-list.txt:
1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com
To reformat the above content to a standard list, run the following command with -w switch is used to
define the maximum line width:
{ tecmint@TecMint ~ $ cat domain-list.txt
1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com
tecmint@TecMint ~ $ fmt -w 1 domain-list.txt
1.tecmint.com
2.news.tecmint.com
3.linuxsay.com
4.windowsmint.com }
9. pr
pr command converts text files or standard input for printing. For instance on Debian systems, list all
installed packages as follows:
{ $dpkg -l }
To organize the list in pages and columns ready for printing, issue the following command.
{ tecmint@TecMint ~ $ dpkg -l | pr --columns 3 -l 20
2017-01-06 13:19 Page 1
Desired=Unknown/Install ii adduser ii apg
| Status=Not/Inst/Conf- ii adwaita-icon-theme ii app-install-data
10. tr
This tool translates or deletes characters from standard input and writes results to standard output.
The syntax for using tr is as follows:
{ $ tr options set1 set2 }
Take a look at the examples below, in the first command, set1( [:upper:] ) represents the case of input
characters (all upper case).
Then set2([:lower:]) represents the case in which the resultant characters will be. It’s same thing in the
second example and the escape sequence \n means print output on a new line:
{ tecmint@TecMint ~ $ echo "WWW.TECMINT.COM" | tr [:upper:] [:lower:]
www.tecmint.com
tecmint@TecMint ~ $ echo "news.tecmint.com" | tr [:lower:] [:upper:]
11. more
more command is a useful file perusal filter created basically for certificate viewing. It shows file content
in a page like format, where users can press [Enter] to view more information.
can use it to view large files like so:
{ tecmint@TecMint ~ $ dmesg | more
[ 0.000000] Initializing cgroupsubsyscpuset
[ 0.000000] Initializing cgroupsubsyscpu
[ 0.000000] Initializing cgroupsubsyscpuacct
[ 0.000000] Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu
5.3.1-14ubuntu2) ) #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 (Ubuntu 4.4.0-21.37-generic
4.4.6)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-21-generic root=UUID=bb29dda3-
bdaa-4b39-86cf-4a6dc9634a1b ro quiet splash vt.handoff=7
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Centaur CentaurHauls
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers'
[ 0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[ 0.000000] x86/fpu: Using 'eager' FPU context switches.
[ 0.000000] e820: BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable
--More--NEWS.TECMINT.COM } }
12. less
less is the opposite of more command above but it offers extra features and it’s a little faster with large
files.
Use it in the same way as more:
{ tecmint@TecMint ~ $ dmesg | less
Decompressing files
List the contents of an archive/compressed file: Some time just wanted to look at files inside
an archive or compressed file. Then all of the above command supports file list option.
install : This command is used to install or upgrade packages. It is followed by one or more package names
the user wishes to install. All the dependencies of the desired packages will also be retrieved and installed.
The user can also select the desired version by following the package name with an ‘equals’ and the desired
version number. Also, the user can select a specific distribution by following the package name with a
forward slash and the version or the archive name (e.g. ‘stable’, ‘testing’ or ‘unstable’). Both of these
version selection methods have the potential to downgrade the packages, so must be used with care.
{ apt-get install [...PACKAGES] }
remove : This is similar to install, with the difference being that it removes the packages instead of
installing. It does not remove any configuration files created by the package.
{ apt-get remove [...PACKAGES] }
purge :This command removes the packages, and also removes any configuration files related to the
packages.
{ apt-get purge [...PACKAGES] }
check : This command is used to update the package cache and checks for broken dependencies.
{ apt-get check }
download : This command is used to download the given binary package in the current directory.
{ apt-get download [...PACKAGES] }
clean : This command is used to clear out the local repository of retrieved package files. It removes
everything but not the lock file from /var/cache/apt/archives/partial/ and /var/cache/apt/archives/.
{ apt-get clean }
autoremove : Sometimes the packages which are automatically installed to satisfy the dependencies of
other packages, are no longer needed then autoremove command is used to remove these kind of packages.
{ apt-get autoremove }
Options:
–no-install-recommends : By passing this option, the user lets apt-get know not to consider recommended
packages as a dependency to install.
{ apt-get --no-install-recommends [...COMMAND] }
–install-suggests : By passing this option, the user lets apt-get know that it should consider suggested
packages as dependencies to install.
{ apt-get --install-suggests [...COMMAND] }
-d or –download-only : By passing this option, the user specifies that apt-get should only retrieve the
packages, and not unpack or install them.
{ apt-get -d [...COMMAND] }
-f or –fix-broken : By passing this option, the user specifies that apt-get should attempt to correct the
system with broken dependencies in place.
{ apt-get -f [...COMMAND] }
-m or –ignore-missing or –fix-missing : By passing this option, the user specifies that apt-get should
ignore the missing packages ( packages that cannot be retrieved or fail the integrity check ) and handle the
result.
{ apt-get -m [...COMMAND] }
–no-download : By passing this command, the user disables downloading for apt-get. It means that it
should only use the .debs it has already downloaded.
{ apt-get [...COMMAND] }
-q or –quiet : When this option is specified, apt-get produces output which is suitable for logging.
{ apt-get [...COMMAND] }
-s or –simulate or –just-print or –dry-run or –recon or –no-act : This option specifies that no action
should be taken, and perform a simulation of events that would occur based on the current system, but do
not change the system.
{ apt-get -s [...COMMAND] }
-y or –yes or –assume-yes : During the execution of apt-get command, it may sometimes prompt the user
for a yes/no. With this option, it is specified that it should assume ‘yes’ for all prompts, and should run
without any interaction.
{ apt-get -y [...COMMAND] }
–assume-no :With this option, apt-get assumes ‘no’ for all prompts.
{ apt-get --assume-no [...COMMAND] }
–no-show-upgraded : With this option, apt-get will not show the list of all packages that are to be
upgraded.
{ apt-get --no-show-upgraded [...COMMAND] }
-V or –verbose-versions : With this option, apt-get will show full versions for upgraded and installed
packages.
{ apt-get -V [...COMMAND] }
–show-progress : With this option, apt-get will show user-friendly progress in the terminal window when
the packages are being installed, removed or upgraded.
{ apt-get --show-progress [...COMMAND] }
-b or –compile or –build : With this option, apt-get will compile/build the source packages it downloads.
{ apt-get -b [...COMMAND] }
–no-upgrade : With this option, apt-get prevents the packages from being upgraded if they are already
installed.
{ apt-get --no-upgrade [...COMMAND] }
–only-upgrade : With this option, apt-get will only upgrade the packages which are already installed, and
not install new packages.
{ apt-get --only-upgrade [...COMMAND] }
–reinstall : With this option, apt-get reinstalls the packages that are already installed, at their latest versions.
{ apt-get --reinstall [...COMMAND] }
–auto-remove or –autoremove : When using apt-get with install or remove command, this option acts like
running the autoremove command.
{ apt-get install/remove --autoremove [...PACKAGES] }
-h or –help : With this option, apt-get displays a short usage summary.
{ apt-get -h }
Output:
-v or –version : With this option, apt-get displays it’s current version number.
{ apt-get [...COMMAND] }
Output:
Vi editor
The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons
which make it a widely used editor –
• It is available in almost all Linux Distributions
• It works the same across different platforms and Distributions
Sir C. R. Reddy College of Engineering Dept. of Information Technology
I.T Work shop (ES1105) Page No: ______
• It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs
• Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM
which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi
because it is feature-rich and offers endless possibilities to edit a file.
• To work on VI editor, need to understand its operation modes. They can be divided into two main
parts.
Command mode
• The vi editor opens in this mode, and it only understands commands
• In this mode, can, move the cursor and cut, copy, paste the text
• This mode also saves the changes have made to the file
• Commands are case sensitive. should use the right letter case.
Insert mode
• This mode is for inserting text in the file.
• can switch to the Insert mode from the command mode by pressing 'i' on the keyboard.
• Once are in Insert mode, any key would be taken as an input for the file on which are currently
working.
• To return to the command mode and save the changes have made need to press the Esc key.
Add content
Vi editor commands:
Note: Should be in the "command mode" to execute these commands. VI editor is case-sensitive type the
commands in the right letter-case.
Keystrokes Action
i Insert at cursor (goes into insert mode)
a Write after cursor (goes into insert mode)
A Write at the end of line (goes into insert mode)
ESC Terminate insert mode
u Undo last change
U Undo all changes to the entire line
o Open a new line (goes into insert mode)
dd Delete line
3dd Delete 3 lines.
D Delete contents of line after the cursor
C Delete contents of a line after the cursor and insert new text. Press
ESC key to end insertion.
dw Delete word
4dw Delete 4 words
CW Change word
X Delete character at the cursor
r Replace character
R Overwrite characters from cursor onward
s Substitute one character under cursor continue to insert
S Substitute entire line and begin to insert at the beginning of the line
~ Change case of individual character
Make sure press the right command otherwise will end up making undesirable changes to the file. Also
enter the insert mode by pressing a, A, o, as required.
Moving within a file: need to be in the command mode to move within a file. The default keys for
navigation are mentioned below else; also use the arrow keys on the keyboard.
keystroke Use
Shift+zz Save the file and quit
:w Save the file but keep it open
:q Quit without saving
:wq Save the file and quit