LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-20-2008, 11:09 AM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
For loop to delete all RPMS except for RPMS on the list.


Hi! I have a problem trying to delete extra rpms from a directory /cdrom/RedHat/RPMS/ which will be used to perform
a kickstart install via CDROM. The directory consist rougly around 1500 RPMS that we can slim down to around 300
required packages.

My goal is to pipe in a file "/var/log/rpmpkgs" and only keep those files since they were used to perform the install
and all dependencies will be resolved. As for the rest of the files we can remove all of them. I am stuck at how to
perform a loop that will save the rpms from /var/log/rpmpkgs but delete the 1300 rpms that remain.

Here are my awful attempts please advise on what I am doing wrong and how to exclude packages from a file.

Attempt #1

for i in `ls -l | awk '{ print $9 }'` do rm -i < /var/log/rpmpkgs

Attempt #2

find . -type f -exec rm -rf < /var/log/rpmpkgs \;

/var/log/rpmpkgs is a list of files on the system so they should remain.


Thanks
 
Old 05-20-2008, 12:07 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Code:
for file in ls 
do if grep $file /var/log/rpmpkgs >/dev/null 2>&1
   then echo "File $file is in /var/log/rpmpkgs so is NOT being deleted."
   else echo " Deleting file $file."
        rm $file
done
You don't need the -l flag of ls if all you want is the file name. Simple ls will give you the names only. (On display these wrap but in scripts it gets them properly - you can force single list on display with the -1 [one] option.)

The if tests whether the file you have exists in the /var/log/rmppkgs file. If so it doesn't do anything except tell you it is not being deleted. If not it tells you it is being deleted then deletes it.

Use this at your own risk as I haven't tested it here. I'd suggest creating a test directory and file to test a few files on before doing this on your main directory.
 
Old 05-20-2008, 12:16 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,805

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
There are probably more elegant ways than this to do it, but here's my offering.

Code:
for i in /cdrom/RedHat/RPMS/*; do
    for j in /var/log/rpmpkgs/*; do
        if [ -e "$i" -a -e "$j" ]; then
            mv $j /path/to/keeper-files/
        else
            continue
       fi
    done
done

rm /var/log/rpmpkgs/*
For each file in the first directory, compare it to every file in the second. If you find a match, move it to another (temporary) location.

Keep looping until all files in the first location have been compared to every file in the second location, with all matches moved for safekeeping.

Then remove the remainder.

You can flesh out the code to move the saved files back to the original location.

Edit: lightner thinks more elegantly than I do, and types faster than I do. I like his solution better than mine.

Last edited by bigrigdriver; 05-20-2008 at 12:18 PM.
 
Old 05-20-2008, 01:01 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Another solution is to compare the actual listing with the content of /var/log/rpmpkgs, print the differences and remove them
Code:
rm $(comm -3 <(ls -1 *.rpm) <(sort /var/log/rpmpkgs))
provided that the output of ls -1 is comparable to the content of /var/log/rpmpkgs.
 
Old 05-20-2008, 01:29 PM   #5
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks, jlightner, and bigrigdriver for your input your scripts worked!. I tweaked jlightner's script and it worked like a charm. Here is the modified script for those who are interested:

for file in `ls`
do if grep $file /var/log/rpmpkgs >/dev/null 2>&1
then echo "File $file is in /var/log/rpmpkgs so is NOT being deleted."
else echo " Deleting file $file."
rm $file
fi
done

I initially ran the script in the /cdrom/RedHat/RPMS directory and the script got blown away too. You might want to modify the script to just do a ls of the /cdrom/RedHat/RPMS directory.

Thanks for the help I have been banging my head for a couple of hours on this.
 
Old 05-20-2008, 04:42 PM   #6
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Oops - my apologies for leaving out the back ticks. Now you know why I told you to test it first. Glad you figured it out.
 
  


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
how to delete rpms of two diff architecture tanveer Linux - General 5 11-17-2007 04:12 AM
LIVNA: List of RPMs available? SlowCoder Linux - General 1 05-05-2007 11:13 PM
saving list of RPMS DAChristen29 Mandriva 8 08-24-2004 12:01 AM
List installed RPMs radam Linux - Software 4 07-21-2004 03:05 PM
List and remove rpms linuxturtle Linux - General 3 09-24-2003 02:01 AM


All times are GMT -5. The time now is 04:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration