简体   繁体   English

使用 ls 命令在 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.我想使用 ls 命令打印特定文件格式的完整路径名,到目前为止,我发现有一段代码可以打印目录中的所有文件,但不是完整路径。

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: Output:

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

I want output like:我想要 output 像:

  /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.编辑:我已经使用了这一切,但它不适用于我在 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.在“/mnt/mediashare/net/192.168.1.220_STORAGE_1d1b7”中找不到文件,它会打印机器根目录文件。

You can use您可以使用

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

It will also catch hidden files.它还将捕获隐藏文件。

你可以试试这个:

ls -d $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)与上面相同,但只匹配特定的扩展名,不区分大小写(在这种情况下为 .sh 文件)

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

$PWD is for current directory, it can be replaced with any directory $PWD 用于当前目录,可以替换为任何目录

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. maxdepth防止 find 进入子目录,例如,您也可以将其设置为“2”以列出子目录中的项目。 Simply remove it if you need it recursive.如果您需要递归,只需将其删除。

To limit it to only files, can use -type f option.要将其限制为仅文件,可以使用-type f选项。

find $PWD -maxdepth 1 -type f

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 -d 列出目录而不是它们的内容

-R recursive -R 递归

-1 list one file per line -1 每行列出一个文件

-l use long listing format -l 使用长列表格式

-a list all including entries starting with . -a 列出所有包括以 开头的条目。 and ..和 ..

-A list all but don't list implied . - 列出所有但不列出隐含的。 and ..和 ..

for more info, just type the following欲了解更多信息,只需输入以下内容

ls --help 

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

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:在交互模式下,您可以使用 shell 扩展列出目录中的所有文件及其完整路径:

realpath *

If you're programming a bash script, I guess you'll have a variable for the individual file names.如果您正在编写 bash 脚本,我想您将拥有一个用于各个文件名的变量。

Thanks to VIPIN KUMAR for pointing to the related readlink command.感谢 VIPIN KUMAR 指出相关的readlink命令。

尝试这个 -

readlink -f file.txt

The ls command will only print the name of the file in the directory. ls命令只会打印目录中文件的名称。 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.这将打印出带有文件名的目录。

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

I have had this issue, and I use the following :我遇到了这个问题,我使用以下内容:

ls -dl $PWD/* | ls -dl $PWD/* | grep $PWD grep $PWD

It has always got me the listingI have wanted, but your mileage may vary.它总是让我得到我想要的清单,但你的里程可能会有所不同。

这对我有用:

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

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你只想要完整的路径,为什么不使用这个实用程序,readlink 和 grep 的组合应该能让你得到你想要的

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 

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).您还将获得大小(如果您想要以字节为单位的大小,请添加“-b”参数)。 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. max-depth 参数可能是必要的,以“鼓励” du 深入了解您的文件结构——或者防止它被带走。

YMMV!天啊!
-101- -101-

If you want recursive listing of absolute pathnames within a subdirectory tree, use du and pipe into cut.如果要递归列出子目录树中的绝对路径名,请使用 du 和 pipe 进行剪切。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在linux中使用ls和-R选项结合grep显示文件的完整路径 - How to display full path of file using ls in linux with -R option in conjunction with grep linux ls 命令列出文件名中包含某些字符的文件 - linux ls command to list file with certain characters in filename 使用带有选项和文件夹的“ ls”并获取完整路径 - using 'ls' with options and folder and get full path 对特定的路径和文件类型使用LS,而无需重新获得完整的文件路径? - Using LS for a specific path & file type without returing full file path? 在 linux 中,如何获取我正在使用的命令的完整路径? - In linux, how to get the full path of a command I am using? FTP“ls -lt”命令:它如何列出Linux FTP和Windows FTP服务器的文件详细信息? - FTP “ls -lt” command: How does it list the file details for Linux FTP and Windows FTP server? 在Linux中执行ls命令 - Execution of ls command in Linux 如何使用“查找”命令获取按时间排序的列表(ASC / DESC)。 型f -ls”在Linux中? - How to Get List Sorted by Time(ASC/DESC) Using The Command “find . -type f -ls” in Linux? 在 Home 目录中使用 ls 命令列出 Home 子目录中的文件 - Linux - Using ls command to list files in subdirectory of Home while in Home directory - Linux 将来自Linux“ ls”命令的文件名存储到单独的数组/列表元素中 - Store filenames from linux “ls” command into separate array/list elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM