LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-12-2005, 05:54 PM   #1
justgiver
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Rep: Reputation: 0
Script to delete files with 0k file size in a directory


im looking to write a script with fairly basic code.

i need to accept a directory as an optional command line parameter.

display error message if more than one parameter is entered or use the current directory if no parameter is provided.

displays an error message and terminates if provided parameter is not a directory

the script will then cycle through the directory, prompting the user if he/she wants to delete the file that is 0k.


im just having a hard time getting started on this script and accepting a parameter. one thing i found was: set -- $(ls -l filename)
but im not too sure how to use it.


thanks a lot for answering this noob question!
 
Old 10-12-2005, 05:57 PM   #2
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian jessie/sid; OpenSuSE; Fedora
Posts: 1,593

Rep: Reputation: 162Reputation: 162
you'll delete all your device files if you do that. There are a lot of files of size 0 that you need. In fact, most files that are 0 are called empty documents. Just leave those alone.
 
Old 10-12-2005, 06:13 PM   #3
justgiver
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Original Poster
Rep: Reputation: 0
the user will be prompted to delete the file before it is deleted.

this will only be used on a test directory. im just new to learning linux and the program has no real value except learning how to do simple commands
 
Old 10-12-2005, 07:15 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
This aught to get you started.
It just list the files and size in the /home/images folder.
You will have to add some more stuff for deleting.
If you want to make a couple of empty files to help in testing,
the easiest way is use the command: >
For example: >file.txt

Edit: you could test for more than one argument with this...
test $# == 1 || usage

Code:
#!/bin/bash
# Example: ./test /home/images

usage()
{
        echo "Usage: $0 Directory_Name"
        exit 1;
}

test -d "$1" || usage

for i in `ls $1/*` ; do
   size=`stat -c %s $i`

if [ $size -lt 1 ]; then
   echo "$i is $size ( This one is empty ) "
else
   echo "$i is $size "
fi

done

Last edited by homey; 10-12-2005 at 07:38 PM.
 
Old 01-28-2008, 05:56 AM   #5
daelomin
LQ Newbie
 
Registered: Mar 2005
Distribution: Ubuntu at home, Mandriva 2007 at work
Posts: 15

Rep: Reputation: 0
My 2 worth.. Unelegant but works.

Quote:
##/bin/bash

dir_to_clean=$1

cd $1
ls -1s |awk '{if ($1==0) print ("rm -f "$2)}' > rmscript
chmod +x rmscript
echo "The following files with zero size will be removed:"
cat rmscript


echo -n "Proceed? [Y/N]: "
read response
if [ "$response" == "Y" ]; then
rmscript
fi

rm -rf rmscript

Last edited by daelomin; 01-28-2008 at 05:59 AM.
 
  


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
Delete Files by size LoafOfBread34 Linux - General 3 11-28-2005 12:16 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM
script/command to delete all symlinks from a directory reddazz Linux - General 5 03-16-2005 12:39 AM
How can I delete a directory and its files? OrganicX Linux - General 6 09-01-2002 05:25 AM
delete files by size Serena Linux - Newbie 2 04-25-2002 08:38 PM


All times are GMT -5. The time now is 11:26 PM.

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