Showing posts with label Linux Command. Show all posts
Showing posts with label Linux Command. Show all posts

Tuesday 9 August 2016

Different Crontab Example in Linux


Crontab Format


Field
Description
Allowed Value
MIN
Minute Field
0-59
HOUR
Hour Field
0-23
DOM
Day of Month
1-31
MON
Month Field
1-12
DOW
Day of Week
0-6
CMD
Command
Any Command to Execute


Run Crontab every 30 mintues between 09:00am to 20:00 pm daily

*/30 09,10,11,12,13,14,15,16,17,18,19,20 * * * /app/script/abs.sh
or
*/30 09-20 * * * /app/script/abs.sh

Run Crontab every 30 mintues daily

*/30 * * * * /app/script/abs.sh

Run Crontab at specific time like 10th June 08:30 AM

30 08 10 06 * /app/script/abs.sh

30 – 30th Minute
08 – 08 AM
10 – 10th Day
06 – 6th Month (June)
* – Every day of the week

Run Crontab every day at 21:55

55 21 * * * /app/script/abs.sh

55 – 55th Minute
21 – 09 PM
* – Daily
* – Every Month
* – Every day of the week

Friday 25 December 2015

Default permission of file and directory in Linux / UNIX

What is UMASK

UMASK (User Mask or User file creation MASK) is the default permission of file and directory in Linux or UNIX. There are three types of permission for every file and directory (read, write and execute) for three types of user (Owner, Groups and Others).

[wasi@saidrasel ~]]$ mkdir cde
[wasi@saidrasel ~]]$ touch abc
[wasi@saidrasel ~]$ ls -l abc
-rw-rw-r--. 1 wasi wasi 0 Dec 26 11:12 abc

In the above example

  • User (wasi) has read,write permission
  • Group has read, write permission
  • Others have read permission

Three file permissions:

read: permission to read the contents of file.
write: permission to write to the file.
execute: permission to execute the file as a program/script.

Three directory permissions:

read: permission to read the contents of directory ( view files and sub-directories in that directory ).
write: permission to write in to the directory. ( create files and sub-directories in that directory )
execute: permission to enter into that directory.

Numeric values for the read, write and execute permissions:
read--4
write--2
execute--1

[wasi@saidrasel ~]$ ls -l abc
-rw-rw-r--. 1 wasi wasi 0 Dec 26 11:12 abc
drwxrwxr-x. 2 wasi wasi 6 Dec 26 11:31 cde

So the numeric value permission for this file is-- 4+2 4+2 4 --->664
and the numeric value permission for the directory is-- 4+2+1 4+2+1 4+1 --->775

Umask is responsible for the default permission

[wasi@saidrasel ~]$ umask
0002
[wasi@saidrasel ~]$

Final default permission for a file is calculated as shown below:

Default file permission: 666
Default umask : 002
Final default file permission: 664 (666-002)

Final default permission for a directory is calculated as shown below:

Default directory permission: 777
Default umask: 002
Final default directory permission: 775 (777-002)

We can find umask value setting from /etc/bashrc or /etc/profile


How to set access permission in a file for specific user in Linux

Files and directories have permission sets for the owner of the file, the group associated with the file, and all other users for the system. However, these permission sets have limitations. For example, different permissions cannot be configured for different users. Thus, Access Control Lists (ACLs) were implemented.

[root@saidrasel]# ll a
-rw-r--r--. 1 root root 0 Dec 25 22:56 a

Above permission means ---file owner is root have permission rw (read,write), group root have permission r (read) and other have permission r (read). Following example showing in details

[root@saidrasel]# getfacl a
# file: a
# owner: root
# group: root
user::rw-
group::r--
other::r--

Now we will set permission in file a for specific user wasi, so for this we need to create a user named wasi

[root@saidrasel]# useradd wasi
[root@saidrasel]# passwd wasi
Changing password for user wasi.
New password: 

Now we will set rwx (read,write and execute) permission for new user wasi

[root@saidrasel]# setfacl -m u:wasi:rwx a
or
[root@saidrasel]# setfacl -m u:wasi:rwx /root/Desktop/a       [for absolute path]

 Now we can check the permission of file a for user wasi

[root@saidrasel]# getfacl a
# file: a
# owner: root
# group: root
user::rw-
user:wasi:rwx
group::r--
mask::rwx
other::r--

