bash ls 命令:如何获得递归完整路径列表,每个文件一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1767384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ls command: how can I get a recursive full-path listing, one line per file?
提问by dreftymac
How can I get ls to spit out a flat list of recursive one-per-line paths?
我怎样才能让 ls 吐出一个单行递归路径的平面列表?
For example, I just want a flat listing of files with their full paths:
例如,我只想要一个带有完整路径的文件的平面列表:
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt
ls -a1
almost does what I need, but I do not want path fragments, I want full paths.
ls -a1
几乎可以满足我的需要,但我不想要路径片段,我想要完整的路径。
采纳答案by ghostdog74
If you really want to use ls
, then format its output using awk:
如果您真的想使用ls
,请使用 awk 格式化其输出:
ls -R /path | awk '
/:$/&&f{s=find .
find /home/dreftymac
;f=0}
/:$/&&!f{sub(/:$/,"");s=find . -type f
find /home/dreftymac -type f
;f=1;next}
NF&&f{ print s"/"find "$PWD"
}'
回答by stefanB
Use find:
使用查找:
tree -fi |grep -v \>
If you want files only (omit directories, devices, etc):
如果您只需要文件(省略目录、设备等):
tree -fi | \
grep -v \> | \
while read first ; do
file ${first}
done | \
while read second; do
echo ${second} | grep ASCII
done
回答by others
ls -ld $(find .)
ls -ld $(find .)
if you want to sort your output by modification time:
如果要按修改时间对输出进行排序:
ls -ltd $(find .)
ls -ltd $(find .)
回答by Ivan Alegre
Try the following simpler way:
尝试以下更简单的方法:
find "$PWD" -type f
回答by kerkael
Best command is: tree -fi
最好的命令是: tree -fi
In order to use the files but not the links, you have to remove >
from your output:
为了使用文件而不是链接,您必须>
从输出中删除:
find "$PWD" -type d
If you want to know the nature of each file, (to read only ASCII files for example) with two while
s:
如果你想知道每个文件的性质,(例如只读取 ASCII 文件)有两个while
s:
find "$PWD"
回答by Godfather
Oh, really a long list of answers. It helped a lot and finally, I created my own which I was looking for :
哦,真是一长串答案。它有很大帮助,最后,我创建了自己正在寻找的:
To List All the Files in a directory and its sub-directories:
列出目录及其子目录中的所有文件:
ls -R1 /path |
while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done
To List All the Directories in a directory and its sub-directories:
列出目录及其子目录中的所有目录:
du -a
To List All the Directories and Files in a directory and its sub-directories:
列出目录及其子目录中的所有目录和文件:
##代码##回答by Justin Johnson
I don't know about the full path, but you can use -R
for recursion. Alternatively, if you're not bent on ls
, you can just do find *
.
我不知道完整路径,但您可以-R
用于递归。或者,如果你不坚持ls
,你可以做find *
。
回答by Idelic
Using no external commands other than ls:
不使用 ls 以外的外部命令:
##代码##回答by Rob D
Handy for some limited appliance shells where find/locate aren't available.
对于某些无法找到/定位的有限设备外壳非常方便。
回答by Dmitry
find / will do the trick
找到/会解决问题