Rm * excluding one file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Rm * excluding one file
# 1  
Old 05-04-2017
Rm * excluding one file

Hi All,

I am trying to remove below file except the last one. With wild char It is removing all. Is there a way to exclude only one file and remove

files:
Code:
MM_6000_001
MM_6000_002
MM_6000_003
MM_6000_004

if I do
Code:
rm *

it is removing all. I want the exclude
Quote:
MM_6000_004
and remove others. any way ?


Code:
ls -lrt  MM_6000_* | grep -v 004

is getting me the desired out put in list not sure how can pass it to rm
# 2  
Old 05-04-2017
Code:
# Overwrite $1 .. parameters with $1=file1, $2=file2, ..., $n=filen
set -- MM_6000_*

while [ "$#" -gt 1 ]
do
        echo rm "$1"
        shift # Set $2=$1, $3=$2, ... $n-1=$n
done

Remove the echo once you've tested and are sure it does what you want.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-04-2017
With a recent bash, try the "extended pattern matching" after setting the option
Code:
shopt -s extglob
ls !(MM_6000_004)

These 3 Users Gave Thanks to RudiC For This Post:
# 4  
Old 05-04-2017
Match MM_6000_ followed by all but 004
with bash
Code:
shopt -s extglob
ls MM_6000_!(004)

With zsh
Code:
setopt extendedglob
ls MM_6000_^004

# 5  
Old 05-04-2017
Unless I'm missing something how about:

Code:
rm [-i -v] `ls -1 * | grep -v -e X -e Y -e X`

# 6  
Old 05-04-2017
Quote:
Originally Posted by arunkumar_mca
Hi All,

I am trying to remove below file except the last one. With wild char It is removing all. Is there a way to exclude only one file and remove

files:
Code:
MM_6000_001
MM_6000_002
MM_6000_003
MM_6000_004

if I do
Code:
rm *

it is removing all. I want the exclude and remove others. any way ?

Code:
ls -lrt  MM_6000_* | grep -v 004

is getting me the desired out put in list not sure how can pass it to rm

Code:
rm MM_6000_00[1-3]

# 7  
Old 05-05-2017
Another option:
Code:
ls (parameters if needed) >my.file
(edit my.file to remove the specific file)
xargs <my.file rm

To test, replace "rm" with "echo" and it will list the files to be removed.

Note my.file should contain only file names. If it was a full listing, the vi command of "1,$s/^.* //" should fix it. Note the space after the asterisk.

Last edited by wbport; 05-05-2017 at 11:32 AM.. Reason: Add 'only file names' line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ConCATenating binaries but excluding last bytes from each file

Hi there, shameful Linux Newbie here :p I was wondering if you could help with my problem... I have plenty of files I'd like to concatenate. I know how to basically use cat command but that won't be enough from what I need : excluding the last xx bytes from files before assembling since there's... (4 Replies)
Discussion started by: grolido
4 Replies

2. UNIX for Dummies Questions & Answers

Finding new file, but excluding directory..

hi, I need to find files that have been created less than 3 days ago. However, I need to only search specific directories. I've searched about the net and found some useful commands such as : find . -type d -name 'dir_to_exclude' -prune -o -print -mtime -3 however I cannot get it... (2 Replies)
Discussion started by: horhif
2 Replies

3. Shell Programming and Scripting

Read file excluding XML in it

Hi , I have a file like below.I want all the content in a single line excluding the XML.How can i proceed? t=21 y=23 rg=xyz ..... <xmlstarts> . . <xmlends> lk=99 lo=09 (3 Replies)
Discussion started by: chetan.c
3 Replies

4. Shell Programming and Scripting

Excluding file from tar

Hello i am using HP-UX rapdb2 B.11.23 U ia64 1068321383 unlimited-user license. I am tryiyng to exclude for tar all files that start with TOT* but i doues not work I am using: tar -cvf /ODS/prepaid/CDR_FLOW/WORK/backup.tar --exclude='TOT*' and i get the error: tar: cannot stat... (3 Replies)
Discussion started by: chriss_58
3 Replies

5. UNIX for Advanced & Expert Users

Excluding a file from tar...

The title is not as easy as it sounds.... I am trying to exclude and file while ssh and untaring the file on the fly. The command I am using is... The command typically works but recently I've add the X option along with the exclude file. Essentially, the exclude file is being ignored when run... (2 Replies)
Discussion started by: lwif
2 Replies

6. Solaris

how to find out the file's name excluding string?

Hello, Under one directory, I can use below command to find out the file names with string "Export terminated successfully without warnings" grep -i -l "Export terminated successfully without warnings" *.* My question is : how I find out the file names without including string "Export... (5 Replies)
Discussion started by: GreatJerry
5 Replies

7. Shell Programming and Scripting

Excluding certain paras from a file

Hi, I have a log file which might have certain paragraphs. Switch not possible Error code 1234 Process number 678 Log not available Error code 567 Process number 874 ..... ...... ...... Now I create an exception file like this. cat text.exp Error code 1234 Process number 874 (3 Replies)
Discussion started by: kaushys
3 Replies

8. UNIX for Advanced & Expert Users

find excluding a directory and a file

Hi I have some 5 folders and two files in the current directory. I want to delete all, expect one folder(files in the folder too should not be deleted) and a file in the current directory. Lets say the folder and file that should not be deleted as 'a'(folder name) and 'b'(file name). Can you... (1 Reply)
Discussion started by: ammu
1 Replies

9. UNIX for Dummies Questions & Answers

Get the name of the file excluding the extension

How to get the name of the file by excluding the extention of file name? For example, my filename is 'test.txt'. I want to get only the name 'test' but not the extention .txt. (2 Replies)
Discussion started by: vinay123
2 Replies

10. UNIX for Dummies Questions & Answers

Excluding a file type from an archive

:confused: I'm trying to archive a site excluding all PDF files in order to keep the size down. I have manged to do this in the past using the exclude argument in the tar command and something along the lines of *.pdf. Unfortunately, I failed at the time to note the syntax that I used and have... (1 Reply)
Discussion started by: Stuart
1 Replies
Login or Register to Ask a Question