Re: script shell (542 Views)
Reply
Regular Advisor
Posts: 192
Registered: ‎03-28-2001
Message 1 of 7 (542 Views)
Accepted Solution

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
Acclaimed Contributor
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 7 (542 Views)

Re: script shell

ls | tail -n -5

If it ain't broke, I can fix that.
Honored Contributor
Posts: 1,842
Registered: ‎07-28-1997
Message 3 of 7 (542 Views)

Re: script shell

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

Regards,
John
Regular Advisor
Posts: 192
Registered: ‎03-28-2001
Message 4 of 7 (542 Views)

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
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 7 (542 Views)

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...
Honored Contributor
Posts: 4,063
Registered: ‎08-29-2000
Message 6 of 7 (542 Views)

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.
Regular Advisor
Posts: 192
Registered: ‎03-28-2001
Message 7 of 7 (542 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.