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 list files from a given directory in Bash
Your algorithm is entering endless loop since lsRec
function implicitly expects its argument to end in "/". First level works, as you pass path ending with "/" as input, but second level doesn't, as the path you're making recursive call with doesn't end in "/". What you could do is either add the slash when you make a recursive call, so it'll look like lsRec $x/
, or (better yet) add the slash in the loop arguments as in for x in $1/*; do
(as system generally ignores multiple adjacent path separators).
Moving forward, I'd advise you to quote the values (e.g. for x in "$1/"*
, lsRec "$x"
, lsRec "$arg"
) to avoid issues when path contains whitespace characters. You'll get there when you create a directory with space in its name under directory hierarchy you're scanning.
How can I get a recursive full-path listing, one line per file?
If you really want to use ls
, then format its output using awk:
ls -R /path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'
How to loop through a directory recursively to delete files with certain extensions
find
is just made for that.
find /tmp -name '*.pdf' -or -name '*.doc' | xargs rm
Bash - What is a good way to recursively find the type of all files in a directory and its subdirectories?
This may help: How to recursively list subdirectories in Bash without using "find" or "ls" commands?
That said, I modified it to accept user input as follows:
#!/bin/bash
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
recurse "$i"
elif [ -f "$i" ]; then
echo "file: $i"
fi
done
}
recurse $1
If you didn't want the files portion (which it appears you don't) then just remove the elif and line below it. I left it in as the original post had it also. Hope this helps.
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.
How can I recursively find all files in current and subfolders based on wildcard matching?
Use find
:
find . -name "foo*"
find
needs a starting point, so the .
(dot) points to the current directory.
Related Topics
Difference Between Wic and Hddimg Format in Yocto
Incomplete Lsb Comment. Insserv: Missing Valid Name for 'Provides:' Please Add
Ubuntu "E: Unable to Locate Package MySQL "
Simulate Microphone (Virtual Mic)
What Happens If There Are Too Many Files Under a Single Directory in Linux
Why Does Munmap Needs a Length as Parameter
Why Does Utf-8 Text Sort in Different Order Between Os X and Linux
Bash "&" Without Printing "[1]+ Done "
Authenticating Gtk App to Run with Root Permissions
Sed Command Works on Linux, But Not on Os X
Reset Bash History Search Position
Vagrant, Shared Folder: Take Advantage of Inotify Over Nfs
Raw Socket Access as Normal User on Linux 2.4
Bash-Script Printing a PDF to a PDF in Linux
Why Does The -R Option (Relocatable) Make Ld Not Find Any Libraries
Controlling The Boot Screen on Linux Embedded App
How to Connect to Sftp Using Linux Command from Perl
Linux: How to Check The Largest Contiguous Address Range Available to a Process