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.
|
what have you done already?
|
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
|
Why dont you give us an example of the file you need to read from?
|
Quote:
The way this reads (to me) is that you first say you have not been able to find anything, and then you say: Quote:
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. |
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
|
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. |
Quote:
|
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.
|
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. |
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.
|
Quote:
|
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. |
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
|
We tried.....
reported for closure |
No you did not
|
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------------------------------------------------------------ |
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 Code:
while read l_files |
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.
|
Quote:
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. |
Quote:
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... |
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.) :) |
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 |
Instead of using back-ticks, try the following:
Code:
#!/bin/bash |
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.
|
Hey...it was worth a suggestion. ;)
|
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 |
All times are GMT -5. The time now is 04:21 PM. |