inverse remove command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers inverse remove command
# 1  
Old 01-16-2009
inverse remove command

Hi All,

Is there an option for 'rm' to "remove everything but these files"?

For instance if I have:
a.txt <-- I just want to remove this one
a_1.txt
a_2.txt

Is there an option ? for this:
rm -? *_*.txt

Thanks,
ScKaSx
# 2  
Old 01-16-2009
Might be something like this.

Code:
[orcl10gdb@SVRDELLD41 flexiSchema]$ ls -ltr *.txt
-rw-r--r--  1 orcl10gdb dba 0 Jan 16 09:28 a.txt
-rw-r--r--  1 orcl10gdb dba 0 Jan 16 09:30 a_1.txt
-rw-r--r--  1 orcl10gdb dba 0 Jan 16 09:30 a_2.txt
[orcl10gdb@SVRDELLD41 flexiSchema]$ rm -v a_[12].txt
removed `a_1.txt'
removed `a_2.txt'
[orcl10gdb@SVRDELLD41 flexiSchema]$ ls -ltr *.txt
-rw-r--r--  1 orcl10gdb dba 0 Jan 16 09:28 a.txt
[orcl10gdb@SVRDELLD41 flexiSchema]$

Smilie
Raj
# 3  
Old 01-16-2009
rajavu didn't you just remove the ones I wanted to keep? I wanted to remove a.txt and KEEP a_[12].txt.

The -v option is just verbose mode, which I thought might work at first as well (thinking of grep).

Cheers,
ScKaSx
# 4  
Old 01-16-2009
Some shells allow some pattern matching sort of 'in reverse':
Code:
ls !(*.lis)

means ls every file that does not end in .lis You could apply this to your problem. However it really is VERY dangerous if you misuse it.
Code:
rm !(a.txt)

# 5  
Old 01-16-2009
I can only second what jim said: applying such complicated logic is dangerous and it is very easy to get into "very deep kimchi" ((c) Neo). I would write a script which takes the filemask as parameter, displays the collection first and asks if this is acceptable, only then deleting it.code]



Having said this: if your shell doesn't support this reversal of matches you could use a find command, which sure does support reversal:

Code:
find . -type f -name "*txt" -exec rm -f {} \;

would delete every file ending in "txt" whereas:

Code:
find . -type f ! -name "*txt" -exec rm -f {} \;

would delete the complementary set.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I generate a script with an inverse action?

I have an interactive script which sorts and processes a variety of filetypes from an unsorted folder into newly created directories. The script works great, But I was wondering how I could generate a script with an inverse action so that I could unwind / undo the executed script and its sorting... (3 Replies)
Discussion started by: Braveheart
3 Replies

2. Programming

Python re.findall inverse Match

I ask of you but yet another simplistic question that I hope can be answered. Its better explained showing my code. Here is my list(tmp_pkglist), which contains a list of all Debian (Jessie) packages: snippet 'zssh (1.5c.debian.1-3.2+b1 , 1.5c.debian.1-3.2 )', 'zsync (0.6.2-1)', 'ztex-bmp... (2 Replies)
Discussion started by: metallica1973
2 Replies

3. Shell Programming and Scripting

Grep -v (inverse matching)

I am totally unexperienced in writing scripts of any kind. I am working on Mac OS X and would like to run a shell script to find files in a directory that do not conform to a specific naming convention and print to a text file in the same directory. For example, I have a folder called... (9 Replies)
Discussion started by: j_alicea
9 Replies

4. Shell Programming and Scripting

Inverse Grep

Hi, I'm trying to wtite a script which actually print the text which doesn't contain a word , i mean to say. eg:- if a file contains the follwoing data Hello how ru ??? What ru doing ? what is the % of data contained ??? I want to write a script such that it prints the line excluding... (1 Reply)
Discussion started by: nagios
1 Replies

5. Shell Programming and Scripting

Inverse of Cut

Hi List, I have a CSV file which I am manipulating. Each time I receive the CSV for processing, there will be extra columns of data. Here's what I want to do; I want to be able to remove specific columns from the CSV data file, but keep the remaining columns, where the remaining columns are... (3 Replies)
Discussion started by: landossa
3 Replies

6. Shell Programming and Scripting

Perl - quick inverse of a number range

Hello, I'm trying to find an nice solution for the following: 1) I have ranges of numbers (begin-end): 10-15, 20-30, 45-50 2) I have begin limit=0 and end limit=60. 3) I need to find out number ranges between begin limit and end limit that do not overlap with the ranges in item1. In this... (6 Replies)
Discussion started by: pn8830
6 Replies

7. Shell Programming and Scripting

matrix inverse (awk)

I need to inverse a matrix given in a file. The problem is I'm stuck with writing determinant finding algoritm into code. I found this algoritm about finding determinant of nxn matrix. This is what i need: Matrices and Determinants and here: a11 a12 a13 a21 a22 a23 a31 a32 a33... (0 Replies)
Discussion started by: vesyyr
0 Replies

8. UNIX for Dummies Questions & Answers

need help-matrix inverse (awk)

I have few days to complete my awk homework. But I'm stucked. i hope some1 will help me out. I have to inverse n x n matrix, but I have problems with finding the determinant of the matrix. I found the algoritm, how to find a determinant of n x n matrix:... (0 Replies)
Discussion started by: vesyyr
0 Replies

9. UNIX for Dummies Questions & Answers

Inverse Video

I am trying to discover the sequence of escape characters that I need to pass ksh in order to turn inverse video on and off. I need this information for two reasons: * I want to include inverse video text in some shell scripts * I wish to highlight a section of an /etc/motd file Thanks for... (2 Replies)
Discussion started by: sam_pointer
2 Replies
Login or Register to Ask a Question