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

发布于 2024-12-12 10:30:42 字数 439 浏览 0 评论 0 原文

我有一个名为 exam 的文件夹。该文件夹有 3 个文件夹,分别称为数学、物理和英语。所有这些文件夹中都有一些子文件夹和文件。我想遍历每个文件夹并将每个文件夹和文件的路径打印到另一个名为 files 的文件上。我已经这样做了:

#!/bin/bash

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

{
  for f in $LOC 
  do
   echo $f
  done
 } > "files"

我得到的出口是:

/home/school/exam/math

/home/school/exam/physicals

/home/school/exam/english

我不知道如何使代码访问并执行考试的子文件夹也是如此。有什么建议吗?

PS 我只是 shell 脚本编写的初学者。

I have a folder called exam. This folder has 3 folders called math, physics and english. All of these folders have some sub folders and files in them. I want to traverse through every folder and print the path of every folder and file on another file called files. I've done this:

#!/bin/bash

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

{
  for f in $LOC 
  do
   echo $f
  done
 } > "files"

The exit I get is:

/home/school/exam/math

/home/school/exam/physics

/home/school/exam/english

I can't figure out how to make the code visit and do the same thing to the sub folders of exam. Any suggestions?

PS I'm just a beginner in shell scripting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单气质女生网名 2024-12-19 10:30:43
find /home/school/exam -print > files
find /home/school/exam -print > files
⒈起吃苦の倖褔 2024-12-19 10:30:43

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

use :

shopt -s globstar
for i in /home/school/exam/**

这里的参考是man 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.

info bash

          *      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.

With the globstar option bash will recurse all filenames in subdirectories when using two adjacent stars

use :

shopt -s globstar
for i in /home/school/exam/**

The reference here is man 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.

and info bash:

          *      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.
掩于岁月 2024-12-19 10:30:43

您可以使用 find 命令,它可以获取所有文件,然后您可以对它们执行某些操作,例如使用 exec 或 xargs。

you can use find command, it could get all files, then you can do something on them, using exec or xargs for example.

梦巷 2024-12-19 10:30:43

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

LOC="/home/school/exam/"
tree -if $LOC > files

You can also use the tree command which is included in most *nix distributions. (Though Ubuntu is a notable exception - but can be installed via apt-get)

LOC="/home/school/exam/"
tree -if $LOC > files
笔落惊风雨 2024-12-19 10:30:43

递归地列出所有文件怎么样?

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

How about this to list all your files recursively.

for i in *; do ls -l $i ; done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文