Make script delete certain files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Make script delete certain files
# 1  
Old 10-15-2008
Make script delete certain files

Hi,

In a directory, say

~/dir

is a lot of files and subdirectories. Some of the files are named

res1.om
res2.om
...
res65.om

The amount of files varies. I need a .csh script that deletes all res*.om files except the one with the highest number.

Any suggestions?

Thanks in advance Smilie
# 2  
Old 10-15-2008

csh is not suited for scripting:
Top Ten Reasons not to use the C shell
C shell problems
Csh Programming Considered Harmful
Assuming that the filenames are all like your example and do not contain spaces or other pathological characters:

Code:
rm $(
    printf "%s\n" res*.om |
    sort -t~ -k2n |
    awk 'NR > 1 { printf "%s ", last } { last = $0 }'
 )

# 3  
Old 10-16-2008
Quote:
Assuming that the filenames are all like your example and do not contain spaces or other pathological characters:

Code:

rm $(
printf "%s\n" res*.om |
sort -t~ -k2n |
awk 'NR > 1 { printf "%s ", last } { last = $0 }'
)
doesn't work. Error says:

Illigal variable name.

The file names are as I wrote originally.
# 4  
Old 10-16-2008

There are no variables used in that script.

Note that it is not a csh script; run it in a POSIX shell. On mnost systems, /bin/sh is a POSIX shell; on Solaris, use ksh or bash or (I think) /usr/pkg4/bin/sh.
# 5  
Old 10-16-2008
Ok thank you!
# 6  
Old 10-16-2008
Try this one

ls -1 res*.om|sort -rn -k1.4|sed -n '2,$p'|xargs rm -f
# 7  
Old 10-16-2008
wow that one really works! thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to make a bash script that goes through directory files and changes things

I'm trying to write a script in a directory that goes through the column the user specifies of 4 files that are inside the directory and calculates the min and the max values. This means that if the user specifies column 5, the script will go through column 5 of all 4 files and all that should give... (2 Replies)
Discussion started by: Eric1
2 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

4. UNIX for Dummies Questions & Answers

script to delete old files

Hi, I want to delete files that are older than 60 days.... i need to execute the script in 7 differnt folders.... i can run the script in crontab to regularly check.... I am struck @ finding out how the file is 60 days old or not... Can u please help me on this? Thanks, NithZ (6 Replies)
Discussion started by: Nithz
6 Replies

5. Emergency UNIX and Linux Support

Help to make awk script more efficient for large files

Hello, Error awk: Internal software error in the tostring function on TS1101?05044400?.0085498227?0?.0011041461?.0034752266?.00397045?0?0?0?0?0?0?11/02/10?09/23/10???10?no??0??no?sct_det3_10_20110516_143936.txt What it is It is a unix shell script that contains an awk program as well as... (4 Replies)
Discussion started by: script_op2a
4 Replies

6. Shell Programming and Scripting

I need to make a script to delete files not in use in /tmp

I need to make a script to delete files not in use in /tmp Thanks! (4 Replies)
Discussion started by: raulochi
4 Replies

7. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

8. Shell Programming and Scripting

How to make an editing script work for multiple files?

Hey everybody, I have a script for making a string substitution in a file. I am trying to modify it in order to make the same modifcation to multiples files. here is what I have so far. #!/bin/csh set p1="$1" shift set p2="$1" shift foreach x ($*) if ( { grep -w -c "$p1" $x } ) then mv... (7 Replies)
Discussion started by: iwatk003
7 Replies

9. UNIX for Dummies Questions & Answers

help to make list of files and run script..

Hey, I have finally made a command that works and now has to run it on 200+ files to run it on. How do I do that? Just fyi and if it complicates anything my commandline is: awk '{if ($1 ~ /1/) print $2}' file (yup, is should print $2 if $1 is a certain value) It doesn't work when I: ... (2 Replies)
Discussion started by: lost
2 Replies
Login or Register to Ask a Question