LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 10-12-2007, 10:04 AM   #1
pbhj
Member
 
Registered: Dec 2002
Location: UK
Distribution: Slackware 12; Ubuntu 7.10
Posts: 358

Rep: Reputation: 31
Question bash: better way to delete files not matching a regex?


I'm trying to delete all non-relevant .po files from a gallery-2.2.3-full release.

I've tried this:
Code:
find ./ -regextype posix-extended ! -regex ^.*\/en_GB\.po$ | grep -e .*\\.po$ >> rm.txt
Which gets me a list of files to remove. But I'm not sure what to do now as I can't seem to pipe that through rm with cat.

How should I do this?

I have actually come up with a fix of:
Code:
find ./ -regextype posix-extended -type f ! -regex ^.*\/en_GB\.po$ | grep po$ | xargs rm
But this seems rather dirty and hackish. Any easier way?
 
Old 10-12-2007, 10:51 AM   #2
andrews-mark
Member
 
Registered: Feb 2007
Location: London
Distribution: debian
Posts: 108

Rep: Reputation: 15
maybe I'm misunderstanding (plus I am a bit stupid) but are you saying that you have a list of files you want to delete stored in a text file called "rm.txt". If so, I would do
Code:
for i in `cat rm.txt `; do rm $i;done
am I misunderstanding?

-mark
 
Old 10-12-2007, 10:53 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
If it works, it's OK.....

You can also use backtics to enclose the find|grep commands and cause their output to go to "rm":

rm `find.....|grep....`

(backtics, not single quotes)
 
Old 10-12-2007, 04:50 PM   #4
pbhj
Member
 
Registered: Dec 2002
Location: UK
Distribution: Slackware 12; Ubuntu 7.10
Posts: 358

Original Poster
Rep: Reputation: 31
andrews-mark, thanks, no you didn't misunderstand and yes it's me that's stupid and not you; that would work fine.

pixellany, thanks too, thats the simple solution (or using $() instead of backticks, which I think is the "modern" approach) which I'd forgotten when I posted ... I was kinda hoping someone would mention a simple single command I could use without the arsing around with multiple pipes just to get a negative-match, but at least I have a couple options that work.

Thanks. Clearly not having a good brain day!
 
Old 10-12-2007, 05:13 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

If I understand correctly you are looking for files ending with .po, leaving out the files ending with en_GB.po.

If that is the case, would this help:

find . -type f -name "*.po" -not -name "*en_GB\.po"

If it comes up with the names you want and, more importantly, leaves out all that you don't want you can modify it to:

find . -type f -name "*.po" -not -name "*en_GB\.po" -exec rm {} \;

All in one command.

If the list that needs to be removed is long, you should use | xargs .... it's faster:

find . -type f -name "*.po" -not -name "*en_GB\.po" | xargs rm

Hope this helps.
 
Old 10-13-2007, 05:28 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Or with zsh:

Code:
setopt kshglob
rm -- **/!(*en_GB).po
and if the files are in the same directory:

ksh

Code:
rm -- !(*en_GB).po
bash

Code:
shopt -s extglob
rm -- !(*en_GB).po
 
Old 10-13-2007, 01:26 PM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,724

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
If you want to delete language files perhaps you should look into the purgelocales utility.
 
Old 10-15-2007, 10:31 AM   #8
pbhj
Member
 
Registered: Dec 2002
Location: UK
Distribution: Slackware 12; Ubuntu 7.10
Posts: 358

Original Poster
Rep: Reputation: 31
thanks again all.

I used druuna's
Code:
find . -type f -name "*.po" -not -name "*en_GB\.po" -exec rm {} \;
and saved 21M from an 80M install of gallery2 (http://codex.gallery2.org/Gallery2ownload)

that's a sizeable chunk!
 
Old 10-15-2007, 03:05 PM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,724

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
I used the purgelocales utility on a full install once which was around 800MB IIRC -it removed nearly 150MB of stuff!
i have incorporated into my 'magic' package builder, src2pkg, so that you can delete the language files from packages as you build them.
src2pkg is here:

http://distro.ibiblio.org/pub/linux/...nload/src2pkg/
 
  


Reply

Tags
find, rm, shell, xargs


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded regex matching in Perl GATTACA Programming 5 01-17-2007 09:16 AM
regex : matching strings of a unknown lenghtr stevie_velvet Programming 5 07-16-2006 10:56 PM
regex matching things like , etc. aunquarra Programming 2 05-04-2005 07:53 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM


All times are GMT -5. The time now is 04:58 PM.

Main Menu
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