如何获取shell脚本中目录中的文件列表?

时间:2022-03-21 00:04:30

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

我正在尝试使用shell脚本获取目录的内容。

My script is:

我的脚本是:

for entry in `ls $search_dir`; do
    echo $entry
done

where $search_dir is a relative path. However, $search_dir contains many files with whitespaces in their names. In that case, this script does not run as expected.

其中$ search_dir是相对路径。但是,$ search_dir包含许多名称中带有空格的文件。在这种情况下,此脚本不会按预期运行。

I know I could use for entry in *, but that would only work for my current directory.

我知道我可以用于*中的输入,但这只适用于我当前的目录。

I know I can change to that directory, use for entry in * then change back, but my particular situation prevents me from doing that.

我知道我可以更改到该目录,用于输入*然后更改回来,但我的特殊情况阻止我这样做。

I have two relative paths $search_dir and $work_dir, and I have to work on both simultaneously, reading them creating/deleting files in them etc.

我有两个相对路径$ search_dir和$ work_dir,我必须同时处理它们,读取它们创建/删除它们中的文件等。

So what do I do now?

那我现在该怎么办?

PS: I use bash.

PS:我用bash。

6 个解决方案

#1


170  

for entry in "$search_dir"/*
do
  echo "$entry"
done

#2


17  

for entry in "$search_dir"/* "$work_dir"/*
do
  if [ -f "$entry" ];then
    echo "$entry"
  fi
done

#3


17  

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:

这里的其他答案很棒,并回答你的问题,但这是“bash获取目录中的文件列表”的最佳谷歌结果,(我一直在寻找保存文件列表)所以我想我会发布一个回答这个问题:

ls $search_path > filename.txt

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

如果您只想要某种类型(例如任何.txt文件):

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

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

请注意,$ search_path是可选的; ls> filename.txt将执行当前目录。

#4


9  

find "${search_dir}" "${work_dir}" -mindepth 1 -maxdepth 1 -type f -print0 | xargs -0 -I {} echo "{}"

#5


9  

This is a way to do it where the syntax is simpler for me to understand:

这是一种方法,我可以更简单地理解语法:

yourfilenames=`ls ./*.txt`
for eachfile in $yourfilenames
do
   echo $eachfile
done

./ is the current working directory but could be replaced with any path
*.txt returns anything.txt
You can check what will be listed easily by typing the ls command straight into the terminal.

./是当前工作目录,但可以用任何路径替换* .txt返回anything.txt您可以通过直接在终端中输入ls命令来检查将要列出的内容。

Basically, you create a variable yourfilenames containing everything the list command returns as a separate element, and then you loop through it. The loop creates a temporary variable eachfile that contains a single element of the variable it's looping through, in this case a filename. This isn't necessarily better than the other answers, but I find it intuitive because I'm already familiar with the ls command and the for loop syntax.

基本上,您创建一个变量yourfilenames,其中包含list命令作为单独元素返回的所有内容,然后循环遍历它。循环创建一个临时变量eachfile,它包含它循环的变量的单个元素,在本例中是一个文件名。这不一定比其他答案更好,但我觉得它很直观,因为我已经熟悉了ls命令和for循环语法。

#6


0  

On the Linux version I work with (x86_64 GNU/Linux) following works:

在我使用的Linux版本(x86_64 GNU / Linux)下面的工作:

for entry in $search_dir/*
do
  echo $entry
done

#1


170  

for entry in "$search_dir"/*
do
  echo "$entry"
done

#2


17  

for entry in "$search_dir"/* "$work_dir"/*
do
  if [ -f "$entry" ];then
    echo "$entry"
  fi
done

#3


17  

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:

这里的其他答案很棒,并回答你的问题,但这是“bash获取目录中的文件列表”的最佳谷歌结果,(我一直在寻找保存文件列表)所以我想我会发布一个回答这个问题:

ls $search_path > filename.txt

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

如果您只想要某种类型(例如任何.txt文件):

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

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

请注意,$ search_path是可选的; ls> filename.txt将执行当前目录。

#4


9  

find "${search_dir}" "${work_dir}" -mindepth 1 -maxdepth 1 -type f -print0 | xargs -0 -I {} echo "{}"

#5


9  

This is a way to do it where the syntax is simpler for me to understand:

这是一种方法,我可以更简单地理解语法:

yourfilenames=`ls ./*.txt`
for eachfile in $yourfilenames
do
   echo $eachfile
done

./ is the current working directory but could be replaced with any path
*.txt returns anything.txt
You can check what will be listed easily by typing the ls command straight into the terminal.

./是当前工作目录,但可以用任何路径替换* .txt返回anything.txt您可以通过直接在终端中输入ls命令来检查将要列出的内容。

Basically, you create a variable yourfilenames containing everything the list command returns as a separate element, and then you loop through it. The loop creates a temporary variable eachfile that contains a single element of the variable it's looping through, in this case a filename. This isn't necessarily better than the other answers, but I find it intuitive because I'm already familiar with the ls command and the for loop syntax.

基本上,您创建一个变量yourfilenames,其中包含list命令作为单独元素返回的所有内容,然后循环遍历它。循环创建一个临时变量eachfile,它包含它循环的变量的单个元素,在本例中是一个文件名。这不一定比其他答案更好,但我觉得它很直观,因为我已经熟悉了ls命令和for循环语法。

#6


0  

On the Linux version I work with (x86_64 GNU/Linux) following works:

在我使用的Linux版本(x86_64 GNU / Linux)下面的工作:

for entry in $search_dir/*
do
  echo $entry
done