Linux important commands illustrations examples


Advertisements

 |  
    In this article we will explain the usage of important linux commands with examples. We have divided all the commands into categories and we will illustrate them one by one using a linux operating system.
    I have executed the commands using a raspberry pi. You can buy a low cost Raspberry Pi Zero WH (Zero W with Headers) or CanaKit Raspberry Pi 3 B+ (B Plus) Starter Kit (32 GB EVO+ Edition, Premium Black Case) or EleDuino BeagleBone Black Rev C (4G) Single Board Computer Development Board to execute the commands listed on this article. You can also install linux on windows using virtualbox simulation environment to execute commands used in this article.

Article contents

File System Commands

System Info Commands

Process Management Commands


File System Commands

ls, ls -al, ls -lt
    ls is a basic linux file system command to list down the files and directories in the current directory. ls will not show the timing of change of the file and directory content and their permissions. You can see the permissions using ls -al. In the illustration below you can see Calculator is having read, write and executable permission where as add.c is having read and write permission. ls -lt will list the content same as ls -al but it will sort the formatted listing by time modification. As you can see from the illustration below Calculator is modified last at 04.41 hence listed at the top and add.c is listed at the bottom.
Linux file system command illustrations

pi@raspberrypi:~/C $ ls
add.c  Calculator  divide.c  libcalc.so  multiply.c  subtract.c
add.o  common.h    divide.o  main.c      multiply.o  subtract.o
pi@raspberrypi:~/C $ ls -al
total 68
drwxr-xr-x  2 pi pi 4096 Jun 28 04:41 .
drwxr-xr-x 31 pi pi 4096 Jun 27 07:50 ..
-rw-r--r--  1 pi pi   42 Jun 28 04:24 add.c
-rw-r--r--  1 pi pi  860 Jun 28 04:37 add.o
-rwxr-xr-x  1 pi pi 8380 Jun 28 04:41 Calculator
-rw-r--r--  1 pi pi   94 Jun 28 04:27 common.h
-rw-r--r--  1 pi pi   49 Jun 28 04:25 divide.c
-rw-r--r--  1 pi pi  952 Jun 28 04:37 divide.o
-rwxr-xr-x  1 pi pi 7968 Jun 28 04:34 libcalc.so
-rw-r--r--  1 pi pi  343 Jun 28 04:40 main.c
-rw-r--r--  1 pi pi   51 Jun 28 04:25 multiply.c
-rw-r--r--  1 pi pi  872 Jun 28 04:37 multiply.o
-rw-r--r--  1 pi pi   47 Jun 28 04:24 subtract.c
-rw-r--r--  1 pi pi  872 Jun 28 04:37 subtract.o
pi@raspberrypi:~/C $ ls -lt
total 60
-rwxr-xr-x 1 pi pi 8380 Jun 28 04:41 Calculator
-rw-r--r-- 1 pi pi  343 Jun 28 04:40 main.c
-rw-r--r-- 1 pi pi  952 Jun 28 04:37 divide.o
-rw-r--r-- 1 pi pi  872 Jun 28 04:37 multiply.o
-rw-r--r-- 1 pi pi  872 Jun 28 04:37 subtract.o
-rw-r--r-- 1 pi pi  860 Jun 28 04:37 add.o
-rwxr-xr-x 1 pi pi 7968 Jun 28 04:34 libcalc.so
-rw-r--r-- 1 pi pi   94 Jun 28 04:27 common.h
-rw-r--r-- 1 pi pi   49 Jun 28 04:25 divide.c
-rw-r--r-- 1 pi pi   51 Jun 28 04:25 multiply.c
-rw-r--r-- 1 pi pi   47 Jun 28 04:24 subtract.c
-rw-r--r-- 1 pi pi   42 Jun 28 04:24 add.c
pi@raspberrypi:~/C $

pwd, cd, cd dir, mkdir dir, touch, rm, rm -rf, cp, mv
    pwd is the command to see your current working directory. in the illustration below it shows I am in /home/pi/C directory. cd ../ will move one directory down where as cd dir Change directory to dir. Similaryly ../../ will change directory to two levels down.
    mkdir dir will create a new directory dir where as rmdir dir will delete directory dir from your current directory. rm file will delete a file. Also note that I have created a directory New and created a file inside it using touch command and then tried to delete the New directory. But it failed saying directory is not empty. In this case we use -rf option to force directory deletion.
    cp is the command used to move a file or directory from one directory to another. In the illustration I have moved the subtract.c from current directory to /home/pi/try directory. mv command is used to rename a file. In the illustration file.c is renamed as file1.c.
Linux file system command illustrations

