【问题标题】:Why does not the readlink show the full path?为什么 readlink 不显示完整路径?
【发布时间】:2014-12-09 19:14:03
【问题描述】:

我正在制作一个 shell 脚本来搜索具有特定名称的文件并显示它们的完整路径和大小。

例如:

/home/miglui/Desktop/SO/teste/1/teste.txt: 14 bytes

我遇到问题的段的代码是下一个:

for i in `find $1 -name $4 -type f -printf "%s "` ; do
    path=`readlink -f $4`
    echo "$path: $i bytes"
done

代码返回:

/home/miglui/Desktop/SO/teste.txt: 14 bytes
/home/miglui/Desktop/SO/teste.txt: 48 bytes
/home/miglui/Desktop/SO/teste.txt: 29 bytes

但应该返回:

/home/miglui/Desktop/SO/teste/1/teste.txt: 14 bytes
/home/miglui/Desktop/SO/teste/2/teste.txt: 48 bytes
/home/miglui/Desktop/SO/teste/teste.txt: 29 bytes

可能是什么问题?

【问题讨论】:

    标签: shell path readlink


    【解决方案1】:

    问题在于循环的每次迭代都会输出脚本的参数 4 ($4)。这与您的find 的结果无关。也许你想要更多这样的东西:

    while read size name; do
        path=`readlink -f $name`
        echo "$path: $size bytes"
    done < `find $1 -name $4 -type f -printf '%s %h/%f\n'`
    

    【讨论】:

    • 编辑了代码以适应包含内部空格的文件名。
    【解决方案2】:

    您正在检索 3 个不同文件的大小,但只报告您传入的参数的名称。

    试试这个:

    ( cd -P -- "$1" && find "$(pwd -P)" -name "$4" -type f -printf "$p: %s bytes\n" )
    
    • 在子 shell 中运行,因此 cd 不会影响当前 shell。

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 1970-01-01
      • 2023-01-18
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多