LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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-12-2009, 06:55 PM   #1
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Rep: Reputation: 0
BASH: Delete all but certain files


I have a directory with ~200 files in it. I want to specify certain files to keep in the local directory and trash the rest. Any idea how I would do this?

i.e. a.txt b.txt c.txt d.dat e.dat

So if I wanted to save a.txt and b.txt but delete all other files.

Code:
ls | grep -v a.txt  | xargs rm
This would work for 1 file but not for multiple files.
 
Old 01-12-2009, 07:39 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
ls|grep -v .txt|xargs rm
 
Old 01-12-2009, 10:35 PM   #3
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
ls|grep -v .txt|xargs rm
The problem is that I want to save multiple files with different formats. So a blanket .txt won't work because I want to delete some of the .txt and save some.
 
1 members found this post helpful.
Old 01-12-2009, 10:51 PM   #4
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Do you have a list of filenames you want to keep?

Is this a one-off?
 
Old 01-12-2009, 11:24 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Try grep -v '\(file1\)\|\(file2\)\| ...'

But it might be easier to do it with gawk.
 
Old 01-13-2009, 05:11 AM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi, tboss888.

One way to do this in BASH, is to use extended patten matching. To enable it, run
Code:
shopt -s extglob
Then, if you have a list with shell patterns of files you want to save (a.txt,b.txt in your example), you may use following:
Code:
echo !(`cat files.lst | tr '\n' '|'`) | xargs rm
According to bash(1) operator !(pattern1|pattern2|...) matches anything except one of the given patterns.
 
Old 01-13-2009, 05:27 AM   #7
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
To delete all except file1,file2 and file3:
Code:
shopt -s extglob
rm !(file1|file2|file3)
Edit:Sorry - just noticed I basically repeated what firstfire said..

Last edited by bgeddy; 01-13-2009 at 05:29 AM.
 
Old 01-13-2009, 02:20 PM   #8
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bgeddy View Post
To delete all except file1,file2 and file3:
Code:
shopt -s extglob
rm !(file1|file2|file3)
Edit:Sorry - just noticed I basically repeated what firstfire said..
Thanks! This worked like a charm.
 
  


Reply



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 write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 12:17 PM
bash: delete files older than 7 days in a folder and subfoldes lupomanaro Programming 1 05-07-2008 10:22 AM
bash: better way to delete files not matching a regex? pbhj Programming 8 10-15-2007 03:05 PM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

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

All times are GMT -5. The time now is 10:41 AM.

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