Mariano Vedovato
Mariano Vedovato

Reputation: 413

How do I limit the results of the command find in bash?

The following command:

find . -name "file2015-0*" -exec mv {} .. \;

Affects about 1500 results. One by one they move a previous level.

If I would that the results not exceeds for example in 400? How could I?

Upvotes: 31

Views: 29356

Answers (2)

Tiago Lopo
Tiago Lopo

Reputation: 7959

You can do this:

 find . -name "file2015-0*" | head -400 | xargs -I filename mv  filename ..

If you want to simulate what it does use echo:

 find . -name "file2015-0*" | head -400 | xargs -I filename echo mv  filename ..

Upvotes: 44

fedorqui
fedorqui

Reputation: 289625

You can for example provide the find output into a while read loop and keep track with a counter:

counter=1
while IFS= read -r file
do
   [ "$counter" -ge 400 ] && exit
   mv "$file" ..
   ((counter++))
done < <(find . -name "file2015-0*")

Note this can lead to problems if the file name contains new lines... which is quite unlikely. Also, note the mv command is now moving to the upper level. If you want it to be related to the path of the dir, some bash conversion can make it.

Upvotes: 2

Related Questions