- Recursively list all files in a directory including files in symlink directories
- 8 Answers 8
- How to List Files Recursively in Linux command line
- Use ls command to list files recursively
- Use the tree command to list files recursively
- Use the find command to list files recursively
- Use the du command to list files recursively
- Wrapping Up
- Recursively List all directories and files
- 7 Answers 7
- Bash/Linux Shell
- Bash/Shell Into a file
Recursively list all files in a directory including files in symlink directories
Suppose I have a directory /dir inside which there are 3 symlinks to other directories /dir/dir11 , /dir/dir12 , and /dir/dir13 . I want to list all the files in dir including the ones in dir11 , dir12 and dir13 . To be more generic, I want to list all files including the ones in the directories which are symlinks. find . , ls -R , etc stop at the symlink without navigating into them to list further.
8 Answers 8
The -L option to ls will accomplish what you want. It dereferences symbolic links.
You can also accomplish this with
The -follow option directs find to follow symbolic links to directories.
as -follow has been deprecated.
@pjz: is there a cross-reference for ‘-follow deprecated; use -L’? Somewhat to my considerable surprise, I did find ‘-L’ and ‘-H’ listed in the POSIX / SUS standard at opengroup.org/onlinepubs/009695399/toc.htm, and even more to my surprise no ‘-follow’, so I answered my own question.
this didn’t work for me. first nothing happened then i tried with -follow — and it said it couln’t find the folder ollow
Your answer is good, but I want to follow only file paths(full path to file) and not directory path, how can I do it ?
How about tree? tree -l will follow symlinks.
Disclaimer: I wrote this package.
It is a nice interface, but sometimes tree is not available and you cannot add it to the system you are trying to explore.
find /dir -type f -follow -print
-type f means it will display real files (not symlinks)
-follow means it will follow your directory symlinks
-print will cause it to display the filenames.
If you want a ls type display, you can do the following
find /dir -type f -follow -print|xargs ls -l
-L, --dereference when showing file information for a symbolic link, show informa‐ tion for the file the link references rather than for the link itself
If you find you want to only follow a few symbolic links (like maybe just the toplevel ones you mentioned), you should look at the -H option, which only follows symlinks that you pass to it on the commandline.
I knew tree was an appropriate, but I didn’t have tree installed. So, I got a pretty close alternate here
find -L /var/www/ -type l # man find
-L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the
properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched.
How to List Files Recursively in Linux command line
Want to list files recursively and see the contents of all the subdirectories in Linux? Here’s how you can do that.
The ls command is the default for viewing the contents of a directory. This is despite the existence of the dedicated dir command.
The ls command list the contents of the present directory, but it doesn’t show the contents of the subdirectories by default. You can make it though.
Use ls command to list files recursively
You can change the default behavior of the ls command to list files recursively by using the -R option.
As you can see, it shows the contents of every subdirectory one by one.
That’s uppercase R . the lowercase r is used for the reverse display of ls output.
But wait, you can’t expect Linux to have just one solution. Here are some other ways of listing files recursively.
Use the tree command to list files recursively
By far, this is my favorite utility when it comes to listing files recursively as it gets the output in the easiest way possible.
But it does not come pre-installed in most distributions. If you’re on a Debian-based distro, you can use this command for installation:
Once you are done with the installation, you just have to append the filename with the tree command:
Use the find command to list files recursively
You’ll find me often praising the find command being so extensive with more than 50 options and can also be used for listing files recursively.
You can recursively search sub-directories with the -ls option of the find command. It will list all the files but not the hidden files. It will show additional information such as read-write permissions:
Similarly, you can also use the -print option with the find command if you just want to list files recursively:
find Directory_name -print
Use the du command to list files recursively
The du command is used to show the storage size of files and when used with the -a option, it will count and print the files recursively.
But I’m also going to use the -c option that will sum the total number of files available in the specified directory:
Wrapping Up
This was my take on how you can print files recursively in Linux. But you can do more with those utilities with enough knowledge.
And we have a detailed guide on how you can make the most out of man pages in Linux so you can be creative whenever you want!
Recursively List all directories and files
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
/dir1 /dir1/dir2 /dir1/dir2/dir3 /dir1/dir2/dir4 /dir1/dir5 /dir1/dir5/dir6 /dir1/dir5/dir7
/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
7 Answers 7
In windows, to list only directories:
to list all files (and no directories):
redirect the output to a file:
Is it possible to list file sizes without grouping by folder? I’d like to import into Excel and do some reports.
Bash/Linux Shell
Bash/Shell Into a file
find ./ -type d > somefile.txt
find ./ -type f > somefile.txt
gives directories from current working directory, and:
gives files from current working directory.
Replace . by your directory of interest.
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
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.
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.