Search code examples
listawkls

get the list of files hirarchically with its absolute path


I want the output as shown in below example.

root@aklinux139:~/.atom# du -sh */* | awk '{print $2}'
blob-store/BLOB
blob-store/INVKEYS
blob-store/MAP
compile-cache/less
compile-cache/root
compile-cache/style-manager
packages/README.md
storage/application.json
root@aklinux139:~/.atom# 

But ls does not give this output with any of its options/arguments. 'ls -R' gives path and then its content not the filename with its absolute path.

I need this very often while writing scripts, Can someone help me with this ? Thanks heap in advance.


Solution

  • You can use find, an example for a depth of 2 :

    find . -maxdepth 2 -mindepth 2 -printf '%P\n'
    

    If you want to exclude dot files :

    find . -maxdepth 2 -mindepth 2 -not -path '*/\.*' -printf '%P\n'
    

    If you want to sort the result (as du -sh) :

    find . -maxdepth 2 -mindepth 2 -not -path '*/\.*' -printf '%P\n' | sort