How to get the list of files in a directory in a shell script?

前端 未结 10 1573
感情败类
感情败类 2020-11-28 02:29

I\'m trying to get the contents of a directory using shell script.

My script is:

for entry in `ls $search_dir`; do
    echo $entry
done
10条回答
  •  暖寄归人
    2020-11-28 02:57

    The other answers on here are great and answer your question, but this is the top google result for "bash get list of files in directory", (which I was looking for to save a list of files) so I thought I would post an answer to that problem:

    ls $search_path > filename.txt
    

    If you want only a certain type (e.g. any .txt files):

    ls $search_path | grep *.txt > filename.txt
    

    Note that $search_path is optional; ls > filename.txt will do the current directory.

提交回复
热议问题