【问题标题】:lsof output get relative pathlsof 输出获取相对路径
【发布时间】:2022-06-10 21:47:46
【问题描述】:

我不擅长 bash,因此提出问题:

我想找到未锁定的文件并将其传递给 robocopy 命令

find . -type f -exec sh -c 'if ! lsof `readlink -f {}` > /dev/null; then echo `basename {}`; fi' \; | tr '\n' '\0' | rsync -avz --from0 --files-from=- ./ /data/uploads/ --dry-run

问题是上述命令只有文件名(u14216.jpg)而不是相对文件名(./comps/gps/pictures/)

完整路径为/www-data/pictures/u14216.jpg

我把echo basename {} 改成了 echo realpath {};

然后使用此代码获取正确的路径:/www-data/pictures/u14216.jpg

find . -type f -exec sh -c 'if ! lsof `readlink -f {}` > /dev/null; then `echo `realpath {}`; fi' \; | tr '\n' '\0'` 

但是当管道输出到 rsync 得到这个错误:

find . -type f -exec sh -c 'if ! lsof `readlink -f {}` > /dev/null; then echo `realpath {}`; fi' \; | tr '\n' '\0' | rsync -avz --from0 --files-from=- ./ /data/map/uploads/ --dry-run

building file list ...
rsync: link_stat "/www-data/www-data/pictures/u14216.jpg" failed: No such file or directory (2)

我在 /www-data folder 中,并且 rsync 以某种方式将另一个 /www-data 附加到完整路径

如何调整上述命令以获得完整路径,显然需要更改echo basename {}

【问题讨论】:

    标签: rsync lsof readlink


    【解决方案1】:

    据我了解您的问题。您想从上面的命令中获得完整路径,该命令实际上是从当前目录中查找元素。虽然它可以从 realpath {} 中实现,但如果你想明确地使用 basename {} 并从当前目录获得文件的完整路径,那么你可以使用 readlink 来实现相同的(确保它会得到最终解析名称。)

    找到 . -type f -exec sh -c '如果! lsof readlink -f {} >/dev/null;然后回显readlink -f basename {};菲';

    reference

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2014-05-24
      • 1970-01-01
      • 2011-12-11
      • 2011-08-11
      相关资源
      最近更新 更多