LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-04-2006, 10:51 AM   #1
milke
LQ Newbie
 
Registered: Jul 2006
Posts: 2

Rep: Reputation: 0
Unhappy scripting problem rm filelist


Hallo I have a simple scripting problem,
which I cant solve.
I have a list of files stored in textfile like this:

/home/file1
/home/file2
/home/file3

And I need a Shellskript to remove the files
which are mentioned in the textfile.
I know it sound's simple, but I still don't get it

Thank you for all kind of answeres in advanced.
 
Old 07-04-2006, 11:04 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,394
Blog Entries: 55

Rep: Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565
Why not post what you've got already?
 
Old 07-04-2006, 11:21 AM   #3
milke
LQ Newbie
 
Registered: Jul 2006
Posts: 2

Original Poster
Rep: Reputation: 0
If it help's?

I trying to write a script, that delete files which are
several times on my PC.

#I build the md5 first

find . -type f -exec md5sum {} \; > ./sum.unsorted

#The I sorte after the checksum


sort < ./sum.unsorted > ./sum.sorted

#Then savethe filename of every filen which appears
# more then ones in a textfile

uniq -w 32 -D < ./sum.sorted > doppelte-Dateien.log

And form there on I see my diggest problem in the
removing of the files from the list.
 
Old 07-04-2006, 11:37 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,394
Blog Entries: 55

Rep: Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565
If it help's?
Sure does.

Code:
cat doppelte-Dateien.log|while read f; do
if [ -e "$f" ]; then rm -f "$f"; fi; done
or
Code:
cat doppelte-Dateien.log|xargs -iF rm -f 'F'
BTW's: watch out for relative paths is you use current working directory "find . -type". You can pipe sort into uniq. Also try to use "mktemp" for tempfiles: works "better" when you script more.
 
Old 07-04-2006, 09:26 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,187

Rep: Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037
One BIG FAT WARNING if you do rm in scripts:

The bash shell scripting language is such that if you make a little mistake with one small character, the command can be entirely different.

Especially when a command like rm is involved.

It happened to me that I wiped out the entire directory I was working in, including my 250 line script I was working on at that moment. I think I forgot a quote or so

It is better to put this somewhere at the start:

Code:
RM=/usr/bin/echo
#RM=/usr/bin/rm

..your code here..

#finally THE command:
$RM whatever.files
If you are 100% sure that the code does what your expect, uncomment the /usr/bin/rm, and comment the /usr/bin/echo

jlinkels
 
Old 07-04-2006, 09:56 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
I like jlinkles idea. I have been preceding the rm command with echo to see what the entire command would look like.
But then, I usually do this interactively instead of from a script.

I would add to his/her sig: If your question is about awk, read "Gawk: Effective Awk Programming" from the gawk-doc package.

If the filenames contain whitespace, you need to pipe the filelist through "tr".
cat doppelte-Dateien.log | tr '\n' '\000' | xargs -0 rm -f

If there are thousands of files in the list, then you also want to use one of the input limiting options to xargs.
 
Old 07-05-2006, 08:48 AM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,187

Rep: Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037
Quote:
Originally Posted by jschiwal
I would add to his/her sig: If your question is about awk, read "Gawk: Effective Awk Programming" from the gawk-doc package.
That's funny... the contents is almost the same as the GAWK user manual, but not quite. Unusual in the community to have two versions of a document, not verbatim equal.
 
Old 07-06-2006, 04:32 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
You found two versions of the Gawk manual. I was referring to "Gawk: Effective AWK programming" which is a different book.
 
Old 07-06-2006, 02:44 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,187

Rep: Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037
I assume you are right, but Google confused me.
On the very top of the page the first line reads:
Gawk: Effective AWK Programming

I'll look it up in the doc package as you say.

jlinkels
 
Old 07-07-2006, 06:41 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
One closer inspection, one of them may be a version of the gawk book I was referring to, but looks a lot different from the version that I printed. A version of a document produced with "make pdf" from the source may contain much more information from the info version.
 
  


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
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
wine dc++ shuts down when trying to download or get filelist tudilica Linux - Software 3 04-18-2005 07:54 AM
Slackware's FILELIST.TXT DaHammer Slackware 5 09-26-2004 05:29 AM
problem in shell scripting visu Linux - Newbie 4 01-06-2004 05:16 PM
scripting problem,its annoying roo Linux - Newbie 2 04-02-2003 04:47 AM

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

All times are GMT -5. The time now is 12:37 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