求脚本:列出一个文件系统内的所有硬链接

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
ebok
帖子: 852
注册时间: 2008-02-15 0:09

求脚本:列出一个文件系统内的所有硬链接

#1

帖子 ebok » 2011-02-21 11:44

一个文件系统内,例如单独挂载的/home,找出/home内所有的硬链接文件。
只知道硬链接的inode相同,ls -i 可以列出文件的inode。
有一个想法是先用ls -i 列出一个目录里的inode,然后用find -inum 来找是否有与某个文件相同inum的文件。
Somebody think they are full of niubility, so they play a zhuangbility, but only reflect their shability.
sk1418
帖子: 229
注册时间: 2007-07-01 17:36
系统: (En):System
来自: (En):address
联系:

Re: 求脚本:列出一个文件系统内的所有硬链接

#3

帖子 sk1418 » 2011-02-22 1:24

见以下例子,你修改要查找的目录为"/"就可以了

代码: 全选

7PLaptop::/tmp/delme2
kent$ ls -l                                                                                                                                                                           [ 18:21 ]
total 0

7PLaptop::/tmp/delme2
kent$ echo "hi there">o.txt                                                                                                                                                        [ 18:21 ]


7PLaptop::/tmp/delme2
kent$ ln o.txt hl.txt                                                                                                                                                              [ 18:21 ]

7PLaptop::/tmp/delme2
kent$ ln -s o.txt sl.txt                                                                                                                                                           [ 18:22 ]

7PLaptop::/tmp/delme2
kent$ touch {1..5}.txt                                                                                                                                                             [ 18:22 ]

7PLaptop::/tmp/delme2
kent$ ls -l                                                                                                                                                                        [ 18:22 ]
total 8
-rw-r--r-- 1 kent kent 0 2011-02-21 18:22 1.txt
-rw-r--r-- 1 kent kent 0 2011-02-21 18:22 2.txt
-rw-r--r-- 1 kent kent 0 2011-02-21 18:22 3.txt
-rw-r--r-- 1 kent kent 0 2011-02-21 18:22 4.txt
-rw-r--r-- 1 kent kent 0 2011-02-21 18:22 5.txt
-rw-r--r-- 2 kent kent 9 2011-02-21 18:21 hl.txt
-rw-r--r-- 2 kent kent 9 2011-02-21 18:21 o.txt
lrwxrwxrwx 1 kent kent 5 2011-02-21 18:22 sl.txt -> o.txt


7PLaptop::/tmp/delme2
kent$ find . -type f -links +1                                                                                                                                                     [ 18:23 ]
./o.txt
./hl.txt

顺便提一句,LZ的签名中有语法错误。 :em03
---
regards,

Kent
头像
ebok
帖子: 852
注册时间: 2008-02-15 0:09

Re: 求脚本:列出一个文件系统内的所有硬链接

#4

帖子 ebok » 2011-02-22 8:29

good!,多谢LS。没仔细地查看过find的用法,该打。
PS:语法错误什么的,LS,你懂的......
Somebody think they are full of niubility, so they play a zhuangbility, but only reflect their shability.
sqm0438
帖子: 4
注册时间: 2011-01-09 14:56

Re: 求脚本:列出一个文件系统内的所有硬链接

#5

帖子 sqm0438 » 2011-04-27 11:58

感谢楼上诸位 :em09
find的功能还是非常强大的。 :em04
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 求脚本:列出一个文件系统内的所有硬链接

#6

帖子 eexpress » 2011-04-27 12:11

find嘛。

要是谁有快速的找出某文件被ln到哪里的,这倒是有技术含量。
● 鸣学
头像
lilydjwg
论坛版主
帖子: 4249
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 求脚本:列出一个文件系统内的所有硬链接

#7

帖子 lilydjwg » 2011-04-27 13:08

eexpress 写了:find嘛。

要是谁有快速的找出某文件被ln到哪里的,这倒是有技术含量。
[python]
#!/usr/bin/env python3
# fileencoding=utf-8

'''
找出文件系统中所有链接数大于1的文件

2010年8月6日
'''

import os, sys, subprocess

def getfilelist(path):
'''使用 find 命令找出所有链接数大于1的文件,返回此列表'''
cmd = "find '%s' -type f -links +1 2> /dev/null" % path
return subprocess.getoutput(cmd).split('\n')

def findsamefile(l):
'''找出 l 中的文件中,哪些是同一文件'''
ret = []
while l:
curfile = l[0]
ret.append([curfile])
for i in l[1:]:
if os.path.samefile(curfile, i):
ret[-1].append(i)
l.remove(i)
del l[0]

return ret

def print2file(l, file=sys.stdout):
'''把 findsamefile 的结果格式化输出'''
sys.stdout = file
for i in l:
for j in i:
print(j)
print()
sys.stdout = sys.__stdout__

if __name__ == '__main__':
if len(sys.argv) not in (2, 3):
print('用法:%s 路径 [输出目标]' % os.path.basename(sys.argv[0]),
file=sys.stderr)
sys.exit(1)

if len(sys.argv) == 3:
f = open(sys.argv[2], 'w')
else:
f = None

l = getfilelist(sys.argv[1])
result = findsamefile(l)
if f:
print2file(result, f)
else:
print2file(result)
[/python]
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 求脚本:列出一个文件系统内的所有硬链接

#8

帖子 eexpress » 2011-04-27 13:15

这代码框太小了。

居然有人作这事情,难得。
● 鸣学
头像
lilydjwg
论坛版主
帖子: 4249
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 求脚本:列出一个文件系统内的所有硬链接

#9

帖子 lilydjwg » 2011-04-27 13:30

eexpress 写了:这代码框太小了。
你手动点下那个“展开”吧。。。
头像
黄美姬
帖子: 8428
注册时间: 2009-10-08 11:15
来自: 大城市铁岭

Re: 求脚本:列出一个文件系统内的所有硬链接

#10

帖子 黄美姬 » 2011-04-27 13:36

这代码框太小了 :em04
我们是命运的妓女,它把我们都嫖了

N卡驱动:http://www.nvidia.cn/Download/index.aspx?lang=cn
极品飞车:http://www.geforce.cn/optimize/optimal- ... ts-450-ops
孤岛危机优化设置:http://www.geforce.cn/optimize/optimal- ... tx-450-ops
:cp /etc/skel/.bashrc ~/
PS1="\[\e]2;\u@\H \w\a\e[32;1m\]\T$\[\e[0m\] "
http://cdimage.ubuntu.com/
http://releases.ubuntu.com/
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 求脚本:列出一个文件系统内的所有硬链接

#11

帖子 eexpress » 2011-04-27 13:39

擦。好高级哦。
我一直code的。 :em04
● 鸣学
回复