递归列出所有目录和文件

时间:2021-03-06 21:49:35

I would like to receive the following output.

我想收到以下输出。

Suppose the directory structure on the file system is like this:

假设文件系统上的目录结构是这样的:

  -dir1
      -dir2
        -file1
        -file2
             -dir3
                -file3
                -file4
            -dir4
                -file5
       -dir5
             -dir6
             -dir7

The output from the script must be like:

脚本的输出必须如下:

Directories:

目录:

/dir1
/dir1/dir2
/dir1/dir2/dir3
/dir1/dir2/dir4
/dir1/dir5
/dir1/dir5/dir6
/dir1/dir5/dir7

Files:

文件:

/dir1
/dir1/dir2/file1
/dir1/dir2/file2
/dir1/dir2/dir3/file3
/dir1/dir2/dir3/file4
/dir1/dir2/dir4/file5
/dir1/dir5/dir6
/dir1/dir5/dir7

Could you tell me how to keep the output of find . -type d and find . -type f into another file?

你能告诉我如何保持find的输出吗? -type d并找到。 -type f进入另一个文件?

7 个解决方案

#1


79  

In windows, to list only directories:

在Windows中,仅列出目录:

dir /ad /b /s

to list all files (and no directories):

列出所有文件(没有目录):

dir /a-d /b /s

redirect the output to a file:

将输出重定向到文件:

dir /a-d /b /s > filename.txt

dir command parameters explained on wikipedia

dir命令参数在*上解释

#2


21  

in shell:

在shell中:

find . -type d

gives directories from current working directory, and:

从当前工作目录中提供目录,并且:

find . -type f

gives files from current working directory.

从当前工作目录中提供文件。

Replace . by your directory of interest.

替换。由您感兴趣的目录。

#3


18  

Bash/Linux Shell

Directories:

目录:

find ./ -type d 

Files:

文件:

find ./ -type f 

Bash/Shell Into a file

Directories:

目录:

find ./ -type d  > somefile.txt

Files:

文件:

find ./ -type f  > somefile.txt

#4


7  

On Windows, you can do it like this as most flexibile solution that allows you to additionally process dir names.

在Windows上,你可以这样做,因为大多数flexibile解决方案允许你额外处理目录名称。

You use FOR /R to recursively execute batch commands.

您使用FOR / R递归执行批处理命令。

Check out this batch file.

看看这个批处理文件。

@echo off
SETLOCAL EnableDelayedExpansion

SET N=0
for /R %%i in (.) do (
     SET DIR=%%i

     ::put anything here, for instance the following code add dir numbers.
     SET /A N=!N!+1
     echo !N! !DIR!
)

Similary for files you can add pattern as a set instead of dot, in your case

在您的情况下,类似于文件,您可以将模式添加为集合而不是点

 (*.*)

#5


3  

In Windows :

在Windows中:

dir /ad /b /s

dir / ad / b / s

dir /a-d /b /s

dir / a-d / b / s

#6


3  

In Linux, a simple

在Linux中,简单

find . -printf '%y %p\n'

will give you a list of all the contained items, with directories and files mixed. You can save this output to a temporary file, then extract all lines that start with 'd'; those will be the directories. Lines that start with an 'f' are files.

将为您提供所有包含项目的列表,其中包含目录和文件。您可以将此输出保存到临时文件,然后提取以'd'开头的所有行;那些将是目录。以'f'开头的行是文件。

#7


2  

This is an old question, but I thought I'd add something anyhow.

这是一个老问题,但我想我无论如何都会添加一些东西。

DIR doesn't traverse correctly all the directory trees you want, in particular not the ones on C:. It simply gives up in places because of different protections.

DIR没有正确遍历您想要的所有目录树,特别是C:上的目录树。由于不同的保护措施,它只是放弃了。

ATTRIB works much better, because it finds more. (Why this difference? Why would MS make one utility work one way and another work different in this respect? Damned if I know.) In my experience the most effective way to handle this, although it's a kludge, is to get two listings:

ATTRIB工作得更好,因为它找到了更多。 (为什么会出现这种差异?为什么MS会让一个实用工具单向工作而另一个工作方式在这方面有所不同?如果我知道的话会诅咒。)根据我的经验,处理这个问题最有效的方法,虽然它是一个kludge,但是要获得两个清单:

