List file using ls command in Linux with full path

2020-02-17 08:55发布

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.

10条回答
smile是对你的礼貌
2楼-- · 2020-02-17 09:34

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楼-- · 2020-02-17 09:35

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 
查看更多
Lonely孤独者°
4楼-- · 2020-02-17 09:36

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.

查看更多
看我几分像从前
5楼-- · 2020-02-17 09:36

This worked for me:

ls -rt -d -1 $PWD/{*,.*}
查看更多
登录 后发表回答