Saturday, May 7, 2016

25 most commonly used LINUX Commands

To create file very first command in Linux we use is cat' lets see how to create files using cat command
[root@ArkIT ~]# cat > firstfile
This is a first file created using cat command
As mentioned in above ‘>’ redirect symbol we have to use along with cat command to create file with content. Whenever you type cat > FILENAME hit enter, than it will just show blank screen below the command now type some data and press CTRL+d first will save and exit.
To read file content below is the command example
[root@ArkIT ~]# cat firstfile
This is a first file created using cat command
[root@ArkIT ~]# cat >> firstfile
Second line of this file
[root@ArkIT ~]# cat firstfile
This is a first file created using cat command
Second line of this file
To append the file content ‘>>’ double grater than we have to use. Type data and press CTRL+d to save.

2. List files and directories

To list files and directories in Linux we have to use ls command 
[root@ArkIT ~]# ls
anaconda-ks.cfg Desktop Documents Downloads file firstfile initial-setup-ks.cfg linux Music Pictures Public rhce Templates Videos
long list with detailed information show. ls -l command will show detailed list of files and directories.
[root@ArkIT ~]# ls -l
total 24
-rw-------. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 17 20:59 Desktop
drwxr-xr-x. 2 root root 6 Mar 17 20:59 Documents
drwxr-xr-x. 2 root root 6 Mar 17 20:59 Downloads
-rw-r--r--. 1 root root 1084 Apr 28 08:16 file
list and sort files based on created date
[root@ArkIT ~]# ls -ltr
total 24
-rw-r--r--. 1 root root 2016 Mar 17 20:58 initial-setup-ks.cfg
-rw-------. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg
-rw-r--r--. 1 root root 1812 Apr 28 05:59 rhce
-rw-r--r--. 1 root root 1227 Apr 28 06:02 linux
-rw-r--r--. 1 root root 1084 Apr 28 08:16 file
-rw-r--r--. 1 root root 72 May 6 22:45 firstfile
ls -ltr means l = long list, t = time, r = recursively 

3. Let you know current working directory

To show in which directory your currently working use pwd commnad
[root@ArkIT ~]# pwd
/root

4.  more – file perusal filter for crt viewing

more command is used to filter for paging through text one screenful at a time.
[root@ArkIT ~]# cat anaconda-ks.cfg |more
[root@ArkIT ~]# more -d anaconda-ks.cfg
[root@ArkIT ~]# more anaconda-ks.cfg

5. copy files and directories from source to destination

To copy the files and directories from one location to other location we have to use cp command
To copy only files use below command. If you use same command for directories will not work, have to use -r option along with cp command
[root@ArkIT ~]# cp firstfile /opt/
[root@ArkIT ~]# cp Music/ /opt/
cp: omitting directory ‘Music/’     <<< --- if you didn't use -r option this error will come
[root@ArkIT ~]# cp -r Music/ /opt/
[root@ArkIT ~]# ls -l /opt/
total 4
-rw-r--r--. 1 root root 72 May 7 07:15 firstfile
drwxr-xr-x. 2 root root 6 May 7 07:17 Music
whenever copying the files & directories we required to preserve time stamps to preserving the time stamps of files & directories use option ‘-p’ along with cp command
[root@ArkIT ~]# ls -l
-rw-------. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg   << -- Original file
[root@ArkIT ~]# cp -p anaconda-ks.cfg /opt/
[root@ArkIT ~]# ls -l /opt/anaconda-ks.cfg
-rw-------. 1 root root 1968 Mar 18 02:26 /opt/anaconda-ks.cfg  <<--- copied="" file="" span="">

ls command with 25 examples

6. Delete files and directories

To delete files and directories use rm command
[root@ArkIT ~]# rm file   <<-- confirmation="" delete="" file="" regular="" span="" with="">
rm: remove regular file ‘file’? y
[root@ArkIT ~]# rm -f firstfile  <<-- asking="" confirmation="" delete="" file="" regular="" span="" without="">
[root@ArkIT ~]# rm -rf Public/  <<-- asking="" confirmation="" delete="" directory="" for="" span="" without="">
To delete normal file use rm command it will ask you for the conformation when deleting. If do not want to prompt any confirmation use -f option. To delete directories use rm -rf .
Note: Be careful whenever your running rm -rf, avoid wildcard * while running rm command, Go to the same directory and run rm -rf using wildcard *.

7. Moving files and directories

Moving files meaning that changing the location of files from one to another path. use mv command 
Syntax: mv [source] [destination]
[root@ArkIT opt]# mv Music/ /root/
mv: overwrite ‘/root/Music’? y

8. Creating New Directories

