Linux - Basic Command                                 Home : www.sharetechnote.com

 

 

 

This page will list the most basic Linux commands that you would use most commonly and the list will get longer as times goes. There are a group of commands that are pretty often used but not in this page, e.g, Network/IP related command or System Management commands. These commands are not listed here because they will be posted as a separate page.

 

Linux Command

DOS Command

Description

ls

dir

List the files in a directory

Usage : 'ls' or 'ls directoryname'

pwd

 

Display the current path (location of current directory)

cd

cd

Change directory

cp

copy

Copy a file

rm

del

 

mkdir

mkdir

 

cat

type

Display a contents of a file

sudo

 

provide the super user previlege

mount

 

map an hardware to a specific directory

whoami

 

Display the current user

ps   Display the list of processes currently running on a machine
kill   kill a specific process
chmod   change access property (read, write, executable) for a file or a directory
grep find find a specified string from a file or the output of other command
echo $PATH   shows the PATH that has been specified in the system
reset   clear screen (similar to 'cls' in DOS command)
./ (dot slash)   This is not a command.. it indicates 'current directory'
fdisk -l   list the information of all the disk information that are connected
lsmod   list the status of all the modules (device drivers) in the Linux Kernel
apt-get  

a command-line tool for working with APT(Advanced Packaging Tool) software packages.

apt-file   search a package name that contains a specified file
yum  

YUM (Yellowdog Updater Modified) is an open source command-line as well as graphical based package management tool for RPM (RedHat Package Manager) based Linux systems

dd diskdump dump a specified area of a disk into a file (binary file) or consol
xxd   view (print out) the contents of a binary file
lscpu   show (list) the information about the CPU

lsblk

 

prints all block devices (except RAM disks)

objdump

  convert an executable binary file to assembly code
uname -a   show the linux kernel information
lsb_release -a   show the linux distribution information
dmesg   shows the boot sequenec log
which   find the path of a specified file/link (mostly executable files)
shutdown   shutdown or reboot
     
     
     

 

 

ls

: List the files in a directory

 

Example :

 

$ ls

 

bin    dev   initrd.img      lib64       mnt   root  selinux  tmp  vmlinuz

boot   etc   initrd.img.old  lost+found  opt   run   srv      usr  vmlinuz.old

cdrom  home  lib             media       proc  sbin  sys      var

 

 

Example :

 

$ ls -al

 

drwxr-xr-x  24 root root  4096 May 30 02:48 .

drwxr-xr-x  24 root root  4096 May 30 02:48 ..

drwxr-xr-x   2 root root  4096 May 30 23:45 bin

 

''''''

drwxr-xr-x  10 root root  4096 Feb 13 17:07 usr

drwxr-xr-x  13 root root  4096 May 30 09:18 var

lrwxrwxrwx   1 root root    29 May 30 02:48 vmlinuz -> boot/vmlinuz-3.5.0-31-generic

lrwxrwxrwx   1 root root    29 May 30 09:57 vmlinuz.old -> boot/vmlinuz-3.5.0-23-generic

 

 

Example :

 

$ ls -R  <-- this shows the list in all the sub directories ('R' stands for Recursive)

 

 

Example :

 

$ ls -al | more

 

drwxr-xr-x  24 root root  4096 May 30 02:48 .

drwxr-xr-x  24 root root  4096 May 30 02:48 ..

drwxr-xr-x   2 root root  4096 May 30 23:45 bin

 

''''''

drwxr-xr-x  10 root root  4096 Feb 13 17:07 usr

drwxr-xr-x  13 root root  4096 May 30 09:18 var

lrwxrwxrwx   1 root root    29 May 30 02:48 vmlinuz -> boot/vmlinuz-3.5.0-31-generic

lrwxrwxrwx   1 root root    29 May 30 09:57 vmlinuz.old -> boot/vmlinuz-3.5.0-23-generic

-- More -- <-- It shows next line as you press [Enter]. Press 'Q' to quit

 

 

Example :

 

$ ls /var

 

backups  cache  crash  games  lib  local  lock  log  mail  opt  run  spool  tmp

 

 

pwd

: Shows the path for the current directory

 

Example

 

#pwd

 

/home/jaeku

 

 

sudo

: Provide the super user previlege

 

Example :

 

$ ifconfig eth0 192.168.1.2 up

 

SIOCSIFADDR: Permission denied  <-- this Linux does not allow to change ifconfig
SIOCSIFFLAGS: Permission denied

SIOCSIFFLAGS: Permission denied

 

$ sudo ifconfig eth0 192.168.1.2 up <-- try the same command with super user previlege

 

[sudo] password for jaeku: <-- type in the password and then it will allow the command to be executed

 

 

rm

: delete a file or directory

 

Example :

 

# rm usbstick

 

