Deleting empty file as well as directory inward Unix
Many times nosotros require to honour as well as delete empty files or directories inward UNIX/Linux. Since at that topographic point is no unmarried ascendancy inward Unix/Linux which allows yous to take away empty files or empty directories rather nosotros require to rely on find command as well as xargs command. In this UNIX as well as linux instance nosotros volition come across How to delete empty files as well as directories. Before removing empty files as well as directories nosotros require to honour those files as well as at that topographic point are lots of selection available to search for empty directories similar find, grep , awk etc. You only require to know right option. Like inward whatever other operating organization empty files as well as directories inward Unix are those whose size is zero. Empty files doesn't contains whatever content spell empty directories does non comprise anything at all e.g files or sub-directories. As discussed inward previous postal service 10 oft used find ascendancy examples nosotros tin equally good purpose honour ascendancy to search as well as delete empty files as well as directories equally it provides searching files based on size equally well.
Creating Empty files as well as directories inward Unix
let's outset exercise empty file as well as directory to demonstrate instance of how to delete empty files inward Unix. We tin purpose same ready of commands which nosotros convey used inward our instance of How to honour size of files as well as directories inward Unix.
//This volition exercise empty file inward electrical flow directory
test@localhost: /unix touch empty.txt
//This volition exercise empty directory within electrical flow directory
test@localhost: /unix mkdir empty_dir
//This ascendancy volition honour all empty files as well as directories inward Unix
test@localhost: /unix find . -empty
./empty.txt
./empty_dir
//This volition exercise empty directory within electrical flow directory
test@localhost: /unix mkdir empty_dir
//This ascendancy volition honour all empty files as well as directories inward Unix
test@localhost: /unix find . -empty
./empty.txt
./empty_dir
Searching Empty Files as well as Directory inward Unix/Linux
find -empty selection prints both empty files as well as directories. If yous only desire to impress files than purpose -type f selection and -type d for listing empty directories. its quite flexible. You tin equally good purpose grep command along alongside ls –lrt to display empty files as well as directories equally shown below :
//this ascendancy volition impress empty files
test@localhost: /unix find . -type f -empty
./empty.txt
./empty.txt
//this ascendancy volition impress empty directories
test@localhost: /unix find . -type d -empty
./empty_dir
test@localhost: /unix find . -type d -empty
./empty_dir
//How to purpose grep ascendancy to impress empty files as well as directories
test@localhost: /unix ls -ltr | grep '\<0\>'
drwxr-xr-x+ 1 test Domain Users 0 Jun 15 11:43 empty_dir/
-rw-r--r-- 1 test Domain Users 0 Jun 15 11:44 empty.txt
//find ascendancy to impress empty files as well as directories
test@localhost: /unix find . -maxdepth 1 -size 0 -ls
90353467524120775 0 drwxr-xr-x 1 test Domain Users 0 Jun 15 11:43 .
9007199255261921 0 -rw-r--r-- 1 test Domain Users 0 Jun 15 11:44 ./empty.txt
19421773393554899 0 drwxr-xr-x 1 test Domain Users 0 Jun 15 11:43 ./empty_dir
test@localhost: /unix find . -maxdepth 1 -size 0 -ls
90353467524120775 0 drwxr-xr-x 1 test Domain Users 0 Jun 15 11:43 .
9007199255261921 0 -rw-r--r-- 1 test Domain Users 0 Jun 15 11:44 ./empty.txt
19421773393554899 0 drwxr-xr-x 1 test Domain Users 0 Jun 15 11:43 ./empty_dir
Deleting Empty Files and Directories inward Unix Linux
Now ane time nosotros sure enough that at that topographic point are empty files as well as directory exists yous tin delete them past times using find -delete selection or executing rm ascendancy inward combination alongside find ascendancy equally shown below:
//removing Empty files as well as directories using honour command
test@localhost: /unix find . -empty -delete
test@localhost: /unix find . -empty -delete
test@localhost: /unix ls -lrt
full 1.0K
-rw-r--r-- 1 test Domain Users 118 Aug 4 2011 contacts.txt
test@localhost: /unix find . -empty -delete
test@localhost: /unix ls -lrt
full 1.0K
-rw-r--r-- 1 test Domain Users 118 Aug 4 2011 contacts.txt
//using honour as well as xargs ascendancy to take away empty files as well as directories
test@localhost: /unix find . -empty | xargs rm -r
test@localhost: /unix find . -empty -type d -exec rm -r {} \;
find: `./empty_dir': Not a directory
test@localhost: /unix find . -empty | xargs rm -r
test@localhost: /unix find . -empty -type d -exec rm -r {} \;
find: `./empty_dir': Not a directory
That’s all on How to honour as well as take away empty files as well as directories inward Unix as well as Linux host. As I mentioned at that topographic point are many ways to honour empty files as well as directory but best agency is past times using honour command, which non exclusively listing empty files but empty directories equally well.
Further Learning
Linux Command Line Basics
Top 10 examples of chmod ascendancy inward Unix