Bash 如何遍历文件夹中的每个文件?

Bash 如何遍历文件夹中的每个文件?,bash,shell,Bash,Shell,我有一个文件夹叫考试。此文件夹有三个文件夹,分别为数学、物理和英语。所有这些文件夹中都有一些子文件夹和文件。我想遍历每个文件夹,并将每个文件夹和文件的路径打印到另一个名为“文件”的文件上。我已经做到了: #!/bin/bash LOC="/home/school/exam/*" { for f in $LOC do echo $f done } > "files" 我得到的出口是: /家庭/学校/考试/数学 /家庭/学校/考试/物理 /家庭/学校/考试/英语 我

我有一个文件夹叫考试。此文件夹有三个文件夹,分别为数学、物理和英语。所有这些文件夹中都有一些子文件夹和文件。我想遍历每个文件夹,并将每个文件夹和文件的路径打印到另一个名为“文件”的文件上。我已经做到了:

#!/bin/bash

LOC="/home/school/exam/*"

{
  for f in $LOC 
  do
   echo $f
  done
 } > "files"
我得到的出口是:

/家庭/学校/考试/数学

/家庭/学校/考试/物理

/家庭/学校/考试/英语

我不知道如何进行代码访问并对考试的子文件夹执行相同的操作。有什么建议吗


PS我只是一个shell脚本的初学者。

你可以使用
find
命令,它可以获取所有文件,然后你可以对它们做一些事情,例如使用exec或xargs。

你可以使用
find
命令,它可以获取所有文件,然后你可以对它们做一些事情,例如,使用exec或xargs。

这样可以递归列出所有文件

find /home/school/exam -print > files
for i in *; do ls -l $i ; done

这样可以递归地列出所有文件

for i in *; do ls -l $i ; done

使用
globstar
选项,当使用两个相邻的星形时,bash将递归子目录中的所有文件名

使用:

这里的参考是
manbash

globstar
                      If set, the pattern ** used in a pathname expansion context
                      will match all files and zero or more directories and
                      subdirectories.  If the pattern is followed by a /, only
                      directories and subdirectories match.
          *      Matches any string, including the null string.  When  the
                 globstar  shell  option  is  enabled,  and * is used in a
                 pathname expansion context, two adjacent  *s  used  as  a
                 single  pattern  will  match  all  files and zero or more
                 directories and subdirectories.  If followed by a /,  two
                 adjacent  *s  will match only directories and subdirecto‐
                 ries.
info bash

globstar
                      If set, the pattern ** used in a pathname expansion context
                      will match all files and zero or more directories and
                      subdirectories.  If the pattern is followed by a /, only
                      directories and subdirectories match.
          *      Matches any string, including the null string.  When  the
                 globstar  shell  option  is  enabled,  and * is used in a
                 pathname expansion context, two adjacent  *s  used  as  a
                 single  pattern  will  match  all  files and zero or more
                 directories and subdirectories.  If followed by a /,  two
                 adjacent  *s  will match only directories and subdirecto‐
                 ries.

使用
globstar
选项,当使用两个相邻的星形时,bash将递归子目录中的所有文件名

使用:

这里的参考是
manbash

globstar
                      If set, the pattern ** used in a pathname expansion context
                      will match all files and zero or more directories and
                      subdirectories.  If the pattern is followed by a /, only
                      directories and subdirectories match.
          *      Matches any string, including the null string.  When  the
                 globstar  shell  option  is  enabled,  and * is used in a
                 pathname expansion context, two adjacent  *s  used  as  a
                 single  pattern  will  match  all  files and zero or more
                 directories and subdirectories.  If followed by a /,  two
                 adjacent  *s  will match only directories and subdirecto‐
                 ries.
info bash

globstar
                      If set, the pattern ** used in a pathname expansion context
                      will match all files and zero or more directories and
                      subdirectories.  If the pattern is followed by a /, only
                      directories and subdirectories match.
          *      Matches any string, including the null string.  When  the
                 globstar  shell  option  is  enabled,  and * is used in a
                 pathname expansion context, two adjacent  *s  used  as  a
                 single  pattern  will  match  all  files and zero or more
                 directories and subdirectories.  If followed by a /,  two
                 adjacent  *s  will match only directories and subdirecto‐
                 ries.

您还可以使用大多数*nix发行版中包含的
命令。(虽然Ubuntu是一个显著的例外——但可以通过apt get安装)


您还可以使用大多数*nix发行版中包含的
命令。(虽然Ubuntu是一个显著的例外——但可以通过apt get安装)


这就是工作。但是现在我想起来了,我作为一个团队的一部分在做这个脚本,因为团队中的每个人对考试文件夹都有不同的路径,我认为它不会工作。我如何修改这个?我试着查找*/exam-print>文件,但它说没有这样的文件或目录。好的,我把它改为find~/exam-print>文件,它就可以工作了。但是它肯定会在其他有考试文件夹的计算机上工作吗?如果我理解你的问题,只需
查找考试-打印>文件
,以考试目录的父目录作为工作目录。使用
~/exam
仅适用于在主目录中有
exam
目录的人。他们有一个exam目录。我想它会起作用的。我会尽快测试的。谢谢你的回答。这就是工作。但是现在我想起来了,我作为一个团队的一部分在做这个脚本,因为团队中的每个人对考试文件夹都有不同的路径,我认为它不会工作。我如何修改这个?我试着查找*/exam-print>文件,但它说没有这样的文件或目录。好的,我把它改为find~/exam-print>文件,它就可以工作了。但是它肯定会在其他有考试文件夹的计算机上工作吗?如果我理解你的问题,只需
查找考试-打印>文件
,以考试目录的父目录作为工作目录。使用
~/exam
仅适用于在主目录中有
exam
目录的人。他们有一个exam目录。我想它会起作用的。我会尽快测试的。谢谢你的回答。