Showing posts with label Find empty file or directory in Linux. Show all posts
Showing posts with label Find empty file or directory 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 ~]#