59

Many will found that this is repeating questions but i have gone through all the questions before asked about this topic but none worked for me.

I want to print full path name of the certain file format using ls command so far i found chunk of code that will print all the files in the directory but not full path.

for i in io.popen("ls /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7"):lines() do
  if string.find(i,"%.*$") then 
     print(i) 
  end
end

this will print out all the file in root diractory and subdiratory.

Output:

  0020111118223425.lvf
  2012
  2012 (2009).mp4
  3 Idiots
  Aashiqui 2
  Agneepath.mkv
  Avatar (2009)
  Captain Phillips (2013)
  Cocktail

I want output like:

  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/0020111118223425.lvf           [File in Root Directory]
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/2012/2012.mkv
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/2012 (2009).mp4                [File in Root Directory]
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/3 Idiots/3 Idiots.mkv
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/Aashiqui 2/Aashiqui 2.mkv
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/Avatar (2009)/Avatar (2009).mkv
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/Captain Phillips (2013).mkv
  /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/Cocktail/Cocktail.mkv

EDIT: I have used this all but its not working with my code in LUA.

when I used with my code it displays wrong directory

for i in io.popen("ls -d $PWD/* /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7"):lines() do
    if string.find(i,"%.*$") then
      print("/mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/"..i)
    end
  end

is not finding files in "/mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7" insted its prints the machines root directory files.

bummi
  • 27,123
  • 14
  • 62
  • 101
Prakash.DTI
  • 913
  • 2
  • 9
  • 19
  • Possible duplicate of [ls command: how can I get a recursive full-path listing, one line per file?](http://stackoverflow.com/questions/1767384/ls-command-how-can-i-get-a-recursive-full-path-listing-one-line-per-file) – Jeff Puckett Aug 26 '16 at 03:13

14 Answers14

79

You can use

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

It will also catch hidden files.

Totoro
  • 1,307
  • 12
  • 18
  • How this will work with my code. for i in io.popen("ls /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7"):lines() do if string.find(i,"%.*$") then print(i) end end – Prakash.DTI Dec 07 '14 at 07:02
  • 1
    @user1640175 `for i in io.popen("ls -lrt -d -1 /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/{*,.*}")` – Totoro Dec 07 '14 at 07:20
  • 1
    It doesn't work when you don't have access to a directory and can't expand the *. (For example, `sudo ls -d -1 /some/dir/i/cant/normally/access/*`) – davemyron Jan 07 '15 at 05:39
  • 3
    Please add quotas around $PWD `ls -lrt -d -1 "$PWD"/{*,.*}` (fixes directories with spaces). – pevik Oct 06 '15 at 08:29
  • 1
    Does not work for directories/paths with a lot of directory entries: `-bash: /bin/ls: Argument list too long` – rtx13 Jun 11 '20 at 12:52
  • 1
    and actually this is not `ls` but the shell expanding the glob. As rtx13 mentioned this going to fail when the shell is unable to. – grin Sep 28 '20 at 10:24
58

You can try this:

ls -d $PWD/*
heemayl
  • 39,294
  • 7
  • 70
  • 76
mgigirey
  • 1,027
  • 9
  • 13
  • 6
    ls -d "$PWD"/* to account for paths with spaces on them. –  Mar 25 '15 at 03:34
  • 2
    For empty directories, this throws `No such file or directory`. I would have expected to just see nothing like when doing `ls` on an empty directory, but no exception. – schreon Oct 06 '15 at 09:29
28

For listing everything with full path, only in current directory

find $PWD -maxdepth 1

Same as above but only matches a particular extension, case insensitive (.sh files in this case)

find $PWD -maxdepth 1 -iregex '.+\.sh'

$PWD is for current directory, it can be replaced with any directory

mydir="/etc/sudoers.d/" ; find $mydir -maxdepth 1

maxdepth prevents find from going into subdirectories, for example you can set it to "2" for listing items in children as well. Simply remove it if you need it recursive.

To limit it to only files, can use -type f option.

find $PWD -maxdepth 1 -type f
  • 3
    After trying all sorts of solutions, this is the only one I could find that would work 1)recursively while 2)referencing symlinks and 3)printing the entire path (which I need for `| grep`). Thanks! – Autumn Leonard May 15 '17 at 21:04
6

You could easily use the following to list only files:

ls -d -1 $PWD/*.*

the following to list directories:

ls -d -1 $PWD/**

the following to list everything (files/dirs):

ls -d -1 $PWD/**/*

More helpful options:

-d list directories not their content

-R recursive

-1 list one file per line

-l use long listing format

-a list all including entries starting with . and ..

-A list all but don't list implied . and ..

for more info, just type the following

ls --help 
Community
  • 1
  • 1
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
5

Print the full path (also called resolved path) with:

realpath README.md

In interactive mode you can use shell expansion to list all files in the directory with their full paths:

realpath *

If you're programming a bash script, I guess you'll have a variable for the individual file names.

Thanks to VIPIN KUMAR for pointing to the related readlink command.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
3

This prints all files, recursively, from the current directory.

find "$PWD" | awk /.ogg/ # filter .ogg files by regex
find "$PWD" | grep .ogg  # filter .ogg files by term
find "$PWD" | ack .ogg   # filter .ogg files by regex/term using https://github.com/petdance/ack2
1

try this -

readlink -f file.txt

VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34
1

How about:

 du -a [-b] [--max-depth=N] 

That should give you a file and directory listing, relative to your current location. You will get sizes as well (add the '-b' parameter if you want the sizes in bytes). The max-depth parameter may be necessary to "encourage" du to dive deeply enough into your file structure -- or to keep it from getting carried away.

YMMV!
-101-

0

There is more than one way to do that, the easiest I think would be:

find /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7

also this should work:

(cd /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7; ls | xargs -i echo `pwd`/{})
Jose Rey
  • 251
  • 1
  • 4
0

I have had this issue, and I use the following :

ls -dl $PWD/* | grep $PWD

It has always got me the listingI have wanted, but your mileage may vary.

0

This worked for me:

ls -rt -d -1 $PWD/{*,.*}
Frits
  • 7,341
  • 10
  • 42
  • 60
0

you just want the full path why not use the utility meant for that a combination of readlink and grep should get you what you want

grep -R  '--include=*.'{mkv,mp4} ? | cut -d ' ' -f3  | xargs readlink -e # 
the question mark should be replaced with the right pattern - this is almost right
# this is probably the best solution remove the grep part if you dont need a filter
find <dirname> | grep .mkv | xargs readlink -e |  xargs ls --color=auto # only matroska files in the dir and subdirs with nice color - also you can edit ls flags
find /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7 | grep .mkv 
find /mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7 | xargs grep -R  '--include=*.'{mkv,mp4} . | cut -d ' ' -f3 # I am sure you can do more with grep 
readlink -f `ls` # in the directory or 

omen23 5
  • 1
  • 3
0

If you want recursive listing of absolute pathnames within a subdirectory tree, use du and pipe into cut.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '22 at 00:17
-1

The ls command will only print the name of the file in the directory. Why not do something like

print("/mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/" + i)

This will print out the directory with the filename.

yxre
  • 3,576
  • 21
  • 20
  • This will not work with subdiractory. adn also i want implement in lua so i think you are telling me to do like print("/mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7/"..i) – Prakash.DTI Dec 07 '14 at 06:50
  • Your code was using a string literal. I only copied and pasted it. It would be trivial to make the changes to your lua script that would allow you to call this on any directory, and get your desired results. – yxre Dec 07 '14 at 07:36