How to list specific type of files in recursive directories in shell?

后端 未结 7 644
無奈伤痛
無奈伤痛 2021-01-31 15:26

How can we find specific type of files i.e. doc pdf files present in nested directories.

command I tried:

$ ls -R | grep .doc

but if th

7条回答
  •  粉色の甜心
    2021-01-31 15:42

    ls command output is mainly intended for reading by humans. For advanced querying for automated processing, you should use more powerful find command:

    find /path -type f \( -iname "*.doc" -o -iname "*.pdf" \) 
    

    As if you have bash 4.0++

    #!/bin/bash
    shopt -s globstar
    shopt -s nullglob
    for file in **/*.{pdf,doc}
    do
      echo "$file"
    done
    

提交回复
热议问题