[root@saidrasel]#

Thats it..........

Wednesday 23 December 2015

How to Clear Cache Memory in Linux

Find Memory Status

[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        915         71          7          0        395
-/+ buffers/cache:        519        467
Swap:         1023          0       1023

In this example the total amount of available memory is 987 M. 519 M are used by processes and 467 M are free for other applications. Do not get confused by the first line which shows that 71 M are free! If you look at the usage figures you can see that most of the memory use is for buffers and cache. Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices). This helps the system to run faster because disk information is already in memory which saves I/O operations. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications. If your system runs for a while you will usually see a small number under the field "free" on the first line.


Find Cache Memory uses in Linux

Use free command to find out cache memory uses by Linux system. Output of free command is like below

Last column is showing cached memory (  395 MB) by system. -m option is used for showing memory details in MB’s.


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        915         71          7          0        395
-/+ buffers/cache:        519        467
Swap:         1023          0       1023

Following command to see much detailed information about current memory usage in 3 second interval:

[root@localhost Desktop]# watch -n 3 cat /proc/meminfo

Every 3.0s: cat /proc/meminfo                                                          Wed Dec 23 23:28:53 2015

MemTotal:        1010884 kB
MemFree:          353384 kB
MemAvailable:     367148 kB
Buffers:              40 kB
Cached:           123052 kB
SwapCached:            0 kB
Active:           427352 kB
Inactive:          83868 kB
Active(anon):     385120 kB
Inactive(anon):    11240 kB
Active(file):   42232 kB
Inactive(file):    72628 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal: 1048572 kB
SwapFree:        1048572 kB
Dirty:                40 kB
Writeback:             0 kB
AnonPages:        388152 kB
Mapped:            95736 kB
Shmem:              8216 kB
Slab:              67208 kB


1. Freeing pagecache only use following command

[root@localhost Desktop]# sync; echo 1 > /proc/sys/vm/drop_caches


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        611        375          7          0         92
-/+ buffers/cache:        518        468
Swap:         1023          0       1023

2. Freeing dentries and inodes use following command

[root@localhost Desktop]# sync; echo 2 > /proc/sys/vm/drop_caches


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        594        392          7          0         93
-/+ buffers/cache:        501        485
Swap:         1023          0       1023

3. Freeing pagecache, dentries and inodes in cache memory

[root@localhost Desktop]# sync; echo 3 > /proc/sys/vm/drop_caches


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        596        391          7          0         91
-/+ buffers/cache:        504        482
Swap:         1023          0       1023
[root@localhost Desktop]#

4. Schedule Cron to Flush Cache Regularly

If we want to schedule following in crontab to automatically flush cache on regular interval.

# crontab -l

0 * * *  * sync; echo 3 > /proc/sys/vm/drop_caches

The above cron will execute on every hour and flushes the cached memory on system.


Monday 21 December 2015

Find empty file or directory in Linux

### Find empty file from current directory ###

[root@localhost ~]# find -type f -empty
./.cache/abrt/applet_dirlist
./.local/share/gnome-settings-daemon/input-sources-converted
./.local/share/folks/relationships.ini
./.local/share/.converted-launchers
./.local/share/tracker/data/.meta.isrunning
./.local/share/keyrings/user.keystore.HQQ99X
./.local/share/keyrings/user.keystore.ZUKY9X
./.local/share/keyrings/user.keystore.VG4S9X
./.local/share/keyrings/user.keystore.G00V9X
./.local/share/keyrings/user.keystore.681AAY
./.local/share/keyrings/user.keystore.A15Y9X
./.local/share/keyrings/user.keystore.7NT89X
./test/saidrasel.lst

### Find empty file from specific directory ###

[root@localhost ~]# find /root/test -type f -empty
/root/test/saidrasel.lst
[root@localhost ~]#
### Find empty directory from current location ###
[root@localhost ~]# find  -type d -empty
./.cache/evolution/addressbook/trash
./.cache/evolution/calendar/trash
./.cache/evolution/mail/trash
./.cache/evolution/memos/trash
./.cache/evolution/sources/trash
./.cache/evolution/tasks/trash
./.cache/folks/avatars
./.config/imsettings
./.config/abrt

### Find empty directory from specific location ###

[root@localhost ~]# mkdir -p /home/rasel/test

[root@localhost ~]# find /home/rasel/ -type d -empty
/home/rasel/.mozilla/extensions
/home/rasel/.mozilla/plugins
/home/rasel/.config/abrt
/home/rasel/test
[root@localhost ~]#

[root@localhost ~]# mkdir -p /home/rasel/test/abc

[root@localhost ~]# find /home/rasel/ -type d -empty
/home/rasel/.mozilla/extensions
/home/rasel/.mozilla/plugins
/home/rasel/.config/abrt
/home/rasel/test/abc
[root@localhost ~]#

### Remove empty directory ###

Create empty directory using following command

[root@localhost rasel]# pwd
/home/rasel
[root@localhost rasel]# mkdir test
[root@localhost rasel]# cd test/
[root@localhost test]# mkdir abc
[root@localhost test]# pwd
/home/rasel/test
[root@localhost test]# cd /root/
[root@localhost ~]# pwd
/root
[root@localhost ~]# find /home/rasel/ -type d -empty
/home/rasel/test/abc
 
[root@localhost ~]# ls -la /home/rasel/test/
total 4
drwxr-xr-x. 3 root  root    16 Dec 21 16:44 .
drwx------. 4 rasel rasel 4096 Dec 21 16:44 ..
drwxr-xr-x. 2 root  root     6 Dec 21 16:44 abc

Now remove the empty directory

[root@localhost ~]# find /home/rasel/test/ -type d -empty -exec rmdir {} \;
find: ‘/home/rasel/test/abc’: No such file or directory

[root@localhost ~]# ls -la /home/rasel/test/
total 4
drwxr-xr-x. 2 root  root     6 Dec 21 16:45 .
drwx------. 4 rasel rasel 4096 Dec 21 16:44 ..
[root@localhost ~]#

Locate command in Linux

locate command search very quickly, because  locate does not search the files on disk rather it searches for file paths in a database. The locate database file is located at:

[root@localhost test]# locate mlocate.db
/usr/share/man/man5/mlocate.db.5.gz
/var/lib/mlocate/mlocate.db/var/lib/mlocate/mlocate.db.UY1Pjl
[root@localhost test]#


[root@localhost test]# locate sysctl.conf
/etc/sysctl.conf
/etc/sysctl.d/99-sysctl.conf
/usr/share/man/man5/sysctl.conf.5.gz
[root@localhost test]#

Now create a directory and touch a file

[root@localhost Desktop]# mkdir -p /root/test
[root@localhost Desktop]# touch /root/test/saidrasel.lst
[root@localhost test]# locate saidrasel*

Now execute the locate command to find "saidrasel.lst" file but could'nt get

[root@localhost test]# locate saidrasel.lst


Now execute the updatedb command,  it scans the whole system and updates the mlocate.db database file.

[root@localhost test]# updatedb
[root@localhost test]# locate saidrasel.lst
/root/test/saidrasel.lst
[root@localhost test]#

Note: So the limitation of the "locate" command is its dependency on the database which can be updated by another utility "updated". Hence, in order to get the latest and reliable results from "locate" command the database on which it works should be updated at regular intervals.

Sunday 20 December 2015

Find file based on access/modification time


We can find files based on following three file time attribute.

Access time of the file: Access time gets updated when the file accessed.
Modification time of the file: Modification time gets updated when the file content modified.
Change time of the file: Change time gets updated when the inode data changes

###Find files whose content got updated within last 1 hour###

[root@stelar_test oracle]# find /home/oracle -mmin -60
/home/oracle
/home/oracle/SaidRasel.c
[root@stelar_test oracle]#

-mmin n File’s data was last modified n minutes ago.
-mtime n File’s data was last modified n*24 hours ago.

[root@stelar_test oracle]# find /home/oracle -mtime -1
/home/oracle
/home/oracle/SaidRasel.c
[root@stelar_test oracle]#

###Find files which got accessed before 1 hour###

-amin n File was last accessed n minutes ago-atime n File was last accessed n*24 hours ago

Finds all the files (under /home/oracle) that got accessed within the last 24 hours (1 day).

