Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

limiting a find to 100 results 1

Status
Not open for further replies.

bkdobs

Technical User
Sep 22, 2005
33
CA
Is it possible to limit a find to 100 results?

find $srcpath -xdev -type f exec mv {} $dstpath \;

The issue is when too many files hit the srcpath a
mv $srcpath\*.* $dstpath
fails due to parameter list too large ... the dstpath processes will fail for the same reason if I execute the above find.
 
Thank you for your reply ... not sure how to use xargs ... I looked at the man pages and don't see how I would be able to use it to move 100 files ...
assuming several hundred files are in the source directory and we don't know the names of the files how can we create a one line command that can be put in a shell script to move 100 of the oldest files to a destination directory


 
Not sitting in front of a screen to test, but try something like this:

find . -xdev -type f | xargs ls -lrt | tail -5 | awk '{ file=$9 ; printf("mv %s %s\n", file, "/newdir") }' /bin/sh
 
Oops, change the end to:

..."/newdir") }' |/bin/sh
 
And of course, the 'tail -5' for you would be 'tail -100'
 
actually for your OLDEST files the tail should be replaced with head when doing the ls -lrt.

Good night.
 
Be careful if you're not using a -name flag, as without this the find will pick up other files (possibly including scripts etc) and move them too, something that can be hard to diagnose when something that used to work doesn't any more following the move. I speak as one who has been there!
 
ogniemi, a for loop was my first thought too, but the poster said one line so i went with a pipe to awk.
 
Thanx for all the ideas ... still don't have a working solution ... been trying the "for" solution ... most of the code/syntax descriptions state the for is;
for <var> in <list>
do
<something> $<var>
done

the 'find $fsrc -xdev -type f' I assume should return a list but AIX is not executing the find ...

for i in 'find $fsrc -xdev -type f'
do
echo Test $i
done

returns

Test find $fsrc -xdev -type f

if I change the quotes to double quotes the $fsrc is expanded but I can't seem to get the for to exec the find

ultimately based on the ideas so far the following should be close to where I would want to go ... if someone could please correct the syntax to allow the find to execute?

for i in "find $fsrc -xdev -type f | head 100"
do
mv $i $fdst
done

I noticed another potential issue with this find syntax ... it returns files from subdirectories as well as the path specified ... while this is not an issue for the current exercise it could be an issue if someone adds a subdirectory to the fsrc directory

 
Enclose your find in backticks or $()

For example:

for i in "find $fsrc -xdev -type f | head 100"

would be:

for i in `find $fsrc -xdev -type f | head 100`

or

for i in $(find $fsrc -xdev -type f | head 100)

Also, if you are expecting to get the oldest 100 files a head won't work, you will have to do an `ls -lrt` first. And you have to use a - before the 100 for the head command. And you have to use xargs after the find, you cannot just run find and pipe to the head command.

So:

for i in $(find $fsrc -xdev -type f | xargs ls -lrt | head -100)
 
Sorry! I missed the back quote detail in the first post ... not sure on the comments regarding xargs and or oldest?

the following code apears to capture the oldest 10 files in the src directory I have it pointed to;

for i in `find $fsrc -xdev -type f | head -n 10`
do
echo $i
done

as this src directory is updated and cleared out every 5 minutes unless the ftp process dies I can probably live with the possibility that the file are out of sequence ... right now the process hangs if a mv $fsrc\*.* $fdst gets a parameter list to large

two things;
if I add the xargs statement I get the error xargs: 0402-057 The ls command was not found or could not be run.

The whole reason for going to "find" is that the "mv *.* blah" or "ls blah" is returning parameter list to long errors ... so wouldn't adding this xargs ls cause the same error?

 
xargs will not return the parameter list too long error. xargs constructs and argument list and executes that list. More like performing each individually instead of a whole, like $@ ("$1" "$2" "$3" ...) instead of $* ("$1 $2 $3...").

Not sure about the 0402-057 because I have never seen that, but try using the full path:
... -type f | xargs /usr/bin/ls ...
 
Very strange ... the xargs with an explicit path give the exact same error message ... I verified that ls exists in that path and has execute priviledges so not sure what is happening with this syntax ... note the error message comes after 3 displayed files and doesn't exit until an enter is received. Note also that ls -l causes $1 to return to much info so just using -rt

for i in `find $fsrc -xdev -type f | xargs /usr/bin/ls -rt | head -3`
do
echo $i
done

file1
file2
file3
xargs: 0402-057 The /usr/bin/ls command was not found or could not be run.
 
I would use:

Code:
for i in `find $fsrc -xdev -type f | xargs /usr/bin/ls -[b]l[/b]rt | [b]awk '{print $9}' |[/b] head -3`

because the pipe to awk $9 will give you ONLY the file name and not the entire line (rwxrw-rw- 2 ...)

See how that works.
 
Try just running an ls and pipe to xargs only and see if you get an error.

Example:

ls -l | xargs
 
kHz: Why an "ls -lrt" if you're then going to awk out the filenames only?

An "ls -rt" would do the same, no?

Perhaps "ls -1rt" to make sure it is just 1 columned output.




HTH,

p5wizard
 
bkdobs,

It sounds like you only were wanting to limit the find to 100 items in order to avoid the "parameter list too long" message from mv. To 'mv $fsrc/*.* $fdst', which I believe is the goal:
Code:
cd $fsrc
ls | xargs -I {} mv {} $fdst
will get you there.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
True, you could save time doing just an ls -rt, but I have always used -lrt and piped to awk. Just my preference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top