Linux Basic commands-chmod, wild cards,zip,df, du,free,top, cal,hostname, uname,sleep,ps

TjMan 04/Jun/2019 Shell Scripting

File/Directory related:

CHMOD (change file mode bits):

As we saw on ls -lrta command that each file is associated with a set of permissions consist of three word as below: r-read w-write x-execute Now lets see if we create a file using touch command then what permissions it got by default.

[tjman]$ touch myfile
[tjman]$ ls -lrt
total 0
-rw-r--r-- 1 tjman tunetotech 0 Apr 27 02:52 myfile

rw-r--r-- This is the permission. You can see permission words are repeated three times that is [rw-],[r--],[r--]. The first set [rw-] is describe that the owner (tjman) has read and write access but do not have execute access. The second set [r--] is for group (tunetotech), all user belong to tunetotech group has read only permission. The third set [r--] is for all other users on the Linux server,they also have read only permission. Now, you may want to change permission as per requirement using chmod command using umask vaule. umask value for read, write and execute is 4,2,1 respectively. Now suppose you want to provide rwxrw-r-- permission to the file, so the umask value will be [4+2+1][4+2+0][4+0+0]=764.

[tjman]$ ls -lrt
total 0
-rw-r--r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile
[tjman]$ chmod 764 myfile
[tjman]$ ls -lrt
total 0
-rwxrw-r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile
[tjman]$

There is a different way to use chmod to modify file or directory permission as below.

[tjman]$ ls -lrt
total 0
-rwxrw-r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile
[tjman]$ chmod u=r myfile
[tjman]$ ls -lrt
total 0
-r--rw-r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile

As you can see we provided u=r so whatever was the permission does not matter after the command execute the permission for user becomes read only. Here you can use 'u' for users 'g' for group and 'o' for others and 'a' for all and three operators are supported as '=','+','-'. Lets see some examples. Add write,execute permission to myfile from user:

[tjman]$ ls -lrt
total 0
-r--rw-r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile
[tjman]$
[tjman]$ chmod u+wx myfile
[tjman]$ ls -lrt
total 0
-rwxrw-r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile

Revoke write permission from myfile for group:

[tjman]$ ls -lrt
total 0
-rwxrw-r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile
[tjman]$ chmod g-w myfile
[tjman]$ ls -lrt
total 0
-rwxr--r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile

Provide read,write,execute permissions to all:

[tjman]$ ls -lrt
total 0
-rwxr--r-- 1 tjman tunetotech 0 Apr 27 03:22 myfile
[tjman]$ chmod a=rwx myfile
[tjman]$ ls -lrt
total 0
-rwxrwxrwx 1 tjman tunetotech 0 Apr 27 03:22 myfile
Linux wild cards:

Wild card on Linux is a technique to filter out words comming from some input. Does not make sense? Lets try a example to understand better. Lets create some files as below:

[tjman]$ touch aaa aab aac aba abb abc bbb baa
[tjman]$ ls -lrt
total 40
drwxr-xr-x 2 tjman tjman      4096 Apr 27 14:21 Desktop
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 bbb
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 baa
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 abb
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aba
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aac
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aab
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aaa

There are three kind of wind cards we can use: * - select every thing ? - select as only one element can be any thing [] - select as one element in the range. Lets discuss this in details: Below example select every thing starting with a and "select every thing" after that.

[tjman]$ ls -lrt a*
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abb
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aac
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aab
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aaa

Below example select every thing starting with b and "select every thing" after that.

[tjman]$ ls -lrt b*
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 bbb
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 baa

Below example select every thing starting with aa and "select every thing" after that.

[tjman]$ ls -lrt aa*
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aac
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aab
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aaa

Below example select every thing starting with a and ending with c and "select every thing" in between.

[tjman]$ ls -lrt a*c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aac

Below example select every thing starting with a and ending with c and "select every thing" in between, even when there are more that one charactor.

[tjman]$ touch axzyc
[tjman]$ ls -lrt
total 44
drwxr-xr-x 2 tjman tjman      4096 Apr 27 14:21 Desktop
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 bbb
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 baa
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 abb
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aba
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aac
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aab
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:02 aaa
-rw-r--r-- 1 tjman tunetotech    0 Apr 28 15:06 axzyc
[tjman]$ ls -lrt a*c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aac
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:06 axzyc

Below example select every thing starting with a and ending with c and "select as only one element can be any thing" in between. File axzyc is not on the list as there are more than one element in between.

[tjman]$ ls -lrt a?c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aac

? can be used many times match the pattern as per need.

[tjman]$ ls -lrt a???c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:06 axzyc