Example : you may get an error message when you remove a directory with rm as shown below. In that case, you may use '-r' or '-rf' option to remove the whole directory.

 

# rm usbstick

 

rm: cannot remove `usbstick': Is a directory <-- rm failed to remove usbstick

 

# rm -r usbstick <-- try with '-r' option

 

# rm -rf usbstick <-- try with '-rf' option

 

 

cat

: View the contents of a file

 

Example :

 

$ cat temp.txt

 

Contents of 'temp.txt' will show up here

....

 

 

Example :

 

$ cat file1 file2

 

Contents of file1 and file2 will show up in sequence

....

 

 

Example :

 

$ cat file1 file2 > file3

 

Contents of file1 and file2 will be out to file3. In other words, file1 and file2 concatenated and create file3.

....

 

 

mount

: map an hardware to a specific directory.

 

Example 1 : map a usb stick to a directory (This is done on Ubuntu)

 

Step 1: Create a directory to be mapped to a IO device (you can use an existing directory as well)

 

# mkdir /mnt/usbstick

 

Step 2: Make it sure that the directory is created properly.

 

# ls /mnt

 

usbstick

 

Step 3: Make it sure that the directory is created properly.

 

# mount -t vfat -o rw,users /dev/sda2 /mnt/usbstick

<-- with vfat, you can mount usbstick which is formated on Windows

<-- /dev/sda2 is the usb port that my usb stick is plugged in. you have to make it sure that you have specified the right port number. you may try 'fdisk -l' command to figure out which device you have to use for this mount

 

Step 4: check if it is properly mounted. The simplest way would be to use 'ls' and see if it shows the contents that is stored in the stick.

 

# ls /mnt/usbstick

 

temp.txt

 

 

ps

: Display the list of processes currently running on a machine. It is same as "Processe" tab on Task Manager in Windows.

 

Example :

 

# ps

 

PID     TTY       TIME   CMD

2079   pts/1    00:00:00 su

2087   pts/1    00:00:00 bash

16819  pts/1    00:00:00 ps

 

 

Example :

 

# ps -al  <-- Note 'a' is in lower case

 

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

4 S     0  2079  2021  0  80   0 - 15478 wait   pts/1    00:00:00 su

0 S     0  2087  2079  0  80   0 -  6810 wait   pts/1    00:00:00 bash

0 R     0 16822  2087  0  80   0 -  3482 -      pts/1    00:00:00 ps

 

 

Example :

 

# ps -Al  <-- Note 'a' is in upper case

 

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

4 S     0     1     0  0  80   0 -  6117 poll_s ?        00:00:01 init

1 S     0     2     0  0  80   0 -     0 kthrea ?        00:00:00 kthreadd

1 S     0     3     2  0  80   0 -     0 run_ks ?        00:00:07 ksoftirqd/0

1 S     0     6     2  0 -40   - -     0 cpu_st ?        00:00:01 migration/0

1 S     0     7     2  0 -40   - -     0 watchd ?        00:00:00 watchdog/0

1 S     0    20     2  0  60 -20 -     0 rescue ?        00:00:00 cpuset

1 S     0    21     2  0  60 -20 -     0 rescue ?        00:00:00 khelper

5 S     0    22     2  0  80   0 -     0 devtmp ?        00:00:00 kdevtmpfs

...

4 S     0   837     1  0  80   0 - 19760 poll_s ?        00:00:00 modem-manager

5 S     0   838     1  0  80   0 -  5297 poll_s ?        00:00:00 bluetoothd

5 S     0   849     2  0  70 -10 -     0 rfcomm ?        00:00:00 krfcommd

5 S     0   851     1  0  80   0 - 61343 poll_s ?        00:00:24 NetworkManager

.....

0 S  1000  1729  1721  0  80   0 -  2845 unix_s ?        00:00:00 cat

....

0 S  1000  2021  2009  0  80   0 -  6799 wait   pts/1    00:00:00 bash

4 S     0  2079  2021  0  80   0 - 15478 wait   pts/1    00:00:00 su

0 S     0  2087  2079  0  80   0 -  6810 wait   pts/1    00:00:00 bash

....

0 S  1000 14645     1  3  80   0 - 264797 poll_s ?       00:22:43 firefox

 

 

kill

: kill a specific process

 

Example :

 

#kill 14645  

<-- 14645 in this example is the PID for the process you want to kill. You have to figure out the PID using ps command

 

 

chmod

: change access property of a file or directory

 

Example :

 

#chmod 777 myfile

<-- set the access property of the file named 'myfile' to be 'readable, writeable, executable' to all users, all groups.

 

 

Example :

 

#chmod 666 myfile

<-- set the access property of the file named 'myfile' to be 'readable, writeable, Not executable' to all users, all groups.

 

 

Example :

 

#chmod 555 myfile

<-- set the access property of the file named 'myfile' to be 'readable, Not writeable, executable' to all users, all groups.

 

 

grep

: find a specified string from a file or the output of other command

 

Example :

 

#grep hello mytext

<-- search the string 'hello' from the file named 'mytext' and displays the lines containing the string 'hello'.

 

 

Example :

 

#ls -Al | grep hello

<-- execute 'ls -Al' and find the string 'hello' from the output of 'ls -Al' and displays the line containing th string 'hello'.

This may be the most common example of 'grep' command.

 

 

./ (dot slash)

: Indicate 'current directory'. When we execute a specific program, we normally type in the 'filename' and press enter. If the executable file is located in a directory specified '$PATH', it executes.. but if it is not in one of these PATH, the system would shows error saying 'No command 'xxxxx' found'. In this case, you have to specify the explicit path(location) of the executable before the filename to run it.

If you are currently at the directory where the executable, you can just put "./" before the filename.

 

Example :

 

#./myprogram

<-- run the executable named 'myprogram' located in 'current directory'. If 'myprogram' is not in the current directory, the system would show 'No command 'xxxxx' found'.

 

 

fdisk -l

 

This command lists information on all the storage device that are currently connected to the PC. it will be helpful to figure out device name to mount for external storage device (e.g, usb memory stick etc)

 

Example 1 >

 

# fdisk -l

<-- this would run when you logged in as root, but it may not work if you are not in root account

 

Example 2 >

 

# sudo fdisk -l

<-- you may try this if you are not logged in as root

 

 

lsmod

 

This command list the status of all the modules (device drivers) in the Linux Kernel

 

Example 1 >

 

# lsmod

 

Module                  Size  Used by

tcp_lp                 12663  0

ip6table_filter        12815  0

ip6_tables             26808  1 ip6table_filter

ebtable_nat            12807  0

ebtables               30758  1 ebtable_nat

sctp                  317063  18

libcrc32c              12603  1 sctp

tun                    27153  2

ipt_MASQUERADE         12880  4

iptable_nat            13011  1

nf_conntrack_ipv4      14656  1

nf_defrag_ipv4         12702  1 nf_conntrack_ipv4

nf_nat_ipv4            13199  1 iptable_nat

nf_nat                 25112  3 ipt_MASQUERADE,nf_nat_ipv4,iptable_nat

nf_conntrack           95080  5 ipt_MASQUERADE,nf_nat,nf_nat_ipv4,iptable_nat,nf_conntrack_ipv4

joydev                 17332  0

btusb                  28290  0

bluetooth             408436  2 btusb

hid_logitech_dj        18581  0

6lowpan_iphc           18591  1 bluetooth

mxm_wmi                12865  0

coretemp               13475  0

kvm_intel             143109  0

kvm                   460312  1 kvm_intel

....

 

 

apt-get

 

This is a command-line tool for working with APT(Advanced Packaging Tool) software packages. I think this is one of the most covinient command to install a new software on various linux family (e.g, Ubuntu). Basic syntax is as shown below. Refer to Linux and Unix apt-get command for the detailed tutorial.

 

apt-get [-asqdyfmubV] [-o=config_string] [-c=config_file] [-t=target_release]

        [-a=architecture] {update | upgrade | dselect-upgrade | dist-upgrade |

        install pkg [{=pkg_version_number | /target_release}]... |

        remove pkg... | purge pkg... |

        source pkg [{=pkg_version_number | /target_release}]... |

        build-dep pkg [{=pkg_version_number | /target_release}]... |

        download pkg [{=pkg_version_number | /target_release}]... |

        check | clean | autoclean | autoremove | {-v | --version} |

        {-h | --help}}

 

Since most of programming installation requires super user authority, so in most case you would use like 'sudo apt-get xxxxxx'.

 

Example >

 

I don't put any specific example here. The best way to use the command is to try Google 'How to install xxxx program (e.g, Chrome) in linux (e.g, Ubuntu) ?' and copy the whole command from the searched page and run it in your linux.

 

 

apt-file

: When you are running a certain program, there are cases where you get errors saying 'a xxxx file is missing'. In this case, you need to install a package that containing the missing file. Now the question is 'how can I figure out the package name that contains the missing file ?'. apt-file is the tool you need to use to find the info.

 

Note : the tool apt-file might have not been installed on your PC by defailt. In that case, you need to install the tool first by following command.

 

# sudo apt install apt-file

 

If you just installed the apt-file, you need to do following to download all the package info into your PC. Otherwise, apt-file would not be able to search the information that you need.

 

# apt-file update

 

Example >

: I will show you a sample case in which how and when you need to use this command

 

# ./qt-linux-opensource-5.0.2-x86-offline.run

// I executed a program as shown here (you would run some other program as you need. this is just my example)

// and I got following error

 

./qt-linux-opensource-5.0.2-x86-offline.run: error while loading shared libraries: libXrender.so.1: cannot open shared object file: No such file or directory

// it says libXrender.so.1 is missing. So I want to find out which package has this file using following command

 

# apt-file search libXrender.so.1

// I got following result

 

libxrender1: /usr/lib/x86_64-linux-gnu/libXrender.so.1

libxrender1: /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0

libxrender1-dbg: /usr/lib/debug/usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0

 

Then I install the package as follows

 

# sudo apt-get install libxrender1:i386

 

 

yum

 

YUM (Yellowdog Updater Modified) is an open source command-line as well as graphical based package management tool for RPM (RedHat Package Manager) based Linux systems.

 

Basic syntax of yum command is as follows. (put yum in the place of *). Refer to yum-LinuxCommand.org for detailed usage.

    * install package1 [package2] [...]

    * update [package1] [package2] [...]

    * check-update

    * upgrade [package1] [package2] [...]

    * remove | erase package1 [package2] [...]

    * list [...]

    * info [...]

    * provides | whatprovides feature1 [feature2] [...]

    * clean [ packages | headers | metadata | cache | dbcache | all ]

    * makecache

    * groupinstall group1 [group2] [...]

    * groupupdate group1 [group2] [...]

    * grouplist [hidden]

    * groupremove group1 [group2] [...]

    * groupinfo group1 [...]

    * search string1 [string2] [...]

    * shell [filename]

    * resolvedep dep1 [dep2] [...]

    * localinstall rpmfile1 [rpmfile2] [...]

    * localupdate rpmfile1 [rpmfile2] [...]

    * deplist package1 [package2] [...]

 

Example >

 

I don't put any specific example here. The best way to use the command is to try Google 'How to install xxxx program (e.g, Chrome) in linux (e.g, Fedora) ?' and copy the whole command from the searched page and run it in your linux.

 

 

lscpu

 

This command list (shows) the CPU information of the PC on which you are running the Linux.

 

Example :

 

# lscpu

 

Architecture:          x86_64

CPU op-mode(s):        32-bit, 64-bit

Byte Order:            Little Endian

CPU(s):                4

On-line CPU(s) list:   0-3

Thread(s) per core:    2

Core(s) per socket:    2

Socket(s):             1

NUMA node(s):          1

Vendor ID:             GenuineIntel

CPU family:            6

Model:                 42

Stepping:              7

CPU MHz:               800.000

BogoMIPS:              2993.22

Virtualization:        VT-x

L1d cache:             32K

L1i cache:             32K

L2 cache:              256K

L3 cache:              3072K

NUMA node0 CPU(s):     0-3

 

 

lsblk

 

This command prints all block devices (except RAM disks) in a tree format (by default)

 

Example :

 

#lsblk

 

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT

sda      8:0    0 465.8G  0 disk

+-sda1   8:1    0   149G  0 part

+-sda2   8:2    0   161G  0 part

+-sda3   8:3    0     1K  0 part

+-sda5   8:5    0 151.8G  0 part /

+-sda6   8:6    0   3.9G  0 part [SWAP]

sr0     11:0    1   3.5G  0 rom  /media/ResourceCD

 

 

objdump

 

This command convert an executable binary file to assembly code. The format of the command is as follows :

    objdump <options> <file>

 

Example :

 

# objdump -D -Mintel,x86-64 -b binary -m i386 -Maddr32,data32 sda-mbr.bin

<-- convert sda-mbr.bin file into assembly code (display sda-mbr.bin in assembly code format) with using the options : -D -Mintel,x86-64 -b binary -m i386 -Maddr32,data32

 

sda-mbr.bin:     file format binary

 

Disassembly of section .data:

 

00000000 <.data>:

   0:   eb 63                   jmp    0x65

   2:   90                      nop

        ...

  3b:   00 00                   add    BYTE PTR [eax],al

  3d:   00 03                   add    BYTE PTR [ebx],al

  3f:   02 ff                   add    bh,bh

  41:   00 00                   add    BYTE PTR [eax],al

  43:   20 01                   and    BYTE PTR [ecx],al

  45:   00 00                   add    BYTE PTR [eax],al

....

 

 

which

 

This command finds path+filename of a specified file (mostly an executable program). This command searches the files through the locations specifed in PATH parameter of the operating system

 

Example :

 

# which gcc

<-- find the path where gcc is located

 

/usr/bin/gcc

 

 

shutdown

 

You can shutdown or reboot the linux using various options of this command. Try "shutdown --help" to find the details.

 

Example :

 

#sudo shutdown -h now

<-- shutdown (power off) the PC

 

 

Example :

 

#sudo shutdown -r now

<-- reboot the PC