我正在编写一个shell脚本,将文件路径作为输入。

出于这个原因,我需要生成带有完整路径的递归文件清单。例如,文件栏的路径为:

/home/ken/foo/bar

但是,据我所知,ls和find都只给出相对路径列表:

./foo/bar   (from the folder ken)

这似乎是一个明显的要求,但我在find或ls手册页中看不到任何东西。

如何在shell中生成文件列表,包括它们的绝对路径?


当前回答

这对我很管用。但它没有按字母顺序排列。

find "$(pwd)" -maxdepth 1

这个命令也会按字母顺序列出隐藏文件。

ls -d -1 "$PWD/".*; ls -d -1 "$PWD/"*;

其他回答

ls -d "$PWD/"*

这只在当前目录中显示。它引用“$PWD”以防包含空格。

你可以这样做

ls -1 |xargs realpath

如果你需要指定一个绝对路径或相对路径,你也可以这样做

 ls -1 $FILEPATH |xargs realpath
for p in <either relative of absolute path of the directory>/*; do
    echo $(realpath -s $p)
done

stat

单个文件的绝对路径:

stat -c %n "$PWD"/foo/bar

试试这个:

find "$PWD"/

你得到工作目录中的绝对路径列表。