Showing posts with label Find command in Linux. Show all posts
Showing posts with label Find command in Linux. Show all posts

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 ~]#

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]#