Remove empty files in home directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove empty files in home directory
# 8  
Old 01-19-2018
Hi,

There's no easy way to do what you want to do with find, or at least not based on a simple -type test. As you've previously been advised, an empty file is defined as a file containing 0 lines and whose contents are 0 bytes in length - in other words, a file that is empty. A file that contains anything else is not empty, and no UNIX utility will ever treat it as such.

The problem you have here is that you're not really working with the correct definition of "empty", as far as UNIX itself is concerned. An empty file is one which contains literally and solely nothing whatsoever, and whose size on disk is always zero bytes. If it contains anything else it's not empty, and you'll have to test for whatever else you want to find on a case-by-case basis, being very careful indeed not to match any legitimate files you wouldn't want to remove.

For example, whilst it's easy to search for all files with a line consisting of a single space (e.g. grep -l ^\ $ *), the grep utility operates on a line-by-line basis. So whilst this would indeed return the names of files which had a line consisting of a single space, this wouldn't take into consideration any lines before or after such a matching line which might contain legitimate text. There's no conceptually-easy way to model what you want to find, since by definition the files you're looking at are not actually empty.

The only thing that comes close to what you want would be if you know that the files you regard as "empty" are always no more than one line in length. You could at least return all files that consist of only one single line by doing something like:

Code:
find . -type f -exec /usr/bin/wc -l \{\} \; | awk '$1 <= 1 {print $2}'

This would find all files in or beneath the current working directory, and only print their filename if they consisted of 0 or 1 lines. But again note that this will not be paying any attention to the actual content of those lines - the files in question could consist of one single line of legitimate text, so you'd have to add further tests to check that they only contained whitespace. But this should be enough to get you started, with any luck.
These 2 Users Gave Thanks to drysdalk For This Post:
# 9  
Old 01-19-2018
What about non-space non-printable characters? Would you consider a file with 2 or more <linefeed> chars a removal candidate although it has two+ empty lines? Or, a <BEL> (0x07, ^G, \a) char?
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I check, if on remote server directory is empty or have files?

I have a script, which is supposed to run 1 day of the month, connect to remote server certain directory, find files, tar the, and copy find . -ctime -1 | tar -cvf transfer_dmz_start_monthly.tar *${Today}*.*; if then echo "Cannot create a tar file, the terminated... (2 Replies)
Discussion started by: digioleg54
2 Replies

2. Shell Programming and Scripting

Shell script to remove empty files

Hi All, Can anyone please write a shell script to remove the empty files using an if condition. please help me out , urgent thanks (6 Replies)
Discussion started by: muthi_murali
6 Replies

3. UNIX for Advanced & Expert Users

cksum for all files in home directory

I know i can run cksum <filename> . However, how i can run cksum on all the files and directories in the $HOME ?? (SUNOS) (4 Replies)
Discussion started by: moe458
4 Replies

4. Shell Programming and Scripting

help with removing files from home directory

hey there folks! I cant figure out, for the life of me, how to procede in removing alll the files in my home directory that are not owned by me. would i have to list them, but after that what do i do. or is there some way I am not aware of. my employer heard i could script in unix, but i havent... (3 Replies)
Discussion started by: Ginkosu
3 Replies

5. Solaris

Newbie questions about HOME directory files

Hi, I am newbie to Solaris and system administration in general, and I have a couple of questions about files in my HOME directory. When I perform ls -la, I get the following list of files: drwxr-xr-x 7 XXXYYY staff 17 Aug 24 07:31 . drwxr-xr-x 7 root root 7... (2 Replies)
Discussion started by: JVerstry
2 Replies

6. Shell Programming and Scripting

remove empty directory

Hi, I need to delete an empty directory in a temp directory except "dir5" (keep everything that is not empty). Plese advise. Here is an example of my directory. /dir/temp/ dir1 - delete if this is empty dir2 - delete if this is empty dir3 - delete if this is empty dir4 - delete if this... (7 Replies)
Discussion started by: sirrtuan
7 Replies

7. Shell Programming and Scripting

How to empty all files in a directory

Hi all, Can you tell me how to empty all files in a directory with a "find" command? It does not seem to work the way I try it: # ls -l *.dat -rw-r--r-- 1 root root 7 Jul 20 20:51 la2.dat -rw-r--r-- 1 root root 4 Jul 20 20:51 la.dat # find... (9 Replies)
Discussion started by: majormark
9 Replies

8. Cybersecurity

Strange files keep appearing in my home directory

Hi everyone, really strange files keep appearing in my home directory. I have absolutely no idea where they come from and I'm a little concerned that they could come from some kind of malware activity or Firefox exploit. I searched Google for parts of the file names but without a result. The... (6 Replies)
Discussion started by: schallstrom
6 Replies

9. Shell Programming and Scripting

Script to remove all empty files within the directory structure?

Hi I need to write a shell script which basically searches for all the empty files within the directory structure, lists them before asking the user to confirm if they would like to delete them. If the user deletes the file then a notice would appear confirming the file is deleted. I've be... (5 Replies)
Discussion started by: cat123
5 Replies

10. Shell Programming and Scripting

deleting empty files in a directory

Hello Gurus, I want to delete only empty files (files with 0 bytes) at once from the local directory. Rightnow I need to go through all the files one by one manually and check the empty files before deleting them. Is there any unix command that finds and deletes empty files in a directory?... (5 Replies)
Discussion started by: berlin_germany
5 Replies
Login or Register to Ask a Question