LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script to rm all files in a dir (https://www.linuxquestions.org/questions/programming-9/bash-script-to-rm-all-files-in-a-dir-23377/)

keirobyn 06-13-2002 08:32 PM

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.

http://www.linuxquestions.org/questi...716#post103716

Thanks!

kahuna 06-14-2002 07:49 AM

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.

acid_kewpie 06-14-2002 08:26 AM

or just say

for i in *
do
rm -f $i
done

sewer_monkey 06-14-2002 11:56 AM

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?

Mik 06-17-2002 04:04 AM

I think the easiest would be:

cd /directory_to_clean && ls | xargs rm -f

amp2000 06-27-2002 02:26 PM

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?

HTH

carlcromer 07-01-2002 07:25 AM

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":cool:

edreddy 07-19-2002 04:31 AM

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.

bye
Dhananajaya

Mik 07-19-2002 07:53 AM

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.


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