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. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-13-2002, 09:32 PM
|
#1
|
Member
Registered: Dec 2001
Location: China
Distribution: Mandrake 7.2 and 8.1
Posts: 51
Rep:
|
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!
|
|
|
06-14-2002, 08:49 AM
|
#2
|
Member
Registered: Jun 2002
Location: Grand Rapids, MI
Distribution: Redhat, Slackware
Posts: 78
Rep:
|
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.
|
|
|
06-14-2002, 09:26 AM
|
#3
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
or just say
for i in *
do
rm -f $i
done
|
|
|
06-14-2002, 12:56 PM
|
#4
|
Member
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624
Rep:
|
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.
|
|
|
06-17-2002, 05:04 AM
|
#5
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
I think the easiest would be:
cd /directory_to_clean && ls | xargs rm -f
|
|
|
06-27-2002, 03:26 PM
|
#6
|
Member
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303
Rep:
|
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
|
|
|
07-01-2002, 08:25 AM
|
#7
|
LQ Newbie
Registered: Apr 2001
Location: Lawton Ok
Distribution: Red Hat 7.2
Posts: 25
Rep:
|
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"
|
|
|
07-19-2002, 05:31 AM
|
#8
|
Member
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34
Rep:
|
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
|
|
|
07-19-2002, 08:53 AM
|
#9
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
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 06:22 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|