Visit The New, Modern Unix Linux Community


Retaining latest file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retaining latest file
# 1  
Retaining latest file

Hi All,
I have a requirement where there are 2 files saved on unix directory with names anil_111 and anil_222. I just have to retain the latest file and delete the old file from the directory.

Please help me with the shell script to perform this.


Thanks,
Anil
# 2  
Try:
Code:
ls -1t | sed -n '$p' | xargs rm -i

# 3  
Code:
 
if [[ anil_111 -nt anil_222 ]]; then
  rm anil_222
else
  rm anil_111
fi

# 4  
Hi,
The file does not necesaarily have these names (anil_111 and anil_222). The first name 'anil' would come in parameter and remaining after '_' is timestamp.
I basically have to write a script which will list files starting with 'anil' (as in this case) and remove the older one and retain the latest one.

I hope i made it clear now. Please advice.

Thanks,
Anil
# 5  
111 and 222 are strange timestamps.

Is your script supposed to determine the age by the timestamps in the files' names, or by the modification timestamps on the files? If the age is determined by the timestamps in the files' names, what is the format of those timestamps?
# 6  
Hi,
The age of files is determined by the modification timestamps on the files not by timestamp in name. Please help.


Thanks,
Anil
# 7  
You could try something like:
Code:
#!/bin/ksh
IAm=${0##*/}
if [ $# -ne 1 ]
then    echo "Usage: $IAm NameBase" >&2
        exit 1
fi      
set -- "$1"_*
name="$1"
shift
if [ "$name" != "${name%_[*]}" ]
then    printf "%s: No files found with names starting with \"%s\"\n" \
                "$IAm" "${name%[*]}" >&2
        exit 2  
fi      
if [ $# -eq 0 ]
then    printf "%s: Only one file found: \"%s\"\n" "$IAm" "$name" >&2
        exit 3
fi      
while [ $# -ge 1 ]
do      if [ "$name" -nt "$1" ]
        then    printf "Removing \"%s\"\n" "$1"
                echo rm "$1"
        else    printf "Removing \"%s\"\n" "$name"
                echo rm "$name"
                name="$1"
        fi      
        shift 1
done    
printf "Keeping \"%s\"\n" "$name"

If you try this and it looks like it will do what you want, remove the text in red so it will actually remove files instead of just telling you which files it thinks it should remove.

This assumes that if there are two or more matching files, it should remove all but the latest file.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #58
Difficulty: Medium
Douglas Engelbart invented the computer mouse.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

2. Shell Programming and Scripting

sed Command on Multiple Files and retaining the same file name

Hi All I am trying to run sed command to remove first 2 charcaters from a file on Multiple Files in my directory and what to retain the same file name . I want to know how to retain the same file name but with changes . Can some one please let me know how to proceed with this . ... (7 Replies)
Discussion started by: honey26
7 Replies

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

4. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

5. Shell Programming and Scripting

To get the latest file

Hi Experts Team, I wish to store the latest file name of partcular pattern in the remote server in a variable. i tried this LATEST_FILE=`ssh ${USER_ID}@${REMOTE_HOSTNAME} 'ls -t ${SOURCE_DIRECTORY}/${SOURCE_FILEPATTERN}'` but its nt working..pls guide me.. Regards, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

6. Shell Programming and Scripting

get latest file

i have the following in my directory x: 3 files with the word "LIST" inside the files 2 files without the word "LIST" 1 folder (sudirectory) i want to get the filename of the latest file (timestamp) with the word "LIST". by the way the script and the list of files are in seperate... (4 Replies)
Discussion started by: inquirer
4 Replies

7. Shell Programming and Scripting

how to get the latest file

I am trying to scp the latest file which ends with "_abc.log". Can some help me figure out how can do that? (3 Replies)
Discussion started by: shehzad_m
3 Replies

8. Shell Programming and Scripting

How do I get the name of latest file?

1) How do I get the name of latest file in a variable? 2) Is it safe to delete all files from a dir I am doing cd $dir_name if return_code > 0 rm * fi what are other alternates to delete all files from a dir in a shell script? :) (5 Replies)
Discussion started by: Hangman2
5 Replies

9. Shell Programming and Scripting

retaining file path

hi all, Is there any way to retain file path? echo "Please enter your old filename" read a #user input : /blah/blah1 echo "please enter the new filename" read b #user input : dumb mv $a $b What i would like to do is for the user to enter just the "filename" for the new filename... (2 Replies)
Discussion started by: c00kie88
2 Replies

10. UNIX for Advanced & Expert Users

Retaining timestamp on copy of a file

While copying, how we can retain the same date for new file as it was on the old file. (1 Reply)
Discussion started by: param_it
1 Replies

Featured Tech Videos