How I can find the last file created and move it to a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How I can find the last file created and move it to a directory?
# 1  
Old 06-17-2015
How I can find the last file created and move it to a directory?

I have this situation
Code:
 /u03/app/banjobs> ls -ltr icg*


        82 Jun 12 10:37 iicgorldi_2419186.log
  56810484 Jun 17 10:35 icgorldi_2421592.xml
      2859 Jun 17 10:35 icgorldi_2421592.lis
-      125 Jun 17 10:35 icgorldi_2421592.log
      82 Jun 12 10:37 iicgorldi_2419187.log
   56810484 Jun 17 10:35 icgorldi_2421597.xml
     2859 Jun 17 10:35 icgorldi_2421597.lis
  1 banjobs  jobsub       125 Jun 17 10:35 icgorldi_2421597.log
2421597.xml

I need to grab the last XML file created
rename and move to a directory the number (2421597) change every time the process run, I need to write a shl script to do that...The process that generates the file is schedule to run 3 times a day, so three different files are created
with a different number like:
Code:
 icgorldi_2421597.xml
icgorldi_2421598.xml
icgorldi_2421599.xml

The number is a sequence but it its share by other processes, so this file is not necessary in a sequence 97..9..99 the number is random..
Thank you

Thank you
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and sample code segments.

Last edited by Don Cragun; 06-17-2015 at 09:38 PM.. Reason: Add CODE tags.
# 2  
Old 06-17-2015
Where are you having problems in your script? Could you post it?
# 3  
Old 06-17-2015
Quote:
Originally Posted by Bernardo Jarami
I have this situation
Code:
 /u03/app/banjobs> ls -ltr icg*


        82 Jun 12 10:37 iicgorldi_2419186.log
  56810484 Jun 17 10:35 icgorldi_2421592.xml
      2859 Jun 17 10:35 icgorldi_2421592.lis
-      125 Jun 17 10:35 icgorldi_2421592.log
      82 Jun 12 10:37 iicgorldi_2419187.log
   56810484 Jun 17 10:35 icgorldi_2421597.xml
     2859 Jun 17 10:35 icgorldi_2421597.lis
  1 banjobs  jobsub       125 Jun 17 10:35 icgorldi_2421597.log
2421597.xml

I need to grab the last XML file created
rename and move to a directory the number (2421597) change every time the process run, I need to write a shl script to do that...The process that generates the file is schedule to run 3 times a day, so three different files are created
with a different number like:
Code:
 icgorldi_2421597.xml
icgorldi_2421598.xml
icgorldi_2421599.xml

The number is a sequence but it its share by other processes, so this file is not necessary in a sequence 97..9..99 the number is random..
Thank you

