简体   繁体   English

如何通过脚本列出Bash中的目录和文件?

[英]How to list directories and files in a Bash by script?

I would like to list directory tree, but I have to write script for it and as parameter script should take path to base directory. 我想列出目录树,但是我必须为其编写脚本,并且由于参数脚本应采用基本目录的路径。 Listing should start from this base directory. 清单应从此基本目录开始。

The output should look like this: 输出应如下所示:

Directory: ./a
File: ./a/A
Directory: ./a/aa
File: ./a/aa/AA
Directory: ./a/ab
File: ./a/ab/AB

So I need to print path from the base directory for every directory and file in this base directory. 因此,我需要从基本目录中为该基本目录中的每个目录和文件打印路径。

UPDATED 更新

Running the script I should type in the terminal this: ".\\test.sh /home/usr/Desktop/myDirectory" or ".\\test.sh myDirectory" - since I run the test.sh from the Desktop level. 运行脚本,我应该在终端中键入以下内容:“。\\ test.sh / home / usr / Desktop / myDirectory”或“。\\ test.sh myDirectory”-因为我是从桌面级别运行test.sh的。 And right now the script should be run from the level of /home/usr/Dekstop/myDirectory" 现在,该脚本应从/ home / usr / Dekstop / myDirectory级别运行”

I have the following command in my test.sh file: 我的test.sh文件中有以下命令:

find . | sed -e "s/[^-][^\/]*\//  |/g"

But It is the command , not shell code and prints the output like this: 但这是命令 ,而不是外壳程序代码,并打印输出,如下所示:

DIR: dir1
    DIR: dir2
      fileA
    DIR: dir3
    fileC
fileB

How to print the path from base directory for every dir or file from the base dir? 如何为基本目录中的每个目录或文件打印基本目录中的路径? Could someone help me to work it out? 有人可以帮我解决吗?

Not clear what you want maybe, 不清楚你想要什么,

find . -type d -printf 'Directory: %p\n' -o -type f -printf 'File: %p\n'

However to see the subtree of a directory, I find more useful 但是,查看目录的子树,我发现更有用

find "$dirname" -type f

To answer comment it can also be done in pure bash (builtin without external commands), using a recursive function. 要回答评论,也可以使用递归函数在纯bash(内置而无需外部命令)中完成。

rec_find() {
    local f
    for f in "$1"/*; do
        [[ -d $f ]] && echo "Directory: $f" && rec_find "$f"
        [[ -f $f ]] && echo "File: $f"
    done
}

rec_find "$1"

You can use tree command. 您可以使用tree命令。 Key -L means max depth. -L表示最大深度。 Examples: 例子:

tree
.
├── 1
│   └── test
├── 2
│   └── test
└── 3
    └── test

3 directories, 3 files

Or 要么

tree -L 1
.
├── 1
├── 2
└── 3

3 directories, 0 files

Create your test.sh with the below codes. 使用以下代码创建test.sh Here you are reading command line parameter in system variable $1 and provides parameter to find command. 在这里,您正在读取系统变量$ 1中的命令行参数,并提供了用于查找命令的参数。

#!/bin/bash #in which shell you want to execute this script

find $1 | sed -e "s/[^-][^\/]*\//  |/g"

Now how will it work:- 现在如何运作:-

./test.sh /home/usr/Dekstop/myDirectory #you execute this command

Here command line parameter will be assign into $1. 此处,命令行参数将分配给$ 1。 More than one parameter you can use $1 till $9 and after that you have to use shift command. 您可以使用$ 1到$ 9的多个参数,此后必须使用shift命令。 (You will get more detail information online). (您将在线获得更多详细信息)。

So your command will be now:- 所以您的命令将是:

#!/bin/bash #in which shell you want to execute this script

find /home/usr/Dekstop/myDirectory | sed -e "s/[^-][^\/]*\//  |/g"  

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM