这与这个问题类似,但是我想包含相对于unix中当前目录的路径。如果我这样做:

ls -LR | grep .txt

它不包括完整路径。例如,我有如下的目录结构:

test1/file.txt
test2/file1.txt
test2/file2.txt

上面的代码将返回:

file.txt
file1.txt
file2.txt

如何使用标准Unix命令让它包含相对于当前目录的路径?


当前回答

在我的情况下,使用树命令

相对路径

tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
  echo $file
done

绝对路径

tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
  echo $file | sed -e "s|^.|$PWD|g"
done

其他回答

在我的情况下,使用树命令

相对路径

tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
  echo $file
done

绝对路径

tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
  echo $file | sed -e "s|^.|$PWD|g"
done

这很管用:

ls -R1 $PWD |当读取l;*) d=${l%:};;" ") d =;;*)返回"$d/$l";;esac;完成| grep -i ".txt"

但是它通过ls的解析“犯罪”来做到这一点,尽管这被GNU和Ghostscript社区认为是糟糕的形式。

从根目录“/”开始搜索,在文件系统中找到名为“filename”的文件。“文件名”

find / -name "filename" 

要使用find命令获得所需文件的实际完整路径文件名,请使用pwd命令:

find $(pwd) -name \*.txt -print

你可以用find代替:

find . -name '*.txt'