[] can be used to specify range of words to match with as single element. We can specify words or range using - . ^ is used to reverse the effect means not to take any matching in that range. Ranges can be: [0-9][a-z][A-Z]

[tjman]$ ls -lrt ab*
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abb
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba
[tjman]$ ls -lrt ab[cab]
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abb
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba
[tjman]$ ls -lrt ab[ca]
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba
[tjman]$ ls -lrt ab[a-b]
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abb
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba

[tjman]$ ls -lrt [a-z]b[ac]
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba

[tjman]$ ls -lrt [^b-z]b[ac]
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 abc
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 15:02 aba

User can use combination of wild cards in partical use cases.

Useful Commands

ZIP/UNZIP:

Compression is an essential part of file management and archiving data. Below we created file a,b,c and d and ziped using zip command then unzip to 'all' folder using unzip command. Usage: zip [target zip file name] [input file names]

[tjman]$ touch a b c d
[tjman]$ ls -lrt
total 16
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 d
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 b
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 a
[tjman]$ zip all.zip *
  adding: a (stored 0%)
  adding: b (stored 0%)
  adding: c (stored 0%)
  adding: d (stored 0%)
[tjman]$ ls -lrt
total 24
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:11 d
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:11 c
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:11 b
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:11 a
-rw-r--r-- 1 tjman tunetotech 470 Apr 28 16:11 all.zip
[tjman]$ mkdir all
[tjman]$ unzip all.zip -d all
Archive:  all.zip
 extracting: all/a
 extracting: all/b
 extracting: all/c
 extracting: all/d
[tjman]$ ll all
total 16
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 a
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 b
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:11 d

Note: While zipping folder you must mention -r option to recursively add all files and folder to the zip otherwise you will get a empty zip file. ********

[tjman]$ mkdir myfiles
[tjman]$ cd myfiles/
[tjman]$ touch a b c d
[tjman]$ cd ..
[tjman]$ pwd
/home/tjman
[tjman]$ ll
total 8
drwxr-xr-x 2 tjman tunetotech 4096 Apr 28 16:33 myfiles
[tjman]$ zip myfiles.zip myfiles
  adding: myfiles/ (stored 0%)
[tjman]$ ls -lrt
total 16
drwxr-xr-x 2 tjman tunetotech 4096 Apr 28 16:33 myfiles
-rw-r--r-- 1 tjman tunetotech  148 Apr 28 16:34 myfiles.zip
[tjman]$ unzip myfiles.zip -d myfiles_unzip
Archive:  myfiles.zip
   creating: myfiles_unzip/myfiles/
[tjman]$ pwd
/home/tjman
[tjman]$ cd myfiles_unzip/
[tjman]$ ls -lrt
total 8
drwxr-xr-x 2 tjman tunetotech 4096 Apr 28 16:33 myfiles
[tjman]$ ll myfiles/
total 0
[tjman]$

******** Options: -r: recursive zipping

[tjman]$ ls -lrt
total 8
drwxr-xr-x 2 tjman tunetotech 4096 Apr 28 16:33 myfiles
[tjman]$ zip -r myfiles.zip myfiles
  adding: myfiles/ (stored 0%)
  adding: myfiles/a (stored 0%)
  adding: myfiles/b (stored 0%)
  adding: myfiles/d (stored 0%)
  adding: myfiles/c (stored 0%)
[tjman]$ ll
total 16
drwxr-xr-x 2 tjman tunetotech 4096 Apr 28 16:33 myfiles
-rw-r--r-- 1 tjman tunetotech  660 Apr 28 16:36 myfiles.zip
[tjman]$ unzip myfiles.zip -d myfiles_unzip
Archive:  myfiles.zip
   creating: myfiles_unzip/myfiles/
 extracting: myfiles_unzip/myfiles/a
 extracting: myfiles_unzip/myfiles/b
 extracting: myfiles_unzip/myfiles/d
 extracting: myfiles_unzip/myfiles/c
[tjman]$ cd myfiles_unzip/
[tjman]$ ll
total 8
drwxr-xr-x 2 tjman tunetotech 4096 Apr 28 16:33 myfiles
[tjman]$ cd myfiles/
[tjman]$ ll
total 16
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:33 a
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:33 b
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:33 c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:33 d

-m: Zip and remove original files.

[tjman]$ touch a b c d
[tjman]$ ll
total 16
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:55 a
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:55 b
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:55 c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:55 d
[tjman]$ zip -m files.zip *
  adding: a (stored 0%)
  adding: b (stored 0%)
  adding: c (stored 0%)
  adding: d (stored 0%)
