How to list only files and not directories of a directory Bash?

前端 未结 9 961
旧巷少年郎
旧巷少年郎 2020-12-07 21:54

How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?

相关标签:
9条回答
  • 2020-12-07 22:09

    Using find:

    find . -maxdepth 1 -type f
    

    Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.

    0 讨论(0)
  • 2020-12-07 22:10
    ls -p | grep -v /
    

    ls -p lets you show / after the folder name, which acts as a tag for you to remove.

    0 讨论(0)
  • 2020-12-07 22:11

    Just adding on to carlpett's answer. For a much useful view of the files, you could pipe the output to ls.

    find . -maxdepth 1 -type f|ls -lt|less
    

    Shows the most recently modified files in a list format, quite useful when you have downloaded a lot of files, and want to see a non-cluttered version of the recent ones.

    0 讨论(0)
  • 2020-12-07 22:14
    { find . -maxdepth 1 -type f | xargs ls -1t | less; }
    

    added xargs to make it works, and used -1 instead of -l to show only filenames without additional ls info

    0 讨论(0)
  • 2020-12-07 22:16

    You can also use ls with grep or egrep and put it in your profile as an alias:

    ls -l | egrep -v '^d'
    ls -l | grep -v '^d'
    
    0 讨论(0)
  • 2020-12-07 22:20

    "find '-maxdepth' " does not work with my old version of bash, therefore I use:

    for f in $(ls) ; do if [ -f $f ] ; then echo $f ; fi ; done

    0 讨论(0)
提交回复
热议问题