LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to delete folder's that are listed in a text file (https://www.linuxquestions.org/questions/programming-9/bash-script-to-delete-folders-that-are-listed-in-a-text-file-697273/)

Bone11409 01-14-2009 10:44 AM

Bash script to delete folder's that are listed in a text file
 
I have been trying to find a bash scrpit that will delete folders that are listed in a text file. The text file is generated once a month and list only the folder's name in a column view. If anyone could help me I would sure appreciate it.

repo 01-14-2009 10:54 AM

what have you done already?

Bone11409 01-14-2009 11:30 AM

no I have look on the forums for a script that is simailar to what I need to do but have had no lucky finding one I am hopeing someone could point me to a script that I could look at and go from there so far I have been able to look at someone else's script and been able to change it to what I need with some help from the forum's and google

jcookeman 01-14-2009 11:48 AM

Why dont you give us an example of the file you need to read from?

pixellany 01-14-2009 11:53 AM

Quote:

Originally Posted by Bone11409 (Post 3408505)
no I have look on the forums for a script that is simailar to what I need to do but have had no lucky finding one I am hopeing someone could point me to a script that I could look at and go from there so far I have been able to look at someone else's script and been able to change it to what I need with some help from the forum's and google

Please break up the run-on sentences.

The way this reads (to me) is that you first say you have not been able to find anything, and then you say:
Quote:

I have been able to look at someone else's script and been able to change it to what I need with some help from the forum's and google
Also, please confirm if this is homework.

Here is a hint to get started

use the "read" command to read one line at a time into a variable (eg dirname), then use "rmdir $dirname".
Use the redirection operator (<) to cause "read" to read from a file and not STDIN.

Bone11409 01-14-2009 12:10 PM

What I have is a text file that has in it a list of folder names that need to be deleted. What I would like to do is have a script look at the text file and delete folders based upon the information in the text file. And what I meant to say is that I have been able to firugre out what I need to do for my other script but not this one mainly cause I am stuck. Sorry if I confused anyone

Disillusionist 01-14-2009 02:58 PM

Please post what you have tried (preferably in [CODE] tags).

pixellany has offered some advice on where to begin but you must understand that this smells of homework to us, and whilst we may offer pointers and suggestions, we are not here to do your homework for you.

This is not intended to cause you problems, it is just that if we give you the answers with out you understanding the issue, we are not helping you in the long term.

There are a number of bash tutorials that may prove useful.

pixellany 01-14-2009 05:43 PM

Quote:

Originally Posted by Disillusionist (Post 3408718)
Please post what you have tried (preferably in [CODE] tags).

As a minimum, get comfortable with the use of the "read" command, and post that portion.....

Bone11409 01-14-2009 08:36 PM

I understand what you mean by homework but I did not ask you to write a script for me I ask if you could point me to scripts similar to what I am doing.

pixellany 01-14-2009 09:24 PM

I am concerned that you don't seem to want to read a book, online tutorial, etc.--or even try the commands that I suggested.

Please tell us where you are in the class---specifically, we need to know what commands and techniques you have already studied.

If it helps, what you are trying to do is quite simple. Many of us could write the script in the same time it would take to find something similar.

Bone11409 01-15-2009 07:43 AM

I have already spend two week's looking on forums and watching online tutorial's just to get to this point and it seem that you do nothing but critsize me for not reading books and watching tutorial's when you don't know what I have done.

repo 01-15-2009 07:56 AM

Quote:

when you don't know what I have done.
Show us what you have so far

pixellany 01-15-2009 07:58 AM

Bone;

No one here has anything to gain by criticizing you---I for one had no such intent.

The issue we are having is that people ask you questions to try and assess where you are in the learning process, and we are not getting clear answers.

So, here are a few specific questions which would help us to help you:

1. Where are you in the class? For example, have you studied all of the basic Bash commands--also called the core utilities?

2. Have you used the "read" command? To read from standard input--ie the keyboard? To read from a file?

2A. As part of this, have you used the redirection operators? ("<" and ">")

3. Have you used the "rmdir" command?

4. Are you familiar with basic loops--eg "for", "while"?

5. Are you familiar with assigning a variable (name = <something>) and then retrieving the value with "$name"

Here's another huge hint: The above is pretty much all you need for your script.

Bone11409 01-15-2009 08:22 AM

Thanks for all the help you gave me but I am going quit trying to do this in linux and do this in windows mainly cause I am use to it and that I can get some help from my friends so once again Thanks

pixellany 01-15-2009 08:25 AM

We tried.....

reported for closure

Bone11409 01-15-2009 09:09 AM

No you did not

indienick 01-15-2009 11:28 AM

FFS:

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, ...).

Disillusionist 01-15-2009 03:18 PM

A google search of "while read" came up with the following link:
http://www.linuxforums.org/forum/lin...read-line.html

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


indienick 01-15-2009 03:27 PM

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.

pixellany 01-15-2009 03:28 PM

Quote:

Originally Posted by Bone11409 (Post 3409761)
No you did not

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.

Disillusionist 01-15-2009 03:37 PM

Quote:

Originally Posted by indienick (Post 3410228)
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...

indienick 01-15-2009 03:50 PM

Bwahaha - thanks for pointing that out. Yeah, Bash hasn't really ever been something that latched onto my interests.

(It's been a long, boring day at work.) :)

Bone11409 01-16-2009 11:08 AM

Here is what I have so far but the problem is it deletes the test.txt file and not the folders.

#!/bin/bash

for file in `cat test.txt`; do
rm -rf $file
done

indienick 01-16-2009 12:02 PM

Instead of using back-ticks, try the following:
Code:

#!/bin/bash

for file in $(cat test.txt)
do
  rm -rf $file
done


Telemachos 01-16-2009 02:34 PM

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.

indienick 01-16-2009 02:37 PM

Hey...it was worth a suggestion. ;)

pixellany 01-16-2009 02:55 PM

Bone;

I am glad to see you making progress here.

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.


All times are GMT -5. The time now is 04:21 PM.