cancel
Showing results for 
Search instead for 
Did you mean: 

script shell

SOLVED
Go to solution
Vogra
Regular Advisor

script shell

Hi All!
help me please,
I get the script that list contents of dir, but I need to limit by number of files.
In other words:
I have 10 files in /ttp,
I want to move only the last 5 new. I'm using sh, but can be csh ou ksh.
Thanx in advance.
We are spirits in the material world
6 REPLIES
Highlighted
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: script shell

ls | tail -n -5

If it ain't broke, I can fix that.
John Palmer
Honored Contributor

Re: script shell

for F in $(ls -rt | tail -5)
do
mv $F wherever
done

Regards,
John
Vogra
Regular Advisor

Re: script shell

...and if I don't know the number of files and I want to list all the files less the last 3 files.
Ex:
a b c d e f g h
list only: a b c d e
f g and h are the new...
thanx...!
We are spirits in the material world
James R. Ferguson
Acclaimed Contributor

Re: script shell

Hi:

Use 'ls -t' to present your list of files sorted by the modification time (latest first) before sorting alphabetically. Then count off the first five entries and do your processing using only them.

Regards!

...JRF...
S.K. Chan
Honored Contributor

Re: script shell

You can use "ls -t" to sort the listing by modified time (latest first) and if you want the lastest 5 just pipe it to "head -5". Something like this ..

#!/usr/bin/sh

for file in $(ls -t /ttp|head -5)
do
# do your move here
done

I'm assuming in /ttp there are only files, no directories.
Vogra
Regular Advisor

Re: script shell

Hi All!

the last script is:


#!/usr/bin/sh
NR=`ls /dir1/*.fff|wc -l` # total
LI=5 # don't touch this
MV=`expr $NR - $LI`
VS=/dir1
VS2=/dir2
export VS VS2 NR LI MV
echo $NR, $LI, $MV
if [ "$NR" -gt "$LI" ] # > LI files ?
then
for file in $(ls -rt $VS/*.fff|head -$MV)
do
mv $file $VS2
done
fi

Thanx to everyone.
Lima.
We are spirits in the material world