Operating System - HP-UX
1847225 Members
2643 Online
110263 Solutions
New Discussion

Re: HELP to made a Script

 
SOLVED
Go to solution
Isaac_4
Frequent Advisor

HELP to made a Script

Hi:

I need move one file from a directory but i need move the most recent from the original directory. ( in original directory have many files equals )
Some body can help me !

Thank you
The time is gold
9 REPLIES 9
Pete Randall
Outstanding Contributor
Solution

Re: HELP to made a Script

I assume the names are similar. You could try something like:

rm `ls -t filename.* |head -r |tail -1`


Pete

Pete
Isaac_4
Frequent Advisor

Re: HELP to made a Script

No I dont need remove i need made a copy
The time is gold
James R. Ferguson
Acclaimed Contributor

Re: HELP to made a Script

Hi Isaac:

Assuming that you do not want to examine subordinate directories, but only want the most recently modified file, do:

# cd yourdir
# FILE=`ls -lt|awk '/^-/ {print $NF}'|head -1`
# mv ${FILE} destdir

The 'ls -lt' lists files in modification timestamp order; the most recent first. We look only for regular files with 'awk' printing the file names, and take only the first one in the ouput stream with 'head'.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: HELP to made a Script

I think a little more detail of your problem would be helpful.

Rod Hills
There be dragons...
Peter Nikitka
Honored Contributor

Re: HELP to made a Script

Hi,

a little modification of James solution,
to avoid an error message when no file is found:

cd yourdir
FILE=`ls -t | sed 1q`
[ "$FILE" ] && mv ${FILE} destdir


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: HELP to made a Script

Sorry for the follow up post,

but just saw the checking for a plain file, so:

cd yourdir
FILE=`ls -t | sed 1q`
[ -n "$FILE" -a -f "$FILE" ] && mv ${FILE} destdir

mfG
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Josiah Henline
Valued Contributor

Re: HELP to made a Script

OLDDIR="" #Set the current dir of the file
NEWDIR="" #Set the new dir of the file

cd $OLDDIR
set -A ARRAY1 $(ll -lt |grep "^-" |tail -1 | cut -d " " -f2-)

mv ${ARRAY1[$(print ${#ARRAY1[*]} - 1 |bc)]} $NEWDIR
If at first you don't succeed, read the man page.
Josiah Henline
Valued Contributor

Re: HELP to made a Script

OLDDIR="" #Set the current dir of the file
NEWDIR="" #Set the new dir of the file

cd $OLDDIR
set -A ARRAY1 $(ll -lrt |grep "^-" |tail -1 | cut -d " " -f2-)

mv ${ARRAY1[$(print ${#ARRAY1[*]} - 1 |bc)]} $NEWDIR

Sorry... forgot the "-r". It is needed for me to use the "tail" command.
If at first you don't succeed, read the man page.
Isaac_4
Frequent Advisor

Re: HELP to made a Script

Thank you for you help
thanks Josiah, Peter, Pete , James R. all this good boys help me to open my eyes !!!

The time is gold