
我一直在處理大量圖像文件。其中一部分是將分佈在 1000 多個目錄中的所有圖像檔案類型直接移動到一個目錄中。有很多圖片名稱相同,但確實是不同的圖片。我使用了以下一行來做到這一點:
find . -type f -exec mv --backup=t '{}' /media/DATA-HDD/AllImages \;
我這樣做是為了使任何具有相同名稱的圖像都會獲得一個隱藏的備份文件,而不是覆蓋。它運行得很好,但現在我有另一個問題需要解決。
現在,我當然有很多像下面這樣的圖塊:
DSC_0616.NEF
DSC_0616.NEF.~1~
DSC_0616.NEF.~2~
我想要做的是運行一個命令(或腳本),通過將備份號添加為文件名後綴來重命名任何這些隱藏文件,並刪除 .~[bu#]~ 以使它們唯一文件名並且沒有隱藏。所以,就像這樣:
DSC_0616.NEF
DSC_0616_1.NEF
DSC_0616_2.NEF
我花了幾個小時的大部分時間嘗試研究自己嘗試這一點,但在我對該主題的知識範圍內找不到任何可以幫助我實現目標的東西。
答案1
只要您確定所有內容的命名與上述一致,一些正規表示式就可以透過 shell 腳本完成工作:
#!/bin/bash
# sets the file separator to be only newlines, in case files have spaces in them
IFS=$'\n'
for file in $(find . -type f); do
# parses just the number(s) between two tildes, and only at the end of the file
number=$(echo $file | grep -Eo "~[0-9]+~$" | sed s/'~'/''/g)
# if no match found, assume this is a "base" file that does not need to be renamed
if [ "$number" == "" ]; then
continue
fi
# parses the file name through "NEF", then deletes ".NEF"
filename=$(echo $file | grep -Eio "^.+\.NEF" | sed s/'\.NEF'/''/g )
if [ "$filename" == "" ]; then
continue
fi
mv -v $file $(echo "$filename"_"$number.NEF")
# if anything went wrong, exit immediately
if [ "$?" != "0" ]; then
echo "Unable to move file $file"
exit 1
fi
done
這也適用於目錄的降序排列,只需將腳本放在專案目錄樹頂部的工作目錄中並執行它即可。使用您提供的範例檔案運行:
###@###:~/project$ find . -type f
./DSC_0616.NEF.~8~
./DSC_0616.NEF.~5~
./DSC_0616.NEF.~1~
./DSC_0616.NEF.~7~
./DSC_0616.NEF.~3~
./DSC_0616.NEF.~4~
./DSC_0616.NEF.~9~
./DSC_0616.NEF.~2~
./DSC_0616.NEF.~6~
./lower_dir/DSC_0616.NEF.~8~
./lower_dir/DSC_0616.NEF.~5~
./lower_dir/DSC_0616.NEF.~1~
./lower_dir/DSC_0616.NEF.~7~
./lower_dir/DSC_0616.NEF.~3~
./lower_dir/DSC_0616.NEF.~4~
./lower_dir/DSC_0616.NEF.~9~
./lower_dir/DSC_0616.NEF.~2~
./lower_dir/DSC_0616.NEF.~6~
運行腳本後:
###@###:~/project$ find . -type f
./DSC_0616_1.NEF
./DSC_0616_3.NEF
./DSC_0616_7.NEF
./DSC_0616_5.NEF
./DSC_0616_2.NEF
./DSC_0616_9.NEF
./DSC_0616_6.NEF
./DSC_0616_8.NEF
./DSC_0616_4.NEF
./lower_dir/DSC_0616_1.NEF
./lower_dir/DSC_0616_3.NEF
./lower_dir/DSC_0616_7.NEF
./lower_dir/DSC_0616_5.NEF
./lower_dir/DSC_0616_2.NEF
./lower_dir/DSC_0616_9.NEF
./lower_dir/DSC_0616_6.NEF
./lower_dir/DSC_0616_8.NEF
./lower_dir/DSC_0616_4.NEF
答案2
在評論中,OP 要求一個適用於任何擴展名(不僅僅是 .NEF)的解決方案,而我也遇到了同樣的問題。 :p
這是一個比第一個答案更現代的巴什主義:
#!/usr/bin/env bash
# Renames the $1 directory's backup files (produced by `cp` or `mv` with the
# option `--backup=numbered`) such that "foo.png.~17~" becomes "foo_17.png"
function rename_backups()
{
for THING in "${1}"/*
do
if [[ -f "${THING}" &&
"${THING}" =~ ^.+\/+.+\..+~([0-9]+)~$ ]] # Take "foo.png.~17~".
then
# Get the "17".
local BACKUP_NUMBER=${BASH_REMATCH[1]}
# Remove the "~17~", leaving "foo.png".
local SANS_BACKUP_NUMBER="${THING%.*}"
# Get the filename, "foo", and the extension, "png".
local FILENAME="${SANS_BACKUP_NUMBER%.*}"
local EXT="${SANS_BACKUP_NUMBER##*.}"
# Rename to "foo_17.png".
local NEW_NAME="${FILENAME}_${BACKUP_NUMBER}.${EXT}"
while [[ -f "${NEW_NAME}" ]]
do
# If "foo_17.png" already exists for some reason, keep
# increasing the number until we find an unused one.
((++BACKUP_NUMBER))
NEW_NAME="${FILENAME}_${BACKUP_NUMBER}.${EXT}"
done
mv --no-clobber "${THING}" "${NEW_NAME}"
fi
done
}
rename_backups your/backup/directory/here/
以下版本也支持無延伸的文件:
#!/usr/bin/env bash
# Renames the $1 directory's backup files (produced by `cp` or `mv` with the
# option `--backup=numbered`) such that:
# - "foo.png.~17~" becomes "foo_17.png"
# - "bar.~17~" becomes "bar_17"
function rename_backups()
{
for THING in "${1}"/*
do
# We'll either have a file like "foo.png.~17~" or "bar.~17~".
if [[ -f "${THING}" && "${THING}" =~ ^.+~([0-9]+)~$ ]]
then
# Get the "17".
local BACKUP_NUMBER=${BASH_REMATCH[1]}
# Remove the "~17~", leaving, e.g., "foo.png".
local SANS_BACKUP_NUMBER="${THING%.*}"
# Get the filename, "foo", and the extension, "png".
local FILENAME="${SANS_BACKUP_NUMBER%.*}"
local EXT="${SANS_BACKUP_NUMBER##*.}"
if [[ "${FILENAME}" == "${SANS_BACKUP_NUMBER}" ]]
then
# This is a "bar.~17~" case, so rename to "bar_17".
local NEW_NAME="${FILENAME}_${BACKUP_NUMBER}"
while [[ -f "${NEW_NAME}" ]]
do
# If "bar_17" already exists for some reason, keep
# increasing the number until we find an unused one.
((++BACKUP_NUMBER))
NEW_NAME="${FILENAME}_${BACKUP_NUMBER}"
done
else
# This is a "foo.png.~17~" case, so rename to "foo_17.png".
local NEW_NAME="${FILENAME}_${BACKUP_NUMBER}.${EXT}"
while [[ -f "${NEW_NAME}" ]]
do
((++BACKUP_NUMBER))
NEW_NAME="${FILENAME}_${BACKUP_NUMBER}.${EXT}"
done
fi
mv --no-clobber "${THING}" "${NEW_NAME}"
fi
done
}
rename_backups your/backup/directory/here/