Featured

    Featured Posts

Find command in linux

Do you want to locate any file in Linux ?
Are you tired of manually finding files in linux system ?

No worries.. We have a great command called "find".
Lets make use of it.... 🙂 



          
Command :    find
Syntax :          find   [dir]    [criteria]    [pattern]   [action]   

Here...
            [dir]       -  specifies path under which you are searching for.
                             ex:    / 
                                     /root
                                     /bin
                                     /home/user
⇨ If you dont enter any thing in a dir field , then command will look under current working directory. [ same as  if you specify  ( . ) dot - current working directory ]

             [criteria]  -   Specifies WHAT to search 
                                ex:   -name       →   Name of a file 
                                        -iname      →   Ignore case of name
                                        -user         →   To find files related to username
                                        -group       →   To find files related to group
                                        -uid           →   To find files related to user id
                                        -gid           →   To find files related to group id
                                        -perm        →   To find files based on file permission
                                        -atime        →   To find files based on access time
                                        -mtime       →   To find files based on Modified time
                                        -ctime        →   To find files based on Creation time
                                        -size          →   To find files based on Size 
                                        -inum         →   To find files based on Inode number
                                          
            [pattern]  -  The pattern that you would like to search/find.

            [actions]  -  Actions to be applied on the output of find.
                               like rm , cp , mv etc.,

--------------------------------------------------------------------------------------------------------------------------------------------------------

Now we will go through some examples of find commands which will be most helpful in realtime.

-name
To find  “flower.jpg”  file under  /

[root@localhost Desktop]# find  /  -name  flower.jpg
/root/Desktop/flower.jpg


-iname
If you are not sure whether the file name is in capitals or small letters:

[root@localhost Desktop]# find /root -iname FLowEr.jpg
/root/Desktop/flower.jpg


-user
To search the files related to user “kiran” :

[root@localhost ~]# find /home/ -user kiran
/home/kiran
/home/kiran/.bashrc
/home/kiran/flower.jpg
/home/kiran/mydir
/home/kiran/scripts
/home/kiran/contacts.txt



If you specify multiple criteria, then by default it will consider as "and"  [ the two criteria should match]

and
without any option
using -a

[root@localhost ~]# find /home/ -user kiran  -iname flower*
/home/kiran/flower.jpg


[root@localhost ~]# find /home/ -user kiran  -a -iname flower*
/home/kiran/flower.jpg



or
using  -o (or) option

[root@localhost ~]# find /home/kiran/ -name "*.jpg"   -o  -name "*.txt"
/home/kiran/flower.jpg
/home/kiran/contacts.txt


  
Both and & or
using-a (and ) &   -o (or) option

[root@localhost ~]# find /home/  -user kiran -a \( -iname "*.txt" -o -iname "*.jpg" \)
/home/kiran/flower.jpg
/home/kiran/contacts.txt


-size
To find the files which has exactly 1kb size
To find the files which has more than 3kb size
To find the files which has less than 3kb size
[kiran@localhost ~]$ find -size 1K
./.bashrc
./.bash_logout
./.bash_profile

[kiran@localhost ~]$ find -size +3M
./mydir
./.mozilla
./.mozilla/extensions
./.mozilla/plugins
./.gnome2
./scripts

[kiran@localhost ~]$ find -size -3M
./.bashrc
./flower.jpg
./.bash_logout
./hi
./.bash_profile
./contacts.txt


-perm
To find the files which has more than 666 permissions
To find the files which has less than 555 size
To find the files which has less than 3kb size

[kiran@localhost ~]$ find . -perm +600
./scripts
./.bash_logout
./hi
./.bash_profile
./contacts.txt


[kiran@localhost ~]$ find . -perm -555
./mydir
./.mozilla
./.mozilla/extensions
./.mozilla/plugins
./.gnome2


[kiran@localhost ~]$ find . -perm 777
./contacts.txt

















-mtime
To find file which was older than 3 days

[root@localhost ~]# find / -mtime 3
/bin/uname
/bin/vi
/bin/gzip
/bin/raw



[action]

Command can be executed on founded files


commands must be preceded with -exec (or) -ok

 -ok will prompts before actiong on each line

⇒ command must end with \;

⇒ we can use {} as an file name place holder


Ex:

-ok
Using –ok option
[kiran@localhost ~]$ find . -name "file*" -ok rm -rf {} \;
< rm ... ./file3 > ? y
< rm ... ./file1 > ? y
< rm ... ./file2 > ? y

-exec
To delete flower file of user kiran

[root@localhost ~]# find /home/kiran/ -name "flower*" -exec rm -f {} \;
[root@localhost ~]#


To delete flower file under /opt which are older than 30 days

[root@localhost ~]# find /opt/  -mtime +30  -exec rm -rf {} \;
[root@localhost ~]#


To copy files to other directory

[root@localhost ~]# find /home/kiran/ -name "flower*" -exec  cp {}  /tmp  \;
[root@localhost ~]#





--------------------------------------------------------------------------------------------------------------------------------------------------------


Hope you guys got an idea of using "find" command.


Thanks! 🙂








Scheduling jobs using ' at ' and ' cron '

If you try to execute any command in linux/unix terminal, it is for one time execution and it will get executed once it is invoked.

Suppose if you have a command/script which should be executed at particular time/period OR at regular intervals of time... is it possible ?

Yes. it is possible!

We have a commands  "at" and "crontab" which were used for scheduling one time job and regular interval of time (recurring jobs)  respectively.

For ex: If you want to remove files in /tmp at evening 4pm today. You can automate this using "at" job . i.e., One time scheduler.

