-   Programming (
-   -   Delete empty files and folders (

trintukaz 10-14-2011 08:16 AM

Delete empty files and folders


echo 'Folder name'
read f
if [ -d "$f" ]
    cd "$f"
    for i in `find -maxdepth 1 -mindepth 1 -empty`

Hello. I am creating script which deletes empty files and folders within directory and shows filename and date when file was created. I want to ask if I am starting to do it in good way and how to make script to show file names and dates when files was created?

trintukaz 10-14-2011 09:57 AM

Atleast maybe someone can tell which command shows only file names and date when they were created because i can't find.

millgates 10-14-2011 10:05 AM

I don't thing the filesystem contains such information. You can, however, get last access or modification time with stat

David the H. 10-14-2011 10:11 AM

First of all, don't use a for loop to read the output of a command. The shell does word-splitting on it before the loop gets to use it.

A while+read loop with null-separators is the recommended way, as detailed in the second link.

Also, $(..) is highly recommended over `..`.

And as long as you're using bash, the new [[ test keyword is also recommended over the old [ test.

As for getting details about a file, try using the stat command.

Other than that, it looks like a pretty good start.

Proud 10-14-2011 10:18 AM

Assuming some ext filesystem

Ext4 also adds support for date-created timestamps. But, as Theodore Ts'o points out, while it is easy to add an extra creation-date field in the inode (thus technically enabling support for date-created timestamps in ext4), it is more difficult to modify or add the necessary system calls, like stat() (which would probably require a new version), and the various libraries that depend on them (like glibc). These changes would require coordination of many projects. So, even if ext4 developers implement initial support for creation-date timestamps, this feature will not be available to user programs for now.

jschiwal 10-14-2011 10:21 AM


cd "$f"
for i in `find -maxdepth 1 -mindepth 1 -empty`
You could use

    find "$dir" -maxdepth 1 -mindepth 1 -empty
to list empty files and directories in $dir/.
You can have more than one directory. If you want to find all empty files, why cd into directories and use -maxdepth?

You can use "-type f" to list just files.

Also look in the find info manual at the section for "-printf". This allows you to print out a number of characteristics of a file, in the format you want. Look at -ls, which prints results in a form similar to "ls -l".

trintukaz 10-14-2011 10:39 AM

Thank you but now I just need command which shows date when folder or file was created because I can't find it.

jschiwal 10-14-2011 10:57 AM

Run the stat command on a file. Is there a date listed for the birth?

If not, it may not be recorded. If it exists, you can use stat with the '%w' option to provide the date a file was created. While the stat command provides the birth of a file, if the kernel doesn't record it when a file is created, you won't get a value.

David the H. 10-14-2011 11:15 AM

As was mentioned above, there is no creation date for most files. Unix has traditionally had only three file date fields: atime (time of last access), mtime (time of last modification, and ctime (time the inode was last updated).

Proud's post shows how a true creation time field has recently been added to the ext4 file system, but also that the system tools that would use it have yet to be updated to support it (stat seems to be one that has).

So you'll likely never find a file that has a recorded crtime. It's too new.

Therefore, the closest you'll come to a "creation" date for most files is the mtime, the last time the file contents changed.

trintukaz 10-14-2011 11:16 AM

Thank you for the help.

All times are GMT -5. The time now is 07:14 PM.