【问题标题】:Resolve name by inode in current direcory通过当前目录中的 inode 解析名称
【发布时间】:2014-05-09 03:58:50
【问题描述】:

如何通过以下脚本中当前目录中的给定 inode 解析名称,该脚本打印指向作为参数传递给脚本的指定文件的符号链接的所有文件名。该列表应按 ctime 排序。

#!/usr/bin/ksh

IFS="`printf '\n\t'`"

USAGE="usage: symlink.sh <file>"

get_ctime() {
    perl -se 'use File::stat; $file=lstat($filename); print $file->ctime' -- -filename="$1"
}

stat_inode() {
    perl -se 'use File::stat; $file=stat($filename); if (defined $file) { print $file->ino; }' -- -filename="$1"
}

lstat_inode() {
    perl -se 'use File::stat; $file=lstat($filename); if (defined $file) { print $file->ino; }' -- -filename="$1"
}


if [ $# -eq 0 ]; then 
    echo "$USAGE"
    exit 1
fi

FILE_NAME="$1"
FILE_INODE=$(stat_inode "$FILE_NAME")

if [ ! -e "$FILE_NAME" ]; then
    echo "no such file \"$FILE_NAME\""
    exit 1
fi


for LINK in ./* ./.[!.]*  ;do
    if [ -L "$LINK" ]; then
    TARGET_INODE=$(stat_inode "$LINK")
    if [ ! -z "$TARGET_INODE" ]; then
        if [ "$FILE_INODE" -eq "$TARGET_INODE" ]; then
        echo $(get_ctime "$LINK") $(lstat_inode "$LINK");
        fi
    fi
    fi
done | sort -nk1 | awk '{print $2}'

基本上,我想通过管道将 awk 传递给某种查找函数,如下所示: | awk ' ' |查找

如果有人提出一种更优雅的方式来完成这项任务,我将不胜感激。

操作系统:SunOS 5.10 壳牌:KSH

【问题讨论】:

    标签: perl awk solaris ksh


    【解决方案1】:

    这样的?

    $ find . -maxdepth 1 -inum 2883399 
    ./.jshintrc
    $
    

    或:

    $ echo 2883399 | xargs -IX find . -maxdepth 1 -inum X
    ./.jshintrc
    $
    

    【讨论】:

    • 谢谢。这正是我一直在寻找的。 (但是 -maxpath 没有在我使用的 find 中实现。你想用这个 -maxdepth 1 完成什么?)
    • -maxdepth 限制 find 的搜索,并使其更快。在你中间,手头有一个 inode,它不需要做太多搜索,所以加速可能不存在。如果没有它也能正常工作,那就开派对吧。 :-)
    猜你喜欢
    • 2011-06-04
    • 2012-08-04
    • 1970-01-01
    • 2012-04-17
    • 2017-04-14
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    相关资源
    最近更新 更多