在OS X shell脚本中,如何从其inode获取文件路径?

时间:2014-09-25 22:26:01

标签: macos shell inode

我有一个文件列表:<volume name>:<directory inode>:<file name>。例如,:Foo:33103829:IMG_2837.JPG。我怎样才能获得文件路径?

我在这里发现an answer看起来正是我想要的,但我无法让它发挥作用。 The answer说在OS X上有一个魔法&#39;适用于inode的目录/.volls告诉我/.vol 存在,但即使在由inode访问时也不包含任何内容:

# verify that /.vol exists:
~$ ls -ld /.vol
drwxr-xr-x@ 2 root  wheel  68 May 18  2009 /.vol/

# get inode of volume Foo
~$ ls -id /Volumes/Foo
32659974 /Volumes/Foo@

# access volume Foo by idnode
~$ ls /.vol/32659974
ls: /.vol/32659974: No such file or directory

# access volume Foo by idnode
~$ cd /.vol/32659974
cd: /.vol/32659974: No such file or directory

# access volume by inode using GetFileInfo
~$ GetFileInfo /.vol/32659974
GetFileInfo: could not refer to file (-43)

2 个答案:

答案 0 :(得分:2)

原来问题在于,我从ls -i获取了无法通过/.vol访问的卷的inode编号,这需要设备ID。当我使用stat获取卷的设备ID时(正如我在an answer here中看到的那样),它可以正常工作。

# ls -id returns inode as '32659974'
~$ ls -id /Volumes/Foo
32659974 /Volumes/Foo@

# stat returns device ID as '234881026' and confirms inode is '32659974'
~$ stat /Volumes/Foo
234881026 32659974 lrwxr-xr-x 1 root admin 0 1 "Sep 16 14:31:52 2014" "Sep 16 14:31:52 2014" "Sep 16 14:31:52 2014" "Sep 16 14:31:52 2014" 4096 8 0 /Volumes/Foo

# access file using ./vol/<device ID>/<inode>
~$ cd /.vol/234881026/1017800
:../Prague 2011 March$

答案 1 :(得分:0)

您可以使用此脚本

#!/bin/sh
export PATH=/bin:/usr/bin
while IFS=: read -r vol inode name
do
    [[ -e "/Volumes/$vol" ]] || continue
    eval $(stat -s "/Volumes/$vol")
    fpath=$(GetFileInfo "/.vol/$st_dev/$inode" | perl -ne 'print "$2\n" if m/^(directory|file):\s*"(.*)"/;')
    printf "%s:%s:%s:%s" "$vol" $inode "$name" "$fpath"
    bname=$(basename "$fpath")
    [[ "$bname" == "$name" ]] && printf "\n" || printf " #DIFF NAMES\n"
done
  • 将其另存为v2p.sh
  • chmod 755 v2p.sh
  • 将其用作./v2p.sh < your_input_file >output_file

来自输入

jhdd:60533283:aliases
jhdd:60526259:apache2
Tunnelblick Uninstaller:19:Online Documentation.webloc
jhdd:68032325:auto_smb
jhdd:60526617:bashrc

产生

jhdd:60533283:aliases:/private/etc/postfix/aliases
jhdd:60526259:apache2:/private/etc/apache2
Tunnelblick Uninstaller:19:Online Documentation.webloc:/Volumes/Tunnelblick Uninstaller/Online Documentation.webloc
jhdd:68032325:auto_smb:/private/etc/auto_smb
jhdd:60526617:bashrc:/private/etc/bashrc

e.g。将路径添加到结尾作为新的冒号分隔字段