Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively List all directories and files

People also ask

How do I list all directories and subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.

How do I grep all files in a directory recursively?

Recursive Search To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

How do I recursively list files in Windows?

How to list all files a folder recursively in command line ? With a ms dos prompt, to list all the files inside a folder. To do so, type the dir command to list the folder content with the recursive dedicated option.

How do you list all files directories and all sub files or sub directories in one command?

ls -lR is to display all files, directories and sub directories of the current directory ls -lR | more is used to show all the files in a flow.


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


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.


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

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

 (*.*)

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.


In Windows :

dir /ad /b /s

dir /a-d /b /s