简体   繁体   English

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

[英]In an OS X shell script, how can I get a file path from its inode?

I have a list of files as :<volume name>:<directory inode>:<file name> . 我有一个文件列表,如:<volume name>:<directory inode>:<file name> For example, :Foo:33103829:IMG_2837.JPG . 例如, :Foo:33103829:IMG_2837.JPG How can I get the file path? 如何获取文件路径?

I found an answer here that looks to be exactly what I want, but I can't get it to work. 我在这里找到了一个答案该答案似乎正是我想要的,但我无法使其正常工作。 The answer says that on OS X there is a 'magic' directory /.vol that works on inodes. 答案表明,在OS X上,存在一个可在inode上运行的'magic'目录/.vol ls tells me that /.vol exists, but doesn't contain anything, even when accessed by inodes: ls告诉我/.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)

Turns out the problem is that I was getting the inode number of the volume from ls -i which isn't usable to access via /.vol , which needs the device ID. 原来的问题是我从ls -i获取了卷的/.vol节点号,该卷的/.vol节点号无法通过/.vol来访问,而需要设备ID。 When I instead get the device ID of the volume using stat (as I saw in an answer here ), it works. 当我改为使用stat获取卷的设备ID时(如我在此处的答案中所见),它可以工作。

# 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$

You can use this script 您可以使用此脚本

#!/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
  • save it as v2p.sh 将其另存为v2p.sh
  • chmod 755 v2p.sh
  • use it as ./v2p.sh < your_input_file >output_file 用作./v2p.sh < your_input_file >output_file

from the input like 从像这样的输入

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

produces 产生

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

eg adds the path to the end as an new colon delimited field 例如,将路径末尾添加为新的冒号分隔字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM