List File Using Ls Command in Linux with Full Path

How can I generate a list of files with their absolute path in Linux?

If you give find an absolute path to start with, it will print absolute paths. For instance, to find all .htaccess files in the current directory:

find "$(pwd)" -name .htaccess

or if your shell expands $PWD to the current directory:

find "$PWD" -name .htaccess

find simply prepends the path it was given to a relative path to the file from that path.

Greg Hewgill also suggested using pwd -P if you want to resolve symlinks in your current directory.

List file using ls command in Linux with full path

You can use

  ls -lrt -d -1 "$PWD"/{*,.*}   

It will also catch hidden files.

Show full path when using options

What about this trick...

ls -lrt -d -1 $PWD/{*,.*}

OR

ls -lrt -d -1 $PWD/*

I think this has problems with empty directories but if another poster has a tweak I'll update my answer. Also, you may already know this but this is probably be a good candidate for an alias given it's lengthiness.

[update] added some tweaks based on comments, thanks guys.

[update] as pointed out by the comments you may need to tweek the matcher expressions depending on the shell (bash vs zsh). I've re-added my older command for reference.

using 'ls' with options and folder and get full path

You can use find:

find PATH_TO_FOLDER -maxdepth 1 -type f -printf "%p\n"

List all files (with full paths) in a directory (and subdirectories), order by access time


find . -type f -exec ls -l {} \; 2> /dev/null | sort -t' ' -k +6,6 -k +7,7

This will find all files, and sort them by date and then time. You can then use awk or cut to extract the dates and files name from the ls -l output

How to get full path of a file?

Use readlink:

readlink -f file.txt

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 }'


Related Topics



Leave a reply



Submit