Find most recent file and copy to another directory.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find most recent file and copy to another directory.
# 1  
Old 04-03-2015
Linux Find most recent file and copy to another directory.

Very new to shell scripting. Not sure if my title is correct but I will try and explain.

Directory has 100+ files with this format, " ABCD_ABC_Abc_AB0126.abc ". When a new file gets created, the 16-19 characters in the file name gets incremented by 1. Ex...todays most recent file is "......0126.abc". Next new file name will be "......0127.abc" then next new file will be ".......0128.abc" and so on.

What I would like to do is search file names for last 16 - 19 characters, ...0126..., and if it finds something greater then copy it to another directory. I have given an example of what my script should do. Not sure if its possible but appreciate all and any help. Thanks in advance.

code:
Code:
x= 0126
y= last 16 - 19 characters in file name
if (x < y)
   copy new file to another directory
   x = y
else
   print "No new files to copy."


Last edited by jim mcnamara; 04-03-2015 at 12:42 PM..
# 2  
Old 04-03-2015
Files have filetimes - one is the last date/time the file was written to. It is called the file mtime, most often.

If you can simply look for files that match your pattern, grab the most recent one. If those files get created every hour or so it becomes even easier.

IF you give us more information about what you are trying to accomplish - NOT how to code it - we can give you very good answers.

As presented your code would skip over copying a file because there are two newer files not one. And how do you track what the last one copied over was? You don't with your code.

Please give us more to go on.
what shell?
What OS?
what do you want to accomplish? -- not how to code it.
# 3  
Old 04-03-2015
Hello Jim,

Appreciate the response and help.

Shell - bash
OS - unix

What I am trying to accomplish is to have a script that I can schedule to look for new file in directory A. If it finds any new file to copy them into directory B. A file normally gets created every other day but that is not always the case. So, I need to check directory A everyday. 3 files get created when a new file is saved. I only need the "......0127.abc" file to be copied.

Code:
.....0127.abc
.....0127.abc.sav
.....0127.abc.log

Hope this makes sense. Thanks in advance.

Last edited by Don Cragun; 04-08-2015 at 04:01 PM.. Reason: Add CODE tags.
# 4  
Old 04-04-2015
Try
Code:
X=126
for FN in $(ls ABCD*)
   do   T=${FN%.*}
        NO=${T##*[A-Z]}
        [[ 10#$NO -gt $X ]] && echo mv ${FN%${NO}*}${NO}${FN#*${NO}} ./NEWDIR/
   done
mv ABCD_ABC_Abc_AB0127.abc ./NEWDIR/
mv ABCD_ABC_Abc_AB0128.abc ./NEWDIR/

This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-06-2015
Hi RudiC,

Sorry for the inconvenience. Still trying to wrap my head around the coding. Can you go over the script line by line so that I can get a better understanding of the coding? Trying to follow the coding and a bit confused. Appreciate the help. Thanks in advance.
# 6  
Old 04-06-2015
Look into your shell's man page: parameter expansion: Remove matching pre-/suffix pattern.
Using those, the number is isolated from the filename for the comparison.

BTW, I forgot to mention that the echo is there for testing only. If happy what is printed, remove it to actually move the files.

Above script can be simplified a bit:
Code:
X=126
for FN in $(ls ABCD*)
   do   T=${FN%.*}
        NO=${T##*[A-Z]}
        [[ 10#$NO -gt $X ]] && echo mv $FN ./NEWDIR/
   done

# 7  
Old 04-06-2015
Hi RudiC,

Got error message when I ran the script. Looks like it didn't like the extension?

Code:
"line 6: [[: 10#0124.rpd: syntax error: invalid arithmetic operator (error token is ".rpd")"


Last edited by Don Cragun; 04-08-2015 at 04:00 PM.. Reason: Add CODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help script find file most recent

Hi, I need to find the most recent files by their name from an X repertoire. The problem is that the name of the files is of type: POWERL10_20151203000.xml POWERL10_20151203001.xml POWERL10_20151202000.xml FIXED VALUE_DATENNN.xml NNN = Sequential number I would need to recover the... (4 Replies)
Discussion started by: verita
4 Replies

2. Shell Programming and Scripting

How to recursively copy directory only for recent files?

I love the -newerct flag for the Cygwin find command on windows. Can I use "/usr/bin/find . -newerct '3 hours ago'" to conditionally copy a directory tree so that only the files in the directory tree that are younger than 3 hours are copied to my destination directory such that the directory... (4 Replies)
Discussion started by: siegfried
4 Replies

3. Shell Programming and Scripting

How to find the recent file in many sub-directories?

Hi guys, Under my root directory there are many sub-directories which contains log file for every day of running. How can I find , in one command only, the recent log file in each sub-directory? For example, If I run the following: find . -name "exp_prod_*_*_yes_*_.log" -exec ls -ltr {} \;... (12 Replies)
Discussion started by: nir_s
12 Replies

4. UNIX for Dummies Questions & Answers

command to find most recent file

Hi, Here is my problem: I try to write a script to find data in a file named "data" for exemple. Let's say I am in the directory /x/y/z, there are several sub-directories in the "z" directory (each sub-directory has a file "data") and I am searching for the word "help". So I use this... (9 Replies)
Discussion started by: StephB
9 Replies

5. UNIX for Dummies Questions & Answers

How to find and copy files from one directory to another

Ok i have three directories Destination - /u/dir1 (has subdirectories dir2 which also has subdirectory dir3) Source1 - /u/test/files/dir1/dir2/dir3 Source2 - /u/out/images/dir1/dir2/dir3 What i would like to do is copy everything from Source1 and Source2 into the Destination directory.... (3 Replies)
Discussion started by: ziggy25
3 Replies

6. Shell Programming and Scripting

find and copy file to another directory..

Hi Everybody, i want a samll help to write a script. i had source location with :/user/bin (bin contains subdirectories with like names emails etc and had several files in each subdirectory) and target location with :/usr/scripts (having same subdirectories names and had some files)... (1 Reply)
Discussion started by: Reddy482
1 Replies

7. Shell Programming and Scripting

find the most recent file containing a certain string

I want to find the most recent file containing ' NORESETLOGS" I'm already here but, how to sort this now in a correct way ? By the way, my version of find does not know about 'fprint' find . -type f -exec grep -i " NORESETLOGS" {} \; -exec ls -l {} \; | grep -vi " RESETLOGS" (5 Replies)
Discussion started by: plelie2
5 Replies

8. Shell Programming and Scripting

crontab; copy most recent *.mpg file from local machine to smb storage device

Hello, I've been searching your forum for an answer to the following question and whilst I've seen several which may help I'm afraid my inexperience with UNIX systems has got the better of me and I'm incapable of piecing your considerable expertise together. Problem: I have a linux box which... (5 Replies)
Discussion started by: julezsht
5 Replies

9. Shell Programming and Scripting

find files and copy into a directory

hi all, can u please help me in finding all ksh file in directory and including all subdirectories and then copy those files into another directory. thanks in advance -bali (4 Replies)
Discussion started by: balireddy_77
4 Replies

10. UNIX for Dummies Questions & Answers

reading directory for most recent file?

Dear All, I'm trying to write a script that searches thru a directory looking for a most recent file and then scp that file. I have the scp working, but I don't know how to browse the directory and select the most recent file. The file name includes a date & time stamp (e.g.... (3 Replies)
Discussion started by: duncan_glover
3 Replies
Login or Register to Ask a Question