Most of the guys who are not fimilier with Linux also they know about mkdir command
[root@ArkIT ~]# mkdir /Testing <<-- creating="" directory="" span="" under="">
[root@ArkIT ~]# mkdir test <<-- creating="" current="" directory="" in="" path="" span="">
[root@ArkIT ~]# mkdir -p /Test/Best/Rest/ <<-- collaborative="" creating="" directories="" span=""> 

9. Changing directories

To go from one directory to another directory we have to use cd command
25 most commonly used Linux commands
as you see in above screenshot . (dot) represents current directory. ..(dot dot) represents its parent directory, what is the use of them..?
whenever we use cd command cd ../../../ which means we are going two directories back from current.
[root@ArkIT ~]# cd /tmp/  <<-- directory="" span="" switch="" tmp="" to="">
[root@ArkIT /]# cd /var/log/cups/  <<-- directories="" multiple="" same="" span="" switch="" the="" time="" to="" yet="">
[root@ArkIT cups]# cd ../../../ <<-- back="" directories="" going="" span="" two="">[root@ArkIT /]# cd -
/var/log/cups    <<-- changed="" going="" path="" previously="" span="" style="border: 0px; box-sizing: border-box; color: black; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;" to="">[root@ArkIT cups]# cd ~   <<-- back="" directory="" go="" home="" span="" to="" user="" whereever="" your="">
[root@ArkIT ~]# pwd
/root
~ (tild) is used to switch back to HOME path

10. Deleting empty directories

We can also make use of rm command but some time we would like to delete only empty directories (any directory didn’t contain any data) so rmdir command is very handy whenever we would like to delete empty directories. as a example we have taken two directories One is having few files in it another one is empty.
[root@ArkIT ~]# rmdir /test   <<-- deleted="" directory="" few="" files="" having="" in="" it="" not="" span="">
rmdir: failed to remove ‘/test’: Directory not empty
[root@ArkIT ~]# rmdir /Testing/  <<-- deleted="" directory="" empty="" is="" span="" successfully="">

11. Print files

To print files from Linux command line we can use lpr command
[root@ArkIT ~]# lpr rhce  <<-- default="" file="" print="" printer="" rhce="" span="" to="">
[root@ArkIT ~]# lpr rhce -P PRINTERNAME  <<-- file="" print="" printer="" rhce="" span="" specified="" to="">
[root@ArkIT ~]# lpr -#5 rhce  <<--- 5="" all="" of="" only="" out="" pages="" print="" span="">

12. List who is logged into system and run-levels

show who logged in to server and which run level is running currently 
[root@ArkIT ~]# who
root tty1 2016-05-06 22:06
root pts/0 2016-05-07 07:10 (192.168.4.1)
root pts/1 2016-05-07 09:17 (192.168.4.1)
[root@ArkIT ~]# who -r
run-level 3 2016-05-06 22:05
[root@ArkIT ~]# who -d
[root@ArkIT ~]# who -H
NAME LINE TIME COMMENT
root tty1 2016-05-06 22:06
root pts/0 2016-05-07 07:10 (192.168.4.1)
root pts/1 2016-05-07 09:17 (192.168.4.1)
[root@ArkIT ~]# who -a
system boot 2016-05-06 22:05
run-level 3 2016-05-06 22:05
root + tty1 2016-05-06 22:06 12:22 1604
root + pts/0 2016-05-07 07:10 02:45 3973 (192.168.4.1)
root + pts/1 2016-05-07 09:17 . 4995 (192.168.4.1)

13. Clear screen 

Typing all the commands on screen and its output in screen will look like messy to clear all this output and commands typed use clear command

14. Check disk and partition spaces

