[linux] List file using ls command in Linux with full path

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.

This question is related to linux bash shell command-line lua

The answer is


You can try this:

ls -d $PWD/*

try this -

readlink -f file.txt


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 


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.


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.


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

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`/{})

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-


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.


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 

This worked for me:

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

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

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to lua

How do I append to a table in Lua List file using ls command in Linux with full path Check if a string isn't nil or empty in Lua Roblox Admin Command Script How to add a "sleep" or "wait" to my Lua Script? How to iterate through table in Lua? Not Equal to This OR That in Lua How to read data from a file in Lua Lua string to int How to check if matching text is found in a string in Lua?