Delete files if they exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete files if they exist
# 1  
Old 10-29-2008
Delete files if they exist

In a directory a number of files named res0.om res1.om ... resN.om

where N can be any unknown number between 1 and 999

Please help me filling out the gaps in the following csh script:

I need to delete all files exept res0.om The easy way is

rm res1*
rm res2*
rm res3*
rm res4*
rm res5*
rm res6*
rm res7*
rm res8*
rm res9*

but that gives a lot of ugly error messages in case N < 9.

Can i use something like:

if (res9* exist) rm res9*

?
# 2  
Old 10-29-2008
Or alternatively:

rm (res* exept res0.om)

???
# 3  
Old 10-29-2008
Hammer & Screwdriver Try this approach

Code:
> ls res[0-9]*                 
res0  res1  res19  res2  res4  res666  res9

list files excluding res0 file
Code:
> ls res[0-9]* | grep -v "res0"
res1
res19
res2
res4
res666
res9

The following will use that filtered list, and do something. Here, I do an ls -l, but you could do an rm.

Code:
> for file in `ls res[0-9]* | grep -v "res0"`; do ls -l $file; done
-rw-rw---- 1 xxx dp 0 Oct 29 07:18 res1
-rw-rw---- 1 xxx dp 0 Oct 29 07:18 res19
-rw-rw---- 1 xxx dp 0 Oct 29 07:18 res2
-rw-rw---- 1 xxx dp 0 Oct 29 07:18 res4
-rw-rw---- 1 xxx dp 0 Oct 29 07:18 res666
-rw-rw---- 1 xxx dp 0 Oct 29 07:18 res9
>

# 4  
Old 10-29-2008
man find
Code:
find . -type f \( -name "res*.om" -a ! -name "res0.om" \) -exec ls {} \;

Replace ls by rm.

Last edited by danmero; 10-29-2008 at 01:02 PM..
# 5  
Old 10-29-2008
try "rm -f res*[1-9]*"

the -f will not give an error if the file does not exist.


Quote:
Originally Posted by pederlol
In a directory a number of files named res0.om res1.om ... resN.om

where N can be any unknown number between 1 and 999

Please help me filling out the gaps in the following csh script:

I need to delete all files exept res0.om The easy way is

rm res1*
rm res2*
rm res3*
rm res4*
rm res5*
rm res6*
rm res7*
rm res8*
rm res9*

but that gives a lot of ugly error messages in case N < 9.

Can i use something like:

if (res9* exist) rm res9*

?
# 6  
Old 10-29-2008
Thanks to all of you. It works now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command when there exist many files

I want to run find and wondering if it struggles when there are many files. I have tried and does not seem to complain. Is this correct? (8 Replies)
Discussion started by: kristinu
8 Replies

2. UNIX for Beginners Questions & Answers

Check if 10 files exist

Hi All, Whenever i get 10 files(file names like sales*) then another file need to create. May i know how to implement this in KSH. (4 Replies)
Discussion started by: siddireddy
4 Replies

3. UNIX for Dummies Questions & Answers

Does rsync check and ignore files that already exist?

Hi, We have two (2) servers named primary and standby. There is a directory named /db01/archive that we need to keep in-sync. Files get transferred from primary and standby. Sometimes when we do a failover or when there is a network issue, some files fail to get transferred. I want to use... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

File exist for multiple files

Hi, I am unable to achieve the file exist conditions if there are multiple files same similar name for e.g. in a $Direct i am having files like aus.txt aus.txt_pr aus.txt_2012 i need to put a file exist condition like which is not working then echo "File present" but the... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

5. Shell Programming and Scripting

Script to mail if files do not exist

Hi all! I need some help with a script, but I want to make it better, my script prints the last two files on each directory: echo " " echo "******************* mediation_sgsn:***********************" ls -lrt /moneta_collected03/mediation_sgsn | tail -2 echo " " echo... (6 Replies)
Discussion started by: fretagi
6 Replies

6. Shell Programming and Scripting

Find out whether files exist.

I have the following data stored in a file. 1 /home/file13 /home/file2 2 /home/file41 /home/file654 3 /home/file61 /home/file45 4 /home/file81 /home/file43 ... I want to print the first column provided the files represented by the second and third column exist. How to do that? (3 Replies)
Discussion started by: kevintse
3 Replies

7. Shell Programming and Scripting

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 Replies

8. UNIX for Dummies Questions & Answers

testing if files exist

I am trying to test arguments to see if they are files in any directory. I have : but it's not working (7 Replies)
Discussion started by: skooly5
7 Replies

9. Shell Programming and Scripting

perl: When dealing with files that do not exist

I have a process run weekly where I must convert data formats for about thirty files. I read a text file that provides all of the filenames and switch settings. My perl code is: for ($j = 1; $j <= $k; $j++) { open(FIN2,$fin2) || die "open: $!"; do other stuff } Every once in... (2 Replies)
Discussion started by: joeyg
2 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question