Find and delete empty files and directories


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Find and delete empty files and directories
# 1  
Old 01-17-2012
Question Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Need to make a script, to remove all empty files and folders from current category.
It also should show the name and creation date of those items.

2. Relevant commands, code, scripts, algorithms:
Must be bash script.


3. The attempts at a solution (include all code and scripts):

Tried this, but it shows only name of deleted folders...
Code:
echo Removed files:
find . -maxdepth 1 -type f -empty;
find . -maxdepth 1 -type f -empty -exec rm {} \;
echo Removed directories:
find . -maxdepth 1 -type d -empty;
find . -maxdepth 1 -type d -empty -exec rmdir {} \;

Then I tried using
Code:
ls

... But it gave too much information.. I just need name and creation time (Ok.. Creation time isn't stored in UNIX.. So I think a date of last modification would be enough.. )

Code:
echo Removed files:
find . -maxdepth 1 -type f -empty -exec ls -l;
find . -maxdepth 1 -type f -empty -exec rm {} \;
echo Removed directories:
find . -maxdepth 1 -type d -empty -exec ls -l ;
find . -maxdepth 1 -type d -empty -exec rmdir {} \;

But in this code first of all it shows only info about files.. For some reason it doesn't show anything about folders. And secondly: That's not that I need. I need name and date (maybe date, maybe date+time) of creation (last modification).

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Vilniaus Kolegija, Vilnius, Lithuania, M.Liogys, PIN11.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 01-18-2012
Maybe you need the "-d" switch to "ls" to make "ls" show the directory not its contents. For consistency the "-d" also works with files.
Code:
find . -maxdepth 1 -type f -empty -exec ls -lad {} \;
find . -maxdepth 1 -type d -empty -exec ls -lad {} \;

Obviously be aware that "rm" on a file changes the "last modified" time on the directory which holds that file.
# 3  
Old 01-18-2012
Thank you.
I really appreciate your help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to delete empty files from specific locations

Hi, I need help in regard to developing a shell script to delete empty files from multiple specific locations. The directory paths will be stored in a text file. So the requirement is to read the text file for one specific path and then remove empty files from that particular path. Looping through... (4 Replies)
Discussion started by: Khan28
4 Replies

2. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

3. Shell Programming and Scripting

Delete empty files from a directory entered in command prompt

My code to "Delete empty files from a directory entered in command promt" #/bin/sh echo "Enter directory" read gh for file in `ls $gh` do # to get the size of file a=$( ls -l file | awk ' {print $7} '); echo $a if then echo "removing file " rm file fi done (6 Replies)
Discussion started by: adirajup
6 Replies

4. UNIX for Advanced & Expert Users

Delete empty directories recursively - HP-UX

Hi, I want to delete all empty directories in a long directore tree structure. I want to use that from a script that will run on HP-UX 11. My definition of empty directory is that there is no regular file under it and directly beneath it. To elaborate, I have below directories. /app/dev/java... (14 Replies)
Discussion started by: asutoshch
14 Replies

5. Shell Programming and Scripting

Script to delete empty files

I'm trying to write a shell script to files of zero length in a specified directory, but I keep getting errors. Would anybody be kind enough to look it over for issues? Thanks a bunch in advance. #!/bin/sh if then if then find $1 -type f -size 0 -print|xargs rm exit 0... (1 Reply)
Discussion started by: ScriptingIssues
1 Replies

6. Shell Programming and Scripting

Find directories only and delete them created 3 days before

Hello I have some directories and files created under /export/local/user I would like to delete directories only under /export/local/user, created before 3 days Can someone help me with command to do this task? Thanks (4 Replies)
Discussion started by: needyourhelp10
4 Replies

7. Shell Programming and Scripting

Deleting empty directories using find

Hello, I'm submitting this thread, because I was looking a way to delete empty directories using find and I found a thread from 2007 that helped me. I have worked from that threat, but I found that the command sent would analyze original directory and may delete it to. I have come up with expanded... (3 Replies)
Discussion started by: lramirev
3 Replies

8. Shell Programming and Scripting

Deleting all empty files in sub directories with a command

Hello Friends, Im trying to delete empty files in subdirectories with a command. I can find them checking only one directory in each step and then show them with my command like below moreover i could not add removing part: ls -l */* | awk '{if ($5==0) printf "%3s %2d %s... (5 Replies)
Discussion started by: EAGL€
5 Replies

9. Red Hat

Find files older than 30 days in directories and delete them

Hi, I have dummies questions: My script here can find the files in any directories older than 30 days then it will delete the files but not the directories. I would like to also be able to delete the directories that hold old files more than 30 days not just the files itself. find . -type f... (2 Replies)
Discussion started by: lamoul
2 Replies

10. UNIX for Advanced & Expert Users

how to delete empty files in a shell script

I'm trying to figure out a way to delete empty files in a directory. I have a cron that runs and creates a flat file every 15 mins. However, most times at night the flat file will be empty. I'd like to run a script to delete empty files that end with *.dat Any suggestions? Rich (1 Reply)
Discussion started by: rpnuge
1 Replies
Login or Register to Ask a Question