attrib /s /d C:\ >%TEMP%\C-with-directories.txt

attrib / s / d C:\>%TEMP%\ C-with-directories.txt

attrib /s C:\ >%TEMP%\C-without-directories.txt

attrib / s C:\>%TEMP%\ C-without-directories.txt

and get the difference between them. That difference is the directories on C: (except the ones that are too well hidden). For C:, I'd usually do this running as administrator.

并获得它们之间的差异。这个区别是C:上的目录(除了隐藏得太多的目录)。对于C:,我通常以管理员身份运行。

#1


79  

In windows, to list only directories:

在Windows中,仅列出目录:

dir /ad /b /s

to list all files (and no directories):

列出所有文件(没有目录):

dir /a-d /b /s

redirect the output to a file:

将输出重定向到文件:

dir /a-d /b /s > filename.txt

dir command parameters explained on wikipedia

dir命令参数在*上解释

#2


21  

in shell:

在shell中:

find . -type d

gives directories from current working directory, and:

从当前工作目录中提供目录,并且:

find . -type f

gives files from current working directory.

从当前工作目录中提供文件。

Replace . by your directory of interest.

替换。由您感兴趣的目录。

#3


18  

Bash/Linux Shell

Directories:

目录:

find ./ -type d 

Files:

文件:

find ./ -type f 

Bash/Shell Into a file

Directories:

目录:

find ./ -type d  > somefile.txt

Files:

文件:

find ./ -type f  > somefile.txt

#4


7  

On Windows, you can do it like this as most flexibile solution that allows you to additionally process dir names.

在Windows上,你可以这样做,因为大多数flexibile解决方案允许你额外处理目录名称。

You use FOR /R to recursively execute batch commands.

您使用FOR / R递归执行批处理命令。

Check out this batch file.

看看这个批处理文件。

@echo off
SETLOCAL EnableDelayedExpansion

SET N=0
for /R %%i in (.) do (
     SET DIR=%%i

     ::put anything here, for instance the following code add dir numbers.
     SET /A N=!N!+1
     echo !N! !DIR!
)

Similary for files you can add pattern as a set instead of dot, in your case

在您的情况下,类似于文件,您可以将模式添加为集合而不是点

 (*.*)

#5


3  

In Windows :

在Windows中:

dir /ad /b /s

dir / ad / b / s

dir /a-d /b /s

dir / a-d / b / s

#6


3  

In Linux, a simple

在Linux中,简单

find . -printf '%y %p\n'

will give you a list of all the contained items, with directories and files mixed. You can save this output to a temporary file, then extract all lines that start with 'd'; those will be the directories. Lines that start with an 'f' are files.

将为您提供所有包含项目的列表,其中包含目录和文件。您可以将此输出保存到临时文件,然后提取以'd'开头的所有行;那些将是目录。以'f'开头的行是文件。

#7


2  

This is an old question, but I thought I'd add something anyhow.

这是一个老问题,但我想我无论如何都会添加一些东西。

DIR doesn't traverse correctly all the directory trees you want, in particular not the ones on C:. It simply gives up in places because of different protections.

DIR没有正确遍历您想要的所有目录树,特别是C:上的目录树。由于不同的保护措施,它只是放弃了。

ATTRIB works much better, because it finds more. (Why this difference? Why would MS make one utility work one way and another work different in this respect? Damned if I know.) In my experience the most effective way to handle this, although it's a kludge, is to get two listings:

ATTRIB工作得更好,因为它找到了更多。 (为什么会出现这种差异?为什么MS会让一个实用工具单向工作而另一个工作方式在这方面有所不同?如果我知道的话会诅咒。)根据我的经验,处理这个问题最有效的方法,虽然它是一个kludge,但是要获得两个清单:

attrib /s /d C:\ >%TEMP%\C-with-directories.txt

attrib / s / d C:\>%TEMP%\ C-with-directories.txt

attrib /s C:\ >%TEMP%\C-without-directories.txt

attrib / s C:\>%TEMP%\ C-without-directories.txt

and get the difference between them. That difference is the directories on C: (except the ones that are too well hidden). For C:, I'd usually do this running as administrator.

并获得它们之间的差异。这个区别是C:上的目录(除了隐藏得太多的目录)。对于C:,我通常以管理员身份运行。