Bash script to delete folder's that are listed in a text file
ProgrammingThis 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.
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.
I am calling this Perl script "xrm" (eXtended RM); I am not into Perl much anymore, so I cannot guarantee that this will work, but give it a shot with a test directory.
Code:
---xrm.pl------------------------------------------------------------
#!/usr/bin/perl -w
my USAGE = "usage: $ARGV[0] [file]";
my INPUTFILE = open(listOfCrap, $ARGV[1]) or die $USAGE;
my x = 0;
foreach $item (@INPUTFILE) {
system("rm -rf $line");
$x += 1;
}
print "Removed $x entry and all of its contents\n" if ($x = 1);
print "Removed $x entries and all of their contents\n" if ($x > 1);
---------------------------------------------------------------------
Next time, Bone11409, be a little more helpful when it comes to supplying information. The task alone is fine, but chances are you have a specific target environment (Windows, Linux, BSD, etc.) and within that environment, support for various languages (Bash, KSH, Perl, Python, C, ...).
Last edited by indienick; 01-15-2009 at 03:49 PM.
Reason: I suck at Bash-scripting.
This shows two examples of passing a file into a loop.
It would not be a large step from this script to what you are after.
Code:
while read l_files
do
rm -rf $l_files
done < inputfile
I have put a request for confirmation (to make things safer):
Code:
while read l_files
do
read -p "Do you want to remove $l_files? [Y/N]" answer
case $answer in
[Yy]|[Yy][Ee][Ss]) echo "Removing $l_files"
rm -rf $l_files;;
*) ;;
esac
done < inputfile
Very nice - I was thinking "hey, you could just run 'rm' with the '-i' option," but then I realized that you are demoting the level of control which would nicely prevent the user from pulling their hair out from having to answer "y" or "n" to every file that gets deleted.
I assume this is a reply to my post stating "we tried".
I have to tell you that your approach demonstrated in this thread is NOT how to get help. I hope that you will follow the advice in my private e-mail to you. LQ is here to help you, but only if you reply to questions and show evidence of your own work.
Very nice - I was thinking "hey, you could just run 'rm' with the '-i' option," but then I realized that you are demoting the level of control which would nicely prevent the user from pulling their hair out from having to answer "y" or "n" to every file that gets deleted.
Thanks, I hate having to say yes to every file.
I admit that I thought your suggestion that you didn't know bash while getting the user to delete the input file was quite amusing.
However, others tend to use this site as a reference, so I though I'd give an answer. Obviously the OP could have found the answer on his own with a few well chosen google searches, but...
Both backticks and the $() version work fine here. And the backticks is the older, more POSIX compliant form, no? I guess I'm just saying that I would be surprised if the backticks were the problem.
First, the "rm -rf" command will only work if you are in the directory where the files are. The best solution is to use the full pathname---eg: "rm -rf /home/username/somedir/$file"
Second, add an echo command to the loop so you can verfiy what is being put into the variable "file". eg:
Code:
for file in `cat test.txt`; do
echo $file
rm -rf $file
done
Finally, use [CODE] tags ("#" in the header of the compose window). This makes things easier to read.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.