Unix ls command: show full path when using options

ShellUnixCommand LineLs

Shell Problem Overview


I often use this list command in Unix (AIX / KSH):

ls -Artl

It displays the files as this:

-rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 test1.txt
-rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 test2.txt

I would like to modify the command such a way that the full path of the file is displayed. For example:

-rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 /usr/test1.txt
-rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 /usr/test2.txt

Any ideas?

I found several resolution methods using pwd or find but - as far as I see - this does not work work if I want to keep the ls options.

Shell Solutions


Solution 1 - Shell

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.

Solution 2 - Shell

Try this, works for me: ls -d /a/b/c/*

Solution 3 - Shell

Use this command:

ls -ltr /mig/mthome/09/log/*

instead of:

ls -ltr /mig/mthome/09/log

to get the full path in the listing.

Solution 4 - Shell

I use this command:

ls -1 | xargs readlink -f

Solution 5 - Shell

optimized from spacedrop answer ...

ls $(pwd)/*

and you can use ls options

ls -alrt $(pwd)/*

Solution 6 - Shell

simply use find tool.

find absolute_path

displays full paths on my Linux machine, while

find relative_path

will not.

Solution 7 - Shell

I wrote a shell script called fullpath that contains this code, use it everyday:

    #!/bin/sh
    for i in $* ; do
        echo $(pwd)/$i
    done

Put it somewhere in your PATH, and make it executable(chmod 755 fullpath) then just use
fullpath file_or_directory

Solution 8 - Shell

You can combine the find command and the ls command. Use the path (.) and selector (*) to narrow down the files you're after. Surround the find command in back quotes. The argument to -name is doublequote star doublequote in case you can't read it.

ls -lart `find . -type f -name "*" `

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTechnoCoreView Question on Stackoverflow
Solution 1 - ShellAndrew WhiteView Answer on Stackoverflow
Solution 2 - ShellQadeer Ahmed ShahView Answer on Stackoverflow
Solution 3 - ShellspacedropView Answer on Stackoverflow
Solution 4 - Shelluser3744148View Answer on Stackoverflow
Solution 5 - Shelljo_View Answer on Stackoverflow
Solution 6 - ShellkarstenView Answer on Stackoverflow
Solution 7 - ShellLawrence LopezView Answer on Stackoverflow
Solution 8 - ShellGordoView Answer on Stackoverflow