Thank you
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and sample code segments.
There are some very strange things going on here:
  1. The output from ls -ltr icg* should never give you files with names that do not start with the string icg.
  2. There are fields missing that should appear in output produced with the -l option.
  3. The fields produced by a given invocation of ls should have all fields aligned.
  4. The output from ls -ltr icg* should give you lines in reverse sorted order of the last modification times of the listed files; not dates that jump back and forth between June 12 and June 17.
  5. If you are looking for xml files why, why are you using ls -ltr icg* instead of ls -ltr *.xml? And, if you're just looking for filenames, why are you using the -l option?
  6. There are no ls options on most UNIX and Linux system filesystems to print file creation times. The ls -t option gives you a list of files sorted by the last file modification timestamp (not the file creation timestamp; most filesystems don't have a file creation timestamp).
  7. Where did the last line that you show as your output come from? It appears to just be a filename in a completely different file naming format than the other files in the directory and has none of the fields one would expect in long listing format?
# 4  
Old 06-18-2015
I do understand those commands, my problem is that you can have more than one file with the extension xml something like this
icgorldi_2421592.xml
icgorldi_2421593.xml
importi_2421592.xml
inform_2421592.xml

so I need a command where I can use the combination of icgorldi and xml to extract the last file created in the directory, remember the number between icgorldi_XXXX.xml changes every time the process run and create the file
I can't use ls -ltr xml* because there others files in the directory with the xml extension...
Hope this is clear
# 5  
Old 06-18-2015
How about ls icgorli*.xml, sort it, head it, and write the result to a file for later comparison?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-18-2015
I'm still not clear as to what you are trying to accomplish.

But, assuming all of the tags are 7 digits long (as in icgorldi_2421593.xml), I don't see why:
Code:
ls -r icgorldi_*.xml | head -n 1

won't work to give you the XML file you want with the highest tag number and why
Code:
ls -t icgorldi_*.xml | head -n 1

won't work to give you the most recently modified XML file you want.

Won't one of these do what you want?
# 7  
Old 06-18-2015
This work
Code:
 s -r icgorldi_*.xml | head -n 1

what I am trying to do is to save that file in a variable
something like this
Code:
fn=$(ls -ltr  icgorldi*.xml | tail -1)

then I need to
rename the file (try this does not work)
Code:
rn $f icgorldi_06191015.xml

and not sure this will work since I haven't been able to rename
Code:
scp icgorldi_06191015.xml jostle:icgorldi_201601.xml


Last edited by Bernardo Jarami; 06-18-2015 at 10:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find if create time of last created file in a directory is older than 5 minutes

A process xyz is running and creating file1, file2, file3, .... filen. how do i know if the process has stopped and createtime of the last file (filen) is older than 5 minutes? OS is AIX (3 Replies)
Discussion started by: malaika
3 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. UNIX for Dummies Questions & Answers

Find a list of files in directory, move to new, allow duplicates

Greetings. I know enough Unix to be dangerous (!) and know that there is a clever way to do the following and it will save me about a day of agony (this time) and I will use it forever after! (many days of agony saved in the future)! Basically I need to find any image files (JPGs, PSDs etc)... (5 Replies)
Discussion started by: Clyde Lovett
5 Replies

4. Shell Programming and Scripting

Please help list/find files greater 1G move to different directory

I have have 6 empty directory below. I would like write bash scipt if any files less "1000000000" bytes then move to "/export/home/mytmp/final" folder first and any files greater than "1000000000" bytes then move to final1, final2, final3, final4, final4, final5 and that depend see how many files,... (6 Replies)
Discussion started by: dotran
6 Replies

5. Shell Programming and Scripting

How to find files created today in a particular directory?

Dear All, I want a Hp Ux command to find out the files created today in a particular directory or mountpoint. Kindly help. Thanks Bhaskar (10 Replies)
Discussion started by: sudiptabhaskar
10 Replies

6. Shell Programming and Scripting

Commando to find and move mp3 file and directory

Hello, I have a lot of mp3 in a various directory. My goal is a command that find all mp3 file and move the files and directory where is the mp3 file to a destination folder. Is it possible? Thanks in advance (7 Replies)
Discussion started by: giangi007
7 Replies

7. AIX

How to find the date on which a particular directory is created?

how to find the date on which a particular directory is created? Thanks. (4 Replies)
Discussion started by: samsungsamsung
4 Replies

8. Shell Programming and Scripting

How to find the newly created directory

Hi, I need to create new directory by increasing the number by 1 of extracted lastly created directory. e.g. Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ become Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ Log\out_log_4\ Can anyone help how to do it in c-shell... (3 Replies)
Discussion started by: Andre_2008
3 Replies

9. Shell Programming and Scripting

Find the directory modified/created before 4 days

Hi, I have an application which creates some directories while running. I want to delete these directories which are 4 days older. i tried find . type d -mtime +1 -print And it is working fine.. but find . type d -mtime +4 -print is not giving any results which are 4 days... (6 Replies)
Discussion started by: Tuxidow
6 Replies

10. UNIX for Dummies Questions & Answers

Find all files created by a specified user in a directory and its subdirectories

Is there a command or shell script which can be used for Finding all files created by a specified userid in a directory and its subdirectories. Say, I want to find all such files in directory /abc as well as in all the subdirectories such as /abc/xyz or /abc/xyz/pqr aqnd so on which was created... (5 Replies)
Discussion started by: abhilashnair
5 Replies
Login or Register to Ask a Question