Linux Basic Commands
Linux Basic Commands
[divakar@divakar BD]# ls
a.txt b.txt
[divakar@divakar BD]#
b) List Files With option l
[divakar@localhost ~]$ ls -l
-rw-rw-r--. 1 divakar divakar 0 Feb 11 18:51 divakar.txt
-rwxr-xr-x. 1 divakar divakar 0 Feb 12 18:22 diva.txt
[divakar@localhost ~]$
c) View Hidden Files
List all files including hidden file starting with ..
Public/
Videos/
[divakar@localhost ~]$
[divakar@localhost ~]$ ls -R
d1.txt Desktop diva.txt Downloads Pictures Templates xyz
h) Reverse Output Order
With combination of -ltr will shows latest modification file or directory date as last.
[divakar@localhost ~]$ ls -ltr
drwxr-xr-x. 2 divakar divakar 4096 Feb 10 18:55 Templates
drwxr-xr-x. 2 divakar divakar 4096 Feb 10 18:55 Downloads
drwxr-xr-x. 2 divakar divakar 4096 Feb 10 18:55 Videos
drwxr-xr-x. 2 divakar divakar 4096 Feb 10 18:55 Public
i) Sort Files by File Size
With combination of -lS displays file size in order, will display big in size first.
[divakar@localhost ~]$ ls -ls
0 -rw-rw-r--. 1 divakar divakar
inode number
[divakar@localhost ~]$ ls -i
272870 d1.txt
272871 d2.txt
of /tmp directory.
[divakar@localhost ~]$ ls -l /
dr-xr-xr-x. 2 root root 4096 Feb 10 19:34 bin
dr-xr-xr-x. 5 root root 1024 Feb 10 13:52 boot
b) The cat command also has a number of options. If youd like to see your
file with line numbers, you can use the n-option
#cat n a.txt
1 Hello Hello Hello
2 Hello hello hello
c) You can also use cat to look at several files at once.
# cat n test*
1 Hello Hello Hello
2 Hello hello hello
[root@localhost ~]# cat a.txt
aksjlkdj
[root@localhost ~]#
[root@localhost ~]# cat b.txt
alksjdjkjaskjkaj
;lajslkdjja
lkashdjfajs
[root@localhost ~]#
d) As you can see, cat has also include a second file in its output.
[root@localhost ~]# cat a.txt b.txt
aksjlkdj
alksjdjkjaskjkaj
;lajslkdjja
lkashdjfajs
[root@localhost ~]#
cat > c.txt
e) To see the numbers using cat
# cat -n divakar.txt
100 divakar 20000
200 diiia 10000
399 ksjjkj 30999[
Reading the files with the 'more' command
[root@localhost ~]# more c.txt
kajslkj
lkasjdkfjjs
klakjshkdjfhkahs
[root@localhost ~]#
Browsing Files with the 'less' command.
less c.txt
Reading the Beginning or End of Files with the head and tail Commands.
head -5 /usr/man/man.txt
head -5 q /usr/man/man.txt
tail -12 /var/log/message/a.txt
The more command is one of a family of Linux commands called pagers.
Creating Files with the 'touch' Command
The touch command is easy to use, and generally, there are two reasons to use it .The
first reason is to create a file, and the second is to update a files modification date.
a) To create a file with touch,use
# touch newfile
#ls l newfile
-rw-r--r-- 1 divakar divakar 0 Feb 21 14:12 newfile
b) To Change time stamp
# touch t 1225110099 newfile2
Trap: when the program is terminated before it would normal end, we can
catch an exit signal.
0- Normal termination, end of script.
1-SIGHUP -> hang up, line disconnected
2-singint-> terminal interrupts, usually ctrl+c
3- SIGQUIT -> Quit Key, Child process to die before terminating.
9-SIGKILL->kill -9 commands cant trap this type of exit status.
15-SIGTERM kill commands default action.
19-SIGSTOP->stop, usually ctrl+z
17 -> dintfunc
Ex: Kill 9 <ps Id>
df -> Report how much free disk space is available for each mount you have.
df a --all -> include dummy file system.
df B 100, --block-size=SIZE -> use SIZE byte blocks
df h ->human readable print sizes in human readable format.
df I -> list inode information instead of block usage.
Df k -> like --block Size =1 k.
df T ->Print file System Type.
du -> disk usage
du ->tells you how much disk space a file occupies.
du a ->display the space that each file is taking up.
Du h -> which can make the output easier to read by displaying it in KB /M/G.
Du sh -> The -s (for suppress or summarize) option tells du to report only the total disk
space occupied by a directory tree and to suppress individual reports for its
subdirectories
[root@localhost Desktop]#
How to add user in sudo list.
Go to vi /etc/sudoers
## Allow root to run any commands anywhere
root ALL=(ALL)
ALL
divakar ALL=(ALL) ALL
Man ->
Find files with the whereis command.
Whereis find
You can also use whereis to find only the binary version of the program with
Whereis b find
If whereis cannt find your request , youll get an empty return string, for example
Whreis foo
It will search in entire system.
Not limiting searches to known directories such as /usr/man, /usr/bin, or /usr/sbin can
speed up the task of finding files.
Although whereis is faster than using find to locate programs or manual pages.
Locate is faster than whereis
Locating files with locate command.
Finding a file using locate is much faster than the find command because locate will go
directly to the database file,find any matching filenames, and print its results.
Locate *.ps
Locate resides in /var/lib
Moving different directories with cd command.
cd or
cd ../.. or
cd or cd
Knowing where you are wit pwd command.
Go to /user/local and type pwd to know your directory.
ps -ef | grep aneel
pkill -f
Link while copying the data?
cp i test 1 test2
Getting command Summaries with whatis and apropos
Specifying other directories with ls
# ls /usr/bin
Listing Directories with the dir and vdir commands
#dir :-> this command works like the default ls command,listing the files in sorted
columns
Vdir -> The vdir command works like the ls l option, and presents a long format listing
by default,
Graphic Directory listings with the tree command
# tree /var/lib
# tree d /usr/local/netscape/
Search Inside Files with the 'grep' Command
grep command will help to search any words in file
Ex: cat hive.log | grep loaded [Here we are searching loaded files in log]
Compressing files with the 'gzip' command
# gzip file.tar
Compressing Files with the 'compress' Command
#compress file
To uncompress a file,use
# uncompress file.Z
Running Programs in the Background
# nohup ./divakar.sh &
Checking the Connection
Using the 'ipconfig' Command, This Command will help to identify the IP address of
your system
# ifconfig
[divakar@localhost ~]$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:7F:24:89
inet addr:192.168.64.130 Bcast:192.168.64.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7f:2489/64 Scope:Link
Using the 'netstat' Command
The netstat command is the definitive command for checking your network
activity,connections,routing tables,and other network messages and statistics.
# netstat
Using the ping Command
# ping <hostname>.com
Find Hostname of the System
#hostname