Linux Commands1
Linux Commands1
$echo $PATH
/home/mrs/bin:/home/mrs/.local/bin:/usr/local/sbin:/usr/loc
al/bin:/usr/sbin:/usr/bin:/sbin:
/bin:/usr/games:/usr/local/games:/snap/bin
The history command
History Events:
In the BASH shell, the history utility keeps a record of the
most recent commands you have executed.
The commands are numbered starting at 1, and a limit
exists to the number of commands remembered—the
default is 500
The history utility is a kind of short-term memory,
keeping track of the most recent commands you have
executed.
$ history
1 cp mydata today
2 vi mydata
10 mv mydata reports
4 cd reports
5 ls
System Commands
To clear the screen
$clear
To change your password with 'passwd' command
$passwd
To shut down your system
$shutdown -h now
Executing commands with admin priviledges
$sudo
sudo (SuperUser DO) Linux command allows you to run
programs or other commands with administrative
privileges like “Run as administrator” in Windows.
$shutdown -h now
kill
This command is used to kill process which is not relevant
now or is not responding.
We can kill a process and start it without restarting the whole
system.
ps and kill command
#pkill rhythmbox
alias command
alias is a built in shell command that lets you assign name for
a long command or frequently used command.
‘ls -l‘ command is used frequently, which
includes 5 characters including space.
unalias command
To remove alias ‘l‘, use the following ‘unalias‘ command
ntfsfix /dev/sda4
Archiving Files
The tar (tape archive) utilities is an archiving tool. It puts one
or more files into an archive, and the common filename suffix
for that is .tar
The files that tar produces are uncompressed
Note:
When you are finished, you can save and close the file by
hitting CTRL-X, followed by "Y", and then hit "ENTER" to
confirm.
Deleting a user
$ date --help
Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--
universal] [MMDDhhmm[[CC]YY][.ss]] Display the current
time in the given FORMAT, or set the system date.
whatis command
$ whatis date
date (1) - print or set the system date and time
$ whatis whatis
whatis (1) - display manual page descriptions
“
‘--help’ option and ‘whatis’ command do not provide
thorough information about the command. For more
detailed information, Linux provides man pages and info
pages
man command
man command gives detailed information of a given
command. Format $man command
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 you 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 command
Example of man command:
$ man -k gzip
gzip (1) - compress or expand files
lz (1) - gunzips and shows a listing of a gzip'd tar'd archive
tgz (1) - makes a gzip'd tar archive
uz (1) - gunzips and extracts a gzip'd tar'd archive
zforce (1) - force a '.gz' extension on all gzip files
info pages
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
Example: $ info ls
help command
with man, info etc commands you can not get much
information about the inbuilt commands.
If you want to know what an inbuilt command do, you have to
use help command
File Commands
pwd
$ pwd
/home/raghu
$ cd /usr/share/
$ pwd
/usr/share
$ cd doc
$ pwd
/usr/share/doc
Listing File And Directories Command
$ ls [files-or-directories]
List files and/or directories. If no argument is given, the
contents of current directory are shown
$ ls -ld odesk/
drwxr-xr-x 4 raghu raghu 4096 2012-07-06 13:46 odesk/
Creating Directories
To create a directory, the ‘mkdir’ command is used.
$ mkdir example
$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
Creating Files
For creating an empty file, use the touch command.
$ cp -r example /tmp/expertslogin/
$ ls -l /tmp/expertslogin
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:12 example
Moving a file or directory
$ mv source destination
Move files or directories. The 'mv' command works like 'cp'
command, except that the original file is removed.
But, the mv command can be used to rename the files (or
directories)
$ mv listing_copy.txt usrcopy
$ ls -l
total 12
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:10 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting
Here, 'listing_copy.txt' is moved with the name 'usrcopy' in
the same directory (or you can say that it has been renamed)
Removing a file or directory
$ rmdir dir
'rmdir' command removes any empty directories, but
cannot delete a directory if a file is present in it.
To use ‘rmdir’ command, you must first remove all the files
present in the directory you wish to remove (and possibly
directories if any).
Removing a file or directory
$ rm files|directories
A directory must be removed recursively with -r option.
$ rm file2
$ rm -r example/
$ ls -l
total 8
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting
Here, the file named 'file2' is removed first, and then the
directory 'example' is removed recursively. This can be seen
in the output of ‘ls -l’ command where these two are no
longer present.
Removing a file or directory
Other File Commands
file command
Determines the file type of a given file.
$ file /etc/passwd
/etc/passwd: ASCII text
You can provide one or more than one file as an argument
to the file command.
$ file td.c td.out ARP.java Screenshot.png StringTokenizing.class
idl.rar List.pdf
td.c: ASCII C program text, with CRLF line terminators
td.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not
stripped
ARP.java: ASCII Java program text, with CRLF line terminators
Screenshot.png: PNG image data, 1366 x 768, 8-bit/color RGB, non-
interlaced
StringTokenizing.class: compiled Java class data, version 50.0
(Java 1.6)
idl.rar: RAR archive data, v1d, os: Win32
List.pdf: PDF document, version 1.4
stat command
To check the status of a file.
This provides more detailed information about a file than ‘ls -l’
output.
$ stat usrcopy
File: `usrcopy'
Size: 491 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 149452 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ raghu) Gid: ( 1000/ raghu)
Access: 2012-07-06 16:07:06.413522009 +0530
Modify: 2012-07-06 16:02:30.204152386 +0530
Change: 2012-07-06 16:17:18.992559654 +0530
cat command
The 'cat' command is actually a concatenator, but can be
used to view the contents of a file.
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
pagers
The cat command lists file as a whole. But if the file is big
enough to fit into one screen, then we will be able to see only
the last page of the file.
The commands 'less' and 'more' display files one page at a
time. So they are also called pagers.
You can navigate through a file using arrow keys. To quit
from a pager, hit 'q'.
In less command, to see remaining pages, we can press
“space bar” etc.
Use ctrl+f to go to next page
Ctrl+b to go to previous page
head command
Displays the first few lines of a file. By default, the ‘head’
command displays the first 10 lines of a file
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
$ tail -n 4 /etc/passwd
raghu:x:1000:1000:Raghu Sharma,,,:/home/raghu:/bin/bash
sshd:x:113:65534::/var/run/sshd:/usr/sbin/nologin
dictd:x:114:123:Dictd Server,,,:/var/lib/dictd:/bin/false
mysql:x:115:124:MySQL Server,,,:/nonexistent:/bin/false
word count command
This command counts lines, words and letters of the input
given to it.
$ wc /etc/passwd
35 57 1698 /etc/passwd
$echo
Syntax:
command 2> file
Just a funny note : You can see this NMAP used by Trinity in
Matrix-II movie, when she tries to hack in to electric grid
super computer
NMAP Command(Tool)
Scan a System with Hostname and IP Address
nmap hostname/ip_add