Suppose if the same task should be performed on everyday at 4pm , you can make use of "crontab" which is used to schedule the recurring jobs.


Scheduling jobs:

    at              ⇒  One time job scheduler ; Executes commands at specified time.
    crontab     ⇒  Recurring jobs ; it is a deamon to execute scheduled commands.


--------------------------------------------------------------------------------------------------------------------------------------------------------

at :

Creating jobs using “at” :
Syntax:    #at  time                                                       [here time is mandatory]
                 at >commands                                           
                 at >commands
                 at >ctrl+d                                                      [ctrl+d is for save]


Note: If you dont specify any time, then it will give grabled time error.
[root@myserver ~]# at
Garbled time
[root@myserver ~]# 


Few examples:
#at 8:31                                [it will schedule the job at 8:31am to remove all files under /tmp]
at> rm –rf   /tmp/*
at> ctrl+d 


Sample at job scheduling:
#at 7am
#at 11:30
#at noon
#at tea time                        → schedules job at evening 4pm or 16:00
#at now+10mits                  →  after 10mins from  current time  
#at 8pm + 3days
#at now + 2hours
#at 7pm monday          
#at 8am tomorrow
#at 8pm december 25


To see pending jobs:
# atq      (or)    # at -l

To remove jobs:
# at  -d  jobno                (OR)          #atrm jobno

ex: # atrm 4       → 4th job will be removed/deleted


To see job details:
# at -c jobno

Few important points to remember:
  • If you set the job for the which is already passed then it will be scheduling at next day Ex: current time is 10:00am if we scheduled at 7:00am then it will run on tomorrow             7:00am
  • Editing (or)modifying at job is not possible after we set it. We need to delete job and recreate it.
  • If the system is down but job exist at the time then once the syatem is up, immediately the job will run
  • We can see our own job using  # atq
  • Other user jobs cannot be seen. only the root user can see all jobs of users.
  • After executing “at” job it will be deleted.
  • Non redirected output will be redirected to mail
eg: in place of commands if you give
               echo "helloooo"
then the output ont be displayed on terminal rather the outpout will be sent to user mail.
if you want to display the output on terminal, then use terminal name
eg:     echo "helloooo" > /dev/tty2                             [to verify tty name : use tty command]

To allow and Deny the user to create crontab:

/etc/at.deny    :  To deny  the user to create “at” job, we need to make an entry of username  into /etc/at.denny

Vi /etc/at.deny
kiran
:wq!

Now system wont allow kiran to create at jobs, it will show permission denied.

# /etc/at.allow    : Block/restrict all current and future users otherthan one user.

Note: by default /etc/at.allow will be empty.
Be careful, if you update /etc/at.allow with any name, remaining all users will be denied to use at jobs.
--------------------------------------------------------------------------------------------------------------------------------------------------------
crontab:

Recurring Jobs: 

crontab:  It is used to schedule the  jobs on daily basis.
                It stands for cron tables
                It consists fo 6 fields. Out of which 5 are time and date related and last filed (6th                     filed) is either command/script to be executed.
 

below is the commad to know crontab fileds:
# cat /etc/crontab 


Here
Minutes              -     0 to 59
Hours                 -    0 to 23
Day of Month     -    1-31
Month                 -     jan-oct or  1-12
Day of Week      -    0-sun ; 1-mon; 6-sat

Commands:

crontab -e
To edit the crontab if exist or create a new one
crontab -l
To display existing crontab.
crontab -r
To remove existing crontab.
crontab  -e -u  user
To set/edit the crontab for another user. (Only root can do this)


Note:
  • An " * " in a fiels represtent all possible values.
  • Multiple values may be seperated by " , " (comma)
  • Ranges can be seperated by " - "
  • Seconds is not possible. Default is atleast a minute. ( you can write script to schedule for a second)
  • If we specify */5 in a minutes fields , it means every 5 minutes
  • 0-10/2 in a minutes fields means, every 2 minutes in  1 to 10 minutes 
  • Root can set crontab for any users , normal users can set crontab for only himself
Examples:

1. Every month; Every monday & Friday ; at 10:40am ; execute a job.
     45               10               *                 *                  1,5                commands

2. January & March; Monday to Friday; 13:40 ; execute a job
     40                13               *                1,3               1-5               commands

3.Every new year starting time ; execute a job
     0                    0               1                 1                  *                   commands

4. Every Month starting time; execute a job
     0                    0               1                 *                  *                   commands
 5 5.
5. Every Day starting time; execute a job
     0                    0               *                  *                  *                  commands

 6. Weekly starting time ; execute a job
0                    0               *                       *                  1                   commands
 kjklfkdlsjlkdksjfklds
7.Every 2 hours ; execute a job
     0                    */2             *                   *                 *                   commands


Few more easy commands:

Fields
Usage
Entries
@reboot

Execute the command immediately after reboot.(once system is UP)
        @reboot          commands
@hourly

To run once an hour
0   *   *   *   *  commands
@daily
To run once a day
0   0   *   *   * commands

@weekly

To run once a week
0   0   *   *   0  commands
@monthly

To run once a month
0   0   1   *   *  commands
@yearly
To run once a year

0    0   1   1   *  commands

Usages of crontab:
To take regular backups, scheduled updates, copying/moving/deleting files, displaying notifications etc.,






--------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks 😊

www.CodeNirvana.in

COPYRIGHT © 2017. Powered by Blogger.

Featured Posts

Featured Posts

Featured Posts

Popular Posts

Translate

Total Page Views

blog counter
Copyright © gskLinux | Linux Tutorials | Designed By | Gnanasekhar