Sponsored Content
Top Forums Shell Programming and Scripting shell script preserving last 2 versions of files Post 302252112 by Lakris on Tuesday 28th of October 2008 09:03:10 PM
Old 10-28-2008
Hi, I thought about it, and even though I may do it the hard way, I think it gets the job done... and to be on the safe side, make working copies, this one moves the wanted files into their own directory.

I tried it and it works on my machine Smilie

Code:
#!/bin/bash
#Assuming Your current working directory is ok to work in and the files
#are located in the directory verfiles (create working copies there)

#First sort, if the naming is consistent, that should be a good start
ls -1 verfiles | sort > verfiles.srt

#split the filenames on each "firstname" so they will have their own file
prefix=""
while read filename; do
#Determine the "prefix"
curprefix=$(expr match "$filename" '\(^[a-Z]*_\)')
#Has it been read before?
if [ x$curprefix != x$prefix ] ; then
	#We have a new "firstname"
	prefix=$curprefix
fi
echo $filename >> $prefix.lst
done < verfiles.srt
#cleanup
rm verfiles.srt

#Where to keep the "last two" of each...
mkdir saved-verfiles

#Now for each .lst file, manipulate it a little for easier numerical sorting
for x in *.lst; do
#And this next bit is VERY lazy of me, but as I said, 
#I ASSUME that the naming is consistent ;)
myarr=($(tr V " " < $x | tr . " " |sort -n -k2 | tail -2))

mv verfiles/${myarr[0]}V${myarr[1]}.${myarr[2]} saved-verfiles
mv verfiles/${myarr[3]}V${myarr[4]}.${myarr[5]} saved-verfiles
done

Hope it helps...

/Lakris
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Moving .html files while preserving hyperlink integrity?

Hi, I use a Windows-based program called <a href="http://www.coast.com">Coast Webmaster</a> for moving large numbers of HTML files in one directory to another directory. As you drag and drop each file or entire directory of files to new locations in the web root directory tree, this utility... (1 Reply)
Discussion started by: Buddy123
1 Replies

2. Shell Programming and Scripting

Purge files, keep 3 versions of last 4 days

Hello, I currently generate a file every 15 minutes for 12 hours a day. I would like to clean the directory on a daily basis. I only want to keep the latest 3 versions for the last 4 days in the directory. Any suggestions? Thanks, Barbara (7 Replies)
Discussion started by: blt123
7 Replies

3. Shell Programming and Scripting

How to manage multiple versions of a set of shell and SQL script utilities

Hi all --- I have the need to manage multiple versions of a set of utility scripts -- both shell and SQL and other .dat files. I am wondering if anyone out there knows of a good way to "PATH" to SQL and text files in a way similar to how PATH facilitates finding executables in a pre-specified... (2 Replies)
Discussion started by: DennisB
2 Replies

4. Shell Programming and Scripting

Shell versions affecting find?

Hi all, I just spent 30hours writing a script (my first) that makes extensive use of the find command on my mac os x terminal and which was meant to be run as a cron job on my server. The script is an automated backup system for a bunch of source control repositories. On the mac it works... (6 Replies)
Discussion started by: cpower
6 Replies

5. Shell Programming and Scripting

Need a script to delete previous versions of files

Hi. I need a script (either bash or perl) that can delete previous versions of files. For instance, from our continuous build process I get directories such as build5_dev_1.21 build5_dev_1.22 build5_dev_1.23 build5_dev_1.24 I need a script that I can run every night (using "at"... (6 Replies)
Discussion started by: jbsimon000
6 Replies

6. Shell Programming and Scripting

Script to delete older versions of unique files

I have directory where new sub directories and files being created every few minutes. The directories are like abc_date, def_date, ghi_date. I am looking to keep the latest 2 unique directories and delete everything else. Here is what I have so far This gives me unique names excluding the... (5 Replies)
Discussion started by: zzstore
5 Replies

7. Shell Programming and Scripting

Copying a directory structure with the latest versions of files

Hello I have three directory structures for code releases. Each directory structure looks like this: bash-3.00$ ls -R | more .: Test_Release_1 Test_Release_2 Test_Release_3 ./Test_Release_1/dbcode: rp_online_import_srdp.pkb-1 srdp_ar_validation.pkb-1... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

8. Shell Programming and Scripting

2 versions, 1 script (A tale of madness.)

So, I have a machine running solaris (x86) and it has two different versions of Math::BigInt installed on it. The older version is 1.77. The newer version is 1.87 (via activeperl). When I run my code, one of the modules I use needs at least version 1.78. Somehow, it defaults to looking at the... (5 Replies)
Discussion started by: mrwatkin
5 Replies

9. Shell Programming and Scripting

Preserving variable in script run with sudo

In a nutshell, I want $USER to reflect my user ID in a script run with sudo. I'm working with OSX in case that makes a difference. If I 'sudo echo $USER', I get my user ID. But if I 'sudo myscript.sh' and myscript.sh has a line to echo $USER, I get 'root' I'm hoping there's a switch I can add... (1 Reply)
Discussion started by: jnojr
1 Replies

10. Shell Programming and Scripting

Help with korn shell script to get the latest file versions in a directory

I want to write a korn shell script to get the latest three versions for a file in the directory having lot of files with various versions (files with prefix as same but time stamp as suffix) and compress it and at the same time have to remove the remaining versions of the file (other than latest... (4 Replies)
Discussion started by: maheshbabu
4 Replies
tiffsplit(1)							   User Commands						      tiffsplit(1)

NAME
tiffsplit - split a multi-image TIFF into single-image TIFF files SYNOPSIS
tiffsplit src.tif [prefix] DESCRIPTION
tiffsplit splits a multi-directory (page) TIFF file to create one or more single-directory (page) TIFF files. The output filenames are cre- ated by concatenating a prefix, a lexically ordered suffix in the range [aa-zz], and the suffix .tif. For example, xaa.tif, xab.tif, ..., xzz.tif. If a prefix is not specified on the command line, the default prefix x is used. OPTIONS
None OPERANDS
The following operands are supported: src.tif The name of the input file. prefix The prefix used to create the output filenames. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWTiff | +-----------------------------+-----------------------------+ |Interface stability |External | +-----------------------------+-----------------------------+ SEE ALSO
tiffinfo(1), tiffcp(1), libtiff(3) NOTES
Only a select set of "known tags" is copied when splitting. Updated by Breda McColgan, Sun Microsystems Inc., 2004. SunOS 5.10 26 Mar 2004 tiffsplit(1)
All times are GMT -4. The time now is 01:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy