linux - 将文件的完整路径添加到文本文件的每一行

标签 linux bash

我可以使用以下命令将文件名添加到名为 test1 的文本文件中的每一行:

nawk '{print FILENAME"\,"$0}' test1

我想将文件的完整路径添加到文件的每一行。

最佳答案

使用 readlink 应该适合你。

更多信息 here

To get the full path use:

readlink -f relative/path/to/file

If readlink -f is not available on your system you can use this:

function myreadlink() {
  (
  cd $(dirname $1)         # or  cd ${1%/*}
  echo $PWD/$(basename $1) # or  echo $PWD/${1##*/}
  )
}

编辑:

如评论中所述,最好编辑 myreadlink 函数以使用 pushd $(dirname $1) ... popd 保存工作目录,执行后恢复。

关于linux - 将文件的完整路径添加到文本文件的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45763500/

相关文章:

mysql - 如何在shell中使用mysql字段

linux - 删除 bash 脚本中除最新 3 个以外的所有文件

linux - 用于查找可执行文件路径的 Bash 脚本

linux - 如何用空格解释 grep 的这种行为?

linux - 如果在一台 x86 主机上运行 2 个 type-2 VMX hypervisor 会怎样?

Python - 从窗口获取文本

bash - bash 脚本中的 mkdir 错误

bash - centos+nodejs+永远(启停)

android - android bash shell脚本的问题

linux - 如何在共享主机帐户上安装 Redis