To list partition space and its utilization you have to use df command
[root@ArkIT ~]# df -t xfs
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel-root 18307072 3113572 15193500 18% /
[root@ArkIT ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/rhel-root 18317312 109332 18207980 1% /
[root@ArkIT ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 18% /
[root@ArkIT ~]# df -m
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/rhel-root 17878 3041 14838 18% /
df -t – it will list only the partition which are formated with specified file system type
df -i – it will show with inode utilization
df -h – human redable format 
df -m – all partition in MB size

15. Searching for files and its content 

Searching for the content in file without opening the file use grep command
[root@ArkIT ~]# grep commands linux
all unix commands with examples
linux commands cheat sheet
list of linux commands pdf
in above example we were searching for ‘commands’ string in ‘linux’ file
[root@ArkIT ~]# grep -v commands linux
linux lab exercises
linux practice labs
linux high performance computing
above example will exclude the specified string (it means which lines does not contain string will be displayed)
For more and more examples see this

16. Check CPU and Memory utilization

top command is used to checl CPU utilization, memory utilization and more. 
top command detail explanation

17. Stream editor command

sed is stream editor to replace the text in file without opening in text editors, insert lines, delete lines and replace strings
[root@ArkIT ~]# sed -i 's/oldstring/newstring/g' FILENAME
Sed command with 20 practical examples

18. killing processes when struck, hang OR not required

To kill the processes when they struck up, Hang state OR some unwanted processes not required we can kill them
Send an signal using signal id. Signal id 9 means SIGKILL
[root@ArkIT ~]# kill -9 7330
[root@ArkIT ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

19. Listing running processes

when we run an command / script which will generate an process with process ID and CPU will assign an Nice value priority for it.
[root@ArkIT ~]# ps
PID TTY TIME CMD
4995 pts/1 00:00:00 bash
7416 pts/1 00:00:00 ps
[root@ArkIT ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 60032 7804 ? Ss 05:30 0:02 /usr/lib/systemd/systemd --switched-root --sy
root 2 0.0 0.0 0 0 ? S 05:30 0:00 [kthreadd]
[root@ArkIT ~]# ps -U root -u root u  <<-- all="" by="" list="" processes="" root="" running="" span="" user="">
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 60032 7804 ? Ss 05:30 0:02 /usr/lib/systemd/systemd --switched-root --sy
root 2 0.0 0.0 0 0 ? S 05:30 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 05:30 0:00 [ksoftirqd/0]

20. Changing the files and directory permissions

This command most of the Linux administrators know, I think nobody will work as Linux Administrator without knowing this command. The command is chmod command
[root@ArkIT ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg
[root@ArkIT ~]# chmod 760 anaconda-ks.cfg
[root@ArkIT ~]# chmod o+x anaconda-ks.cfg

[root@ArkIT ~]# ls -l
-rwxrw---x. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg
[root@ArkIT ~]# chmod g+x anaconda-ks.cfg
[root@ArkIT ~]# ls -l anaconda-ks.cfg
-rwxrwx--x. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg
We can use chmod command along as numeric numbers and alpha 
chmod command

4 = read

2 = write

1 = execute

u = rwx — User permissions

g = rwx — Group permissions

o = rwx — Other permissions

21. Changing group ownership for files and directories

Changing ownership of files and directories using chown and chgrp command
chown USERNAME:GROUPNAME FILENAME
[root@ArkIT ~]# ls -l anaconda-ks.cfg
-rwxrwx--x. 1 root root 1968 Mar 18 02:26 anaconda-ks.cfg
[root@ArkIT ~]# chown root:project1 anaconda-ks.cfg
[root@ArkIT ~]# ls -l anaconda-ks.cfg
-rwxrwx--x. 1 root project1 1968 Mar 18 02:26 anaconda-ks.cfg
As shown in above example ownership of anaconds-ks.cfg has been changed to project1 group and user root. 
[root@ArkIT ~]# chgrp u1 anaconda-ks.cfg
[root@ArkIT ~]# ls -l anaconda-ks.cfg
-rwxrwx--x. 1 root u1 1968 Mar 18 02:26 anaconda-ks.cfg

22. Creating and extracting compressed files (zip files)

tar is the command used to create archive and extract archive files
[root@ArkIT ~]# tar -czvf test.tar.gz *
anaconda-ks.cfg
Desktop/
Documents/
Downloads/
tar command options

-c = create

-z = gzip type file

-v = verbose

-f = file

-x = extract

[root@ArkIT ~]# tar -xvf test.tar.gz

23. Connecting to remote host using secure shell

In regular times we use this command to connect remote Linux based host. To connect remote host using root user we have to use below command.
[root@ArkIT ~]# ssh root@192.168.4.21
Above comand will not support GUI to connect remote host using GUI support, we have to use below command
[root@ArkIT ~]# ssh -XY root@192.168.4.21

24. Securely copy the files and directories to remote host

To copy the files from present host to remote host we have to use scp command
[root@ArkIT ~]# scp anaconda-ks.cfg root@192.168.4.21:/root/Desktop/

25. cheeking system Date and Time

To check system Date and Time we have to use date command
[root@ArkIT ~]# date
Sat May 7 14:18:32 IST 2016
[root@ArkIT ~]# date +%D:%M:%Y
05/07/16:18:2016
That’s it. 25 most commonly used Linux commands  25 most commonly used Linux commands  25 most commonly used Linux commands  25 most commonly used Linux commands  25 most commonly used Linux commands 

25 most commonly used Linux commands

Conclusion::

In this article we learned cat, ls, pwd, more, cp, rm, mv, mkdir, cd, rmdir, lpr, who, clear, df, grep, top, sed, kill, ps, chmod, chown, tar, ssh, scp commands.
Please provide your valuable feedback on the same  25 most commonly used Lin

No comments:

Post a Comment