65

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
TechnoCore
  • 1,394
  • 2
  • 16
  • 21

8 Answers8

67

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.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • After your update the command does not work anymore for me. Message `cannot access /mydir/mysubdir/{*,.*}: No such file or directory` – TechnoCore Apr 07 '11 at 12:53
  • `.*` seems to match `.` and `..` on bash, but not on zsh. (`ls -A` doesn't show them, so my previous suggestion only fully works on zsh, and maybe even then my .zshrc has something to do with it) – StackExchange saddens dancek Apr 07 '11 at 12:55
  • One more thing: `ls -lrt -d -1 $PWD/{*,.[^.]*}` would not show `.` and `..` in sh, but in zsh shows dot-files twice. There `$PWD/*` seems to be sufficient. – bmk Apr 07 '11 at 13:07
  • Another challenge: you can't use wildcard filter, right? What if I want to see `ls -Artl *.log` with full path? – TechnoCore Apr 07 '11 at 14:12
  • you *could* wrap in a tiny shell script that changes the $PWD/* part. But that's probably for a different question. – Andrew White Apr 07 '11 at 14:15
  • I made an alias for the command, and now I use the alias all the time – Vijay Kumar Aug 24 '18 at 11:29
  • Sorry, I didn't mean to downvote this answer... but now I can't change it. Anyway... this works, especially the second form. The first form seems extravagant. – jrypkahauer Jul 28 '21 at 18:52
  • it works! Could you add more detail into the syntax of the command? – Govind Rai Feb 17 '22 at 01:28
40

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

sodd
  • 12,482
  • 3
  • 54
  • 62
17

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.

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
spacedrop
  • 187
  • 1
  • 3
  • 1
    I like this solution better for its simplicity and ease of use. I suppose the other solution might be usable if you created an alias out of it, but realistically, nobody is going to bother to type all that in. – jsarma Jul 13 '13 at 18:15
  • **Not display the full path**, try `ls -ltr . |more` in any place, shows only filenames, not absolute paths. – Peter Krauss Jan 12 '18 at 11:24
  • I like this solution, too because most of the other solutions on SO assumes the PO wants to ls the $PWD.. The key is really to list * inside the target directory instead of the directory alone. @PeterKrauss the emphasis is not on -ltr but it's on the glob *. Using your example, it would need to be ls -ltr *|more. – wr200m Apr 26 '21 at 17:56
8

I use this command:

ls -1 | xargs readlink -f
user3744148
  • 89
  • 1
  • 2
  • Depending on your file names might need to do `ls -Q1 | xargs readlink -f` But this worked great for me. – mlibby Aug 02 '14 at 15:52
  • 2
    that's a great solution, but what's the difference from a simple `readlink -f`? – user2141046 Sep 06 '15 at 13:19
  • 1
    you must have xargs aliased on your machine as 'xargs -L 1'. otherwise this doesn't work for more than one file. the command, in that case is `\ls -1 | xargs -L 1 readlink -f` – user2141046 Oct 06 '15 at 09:36
  • This is the **only answer** that really **display the full path** (absolute paths)! – Peter Krauss Jan 12 '18 at 11:25
  • 1
    on macOS, use the homebrew package `coreutils` (likely already installed) which will install `greadlink` instead. – Vivek Gani Nov 24 '18 at 21:14
  • This just sticks your CWD in front, e.g. if you ls /other/path while you are /somewhere, it will list for you the files under /other/path as though they are under /somewhere/. Bad answer IMO. – Steven Lu Dec 18 '18 at 00:04
5

optimized from spacedrop answer ...

ls $(pwd)/*

and you can use ls options

ls -alrt $(pwd)/*
jo_
  • 8,324
  • 1
  • 18
  • 14
  • 2
    backticks ```` are discourage now and new way to to command substitution is to use `$()` – Kiwy Jun 08 '18 at 15:02
  • @Kiwy ok ! doing this we get Andrew White 's answer (wich is very nice I agree) – jo_ Jun 11 '18 at 21:16
1

simply use find tool.

find absolute_path

displays full paths on my Linux machine, while

find relative_path

will not.

karsten
  • 639
  • 5
  • 13
  • 1
    Thanks, this worked for me. I used `find "/media/lubuntu/mount point/folder/"`. I used `find "/media/lubuntu/mount point/folder/" | grep "pattern"` to find the files I wanted. Any time I used that `$PWD/*` method as other user suggested it gave me errors. – Rublacava Jan 16 '21 at 14:38
0

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

-1

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 "*" `
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Gordo
  • 1