[tjman]$ ls -lrt
total 8
-rw-r--r-- 1 tjman tunetotech 470 Apr 28 16:55 files.zip
[tjman]$ unzip files.zip
Archive:  files.zip
 extracting: a
 extracting: b
 extracting: c
 extracting: d
[tjman]$ ls -lrt
total 24
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 d
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 c
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 b
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 a
-rw-r--r-- 1 tjman tunetotech 470 Apr 28 16:55 files.zip

-x: exclude files while zipping

[tjman]$ zip files.zip * -x a b
  adding: c (stored 0%)
  adding: d (stored 0%)
[tjman]$ ls -lrt
total 24
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 d
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 c
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 b
-rw-r--r-- 1 tjman tunetotech   0 Apr 28 16:55 a
-rw-r--r-- 1 tjman tunetotech 246 Apr 28 17:01 files.zip
[tjman]$ unzip files.zip -d files_unzip
Archive:  files.zip
 extracting: files_unzip/c
 extracting: files_unzip/d
[tjman]$ ll files_unzip/
total 8
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:55 c
-rw-r--r-- 1 tjman tunetotech 0 Apr 28 16:55 d

DF:

It show disks information of a Linux machine.

[tjman]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      3.9G  2.4G  1.3G  65% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                1006M     0 1006M   0% /dev/shm

DU:

Space usage of a file can be checked using du command.

[tjman]$ du -h *
4.0K    a
4.0K    b
4.0K    c
4.0K    d
16K     files_unzip
8.0K    files.zip

Options: -b to check in bytes -k to check in KB -m to check in MB -h to check in human readable format -c to check total size

FREE:

Check RAM on a Linux machine(in KB by default).

[tjman]$ free
             total       used       free     shared    buffers     cached
Mem:       2058700    1064204     994496          0      68880     759236
-/+ buffers/cache:     236088    1822612
Swap:      4095992          0    4095992

[tjman]$ free -m
             total       used       free     shared    buffers     cached
Mem:          2010       1039        971          0         67        741
-/+ buffers/cache:        230       1779
Swap:         3999          0       3999

Options: -b to check in bytes -k to check in KB -m to check in MB -g to check in GB -h to check in human readable format

TOP:

Check running processes which are consuming most resources and RAM,CPU live information.

[tjman]$ top
top - 17:30:34 up  3:40,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 153 total,   1 running, 119 sleeping,  33 stopped,   0 zombie
Cpu(s):  0.2%us,  0.3%sy,  0.0%ni, 79.2%id,  0.0%wa,  0.7%hi, 19.6%si,  0.0%st
Mem:   2058700k total,  1064396k used,   994304k free,    68968k buffers
Swap:  4095992k total,        0k used,  4095992k free,   759252k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2546 root      16   0 10252  860  748 S  1.3  0.0   3:28.78 hald-addon-stor
 2520 haldaemo  16   0 31420 4504 1724 S  1.0  0.2   0:29.33 hald
    9 root      10  -5     0    0    0 S  0.3  0.0   0:09.80 events/1
 2631 root      18   0  151m 6660  992 S  0.3  0.3   0:00.83 hpssd.py
13912 tjman     15   0 12764 1144  840 R  0.3  0.1   0:00.03 top
    1 root      15   0 10372  644  548 S  0.0  0.0   0:01.49 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.07 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0

CAL:

Check calender.

[tjman]$ cal
     April 2019
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

HOSTNAME:

Check Linux machine name.

[tjman]$ hostname
www.tunetotech.com

UNAME:

Checked detailed host and OS information.

[tjman]$ uname -a
Linux www.tunetotech.com 2.6.18-398.el5 #1 SMP Tue Aug 12 06:26:17 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

SLEEP:

Sleep command pause terminal/script for mention second with it. For below example, terminal pause for 10 seconds.

[tjman]$ sleep 10
[tjman]$

PS:

It shows all running processes. Very useful to check a perticuler process. Here we created a script with sleep and ran it in background using &. At that time we checked the process using ps -ef|grep sleep_script.sh as below.

[tjman]$ echo 'echo sleep starts' >> sleep_script.sh
[tjman]$ echo 'sleep 10' >> sleep_script.sh
[tjman]$ echo 'echo sleep ends' >> sleep_script.sh
[tjman]$ cat sleep_script.sh
echo sleep starts
sleep 10
echo sleep ends

[tjman]$ sh sleep_script.sh &
[11] 13949
[tjman]$ sleep starts

[tjman]$ ps -ef|grep sleep_script.sh
tjman    13949 13444  0 17:43 pts/3    00:00:00 sh sleep_script.sh
tjman    13952 13444  0 17:43 pts/3    00:00:00 grep sleep_script.sh
[tjman]$ sleep ends

[11]   Done                    sh sleep_script.sh

Recent Post