Recursively List all directories and files

后端 未结 7 1765
萌比男神i
萌比男神i 2021-01-30 20:19

I would like to receive the following output.

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

  -dir1
      -dir2
        -file1
        -fi         


        
相关标签:
7条回答
  • 2021-01-30 20:58

    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.

    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 /s /d C:\ >%TEMP%\C-with-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.

    0 讨论(0)
  • 2021-01-30 20:59

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

    You use FOR /R to recursively execute batch commands.

    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

     (*.*)
    
    0 讨论(0)
  • 2021-01-30 21:03

    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
    
    0 讨论(0)
  • 2021-01-30 21:17

    In Windows :

    dir /ad /b /s

    dir /a-d /b /s

    0 讨论(0)
  • 2021-01-30 21:21

    In Linux, a simple

    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.

    0 讨论(0)
  • 2021-01-30 21:23

    In windows, to list only directories:

    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

    0 讨论(0)
提交回复
热议问题