[root@stelar_test oracle]# find /home/oracle -atime -1
/home/oracle/.ssh
/home/oracle/.oracle
/home/oracle/.oracle/logs
/home/oracle/.local
/home/oracle/.local/share
/home/oracle/.local/share/applications
/home/oracle/.local/share/gvfs-metadata
/home/oracle/.thumbnails
/home/oracle/.thumbnails/normal
/home/oracle/Downloads
/home/oracle/.gvfs
/home/oracle/.spice-vdagent
/home/oracle/.bash_profile
/home/oracle/.cache
/home/oracle/Templates
/home/oracle/tomcat jdk
/home/oracle/.dbus
/home/oracle/.dbus/session-bus

Finds all the files (under /root/test/backup) that got accessed within the last 24 hours (1 day).


[root@stelar_test oracle]# find /root/test/backup/ -atime -1
/root/test/backup/
/root/test/backup/Program.c
/root/test/backup/mycprogram.c
/root/test/backup/MyCProgram.c
/root/test/backup/saidrasel.c
/root/test/backup/MybashProgram.sh
[root@stelar_test oracle]#

Find files in the current directory and sub-directories, which got accessed within last 1 hour (60 minutes)

[root@stelar_test oracle]#  find -amin -60
.
./SaidRasel.c
./.bash_history
[root@stelar_test oracle]#

Find files in the current directory and sub-directories, which got accessed within last 1 hour (60 minutes)

[root@stelar_test oracle]#  find /home/oracle/ -amin -60
/home/oracle/
/home/oracle/SaidRasel.c
/home/oracle/.bash_history
[root@stelar_test oracle]#
-amin n File was last accessed n minutes ago
-atime n File was last accessed n*24 hours ago

###Find files which got changed before 1 hour###

-cmin n File’s status was last changed n minutes ago.
-ctime n File’s status was last changed n*24 hours ago.

find files in the current directory and sub-directories, which changed within last 1 hour (60 minutes)

[root@stelar_test oracle]#  find /home/oracle/ -cmin -60
/home/oracle/
/home/oracle/SaidRasel.c
/home/oracle/.bash_history
[root@stelar_test oracle]#  find . -cmin -60
.
./SaidRasel.c
./.bash_history
[root@stelar_test oracle]#

finds all the files (under /root/test/backup/) that got changed within the last 24 hours (1 day).

[root@stelar_test oracle]# find /root/test/backup/ -ctime -1
/root/test/backup/
/root/test/backup/Program.c
/root/test/backup/mycprogram.c
/root/test/backup/MyCProgram.c
/root/test/backup/saidrasel.c
/root/test/backup/MybashProgram.sh
[root@stelar_test oracle]#



Reference :
http://www.thegeekstuff.com/2009/06/15-practical-unix-linux-find-command-examples-part-2/


Find files using file name in Linux

create two file named saidrasel.c into current directory /root/test and /root/test/backup directory

[root@stelar_test test]# touch saidrasel.c
[root@stelar_test test]# cd backup/
[root@stelar_test backup]# touch saidrasel.c


Now execute the find command to search saidrasel.c file from /root/test location
[root@stelar_test test]#pwd
/root/test
[root@stelar_test test]# find -name saidrasel.c
./backup/saidrasel.c
./saidrasel.c
[root@stelar_test test]#

Now execute the find command to search saidrasel.c file from / location

[root@stelar_test /]# find -name saidrasel.c
./root/test/backup/saidrasel.c
./root/test/saidrasel.c
[root@stelar_test /]#

Now execute the find command to search saidrasel.c file from any location

[root@stelar_test oracle]# pwd
/home/oracle

Following search will start from / (root) location

[root@stelar_test oracle]# find / -name  saidrasel.c
/root/test/backup/saidrasel.c
/root/test/saidrasel.c
[root@stelar_test oracle]#

[root@stelar_test oracle]# find / -name  saidrasel*
/root/test/backup/saidrasel.c
/root/test/saidrasel.c

[root@stelar_test oracle]# find / -name  saidrase*
/root/test/backup/saidrasel.c
/root/test/saidrasel.c

[root@stelar_test oracle]# find / -name  saidra*
/root/test/backup/saidrasel.c
/root/test/saidrasel.c
[root@stelar_test oracle]#

Find Files Using Name and Ignoring Case


[root@stelar_test oracle]# find / -iname  "SaidRasel.c"
/home/oracle/SaidRasel.c
/root/test/SaidRasel.c
/root/test/backup/saidrasel.c
/root/test/saidrasel.c
[root@stelar_test oracle]#

[root@stelar_test oracle]# find / -iname  "saidrasel.c"
/home/oracle/SaidRasel.c
/root/test/SaidRasel.c
/root/test/backup/saidrasel.c
/root/test/saidrasel.c
[root@stelar_test oracle]#




Tuesday 15 December 2015

Directory or File Permission in Linux

For Example---

[oracle@sbldb2 ~]$ ll
total 516
-rw-r--r-- 1 oracle oinstall 235340 Nov  2 11:50 02112015.lst
-rw-r--r-- 1 oracle oinstall 257702 Sep  8  2014 awrrpt_1_7407_7409.txt
drwxr-xr-x 2 oracle oinstall   4096 Mar 12  2014 Desktop
-rw-r--r-- 1 oracle oinstall    364 Apr  1  2014 Dhaka.bkp
drwxr-xr-x 3 oracle oinstall   4096 Mar 23  2014 oradiag_oracle
drwxr-xr-x 4 oracle oinstall   4096 Sep  2 15:45 rman
-rw-r--r-- 1 oracle oinstall   3965 Jan  5  2015 tuning.sql

Total 10 fields in prefix of every files and directory as above

Here
user ---> oracle
group ---> oinstall

-rwxr-xr-x

1st filed represents ---> Directory
2nd,3rd and 4th filed represent ---> Owner Permission
5th,6th and 7th filed represent ---> Group Member Permission
8th,9th and 10th field represent ---> Other Permission

You can setup following mode on each files. In a Linux and UNIX set of permissions is called as mode:

Read (r)
Write (w)
Execute (x)

Linux Read mode permissions

--Read access on a file allows you to view file
--Read access on a directory allows you to view directory contents with ls command
Write mode permissions

--Write access on a file allows you to write to file
--Write access on a directory allows you to remove or add new files
Execute mode permissions

--Execute access on a file allows to run program or script
--Execute access on a directory allows you access file in the directory

Octal numbers and permissions

You can use octal number to represent mode/permission:
r: 4
w: 2
x: 1

Use above method to calculate permission for group and others. Let us say you wish to give full permission to owner, read & execute permission to group, and read only permission to others, then you need to calculate permission as follows:

-rwx-rw--x-- 1 oracle oinstall   3965 Jan  5  2015 tuning.sql

User (oracle) = r+w+x = 4+2+1 = 7
Group (oinstall) = r+w = 4+2+0 = 6
Others = r= 4+0+0 = 4

Effective permission is 764.

Saturday 12 December 2015

Server uptime command in Linux

[root@stelar_test ~]# uptime
 13:37:01 up 15 days, 16:47,  1 user,  load average: 0.11, 0.05, 0.01

 The uptime command gives a one line display of the following information.


-The current time (13:37:01)
-How long the system has been running (up 15 days)
-How many users are currently logged on (1 user)
-The system load averages for the past 1, 5, and 15 minutes (0.11, 0.05, 0.01)

This is the same information contained in the header line displayed by the w and top commands:

[root@stelar_test ~]# w
 13:37:05 up 15 days, 16:47,  1 user,  load average: 0.10, 0.05, 0.01
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.201.94   11:41    0.00s  0.05s  0.00s w

[root@stelar_test ~]# top
top - 13:37:51 up 15 days, 16:47,  1 user,  load average: 0.05, 0.04, 0.00
Tasks: 208 total,   1 running, 207 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.5%us,  0.4%sy,  0.0%ni, 98.0%id,  1.1%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:   4791876k total,  4635552k used,   156324k free,    81896k buffers
Swap:  8388600k total,    57012k used,  8331588k free,  3932264k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
26884 root      20   0 15084 1188  832 R  2.0  0.0   0:00.01 top
    1 root      20   0 19400  788  580 S  0.0  0.0   0:08.90 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.02 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.89 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    7 root      RT   0     0    0    0 S  0.0  0.0   0:00.03 migration/1
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/1
    9 root      20   0     0    0    0 S  0.0  0.0   0:02.85 ksoftirqd/1
   10 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/1
   11 root      20   0     0    0    0 S  0.0  0.0   0:02.38 events/0
   12 root      20   0     0    0    0 S  0.0  0.0   7:39.35 events/1
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cpuset
   14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper