在文件夹层次结构内搜索重复的文件名?

在文件夹层次结构内搜索重复的文件名?

我有一个名为 的文件夹img,该文件夹有许多层级的子文件夹,所有子文件夹都包含图像。我要将它们导入到图像服务器中。

通常情况下,只要图像(或任何文件)位于不同的目录路径或具有不同的扩展名,它们就可以具有相同的名称。但是,我要将它们导入到的图像服务器要求所有图像名称都是唯一的(即使扩展名不同)。

例如,图像background.pngbackground.gif文件是不允许的,因为即使它们有不同的扩展名,但文件名仍然相同。即使它们位于不同的子文件夹中,它们仍然必须是唯一的。

所以我想知道我是否可以在文件夹中进行递归搜索img以查找具有相同名称(不包括扩展名)的文件列表。

是否有命令可以做到这一点?

答案1

find . -mindepth 1 -printf '%h %f\n' | sort -t ' ' -k 2,2 | uniq -f 1 --all-repeated=separate | tr ' ' '/'

正如注释所述,这也会找到文件夹。以下是将其限制为文件的命令:

find . -mindepth 1 -type f -printf '%p %f\n' | sort -t ' ' -k 2,2 | uniq -f 1 --all-repeated=separate | cut -d' ' -f1

答案2

FSlint 安装 fslint是一个多功能的重复查找器,其中包括查找重复名称的功能:

FSlint

Ubuntu 的 FSlint 软件包强调图形界面,但正如FSlint 常见问题解答命令行界面可通过 中的程序使用/usr/share/fslint/fslint/。使用--help文档选项,例如:

$ /usr/share/fslint/fslint/fslint --help
File system lint.
A collection of utilities to find lint on a filesystem.
To get more info on each utility run 'util --help'.

findup -- find DUPlicate files
findnl -- find Name Lint (problems with filenames)
findu8 -- find filenames with invalid utf8 encoding
findbl -- find Bad Links (various problems with symlinks)
findsn -- find Same Name (problems with clashing names)
finded -- find Empty Directories
findid -- find files with dead user IDs
findns -- find Non Stripped executables
findrs -- find Redundant Whitespace in files
findtf -- find Temporary Files
findul -- find possibly Unused Libraries
zipdir -- Reclaim wasted space in ext2 directory entries
$ /usr/share/fslint/fslint/findsn --help
find (files) with duplicate or conflicting names.
Usage: findsn [-A -c -C] [[-r] [-f] paths(s) ...]

If no arguments are supplied the $PATH is searched for any redundant
or conflicting files.

-A reports all aliases (soft and hard links) to files.
If no path(s) specified then the $PATH is searched.

If only path(s) specified then they are checked for duplicate named
files. You can qualify this with -C to ignore case in this search.
Qualifying with -c is more restictive as only files (or directories)
in the same directory whose names differ only in case are reported.
I.E. -c will flag files & directories that will conflict if transfered
to a case insensitive file system. Note if -c or -C specified and
no path(s) specifed the current directory is assumed.

使用示例:

$ /usr/share/fslint/fslint/findsn /usr/share/icons/ > icons-with-duplicate-names.txt
$ head icons-with-duplicate-names.txt 
-rw-r--r-- 1 root root    683 2011-04-15 10:31 Humanity-Dark/AUTHORS
-rw-r--r-- 1 root root    683 2011-04-15 10:31 Humanity/AUTHORS
-rw-r--r-- 1 root root  17992 2011-04-15 10:31 Humanity-Dark/COPYING
-rw-r--r-- 1 root root  17992 2011-04-15 10:31 Humanity/COPYING
-rw-r--r-- 1 root root   4776 2011-03-29 08:57 Faenza/apps/16/DC++.xpm
-rw-r--r-- 1 root root   3816 2011-03-29 08:57 Faenza/apps/22/DC++.xpm
-rw-r--r-- 1 root root   4008 2011-03-29 08:57 Faenza/apps/24/DC++.xpm
-rw-r--r-- 1 root root   4456 2011-03-29 08:57 Faenza/apps/32/DC++.xpm
-rw-r--r-- 1 root root   7336 2011-03-29 08:57 Faenza/apps/48/DC++.xpm
-rw-r--r-- 1 root root    918 2011-03-29 09:03 Faenza/apps/16/Thunar.png

答案3

将其保存到名为duplicates.py

#!/usr/bin/env python

# Syntax: duplicates.py DIRECTORY

import os, sys

top = sys.argv[1]
d = {}

for root, dirs, files in os.walk(top, topdown=False):
    for name in files:
        fn = os.path.join(root, name)
        basename, extension = os.path.splitext(name)

        basename = basename.lower() # ignore case

        if basename in d:
            print(d[basename])
            print(fn)
        else:
            d[basename] = fn

然后使文件可执行:

chmod +x duplicates.py

例如像这样运行:

./duplicates.py ~/images

它应该输出具有相同基本名称 (1) 的文件对。用 python 编写,您应该能够修改它。

答案4

这是 bname:

#!/bin/bash
#
#  find for jpg/png/gif more files of same basename 
#
# echo "processing ($1) $2"
bname=$(basename "$1" .$2)
find -name "$bname.jpg" -or -name "$bname.png"

使其可执行:

chmod a+x bname 

调用它:

for ext in jpg png jpeg gif tiff; do find -name "*.$ext" -exec ./bname "{}" $ext ";"  ; done

优点:

  • 它简单直接,因此可扩展。
  • 据我所知,处理文件名中的空格、制表符、换行符和分页符。(假设扩展名中没有这些内容)。

缺点:

  • 它总是查找文件本身,如果它为 a.jpg 找到 a.gif,它也会为 a.gif 找到 a.jpg。因此,对于 10 个具有相同基本名称的文件,它最终会找到 100 个匹配项。

相关内容