Linux Useful Command Soe Thurein Tun
Linux Useful Command
Print the name of the current working directory
[root@techbrown~]# pwd
List of Files and Directories
To see the list of files and directories
[root@techbrown~]# ls <options> <arguments>
Options
-l – Long list including attributes
-a – All files and directories including hidden
-d – For a particular file or directory
-R – Recursive to see the tree structure
Creation of files
Files can be created by using any of the three methods given below:
Cat command
Touch command
Vi editor
Cat (Concatenation) Command
Creating and displayed text files
[root@techbrown~]# cat <option> <arguments><filesname>
To create a file
[root@techbrown~]# cat > <filename>
To view the contents of a file
[root@techbrown~]# cat <files=name>
To append or add to an existing file
[root@techbrown~]# cat >> <filename>
To combines the data of two or more files into a third file
[root@techbrown~]# cat <first file> <second file> >> <third file>
Touch Command
To create a zero byte file
[root@techbrown~]# touch <filename>
To create multiple zero byte files
[root@techbrown~]# touch <first file> <second file> <third file>
To change the time stamp of a file or directory
[root@techbrown~]# touch <directory or filename>
1
Linux Useful Command Soe Thurein Tun
Vi Editor
To create file
[root@techbrown~]# vi <filename>
Creating Directories
To create a directory
[root@techbrown~]# mkdir <directory name>
To create multiple directories
[root@techbrown~]# mkdir <first dir> <second dir> <third dir>
To create nested directories
[root@techbrown~]# mkdir –p <first dir>/<second dir>/<third dir>
Navigation of Directories
To change the directory
[root@techbrown~]# cd <path of the directory>
To change directory one level back
[root@techbrown~]# cd ..
To change directory two levels back
[root@techbrown~]# cd ../..
To change to the last working directory
[root@techbrown~]# cd –
To change to the users home directory
[root@techbrown~]# cd
Help or Manual Page
To view the manual page of a command
[root@techbrown~]# man <command>
Copying
To copy a file or directory
[root@techbrown~]# cp <options> <source file> <destination>
Options
-r – Recursive (to copy the directory along with its contents)
-v – Verbose
-p – Copy with permissions
Moving and Renaming
To move a file or directory to a different location
[root@techbrown~]# mv <source file or directory> <destination>
2
Linux Useful Command Soe Thurein Tun
Rename a file or directory
[root@techbrown~]# mv <old name> <new name>
Deleting
To remove or delete an empty directory
[root@techbrown~]# rmdir <directory name>
To remove or delete a file or directory
[root@techbrown~]# rm <option> <file or directory name>
Options
-r – Recursive (directory along with contents)
-f – forcefully
Some other commands
To see the date
[root@techbrown~]# date
To see the calendar
[root@techbrown~]# cal
File Viewing Commands
To view the contents o f a file screen-wise
[root@techbrown~]# less <file name>
To view the top lines of a file
[root@techbrown~]# head <filename>
[root@techbrown~]# head -5 <filename>
To view the bottom line of a file
[root@techbrown~]# tail <filename>
[root@techbrown~]# tail -3 <filename>
VI editor modes
VI editor has three modes of operations
Command Mode
Insert mode
Ex Mode (Extended Command Mode)
Insert Mode
I – Insert the text at the current cursor position.
l – Insert the text in beginning of a line
a – Adds the text after the current cursor position
A – Adds the text at the end of a line
o – Insert the text one line below current cursor position
O – Insert the text one line above current cursor position
3
Linux Useful Command Soe Thurein Tun
Ex mode
:q – Quit without saving
:q! – Quit forcefully without saving
:w – Write (save)
:wq – Save and quit
:wq! – Save and quit forcefully
:se nu – sets line numbers
:se nonu– Remove line numbers
:84 – The cursor goes to line 84
Command Mode
dd – Deletes a line
ndd – Deletes ‘n’ lines (eg. 20dd)
yy – Copies a line
nyy – Copies ‘n’ lines
p – Put (pastes the deleted or copied text)
u – Undo(you can undo 1000 times)
G – Moves the cursor to the last line of the file
Uptime Command
In Linux uptime command shows since how long your system is running and the number of
users are currently logged in and also displays load average for 1,5 and 15 minutes intervals.
# uptime
08:16:26 up 22 min, 1 user, load average: 0.00, 0.03, 0.22
Check Uptime Version
Uptime command don’t have other options other than uptime and version. It gives
information only in hours:mins if it less than 1 day.
[tecmint@tecmint ~]$ uptime -V
procps version 3.2.8
W Command
It will displays users currently logged in and their process along-with shows load averages.
also shows the login name, tty name, remote host, login time, idle time, JCPU, PCPU,
command and processes.
#w
08:27:44 up 34 min, 1 user, load average: 0.00, 0.00, 0.08
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint pts/0 192.168.50.1 07:59 0.00s 0.29s 0.09s w
4
Linux Useful Command Soe Thurein Tun
Users Command
Users command displays currently logged in users. This command don’t have other
parameters other than help and version.
# users
tecmint
Who Command
who command simply return user name, date, time and host information. who command is
similar to w command. Unlike w command who doesn’t print what users are doing. Lets
illustrate and see the different between who and w commands.
# who
tecmint pts/0 2012-09-18 07:59 (192.168.50.1)
#w
08:43:58 up 50 min, 1 user, load average: 0.64, 0.18, 0.06
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint pts/0 192.168.50.1 07:59 0.00s 0.43s 0.10s w
Who command Options
1. -b : Displays last system reboot date and time.
2. -r : Shows current runlet.
3. -a, –all : Displays all information in cumulatively.
Whoami Command
whoami command print the name of current user. You can also use “who am i” command to
display the current user. If you are logged in as a root using sudo command “whoami”
command return root as current user. Use “who am i” command if you want to know the
exact user logged in.
# whoami
tecmint
ls Command
ls command display list of files in human readable format.
# ls -l
total 114
dr-xr-xr-x. 2 root root 4096 Sep 18 08:46 bin
dr-xr-xr-x. 5 root root 1024 Sep 8 15:49 boot
Sort file as per last modified time.
# ls -ltr
total 40
-rw-r--r--. 1 root root 6546 Sep 17 18:42 install.log.syslog
-rw-r--r--. 1 root root 22435 Sep 17 18:45 install.log
-rw-------. 1 root root 1003 Sep 17 18:45 anaconda-ks.cfg
5
Linux Useful Command Soe Thurein Tun
Less Command
less command allows quickly view file. You can page up and down. Press ‘q‘ to quit from
less window.
# less install.log
Installing setup-2.8.14-10.el6.noarch
warning: setup-2.8.14-10.el6.noarch: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Installing filesystem-2.4.30-2.1.el6.i686
Installing ca-certificates-2010.63-3.el6.noarch
Installing xml-common-0.6.3-32.el6.noarch
Installing tzdata-2010l-1.el6.noarch
Installing iso-codes-3.16-2.el6.noarch
More Command
more command allows quickly view file and shows details in percentage. You can page up
and down. Press ‘q‘ to quit out from more window.
# more install.log
Installing setup-2.8.14-10.el6.noarch
warning: setup-2.8.14-10.el6.noarch: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Installing filesystem-2.4.30-2.1.el6.i686
Installing ca-certificates-2010.63-3.el6.noarch
Installing xml-common-0.6.3-32.el6.noarch
Installing tzdata-2010l-1.el6.noarch
Installing iso-codes-3.16-2.el6.noarch
--More--(10%)
CP Command
Copy file from source to destination preserving same mode.
# cp -p fileA fileB
MV Command
Rename fileA to fileB. -i options prompt before overwrite. Ask for confirmation if exist
already.
# mv -i fileA fileB