Remove file versions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove file versions
# 1  
Old 05-24-2012
Question Remove file versions

Hi All,

I have a difficult problem (at least for me) and need your help.
Situation is like this, there are a lot of files in a lot of folders and some of them files are just versions of the same file saved by careful users . Once every few months I have to clean up and keep the last version of each file.
Content of one folder could look like this:
filename01.ext
filename02.ext
filename03.ext
anotherfilename02.ext
anotherfilename03.ext
anotherfilename04.ext
justaname.exte
andyetanothername01.ex
andyetanothername02.ex
andyetanothername03.ex
andyetanothername04.ex
andyetanothername05.ex

After running a script remaining files in this folder should be:
filename03.ext
anotherfilename04.ext
justaname.exte
andyetanothername05.ex

File extension "ext" could be 2, 3 or 4 letters, the number which gives the version is < 100.
Now, a script that solves just the files in the current folder would suffice, I think I'll manage to run it recursive in all folders.
I've searched other similar posts, found that e.g. ls -lt | awk '{ print $9 }' | cut -d "." -f1 | sort | uniq will print all the filenames without extension, if I could remove the last two numeric characters I would have a list of names to search for versions for each name, then sort this versions list, remove the last one from the list (sort -r | tail -n +2) and remove the remaining ones.
Another approach would be to sort the list of versions and delete by inode all but the last index.


Any help would be appreciated.
# 2  
Old 05-24-2012
is this command work?
Code:
ls -lt | awk '{ print $9 }' | cut -d "." -f1 | sed 's/[0-9]*$//g' | sort -u | while read line
do
  ls "${line}"* | sort -r | tail -n +2
  #~ ls "${line}"* | sort -r | tail -n +2 | xargs rm
done



if that command work, it will list name of files that will be deleted. and if you uncomment second line after do, it will remove the listed files.
I only know that way, maybe someone have more simple one that works.

Last edited by Franklin52; 05-24-2012 at 06:02 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to 14th For This Post:
# 3  
Old 05-24-2012
Well... it almost works. Smilie
It deletes all the files in that folder....

Still, good approach, I will study it further.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Retaining file versions based on input parameter

Hello Forum. I have the following files in one directory: abc_july01_2013.txt abc_july02_2013.txt abc_july03_2013.txt abc_july04_2013.txt abc_july05_2013.txt abc_july06_2013.txt abc_july07_2013.txt abc_july08_2013.txt If I want to be able to keep the last 5 versions of the file and... (4 Replies)
Discussion started by: pchang
4 Replies

3. Shell Programming and Scripting

Merge different versions of file in UNIX

Hello Experts, I want to merge my local file (File1) with the changes in remote server (File1). For Example: File1 contents are... (file @ remote server) ABC <blankLine> GHI <EOF> I, as USER#1, have updated the contents of File1 in my local as... ABC DEFGhi <blankLine> JKL... (2 Replies)
Discussion started by: AjMyTechGroup
2 Replies

4. Shell Programming and Scripting

Needed script to generate versions for a file and maintian them in a folder

need a shell script for the following requirement how to generate 5 versions of a file in a folder after generating them in a folder i want to maintian the past 2 versions of the file in the folder and i have to delete the the remaining. for example if i ran the script today and tommorow... (3 Replies)
Discussion started by: hemanthsaikumar
3 Replies

5. Shell Programming and Scripting

Script to maintain file versions

I am developing a script to maintain 'n' number of versions of a file. The script will take a filename as a parameter and the number of versions to maintain. This basically does something like a FIFO. Here is what I developed. But something is not right. I have attached the script. Can u pls help... (2 Replies)
Discussion started by: vskr72
2 Replies

6. Shell Programming and Scripting

ftp a file after checking the versions not greater then 8 in archive directory

Hi , I want to write a FTP Script which checks the No of Vesions of the files in Archive Dir and if count >= 8 Delete the oldest file from the Archive Dir and if the count is <= 8 Move the file to the Archive Dir with a CurrentDate concatenation and FTP the file to the FTP directory and send... (1 Reply)
Discussion started by: kailash.jadhav
1 Replies

7. UNIX for Advanced & Expert Users

RCS - Find difference between 2 different versions of a file

Hi, I have a c file in my repository. We are using RCS(Revision Control System) to control and manage the versions. I need to find 1. Difference between the current version with a different version 2. Difference between any two different versions of a file. Ex Difference between 1.14 and... (1 Reply)
Discussion started by: kelangovan
1 Replies

8. UNIX for Dummies Questions & Answers

File Versions (GDG)

Hi There, This is more a question for the 'mainframers' of old. I need a control structure (AIX) very similar to "GDG" files (Generation data group) as found on OS/390 to control versions of files. Specific files have specific needs, some need to be kept for 5 versions, some for 30 and... (0 Replies)
Discussion started by: Bizcut
0 Replies

9. Shell Programming and Scripting

different versions of the same file.

How can I find out in UNIX the number of superusers logged in at a system at a given time??? (2 Replies)
Discussion started by: deeptia
2 Replies

10. Shell Programming and Scripting

different versions?

can someone tell me a shell-script to convert an older version of a file with the current one? (1 Reply)
Discussion started by: deeptia
1 Replies
Login or Register to Ask a Question