5

I'd like to limit output of find command. In the past I used to use for this ls command, e.g:

ls *tgz|head -100|xargs -i mv "{}" ../

but I got to know that the result may be unpredictable if name of file contains new line character. So more correct way to do this is something like that:

find ... -print0 | xargs -0

But taking this approach I'm not able to limit output of find with head command - it shows all file names separated with ^@ special sign:

 find . -name '*tgz' -print0|head -2|less

file1.tgz^@file2.tgz^@file3.tgz^@file4.tgz^@file5.tgz^@

Is there a method to work this awkwardness away?

I tried to resolve it with help of awk:

find . -name 'BATCHED*' -print0|awk 'BEGIN{RS=""}'

but it still displays either all or zero lines.

May it be solved with help of awk? Is there better solution?

BTW. I found this very instructive reference but there is not answer on my question.

szamasz
  • 53

3 Answers3

3

Another opportunity for safe find:

while IFS= read -r -d '' -u 9
do
    let ++count
    if [[ count -gt 10 ]]
    then
        unset count
        break
    fi
    printf "$REPLY"
    printf "$\x00"
done 9< <( find /full/path -print0 )

To verify, simply pipe it into this:

while IFS= read -r -d ''
do
    echo "START${REPLY}END"
done
l0b0
  • 7,453
2

The problem stems from xargs being word-orientated, while commands like head and tail are line-orientated. One solution could be to not use xargs but instead GNU parallel.

Magnus
  • 4,696
0

I believe you're looking for the --max-args or --max-lines argument to xargs, depending on your input format:

# Run less on no more than two files at a time
find . -type f -print0 | xargs --null --max-args=2 less
CodeGnome
  • 2,131