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! |
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. |
or just say
for i in * do rm -f $i done |
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?
|
I think the easiest would be:
cd /directory_to_clean && ls | xargs rm -f |
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 |
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: |
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 |
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. |