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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-04-2006, 10:51 AM
|
#1
|
LQ Newbie
Registered: Jul 2006
Posts: 2
Rep:
|
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.
|
|
|
07-04-2006, 11:04 AM
|
#2
|
Moderator
Registered: May 2001
Posts: 29,415
|
Why not post what you've got already?
|
|
|
07-04-2006, 11:21 AM
|
#3
|
LQ Newbie
Registered: Jul 2006
Posts: 2
Original Poster
Rep:
|
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.
|
|
|
07-04-2006, 11:37 AM
|
#4
|
Moderator
Registered: May 2001
Posts: 29,415
|
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.
|
|
|
07-04-2006, 09:26 PM
|
#5
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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
|
|
|
07-04-2006, 09:56 PM
|
#6
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
07-05-2006, 08:48 AM
|
#7
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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.
|
|
|
07-06-2006, 04:32 AM
|
#8
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You found two versions of the Gawk manual. I was referring to "Gawk: Effective AWK programming" which is a different book.
|
|
|
07-06-2006, 02:44 PM
|
#9
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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
|
|
|
07-07-2006, 06:41 AM
|
#10
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
All times are GMT -5. The time now is 01:21 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|