Search

Basic Linux commands

The following basic commands are necessary to start working with Linux BASH shell. The Linux commands and the worked out examples are shown one by one.

tty

The Linux "tty" command display the name of the connected terminal.

Example:

[root@RHEL01 ~]# tty
/dev/pts/0

Note: The above terminal displayed is a pseudo-terminal.

which

The Linux "which" command prints where in the search path an executable binary is located.

Example:

[root@RHEL01 ~]# which mail
/bin/mail

whoami

The Linux "whoami" command displays who is the current user

Example:

[root@RHEL01 ~]# whoami
root

echo

The Linux "echo" command is used to print to the terminal.

 Example 1 (Prints the given string to the terminal):

[root@RHEL01 ~]# echo "hell world"
hell world

Example 2 (Prints the variable $USER to the terminal):

[root@RHEL01 ~]# echo $USER
root

Example 3 (Prints the variable $PWD to the terminal):

[root@RHEL01 ~]# echo $PWD
/root

clear

The Linux "clear" command clears the terminal.

reset

The Linux "reset" command resets the screen buffer

history

The Linux "history" command displays the command history. The Linux "history" command will display the entire command history. To display a number of previous commands which are remembered, run the history command with a number. The BASH command history is stored in ".bash_hostory", which is a hidden file at the home directory of the Linux user. Usually the home directory of normal Linux user is "/home/<user_name>", and the home directory of the super user is "/root". Note that the up and down arrow keys can be used to navigate through your command history.

Example 1 (This command will display ten commands executed previously) :

[root@RHEL01 ~]# history 10
387 fdisk /dev/sdb
388 shut
389 fdisk -l
390 shut
391 echo $USER
392 reset
393 echo $PATH
394 reset
395 echo $PWD
396 history 10

Example 2 (This command will run the 387th command stored in history)

[root@RHEL01 ~]# !387
fdisk /dev/sdb

The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):

Example 3 (This command will run the previous command in history)

[root@RHEL01 ~]# !!
fdisk /dev/sdb

The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):


Note: Another command to manage hitory is fc. Using fc, you open the chosen command from history using the vi editor. The edited command runs when you exit the editor.

find

The Linux "find" command can be used to find files using search patterns.

Example:

 [root@RHEL01 ~]# find / -name messages
/var/log/messages

locate

The Linux "locate" command can be used to find the files fast in a Linux machine. The locate command make use of a index database, /var/lib/mlocate/mlocate.db. This index can be made current by using ‘updatedb’ command.

Example:

[root@RHEL01 ~]# updatedb
[root@RHEL01 ~]# locate messages
/var/log/messages

pwd

The Linux pwd command show the name of the present working directory

Example:

[root@localhost bin]# pwd
/usr/bin

date

The Linux date command print or set the system date and time

Example:

[root@localhost bin]# date
Sat Dec 26 11:49:39 IST 2009

shutdown

The Linux shutdown command can be used to shutdown the system (halt) or restart the system.

Example (The first command shuts the computer immediatly and second command restarts the computer immediatly:

[root@localhost bin]# shutdown -h now

[root@localhost bin]# shutdown -r now

cat

The Linux cat command sends file contents to standard output. This is a way to list the contents of short files to the screen.

Example:

[root@localhost ~]# cat .bash_history

cd

The Linux cd command can be used to change the working directory.

Examples:

[root@localhost ~]#cd /home (Changes the working directory to /home)

[root@localhost ~]#cd .. (Changes the working directory to the parent directory)

[root@localhost ~]#cd~(Changes the working directory to the users home directory)

mv

The Linux mv command is used to move a file from one location to another location. Please refer the following link to learn more about the Linux mv command.

cp

The Linux cp command is used to copy a file from one location to another location. Please refer the following link to learn more about the Linux cp command.

df

The Linux df command provides a very quick check of your file system disk space. Type df -h to get a more easily readable version of the output.

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 14G 2.3G 11G 18% /
/dev/sda1 99M 9.9M 84M 11% /boot
tmpfs 252M 0 252M 0% /dev/shm

finger

The Linux finger command allows you to see who else is on the computer or get detailed information about a user who has access to the system.

The following Linux finger command displays the who else are logged in on the computer currently.

[root@localhost ~]# finger
Login Name Tty Idle Login Time Office Office Phone
tintin tty3 Dec 27 12:20

The following Linux finger commad displays detailed information about the user tintin.

[root@localhost ~]# finger tintin
Login: tintin Name: (null)
Directory: /home/tintin Shell: /bin/bash
Never logged in.
No mail.
No Plan.

uptime

The Linux uptime command returns useful system utilization information like . current time, uptime in days, hours and minutes, connected users, load averaged - 1,5,15 minute values.

[root@localhost ~]# uptime
12:25:01 up 15 min, 1 user, load average: 0.17, 0.16, 0.16

free

The Linus free command returns memory utilization like Physical Memory (RAM), . Virtual Memory (SWAP), The free command with option, “free –m” displays the output in user friendly format (in MBs)

[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 503 466 37 0 34 355
-/+ buffers/cache: 76 426
Swap: 1027 0 1027

more and less

The Linux "more" and "less" commands are paginators, which display text one-page at a time. Less allows backward scrolling also.

file

The Linux file command can be used to determine file type.

[root@localhost ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII English text

Related Tutorials