LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-24-2010, 12:59 AM   #1
quanvu07
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Script to delete a list of Directories


Hi Expert,

Hope someone have script or can show me how to write a short script file to read file (text) contains a list of directories name and delete everything in it.

There are 10,000 directories - So there is NO WAY I can do manually. Hope some expert out there has a scripts in place. That would be greatly appreciated.

Thanks,
 
Old 10-24-2010, 01:05 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello and Welcome to LinuxQuestions,

You'll have to look into the find command. You wouldn't even need to write a script to just delete directories and there contents if that's what you want to do. Have a look at the man page for starters.
Code:
man find
If like you said you have a file that contains a list of the directories, then you'll only need to write a script to loop through the file, check if the directory exists and delete it (and it's contents).

Some good reading about bash scripting:
Bash Guide for Beginners
Advanced Bash Scripting Guide

You'll need to put in the work and when you encounter problems with it, then you come to LQ to ask for help. Don't expect LQ users to give you ready made solutions.

Kind regards,

Eric
 
Old 10-24-2010, 10:48 AM   #3
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
If you already have a file with all the directories a command like
Code:
for name in `cat dirlist`; do rm -rf $name; done
Note1: Make sure the directory names in your dirlist have complete path specifications or that your current directory is the one that contains all the directories you want to remove.

Note2: Make sure you understand what you are doing before you issue above command. Because rm will remove the directories and its contents with only a very, very small and painful chance to recover them.
 
Old 10-24-2010, 04:28 PM   #4
martinbc
Member
 
Registered: Jun 2010
Distribution: Ubuntu, played with Puppy Slitaz & OpenSUSE
Posts: 40

Rep: Reputation: 4
Hi

If I'm manipulating large numbers of files or directories I normally use a command which outputs the required command lines (for example using echo "rm -rf ..."). I can then redirect the output to a file with ">", check that the file contains sensible commands, and then run the file.
This way has saved me from more than a few mistakes.

Martin
 
Old 10-24-2010, 05:51 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by gdejonge View Post
If you already have a file with all the directories a command like
Code:
for name in `cat dirlist`; do rm -rf $name; done
Note3
The above command could go seriously wrong if any of the filenames contain spaces. A safer version of the above would be:
Code:
while read name ; do rm -rf $name ; done < dirlist
Evo2.
 
Old 10-24-2010, 07:30 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,492

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Note4
Better also hope that none of your files have the same name any of the directories you are deleting.
 
Old 10-24-2010, 07:38 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,089

Rep: Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474Reputation: 2474
and no typos
/usr/opt/www/mysql/
is a bit different than the two
/usr/opt/www/mysql /
 
Old 10-25-2010, 05:18 PM   #8
quanvu07
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Script to delete

Thanks everyone - That is all I need the command and loop to open a file.

Thanks, Expert.
 
Old 10-25-2010, 08:26 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,492

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Please mark as SOLVED if you have your solution.
 
  


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 all except three specified directories supulton Linux - Newbie 6 07-29-2010 05:19 AM
How to delete files in many directories? pandronic Linux - Newbie 3 10-23-2006 09:20 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
How does one delete directories with content as the SU? Ferrell Ramey Linux - General 2 08-22-2002 07:16 PM
Cant delete directories/files skopje909 Linux - General 2 11-07-2001 05:59 PM


All times are GMT -5. The time now is 03:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration