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

后端 未结 2 803
悲&欢浪女
悲&欢浪女 2021-01-07 22:51

The following command:

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

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

相关标签:
2条回答
  • 2021-01-07 23:49

    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.

    0 讨论(0)
  • 2021-01-07 23:53

    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 ..
    
    0 讨论(0)
提交回复
热议问题