LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-14-2011, 09:16 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
Delete empty files and folders


Code:
#!/bin/bash

echo 'Folder name'
read f
if [ -d "$f" ] 
then
     cd "$f"
     for i in `find -maxdepth 1 -mindepth 1 -empty`
do
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?
 
Old 10-14-2011, 10:57 AM   #2
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Atleast maybe someone can tell which command shows only file names and date when they were created because i can't find.

Last edited by trintukaz; 10-14-2011 at 10:58 AM.
 
Old 10-14-2011, 11:05 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
I don't thing the filesystem contains such information. You can, however, get last access or modification time with stat
 
Old 10-14-2011, 11:11 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.

http://mywiki.wooledge.org/DontReadLinesWithFor
http://mywiki.wooledge.org/BashFAQ/001

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.

http://mywiki.wooledge.org/BashFAQ/031


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

Other than that, it looks like a pretty good start.
 
Old 10-14-2011, 11:18 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Assuming some ext filesystem
Quote:
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.
 
Old 10-14-2011, 11:21 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Quote:
then
cd "$f"
for i in `find -maxdepth 1 -mindepth 1 -empty`
do
You could use
Code:
     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".
 
Old 10-14-2011, 11:39 AM   #7
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thank you but now I just need command which shows date when folder or file was created because I can't find it.
 
Old 10-14-2011, 11:57 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.

Last edited by jschiwal; 10-14-2011 at 12:17 PM.
 
Old 10-14-2011, 12:15 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
Old 10-14-2011, 12:16 PM   #10
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thank you for the help.

Last edited by trintukaz; 10-14-2011 at 12:27 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to delete all empty folders rjo98 Linux - Newbie 3 06-07-2010 12:01 PM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 05:35 PM
Can't delete certain files or folders...how do I change this? xsithlordx Suse/Novell 10 03-05-2006 11:32 PM
HELP: can't delete files/folders :( kevingpo General 1 12-07-2004 08:51 PM
Delete Empty files whospeaks2me Linux - Newbie 2 07-18-2003 11:39 PM


All times are GMT -5. The time now is 09:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration