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

I have this situation

 /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:

 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

Where are you having problems in your script? Could you post it?

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?

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

How about ls icgorli*.xml , sort it, head it, and write the result to a file for later comparison?

1 Like

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:

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

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?

This work

 s -r icgorldi_*.xml | head -n 1

what I am trying to do is to save that file in a variable
something like this

fn=$(ls -ltr  icgorldi*.xml | tail -1)

then I need to
rename the file (try this does not work)

rn $f icgorldi_06191015.xml 

and not sure this will work since I haven't been able to rename

scp icgorldi_06191015.xml jostle:icgorldi_201601.xml

Assuming that you meant ls -r icgorldi_*.xm instead of s -r icgorldi_*.xm , that will give the the name of the file with the highest tag number (i.e., something like icgorldi_2421599.xml ).

And that will set fn to something like:

-rw-r--r--      1 banjobs  jobsub       125 Jun 17 10:35 icgorldi_2421597.log

either for the same or a different file (this time choosing the file with the most recent modification timestamp instead of the file with the highest tag number).

And why would invoking a news reading utility ( rn ) with ten operands be expected to rename a file. Is there some significance to the new name you want to assign to this file? It almost looks like it is named for the day that would be 1000 years ago tomorrow???

Whatever the problem, saying "this does not work" instead of showing us what it does do, what you wanted it to do, and showing us any diagnostic messages it produced when you ran it means that we have absolutely no information about what went wrong other than our wild imaginations.

Help us help you; show is what is going on!

If you would set fn using one of them methods I suggested before:

fn=$(ls -r icgorldi_*.xml | head -n 1)

if you want the file with highest tag number, or:

fn=$(ls -t icgorldi_*.xml | head -n 1

if you want the file with the most recent modification timestamp, and then used:

mv "$fn" icgorldi_06191015.xml

it MIGHT do what you want.

How you would write a program to make up the two seemingly random new local and remote filenames is also hard for us to guess. Are you passing in these names as parameters? Are they somehow created using some transformation of today's or tomorrow's date?

Again, if you would give us a CLEAR, explicit description of your environment and what you are trying to do, we would be happy to help you. If you keep us in the dark, you're either going to get silence or wild guesses that are not likely to lead you in the right direction.

Sorry for the delay but yes, I got my answers