Recursively List all directories and files
Asked Answered
N

7

57

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?

Naples answered 14/4, 2009 at 13:10 Comment(2)
er, there's an inconsistency, you have dirs listed in your file output. Isn't that like, wrong?Echolocation
Hi I use unix/aix - korn shell apologies if there is something wrong. . I am new to this fieldNaples
A
90

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

Arrowy answered 14/4, 2009 at 13:16 Comment(3)
Is it possible to list file sizes without grouping by folder? I'd like to import into Excel and do some reports.Essentialism
@Nic Cottrell If you remove /b, it will show with file sizes and timestampsSlush
The OP asked for shell, not windows. Why does this have so many upvotes?Deanadeanda
E
39

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
Echolocation answered 14/4, 2009 at 13:13 Comment(2)
find ./ -type d,fGlynn
Or just tree (sudo apt install tree)Savoury
M
30

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

Mcmann answered 14/4, 2009 at 13:13 Comment(0)
A
6

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

 (*.*)
Acerb answered 14/4, 2009 at 13:47 Comment(1)
The OP asked for shell, not windows batch.Deanadeanda
S
4

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.

Septuor answered 14/4, 2009 at 13:18 Comment(1)
find ./ -type d,fGlynn
A
3

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.

Ankledeep answered 16/1, 2014 at 11:34 Comment(0)
G
2

In Windows :

dir /ad /b /s

dir /a-d /b /s

Ginn answered 14/4, 2009 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.