我如何吐出一行递归路径的平面列表?

例如,我只想要一个文件的完整路径的平面列表:

/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几乎满足了我的需要,但我不想要路径片段,我想要完整的路径。


当前回答

如果将目录作为相对路径传递,则需要在调用find之前将其转换为绝对路径。在下面的例子中,目录作为第一个参数传递给脚本:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find "$directory"

其他回答

使用找到:

find .
find /home/dreftymac

如果你只想要文件(忽略目录,设备等):

find . -type f
find /home/dreftymac -type f

我知道文件名,但也想知道目录。

find $PWD | fgrep filename

在Mac OS 10.12.1中完美运行

最好的命令是tree -fi

打印每个文件的完整路径前缀 -i不打印缩进

e.g.

$ tree -fi
.
./README.md
./node_modules
./package.json
./src
./src/datasources
./src/datasources/bookmarks.js
./src/example.json
./src/index.js
./src/resolvers.js
./src/schema.js

为了使用文件而不是链接,你必须从输出中删除>:

tree -fi |grep -v \>

如果你想知道每个文件的性质,(例如只读取ASCII文件)尝试while循环:

tree -fi |
grep -v \> |
while read -r first ; do 
    file "${first}"
done |
grep ASCII

除了ls,不使用任何外部命令:

ls -R1 /path | 
  while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done

如果将目录作为相对路径传递,则需要在调用find之前将其转换为绝对路径。在下面的例子中,目录作为第一个参数传递给脚本:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find "$directory"