pi@raspberrypi:~/C $ pwd
/home/pi/C
pi@raspberrypi:~/C $ cd ..
pi@raspberrypi:~ $ pwd
/home/pi
pi@raspberrypi:~ $ cd C
pi@raspberrypi:~/C $ pwd
/home/pi/C
pi@raspberrypi:~/C $
pi@raspberrypi:~/C $ cd ../../
pi@raspberrypi:/home $ pwd
/home
pi@raspberrypi:/home $
pi@raspberrypi:/home $ cd pi/C
pi@raspberrypi:~/C $ mkdir NewDir
pi@raspberrypi:~/C $ ls
add.c  add.o  Calculator  common.h  divide.c  divide.o  libcalc.so  main.c  multiply.c  multiply.o  NewDir  subtract.c  subtract.o
pi@raspberrypi:~/C $ rmdir NewDir/
pi@raspberrypi:~/C $ ls
add.c  add.o  Calculator  common.h  divide.c  divide.o  libcalc.so  main.c  multiply.c  multiply.o  subtract.c  subtract.o
pi@raspberrypi:~/C $
pi@raspberrypi:~/C $ rm Calculator
pi@raspberrypi:~/C $ ls
add.c  add.o  common.h  divide.c  divide.o  libcalc.so  main.c  multiply.c  multiply.o  subtract.c  subtract.o
pi@raspberrypi:~/C $
pi@raspberrypi:~/C $ mkdir New
pi@raspberrypi:~/C $ ls
add.c  add.o  common.h  divide.c  divide.o  libcalc.so  main.c  multiply.c  multiply.o  New  subtract.c  subtract.o
pi@raspberrypi:~/C $ cd New
pi@raspberrypi:~/C/New $ touch file.c
pi@raspberrypi:~/C/New $ vi file.c
pi@raspberrypi:~/C/New $ vi file.c
pi@raspberrypi:~/C/New $ cd ..
pi@raspberrypi:~/C $ rmdir New/
rmdir: failed to remove 'New/': Directory not empty
pi@raspberrypi:~/C $
pi@raspberrypi:~/C $ rm -rf New
pi@raspberrypi:~/C $ ls
add.c  add.o  common.h  divide.c  divide.o  libcalc.so  main.c  multiply.c  multiply.o  subtract.c  subtract.o
pi@raspberrypi:~/C $ ls
add.c  add.o  common.h  divide.c  divide.o  libcalc.so  main.c  multiply.c  multiply.o  subtract.c  subtract.o
pi@raspberrypi:~/C $ cp subtract.c /home/pi/try/
pi@raspberrypi:~/C $ cd /home/pi/try/
pi@raspberrypi:~/try $ ls
a.out  file.c  subtract.c
pi@raspberrypi:~/try $ mv file.c file1.c
pi@raspberrypi:~/try $ ls
a.out  file1.c  subtract.c
pi@raspberrypi:~/try $
cat, tail, more, head
    cat command is used to display the content of a file. tail file will output the last 10 lines of the file where as head file will output first 10 lines of the file. more file will Output the contents of the file entirely.
cat, tail, more, head command illustrations

pi@raspberrypi:~/try $ ls
a.out  file1.c  subtract.c
pi@raspberrypi:~/try $ cat subtract.c
int subtract(int a,int b)
{
        return a - b;
}pi@raspberrypi:~/try $ tail subtract.c
int subtract(int a,int b)
{
        return a - b;
}pi@raspberrypi:~/try $ head file1.c
#include <stdio.h>
void NumOfWords(char *s, int );
int main() {
        char str[] = "Hello how are you";
        int res = 1;
        NumOfWords(str,res);
        printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int r)
{
pi@raspberrypi:~/try $
pi@raspberrypi:~/try $ more file1.c
#include <stdio.h>
void NumOfWords(char *s, int );
int main() {
        char str[] = "Hello how are you";
        int res = 1;
        NumOfWords(str,res);
        printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int r)
{
  int i = 0;
  while(s[i] != '\0')
  {
    if(s[i] == ' ')
    {
        r = r + 1;
    }
    i++;
  }
}

pi@raspberrypi:~/try $

date, cal, uptime, whoami, uname -a, cat /proc/ df, du
  • date:Show the current date and time
  • cal:Show this month's calender
  • uptime:Show current uptime
  • whoami:Who you are logged in as
  • uname -a:Show kernel information
  • cat /proc/cpuinfo:Display cpu information
  • cat proc/meminfo:Display memory information
  • df:Show the disk usage
  • du:Show directory space usage
  • free:Show memory and swap usage

date, cal, uptime, whoami, uname -a, cat /proc/ df, du command illustrations

pi@raspberrypi:~ $ date
Sun 28 Jun 10:01:38 UTC 2020
pi@raspberrypi:~ $ cal
     June 2020
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

pi@raspberrypi:~ $
pi@raspberrypi:~ $ uptime
 10:04:17 up 1 day,  1:59,  3 users,  load average: 0.00, 0.00, 0.00
pi@raspberrypi:~ $
pi@raspberrypi:~ $ whoami
pi
pi@raspberrypi:~ $
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l GNU/Linux
pi@raspberrypi:~ $
pi@raspberrypi:~ $ cat /proc/cpuinfo
processor       : 0
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 1
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 2
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 3
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

Hardware        : BCM2835
Revision        : a02083
Serial          : 0000000057cc1274
pi@raspberrypi:~ $ cat /proc/meminfo
MemTotal:         949452 kB
MemFree:          603064 kB
MemAvailable:     788464 kB
Buffers:           45004 kB
Cached:           191700 kB
SwapCached:            0 kB
Active:           194452 kB
Inactive:         109724 kB
Active(anon):      67728 kB
Inactive(anon):    13952 kB
Active(file):     126724 kB
Inactive(file):    95772 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:        102396 kB
SwapFree:         102396 kB
Dirty:                 8 kB
Writeback:             0 kB
AnonPages:         67512 kB
Mapped:            74676 kB
Shmem:             14212 kB
Slab:              26964 kB
SReclaimable:      15916 kB
SUnreclaim:        11048 kB
KernelStack:        1536 kB
PageTables:         2940 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      577120 kB
Committed_AS:     806744 kB
VmallocTotal:    1114112 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
CmaTotal:           8192 kB
CmaFree:            6796 kB
pi@raspberrypi:~ $
pi@raspberrypi:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root       15201568 4281144  10238272  30% /
devtmpfs          470116       0    470116   0% /dev
tmpfs             474724       0    474724   0% /dev/shm
tmpfs             474724   12304    462420   3% /run
tmpfs               5120       4      5116   1% /run/lock
tmpfs             474724       0    474724   0% /sys/fs/cgroup
/dev/mmcblk0p1     43436   22156     21280  52% /boot
tmpfs              94944       0     94944   0% /run/user/1000
pi@raspberrypi:~ $ du
24      ./EAS01001/Functions
28      ./EAS01001/Rfid
24      ./EAS01001/Calc
pi@raspberrypi:~ $
ps, ps -aux, top, kill
  • ps:To display the currently working processes
  • ps -aux:To display the all processes including processes running on background
  • top:Display all running process live
  • kill:kill a process using its process ID(PID)
  • To run a process ./Calculator on background use &

ps, top, kill command illustrations

pi@raspberrypi:~ $ ps
  PID TTY          TIME CMD
 7220 pts/0    00:00:00 bash
 7437 pts/0    00:00:00 ps
pi@raspberrypi:~ $ top
top - 10:29:41 up 1 day,  2:25,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 130 total,   1 running,  87 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.2 sy,  0.0 ni, 99.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   949452 total,   600920 free,    94564 used,   253968 buff/cache
KiB Swap:   102396 total,   102396 free,        0 used.   787392 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 7438 pi        20   0    8216   3308   2752 R   1.0  0.3   0:00.45 top
  426 root      20   0  140460  34696  23536 S   0.3  3.7   0:46.72 Xorg
  591 pi        20   0  154564  29924  19888 S   0.3  3.2   4:03.75 lxpanel
    1 root      20   0   26964   6020   4904 S   0.0  0.6   0:03.09 systemd
pi@raspberrypi:~ $ ps -aux
root       868  0.0  0.0   1820   332 ?        S    Jun27   0:00 ./EfApln
root       869  0.0  0.0   2952   612 ?        S    Jun27   0:00 ./EfDrvr
root       870  0.0  0.0   1820   348 ?        S    Jun27   0:00 ./PicApln
root       871  0.0  0.0   2956   608 ?        S    Jun27   0:00 ./PicDrvr
root       872  0.0  0.0   1820   328 ?        S    Jun27   0:00 ./ArmApln
root       873  0.0  0.0   2956   600 ?        S    Jun27   0:00 ./ArmDrvr
root       874  0.0  0.0   1820   340 ?        S    Jun27   0:00 ./AvrApln
root       875  0.0  0.0   2956   592 ?        S    Jun27   0:00 ./AvrDrvr
pi@raspberrypi:~ $ kill 875
pi@raspberrypi:~ $ ./Calculator &
pi@raspberrypi:~ $



Want to contribute or ask a new article? Write and upload your article information .
 |  
Share

Articles


Linux Software

More Articles Categories

Technical articles

Prepare for software jobs


Test your skills