Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-13-2002, 09:32 PM   #1
Registered: Dec 2001
Location: China
Distribution: Mandrake 7.2 and 8.1
Posts: 51

Rep: Reputation: 15
bash script to rm all files in a dir

I want to write a short bash script to rm all files in a directory. I have a couple of directories that have too many files to use wild cards (!, returns a 'too many arguements error), which is hosing up my machine. So, I want to use a bash script that deletes them one at a time (perhaps a repeating loop that deletes the first file in the directory, the most recent file or whatever). My bash skills are negligible. Can anyone suggest a short script?

I posted something similar in the Newbie sections, which has a description of the more general problem.

Old 06-14-2002, 08:49 AM   #2
Registered: Jun 2002
Location: Grand Rapids, MI
Distribution: Redhat, Slackware
Posts: 78

Rep: Reputation: 15
Perl or Pyhton could do this also. One algorithm would be:

1. Get a directory listing into an array or list.
2. Filter out any file you do not want deleted (namely . and ..)
3. iterate over the array and exec the rm command for each filename.

Pretty simple to do.
Old 06-14-2002, 09:26 AM   #3
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971
or just say

for i in *
rm -f $i
Old 06-14-2002, 12:56 PM   #4
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Rep: Reputation: 31
How about a cron job that "rm -rf /whatever/directory/you/want/whatever_wildcard_you_want*" every once in a while? Any ideas why this doesn't work? Why are you getting the "too many arguments" error?

Last edited by sewer_monkey; 06-14-2002 at 12:58 PM.
Old 06-17-2002, 05:04 AM   #5
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
I think the easiest would be:

cd /directory_to_clean && ls | xargs rm -f
Old 06-27-2002, 03:26 PM   #6
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303

Rep: Reputation: 30
I havent read your other thread but if I wanted to delete everything in a directory, I would use "rm -rf /dir" & that removes EVERYTHING in the /dir, the r switch tell's it to recursively travel through the directory & f tell's it to force so your not repeatedly asked , are you sure?

Old 07-01-2002, 08:25 AM   #7
LQ Newbie
Registered: Apr 2001
Location: Lawton Ok
Distribution: Red Hat 7.2
Posts: 25

Rep: Reputation: 15

If you want to remove all file just do "rm -Rf *" and that will remove them all from the current directory recursively down.

If you want to decide on a file by file basis just add an "-i"
Old 07-19-2002, 05:31 AM   #8
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34

Rep: Reputation: 15
Hi ! keirobyn,
try something like this, hope it work out.

use -i switch with rm command with in loop. like this :
rm -i <file-name> or with wild card entries as follws in the script.

rm -i <directory-name>/*
it will propmt you each time before deleting a file and you can decide whether or not to delete that particular file.

Old 07-19-2002, 08:53 AM   #9
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
The whole point was that he couldn't use wildcards because he had to many files. So with or without the -i options it wouldn't work either way.
But this is a very old thread so I'm sure he's got his solution by now.


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
Bash Script test for empty dir uopjohnson Linux - Software 3 10-07-2005 07:45 PM
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 08:32 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 01:24 AM
Bash script (untar *.tar files and then cd to the untared dir) k0ljat Linux - Newbie 2 12-30-2003 06:57 AM

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

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