Recursively List All Files in a Directory Including Files in Symlink Directories

Recursively list all files in a directory including files in symlink directories

The -L option to ls will accomplish what you want. It dereferences symbolic links.

So your command would be:

ls -LR

You can also accomplish this with

find -follow

The -follow option directs find to follow symbolic links to directories.

On Mac OS X use

find -L

as -follow has been deprecated.

Recursively listing symbolic links to a given folder with their full path

find -type l -print0|xargs -0 ls -ld|grep Dropbox

How do I find all of the symlinks in a directory tree?

This will recursively traverse the /path/to/folder directory and list only the symbolic links:

ls -lR /path/to/folder | grep ^l

If your intention is to follow the symbolic links too, you should use your find command but you should include the -L option; in fact the find man page says:

   -L     Follow symbolic links.  When find examines or prints information
about files, the information used shall be taken from the prop‐
erties 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 subdirec‐
tory pointed to by the symbolic link will be searched.

When the -L option is in effect, the -type predicate will always
match against the type of the file that a symbolic link points
to rather than the link itself (unless the symbolic link is bro‐
ken). Using -L causes the -lname and -ilname predicates always
to return false.

Then try this:

find -L /var/www/ -type l

This will probably work: I found in the find man page this diamond: if you are using the -type option you have to change it to the -xtype option:

          l      symbolic link; this is never true if the -L option or the
-follow option is in effect, unless the symbolic link is
broken. If you want to search for symbolic links when -L
is in effect, use -xtype.

Then:

find -L /var/www/ -xtype l

List files recursively in Linux CLI with path relative to the current directory

Use find:

find . -name \*.txt -print

On systems that use GNU find, like most GNU/Linux distributions, you can leave out the -print.

Recursively list files in Java

Java 8 provides a nice stream to process all files in a tree.

try (Stream<Path> stream = Files.walk(Paths.get(path))) {
stream.filter(Files::isRegularFile)
.forEach(System.out::println);
}

This provides a natural way to traverse files. Since it's a stream you can do all nice stream operations on the result such as limit, grouping, mapping, exit early etc.

UPDATE: I might point out there is also Files.find which takes a BiPredicate that could be more efficient if you need to check file attributes.

Files.find(Paths.get(path),
Integer.MAX_VALUE,
(filePath, fileAttr) -> fileAttr.isRegularFile())
.forEach(System.out::println);

Note that while the JavaDoc eludes that this method could be more efficient than Files.walk it is effectively identical, the difference in performance can be observed if you are also retrieving file attributes within your filter. In the end, if you need to filter on attributes use Files.find, otherwise use Files.walk, mostly because there are overloads and it's more convenient.

TESTS: As requested I've provided a performance comparison of many of the answers. Check out the Github project which contains results and a test case.

How to recursively copy files under a folder and its subfolders as symbolic links

The command you are looking for is cp -rs /path/to/source dest.

Note that you need to provide full path to the source directory so that it can make absolute symlinks.

Delete directory and all symlinks recursively

That is strange, I have no issues with shutil.rmtree() with or without symlink under the folder to be deleted, both in windows 10 and Ubuntu 20.04.2 LTS.

Anyhow try the following code. I tried it in windows 10 and Ubuntu.

from pathlib import Path
import shutil

def delete_dir_recursion(p):
"""
Delete folder, sub-folders and files.
"""
for f in p.glob('**/*'):
if f.is_symlink():
f.unlink(missing_ok=True) # missing_ok is added in python 3.8
print(f'symlink {f.name} from path {f} was deleted')
elif f.is_file():
f.unlink()
print(f'file: {f.name} from path {f} was deleted')
elif f.is_dir():
try:
f.rmdir() # delete empty sub-folder
print(f'folder: {f.name} from path {f} was deleted')
except OSError: # sub-folder is not empty
delete_dir_recursion(f) # recurse the current sub-folder
except Exception as exception: # capture other exception
print(f'exception name: {exception.__class__.__name__}')
print(f'exception msg: {exception}')

try:
p.rmdir() # time to delete an empty folder
print(f'folder: {p.name} from path {p} was deleted')
except NotADirectoryError:
p.unlink() # delete folder even if it is a symlink, linux
print(f'symlink folder: {p.name} from path {p} was deleted')
except Exception as exception:
print(f'exception name: {exception.__class__.__name__}')
print(f'exception msg: {exception}')

def delete_dir(folder):
p = Path(folder)

if not p.exists():
print(f'The path {p} does not exists!')
return

# Attempt to delete the whole folder at once.
try:
shutil.rmtree(p)
except Exception as exception:
print(f'exception name: {exception.__class__.__name__}')
print(f'exception msg: {exception}')
# continue parsing the folder
else: # else if no issues on rmtree()
if not p.exists(): # verify
print(f'folder {p} was successfully deleted by shutil.rmtree!')
return

print(f'Parse the folder {folder} ...')
delete_dir_recursion(p)

if not p.exists(): # verify
print(f'folder {p} was successfully deleted!')

# start
folder_to_delete = '/home/zz/tmp/sample/b' # delete folder b
delete_dir(folder_to_delete)

Sample output:

We are going to delete the folder b.

.
├── 1.txt
├── a
├── b
│   ├── 1
│   ├── 1.txt -> ../1.txt
│   ├── 2
│   │   └── 21
│   │   └── 21.txt
│   ├── 3
│   │   └── 31
│   ├── 4
│   │   └── c -> ../../c
│   ├── a -> ../a
│   └── b.txt
├── c

Parse the folder /home/zz/tmp/sample/b ...
symlink a from path /home/zz/tmp/sample/b/a was deleted
symlink c from path /home/zz/tmp/sample/b/4/c was deleted
folder: 4 from path /home/zz/tmp/sample/b/4 was deleted
symlink 1.txt from path /home/zz/tmp/sample/b/1.txt was deleted
file: b.txt from path /home/zz/tmp/sample/b/b.txt was deleted
file: 21.txt from path /home/zz/tmp/sample/b/2/21/21.txt was deleted
folder: 21 from path /home/zz/tmp/sample/b/2/21 was deleted
folder: 2 from path /home/zz/tmp/sample/b/2 was deleted
folder: 1 from path /home/zz/tmp/sample/b/1 was deleted
folder: 31 from path /home/zz/tmp/sample/b/3/31 was deleted
folder: 3 from path /home/zz/tmp/sample/b/3 was deleted
folder: b from path /home/zz/tmp/sample/b was deleted
folder /home/zz/tmp/sample/b was successfully deleted!


Related Topics



Leave a reply



Submit