LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-15-2009, 09:09 AM   #16
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Original Poster
Rep: Reputation: 0

No you did not
 
Old 01-15-2009, 11:28 AM   #17
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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, ...).

Last edited by indienick; 01-15-2009 at 03:49 PM. Reason: I suck at Bash-scripting.
 
Old 01-15-2009, 03:18 PM   #18
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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
 
Old 01-15-2009, 03:27 PM   #19
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 01-15-2009, 03:28 PM   #20
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Bone11409 View Post
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.
 
Old 01-15-2009, 03:37 PM   #21
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by indienick View Post
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...
 
Old 01-15-2009, 03:50 PM   #22
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.)
 
Old 01-16-2009, 11:08 AM   #23
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 01-16-2009, 12:02 PM   #24
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Instead of using back-ticks, try the following:
Code:
#!/bin/bash

for file in $(cat test.txt)
do
  rm -rf $file
done
 
Old 01-16-2009, 02:34 PM   #25
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.
 
Old 01-16-2009, 02:37 PM   #26
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Hey...it was worth a suggestion.
 
Old 01-16-2009, 02:55 PM   #27
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
  


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
bash script- text file manip- delete everything before/after string justin99 Programming 9 11-20-2014 03:16 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
A script within a folder to delete the folder, script, and the folder's contents Cyberman Programming 15 10-17-2007 07:32 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 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
Open Source Consulting | Domain Registration