2

How to list files recursively, in alphabetical order and without lines that show exclusively folder names?

For example, I can get the following output with tree --dirsfirst -fihan * -o filelist:

00.-ScriptHookV [1.0.1737.0]
[128K]  00.-ScriptHookV [1.0.1737.0]/dinput8.dll*
[1.2M]  00.-ScriptHookV [1.0.1737.0]/ScriptHookV.dll*
01.-ScriptHookVDotNet [2.10.10]
[ 891]  01.-ScriptHookVDotNet [2.10.10]/LICENSE.txt*
[1.8K]  01.-ScriptHookVDotNet [2.10.10]/README.txt*
[1018K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet2.dll*
[7.5K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet2.pdb*
[ 92K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet2.xml*
[ 34K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet.asi*
02.-Heap Limit Adjuster [1.0.0]
[ 98K]  02.-Heap Limit Adjuster [1.0.0]/GTAV.HeapAdjuster.asi*
03.-OpenIV [3.1]
[132K]  03.-OpenIV [3.1]/OpenIV.asi*
04.-Enhanced Native Trainer [1.41 Update 1]
[ 15M]  04.-Enhanced Native Trainer [1.41 Update 1]/EnhancedNativeTrainer.asi*
[8.4K]  04.-Enhanced Native Trainer [1.41 Update 1]/ent-config.xml*

But I need this kind of output (not necessarily with filesizes):

[128K]  00.-ScriptHookV [1.0.1737.0]/dinput8.dll
[1.2M]  00.-ScriptHookV [1.0.1737.0]/ScriptHookV.dll
[ 891]  01.-ScriptHookVDotNet [2.10.10]/LICENSE.txt
[1.8K]  01.-ScriptHookVDotNet [2.10.10]/README.txt
[1018K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet2.dll
[7.5K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet2.pdb
[ 92K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet2.xml
[ 34K]  01.-ScriptHookVDotNet [2.10.10]/ScriptHookVDotNet.asi
[ 98K]  02.-Heap Limit Adjuster [1.0.0]/GTAV.HeapAdjuster.asi
[132K]  03.-OpenIV [3.1]/OpenIV.asi
[ 15M]  04.-Enhanced Native Trainer [1.41 Update 1]/EnhancedNativeTrainer.asi
[8.4K]  04.-Enhanced Native Trainer [1.41 Update 1]/ent-config.xml

Any suggestions?

3 Answers 3

1

Using the zsh shell:

print -C1 **/*(.DN)

With the zsh shell this would print the result of expanding the given globbing pattern as a single column (this is what print -C1 does). The pattern **/*(.DN) expands to the pathnames of all regular files (not directories etc.; this is what the glob qualifier (.) does) in or beneath the current directory. These will, by default, be orderer in lexicographical order. The (DN) glob qualifier has the same effect as setting dotglob and nullglob, respectively, in the bash shell (i.e. it makes the pattern match hidden names and also makes it expand to nothing at all if it doesn't match anything).

Instead of (.) to only list regular files, one could instead use (^/) to list non-directories.

In bash, you can do something similar with

shopt -s globstar dotglob nullglob
for pathname in **/*; do
    [[ ! -d $pathname ]] && printf '%s\n' "$pathname"
done

which prints the lexicographically sorted pathnames of all non-directories (including hidden files) recursively.

2
  • POSIX-ly, and also cleaner design: ! [ -d "$pathname" ] && ..., still not really relevant since you rely on shopt in there... but I thought of it merely as for new readers to learn clean designs. | +1 Oct 27, 2019 at 9:36
  • @LinuxSecurityFreak Yeah, when you pull in globstar and the others in a bash script, you might as well use other bash features without worrying too much about POSIX compatibility.
    – Kusalananda
    Oct 27, 2019 at 9:40
0

Should be pretty portable:

find . -type f | sort

On the off chance that your input data contains files with new lines in their name, I believe this should handle them better (thanks to Kusalananda for pointing out this possible scenario):

find . -type f -print0 | sort -z | tr '\0' '\n'
5
  • 1
    A file named $'3\n2\n1' would end up listed weirdly since sort sorts line, not filenames.
    – Kusalananda
    Oct 27, 2019 at 8:46
  • That’s a fair point, but assumes facts not in evidence. Also the input looks an awful lot like Windows files to me – and Windows does not support new lines in file names, to the best of my knowledge.
    – bxm
    Oct 27, 2019 at 8:50
  • And assuming these are Windows files is not doing the same thing?
    – Kusalananda
    Oct 27, 2019 at 8:51
  • The names of the files are the evidence for my assumption.
    – bxm
    Oct 27, 2019 at 8:53
  • Thanks, I'll always use the 2nd option just in case, this worked great, and yes, these are windows files, GTA V mods organized by install order
    – terminal64
    Oct 28, 2019 at 3:57
-1

If you want to list all the files in all the sub-directories from your current directory and sort on filename use the find command.

find . -type f -exec basename {} \; | sort
2
  • 1
    I believe the OP wants the directory name included with the file name, but not lines which are solely the directory name.
    – bxm
    Oct 27, 2019 at 8:46
  • Exactly, directory name included but without lines which are solely the directory name
    – terminal64
    Oct 28, 2019 at 0:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .