如何将文件重命名为其创建日期和哈希值组合以获得唯一的文件名?

如何将文件重命名为其创建日期和哈希值组合以获得唯一的文件名?

我有一堆.JPG,.NEF.MOV用我的制作的文件尼康单反相机,命名为DSC_0001.JPGDSC_0002.JPG等等。达到一定数量后,将创建新目录并重新开始计数,这会导致当我将它们移至一个大目录时出现重复。

我想出了如何使用循环和bash在创建日期后命名它们:forexiftool

for i in *.JPG
do
  mv -nv "$i" "$(exiftool -d "%Y%m%d-%H%M%S" -CreateDate "$i" | awk '{print $4".jpg"}')
done

这种方法产生了另一个问题,我意识到得太晚了,并且在此过程中丢失了大约 100 个文件 - 有时一秒钟内拍摄了不止一张照片,而且我找不到一种简单的方法来连接小于一秒的单元,保持文件名可读。

所以我想附加文件的校验和,例如 CRC32,一些简短且相当随机的东西,并造成了这个怪物:

sum="$(crc32 * | cut -d' ' -f1)"

for i in *.jpg
do

        mv -nv "$i" "$(exiftool -d %Y%m%d-%H%M%S" -CreateDate "$i" | awk '{print $4"-"}')$sum.jpg"

done

它适用于目录中的一个文件,但在我拥有的所有照片的主目录中应用时产生了这些类型的错误消息:

mv: failed to access '504a5b89'$'\t''DSC_0001.NEF'$'\n''629a031e'$'\t''DSC_0002.NEF'$'\n''1af2720c'$'\t''DSC_0003.NEF'$'\n''852f62de'$'\t''DSC_0004.NEF'$'\n''874bd1f0'$'\t''DSC_0005.NEF'$'\n''f3fceda8'$'\t''DSC_0006.NEF'$'\n''28207fa2'$'\t''DSC_0007.NEF'$'\n''046ca494'$'\t''DSC_0008.NEF'$'\n''abf11428'$'\t''DSC_0009.NEF'$'\n''479e728d'$'\t''DSC_0010.NEF'$'\n''8df21237'$'\t''DSC_0011.NEF'$'\n''77663953'$'\t''DSC_0012.NEF'$'\n''7d9871c7'$'\t''DSC_0013.NEF'$'\n''85608081'$'\t''DSC_0014.NEF'$'\n''49246d62'$'\t''DSC_0016.NEF'$'\n''a2f927bc'$'\t''DSC_0017.NEF'$'\n''33277700'$'\t''DSC_0018.NEF'$'\n''76410b41'$'\t''DSC_0019.NEF'$'\n''c30d7d1c'$'\t''DSC_0020.NEF'$'\n''de239afe'$'\t''DSC_0021.NEF'$'\n''c9a1999e'$'\t''DSC_0022.NEF'$'\n''5e124a8a'$'\t''DSC_0023.NEF'$'\n''8e5eb670'$'\t''DSC_0024.NEF'$'\n''a26eb4cb'$'\t''DSC_0025.NEF'$'\n''a0f0444e'$'\t''DSC_0026.NEF'$'\n''7b686084'$'\t''DSC_0027.NEF'$'\n''eff42939'$'\t''DSC_0028.NEF'$'\n''7ed21f22'$'\t''DSC_0029.NEF'$'\n''85f8f493'$'\t''DSC_0030.NEF'$'\n''97227a4f'$'\t''DSC_0031.NEF'$'\n''a0a96fb2'$'\t''DSC_0032.NEF'$'\n''e82c47e6'$'\t''DSC_0033.NEF'$'\n''55f5e030'$'\t''DSC_0034.NEF'$'\n''4fd188b4'$'\t''DSC_0035.NEF'$'\n''357d0757'$'\t''DSC_0036.NEF'$'\n''87cd39d6'$'\t''DSC_0037.NEF'$'\n''34b6fb45'$'\t''DSC_0038.NEF'$'\n''ef6f09f0'$'\t''DSC_0039.NEF'$'\n''b8d12e00'$'\t''DSC_0040.NEF'$'\n''bd2d4cae'$'\t''DSC_0041.NEF'$'\n''55dc67c0'$'\t''DSC_0042.NEF'$'\n''08ba39e4'$'\t''DSC_0043.NEF'$'\n''bcfefc4a'$'\t''DSC_0044.NEF'$'\n''ce69e1fd'$'\t''DSC_0045.NEF'$'\n''275af151'$'\t''DSC_0046.NEF'$'\n''6a30a875'$'\t''DSC_0047.NEF'$'\n''3aefb843'$'\t''DSC_0048.NEF'$'\n''674458fb'$'\t''DSC_0049.NEF'$'\n''aab60fc7'$'\t''DSC_0050.NEF'$'\n''13502f06'$'\t''DSC_0051.NEF'$'\n''ac3eba1e'$'\t''DSC_0052.NEF'$'\n''c5052ad9'$'\t''DSC_0053.NEF'$'\n''80c3ee64'$'\t''DSC_0054.NEF'$'\n''d17f3177'$'\t''DSC_0055.NEF'$'\n''53f51ccf'$'\t''DSC_0056.NEF'$'\n''f91427af'$'\t''DSC_0057.NEF'$'\n''0d596f23'$'\t''DSC_0058.NEF'$'\n''fc378e62'$'\t''DSC_0059.NEF'$'\n''c72be5b3'$'\t''DSC_0060.NEF'$'\n''8bf29954'$'\t''DSC_0061.NEF'$'\n''f1193bbf'$'\t''DSC_0062.NEF'$'\n''d4460f24'$'\t''DSC_0063.NEF'$'\n''1e7b1c07'$'\t''DSC_0064.NEF'$'\n''3cc1cbd2'$'\t''DSC_0065.NEF'$'\n''ae935236'$'\t''DSC_0066.NEF'$'\n''f0ff02c1'$'\t''DSC_0067.NEF'$'\n''a16c6e58'$'\t''DSC_0068.NEF'$'\n''57ae8019'$'\t''DSC_0069.NEF'$'\n''82fc94df'$'\t''DSC_0070.NEF'$'\n''2ac41f26'$'\t''DSC_0071.NEF'$'\n''76b0493a'$'\t''DSC_0072.NEF'$'\n''9791ccf7'$'\t''DSC_0073.NEF'$'\n''eac3e7aa'$'\t''DSC_0074.NEF'$'\n''14f7c55c'$'\t''DSC_0075.NEF'$'\n''86df85b0'$'\t''DSC_0076.NEF'$'mv: failed to access '504a5b89'$'\t''DSC_0001.NEF'$'\n''629a031e'$'\t''DSC_0002.NEF'$'\n''1af2720c'$'\t''DSC_0003.NEF'$'\n''852f62de'$'\t''DSC_0004.NEF'$'\n''874bd1f0'$'\t''DSC_0005.NEF'$'\n''f3fceda8'$'\t''DSC_0006.NEF'$'\n''28207fa2'$'\t''DSC_0007.NEF'$'\n''046ca494'$'\t''DSC_0008.NEF'$'\n''abf11428'$'\t''DSC_0009.NEF'$'\n''479e728d'$'\t''DSC_0010.NEF'$'\n''8df21237'$'\t''DSC_0011.NEF'$'\n''77663953'$'\t''DSC_0012.NEF'$'\n''7d9871c7'$'\t''DSC_0013.NEF'$'\n''85608081'$'\t''DSC_0014.NEF'$'\n''49246d62'$'\t''DSC_0016.NEF'$'\n''a2f927bc'$'\t''DSC_0017.NEF'$'\n''33277700'$'\t''DSC_0018.NEF'$'\n''76410b41'$'\t''DSC_0019.NEF'$'\n''c30d7d1c'$'\t''DSC_0020.NEF'$'\n''de239afe'$'\t''DSC_0021.NEF'$'\n''c9a1999e'$'\t''DSC_0022.NEF'$'\n''5e124a8a'$'\t''DSC_0023.NEF'$'\n''8e5eb670'$'\t''DSC_0024.NEF'$'\n''a26eb4cb'$'\t''DSC_0025.NEF'$'\n''a0f0444e'$'\t''DSC_0026.NEF'$'\n''7b686084'$'\t''DSC_0027.NEF'$'\n''eff42939'$'\t''DSC_0028.NEF'$'\n''7ed21f22'$'\t''DSC_0029.NEF'$'\n''85f8f493'$'\t''DSC_0030.NEF'$'\n''97227a4f'$'\t''DSC_0031.NEF'$'\n''a0a96fb2'$'\t''DSC_0032.NEF'$'\n''e82c47e6'$'\t''DSC_0033.NEF'$'\n''55f5e030'$'\t''DSC_0034.NEF'$'\n''4fd188b4'$'\t''DSC_0035.NEF'$'\n''357d0757'$'\t''DSC_0036.NEF'$'\n''87cd39d6'$'\t''DSC_0037.NEF'$'\n''34b6fb45'$'\t''DSC_0038.NEF'$'\n''ef6f09f0'$'\t''DSC_0039.NEF'$'\n''b8d12e00'$'\t''DSC_0040.NEF'$'\n''bd2d4cae'$'\t''DSC_0041.NEF'$'\n''55dc67c0'$'\t''DSC_0042.NEF'$'\n''08ba39e4'$'\t''DSC_0043.NEF'$'\n''bcfefc4a'$'\t''DSC_0044.NEF'$'\n''ce69e1fd'$'\t''DSC_0045.NEF'$'\n''275af151'$'\t''DSC_0046.NEF'$'\n''6a30a875'$'\t''DSC_0047.NEF'$'\n''3aefb843'$'\t''DSC_0048.NEF'$'\n''674458fb'$'\t''DSC_0049.NEF'$'\n''aab60fc7'$'\t''DSC_0050.NEF'$'\n''13502f06'$'\t''mv: failed to access '504a5b89'$'\t''DSC_0001.NEF'$'\n''629a031e'$'\t''DSC_0002.NEF'$'\n''1af2720c'$'\t''DSC_0003.NEF'$'\n''852f62de'$'\t''DSC_0004.NEF'$'\n''874bd1f0'$'\t''DSC_0005.NEF'$'\n''f3fceda8'$'\t''DSC_0006.NEF'$'\n''28207fa2'$'\t''DSC_0007.NEF'$'\n''046ca494'$'\t''DSC_0008.NEF'$'\n''abf11428'$'\t''DSC_0009.NEF'$'\n''479e728d'$'\t''DSC_0010.NEF'$'\n''8df21237'$'\t''DSC_0011.NEF'$'\n''77663953'$'\t''DSC_0012.NEF'$'\n''7d9871c7'$'\t''DSC_0013.NEF'$'\n''85608081'$'\t''DSC_0014.NEF'$'\n''49246d62'$'\t''DSC_0016.NEF'$'\n''a2f927bc'$'\t''DSC_0017.NEF'$'\n''33277700'$'\t''DSC_0018.NEF'$'\n''76410b41'$'\t''DSC_0019.NEF'$'\n''c30d7d1c'$'\t''DSC_0020.NEF'$'\n''de239afe'$'\t''DSC_0021.NEF'$'\n''c9a1999e'$'\t''DSC_0022.NEF'$'\n''5e124a8a'$'\t''DSC_0023.NEF'$'\n''8e5eb670'$'\t''DSC_0024.NEF'$'\n''a26eb4cb'$'\t''DSC_0025.NEF'$'\n''a0f0444e'$'\t''DSC_0026.NEF'$'\n''7b686084'$'\t''DSC_0027.NEF'$'\n''eff42939'$'\t''DSC_0028.NEF'$'\n''7ed21f22'$'\t''DSC_0029.NEF'$'\n''85f8f493'$'\t''DSC_0030.NEF'$'\n''97227a4f'$'\t''DSC_0031.NEF'$'\n''a0a96fb2'$'\t''DSC_0032.NEF'$'\n''e82c47e6'$'\t''DSC_0033.NEF'$'\n''55f5e030'$'\t''DSC_0034.NEF'$'\n''4fd188b4'$'\t''DSC_0035.NEF'$'\n''357d0757'$'\t''DSC_0036.NEF'$'\n''87cd39d6'$'\t''DSC_0037.NEF'$'\n''34b6fb45'$'\t''DSC_0038.NEF'$'\n''ef6f09f0'$'\t''DSC_0039.NEF'$'\n''b8d12e00'$'\t''DSC_0040.NEF'$'\n''bd2d4cae'$'\t''DSC_0041.NEF'$'\n''55dc67c0'$'\t''DSC_0042.NEF'$'\n''08ba39e4'$'\t''DSC_0043.NEF'$'\n''bcfefc4a'$'\t''DSC_0044.NEF'$'\n''ce69e1fd'$'\t''DSC_0045.NEF'$'\n''275af151'$'\t''DSC_0046.NEF'$'\n''6a30a875'$'\t''DSC_0047.NEF'$'\n''3aefb843'$'\t''DSC_0048.NEF'$'\n''674458fb'$'\t''DSC_0049.NEF'$'\n''aab60fc7'$'\t''DSC_0050.NEF'$'\n''13502f06'$'\t''DSC_0051.NEF'$'\n''ac3eba1e'$'\t''DSC_0052.NEF'$'\n''c5052ad9'$'\t''DSC_0053.NEF'$'\n''80c3ee64'$'\t''DSC_0054.NEF'$'\n''d17f3177'$'\t''DSC_0055.NEF'$'\n''53f51ccf'$'\t''DSC_0056.NEF'$'\n''f91427af'$'\t''DSC_0057.NEF'$'\n''0d596f23'$'\t''DSC_0058.NEF'$'\n''fc378e62'$'\t''DSC_0059.NEF'$'\n''c72be5b3'$'\t''DSC_0060.NEF'$'\n''8bf29954'$'\t''DSC_0061.NEF'$'\n''f1193bbf'$'\t''DSC_0062.NEF'$'\n''d4460f24'$'\t''DSC_0063.NEF'$'\n''1e7b1c07'$'\t''DSC_0064.NEF'$'\n''3cc1cbd2'$'\t''DSC_0065.NEF'$'\n''ae935236'$'\t''DSC_0066.NEF'$'\n''f0ff02c1'$'\t''DSC_0067.NEF'$'\n''a16c6e58'$'\t''DSC_0068.NEF'$'\n''57ae8019'$'\t''DSC_0069.NEF'$'\n''82fc94df'$'\t''DSC_0070.NEF'$'\n''2ac41f26'$'\t''DSC_0071.NEF'$'\n''76b0493a'$'\t''DSC_0072.NEF'$'\n''9791ccf7'$'\t''DSC_0073.NEF'$'\n''eac3e7aa'$'\t''DSC_0074.NEF'$'\n''14f7c55c'$'\t''DSC_0075.NEF'$'\n''86df85b0'$'\t''DSC_0076.NEF'$'\n''d23ebeb8'$'\t''DSC_0077.NEF'$'\n''b1f51ea1'$'\t''DSC_0078.NEF'$'\n''1fb307bc'$'\t''DSC_0079.NEF'$'\n''91c17294'$'\t''DSC_0080.NEF'$'\n''c590cfb0'$'\t''DSC_0081.NEF'$'\n''9fc1eaad'$'\t''DSC_0082.NEF'$'\n''31de2e7c'$'\t''DSC_0083.NEF'$'\n''b4858068'$'\t''DSC_0084.NEF'$'\n''04371839'$'\t''DSC_0085.NEF'$'\n''fc440b4a'$'\t''DSC_0086.NEF'$'\n''9de00d44'$'\t''DSC_0087.NEF'$'\n''b9ab2214'$'\t''DSC_0088.NEF'$'\n''4c6f37c8'$'\t''DSC_0089.NEF'$'\n''14de5216'$'\t''DSC_0090.NEF'$'\n''8a565c42'$'\t''DSC_0091.NEF'$'\n''d05282d6'$'\t''DSC_0092.NEF'$'\n''fc032016'$'\t''DSC_0093.NEF'$'\n''ada77bc0'$'\t''DSC_0094.NEF'$'\n''3e6e288c'$'\t''DSC_0095.NEF'$'\n''6bfdb74a'$'\t''DSC_0096.NEF'$'\n''f2529938'$'\t''DSC_0097.NEF'$'\n''8193fcd9'$'\t''DSC_0098.NEF'$'\n''7786e3e1'$'\t''DSC_0099.NEF'$'\n''f2c36981'$'\t''DSC_0100.NEF'$'\n''b0e548e9'$'\t''DSC_0101.NEF'$'\n''b222e465'$'\t''DSC_0102.NEF'$'\n''b32683ac'$'\t''DSC_0103.NEF'$'\n''8511325d'$'\t''DSC_0104.NEF'$'\n''6ae62bf8'$'\t''DSC_0105.NEF'$'\n''bc15a457'$'\t''DSC_0106.NEF'$'\n''31f00e91'$'\t''DSC_0107.NEF'$'\n''355b0664'$'\t''DSC_0108.NEF'$'\n''201e3b02'$'\t''DSC_0109.NEF'$'\n''09456d16'$'\t''DSC_0110.NEF'$'\n''1bfa57da'$'\t''DSC_0111.NEF'$'\n''7171b5b8'$'\t''DSC_0112.NEF'$'\n''6c29ae1a'$'\t''DSC_0113.NEF'$'\n''92861cfd'$'\t''DSC_0114.NEF'$'\n''ed24a0a5'$'\t''DSC_0115.NEF'$'\n''2583d832'$'\t''DSC_0116.NEF'$'\n''6a45e5c5'$'\t''DSC_0117.NEF.mov': File name too long
DSC_0051.NEF'$'\n''ac3eba1e'$'\t''DSC_0052.NEF'$'\n''c5052ad9'$'\t''DSC_0053.NEF'$'\n''80c3ee64'$'\t''DSC_0054.NEF'$'\n''d17f3177'$'\t''DSC_0055.NEF'$'\n''53f51ccf'$'\t''DSC_0056.NEF'$'\n''f91427af'$'\t''DSC_0057.NEF'$'\n''0d596f23'$'\t''DSC_0058.NEF'$'\n''fc378e62'$'\t''DSC_0059.NEF'$'\n''c72be5b3'$'\t''DSC_0060.NEF'$'\n''8bf29954'$'\t''DSC_0061.NEF'$'\n''f1193bbf'$'\t''DSC_0062.NEF'$'\n''d4460f24'$'\t''DSC_0063.NEF'$'\n''1e7b1c07'$'\t''DSC_0064.NEF'$'\n''3cc1cbd2'$'\t''DSC_0065.NEF'$'\n''ae935236'$'\t''DSC_0066.NEF'$'\n''f0ff02c1'$'\t''DSC_0067.NEF'$'\n''a16c6e58'$'\t''DSC_0068.NEF'$'\n''57ae8019'$'\t''DSC_0069.NEF'$'\n''82fc94df'$'\t''DSC_0070.NEF'$'\n''2ac41f26'$'\t''DSC_0071.NEF'$'\n''76b0493a'$'\t''DSC_0072.NEF'$'\n''9791ccf7'$'\t''DSC_0073.NEF'$'\n''eac3e7aa'$'\t''DSC_0074.NEF'$'\n''14f7c55c'$'\t''DSC_0075.NEF'$'\n''86df85b0'$'\t''DSC_0076.NEF'$'\n''d23ebeb8'$'\t''DSC_0077.NEF'$'\n''b1f51ea1'$'\t''DSC_0078.NEF'$'\n''1fb307bc'$'\t''DSC_0079.NEF'$'\n''91c17294'$'\t''DSC_0080.NEF'$'\n''c590cfb0'$'\t''DSC_0081.NEF'$'\n''9fc1eaad'$'\t''DSC_0082.NEF'$'\n''31de2e7c'$'\t''DSC_0083.NEF'$'\n''b4858068'$'\t''DSC_0084.NEF'$'\n''04371839'$'\t''DSC_0085.NEF'$'\n''fc440b4a'$'\t''DSC_0086.NEF'$'\n''9de00d44'$'\t''DSC_0087.NEF'$'\n''b9ab2214'$'\t''DSC_0088.NEF'$'\n''4c6f37c8'$'\t''DSC_0089.NEF'$'\n''14de5216'$'\t''DSC_0090.NEF'$'\n''8a565c42'$'\t''DSC_0091.NEF'$'\n''d05282d6'$'\t''DSC_0092.NEF'$'\n''fc032016'$'\t''DSC_0093.NEF'$'\n''ada77bc0'$'\t''DSC_0094.NEF'$'\n''3e6e288c'$'\t''DSC_0095.NEF'$'\n''6bfdb74a'$'\t''DSC_0096.NEF'$'\n''f2529938'$'\t''DSC_0097.NEF'$'\n''8193fcd9'$'\t''DSC_0098.NEF'$'\n''7786e3e1'$'\t''DSC_0099.NEF'$'\n''f2c36981'$'\t''DSC_0100.NEF'$'\n''b0e548e9'$'\t''DSC_0101.NEF'$'\n''b222e465'$'\t''DSC_0102.NEF'$'\n''b32683ac'$'\t''DSC_0103.NEF'$'\n''8511325d'$'\t''DSC_0104.NEF'$'\n''6ae62bf8'$'\t''DSC_0105.NEF'$'\n''bc15a457'$'\t''DSC_0106.NEF'$'\n''31f00e91'$'\t''DSC_0107.NEF'$'\n''355b0664'$'\t''DSC_0108.NEF'$'\n''201e3b02'$'\t''DSC_0109.NEF'$'\n''09456d16'$'\t''DSC_0110.NEF'$'\n''1bfa57da'$'\t''DSC_0111.NEF'$'\n''7171b5b8'$'\t''DSC_0112.NEF'$'\n''6c29ae1a'$'\t''DSC_0113.NEF'$'\n''92861cfd'$'\t''DSC_0114.NEF'$'\n''ed24a0a5'$'\t''DSC_0115.NEF'$'\n''2583d832'$'\t''DSC_0116.NEF'$'\n''6a45e5c5'$'\t''DSC_0117.NEF.mov': File name too long
\n''d23ebeb8'$'\t''DSC_0077.NEF'$'\n''b1f51ea1'$'\t''DSC_0078.NEF'$'\n''1fb307bc'$'\t''DSC_0079.NEF'$'\n''91c17294'$'\t''DSC_0080.NEF'$'\n''c590cfb0'$'\t''DSC_0081.NEF'$'\n''9fc1eaad'$'\t''DSC_0082.NEF'$'\n''31de2e7c'$'\t''DSC_0083.NEF'$'\n''b4858068'$'\t''DSC_0084.NEF'$'\n''04371839'$'\t''DSC_0085.NEF'$'\n''fc440b4a'$'\t''DSC_0086.NEF'$'\n''9de00d44'$'\t''DSC_0087.NEF'$'\n''b9ab2214'$'\t''DSC_0088.NEF'$'\n''4c6f37c8'$'\t''DSC_0089.NEF'$'\n''14de5216'$'\t''DSC_0090.NEF'$'\n''8a565c42'$'\t''DSC_0091.NEF'$'\n''d05282d6'$'\t''DSC_0092.NEF'$'\n''fc032016'$'\t''DSC_0093.NEF'$'\n''ada77bc0'$'\t''DSC_0094.NEF'$'\n''3e6e288c'$'\t''DSC_0095.NEF'$'\n''6bfdb74a'$'\t''DSC_0096.NEF'$'\n''f2529938'$'\t''DSC_0097.NEF'$'\n''8193fcd9'$'\t''DSC_0098.NEF'$'\n''7786e3e1'$'\t''DSC_0099.NEF'$'\n''f2c36981'$'\t''DSC_0100.NEF'$'\n''b0e548e9'$'\t''DSC_0101.NEF'$'\n''b222e465'$'\t''DSC_0102.NEF'$'\n''b32683ac'$'\t''DSC_0103.NEF'$'\n''8511325d'$'\t''DSC_0104.NEF'$'\n''6ae62bf8'$'\t''DSC_0105.NEF'$'\n''bc15a457'$'\t''DSC_0106.NEF'$'\n''31f00e91'$'\t''DSC_0107.NEF'$'\n''355b0664'$'\t''DSC_0108.NEF'$'\n''201e3b02'$'\t''DSC_0109.NEF'$'\n''09456d16'$'\t''DSC_0110.NEF'$'\n''1bfa57da'$'\t''DSC_0111.NEF'$'\n''7171b5b8'$'\t''DSC_0112.NEF'$'\n''6c29ae1a'$'\t''DSC_0113.NEF'$'\n''92861cfd'$'\t''DSC_0114.NEF'$'\n''ed24a0a5'$'\t''DSC_0115.NEF'$'\n''2583d832'$'\t''DSC_0116.NEF'$'\n''6a45e5c5'$'\t''DSC_0117.NEF.mov': File name too long

我希望所有文件都存储在一个具有唯一名称的目录中。欢迎任何改进、优雅的解决方案或替代方案!

答案1

您这样做的方式crc32同时在所有文件上运行,生成一个包含所有校验和和文件名的字符串。这就是您看到的mv抱怨的字符串。

因此,在循环内运行 crc32。假设您的文件位于当前目录的子目录中(so、./dir0001/DCIM_0000.JPG./dir0002/DCIM_0123.JPG类似的目录)并且您希望将它们放入destdir/

#!/bin/bash
shopt -s globstar
for file in **/*.JPG; do
    crc=$(crc32 "$file")
    date=$(exiftool -d "%Y%m%d-%H%M%S" -CreateDate "$file" | awk '{print $4}')
    basename=$(basename "$file" .JPG)            # remove directory and extension
    newname="destdir/$basename-$date-$crc.jpg"   # piece together a new name
    echo mv -nv "$file" "$newname"
done

**是一个非标准扩展,它为整个目录树运行 glob。您还可以*/*.JPG仅在直接子目录中查找文件。

与 类似$date,您可以为亚秒时间添加另一个变量,并将其包含在如何newname形成中:

subsec=$(exiftool -d "%Y%m%d-%H%M%S" -SubSecTimeDigitized "$file" | awk '{print $4}')
newname="destination/$basename-$date-$subsec-$crc.jpg"

或类似的东西。检查文件的输出。

一旦您对脚本正常工作感到满意,请echo从 中删除。mv

答案2

如果我理解正确的话,我认为这至少在我的测试中是可行的:

c=$(($(ls /target/dir | wc -l)+1))
for i in *.JPG; do 
  mv "$i" "/target/dir/$c.JPG"
  ((++c))
done

因此,您将计数器设置为目标目录中的文件数。假设一开始是空的,则数字为 0,然后加 1。您输入要移动的目录,并根据每次循环中加 1 的计数器重命名文件。那么你就会有例如:

1.JPG
2.JPG
3.JPG

从另一个目录再次运行该命令,现在计数器将为 4,因此结果将是:

1.JPG
2.JPG
3.JPG
4.JPG
5.JPG
6.JPG
...

相关内容