gpt4 book ai didi

linux - 在一个目录中查找不在另一个目录中的文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:40:22 24 4
gpt4 key购买 nike

我写了这个 bash 脚本:

#!/bin/bash
DIR_TORRENTS="/home/simon/.wine/drive_c/users/simon/Datos de programa/uTorrent"
DIR_DESCARGA=/home/simon/torrent-descargas/
DIR_TEMPORAL=/home/simon/torrent-temporal/
cd "$DIR_TORRENTS"
rm -f /tmp/torrent_existentes
for torrent in *.torrent
do
nombre=`basename "$torrent" .torrent`
find "$DIR_TEMPORAL" "$DIR_DESCARGA" -maxdepth 2 -name "$nombre" -printf '%f.torrent\n' >> /tmp/torrent_existentes
done

通过这个脚本,我想获得一个 torrent 文件的列表,这些文件的数据仍然存在于 uTorrent 的数据文件夹中。
该脚本在文件名包含诸如“[]”之类的字符时除外。我认为问题在于“-name”将“$nombre”解释为一种模式。如何禁用此行为?


好吧,我找到了解决方案:nombre=$(basename "$torrent".torrent | sed 's/\[/\\[/g; s/\]/\\]/g')

但是现在我有另一个问题。我想删除 uTorrent 数据文件夹中不存在的 torrent 文件。
我已经修改了我以前的脚本(这个脚本不会删除任何内容,因为我先测试):

#!/bin/bash

DIR_TORRENTS="/home/simon/.wine/drive_c/users/simon/Datos de programa/uTorrent"
DIR_DESCARGA=/home/simon/torrent-descargas/
DIR_TEMPORAL=/home/simon/torrent-temporal/

cd "$DIR_TORRENTS"
for torrent in *.torrent
do
nombre=$(basename "$torrent" .torrent | sed 's/\[/\\[/g; s/\]/\\]/g')
if ! find "$DIR_TEMPORAL" "$DIR_DESCARGA" -maxdepth 2 -name "$nombre" &> /dev/null
then
echo "$torrent"
fi
done

但是它什么也不打印,为什么?


嗯,这也解决了:

#!/bin/bash

DIR_TORRENTS="/home/simon/.wine/drive_c/users/simon/Datos de programa/uTorrent"
DIR_DESCARGA=/home/simon/torrent-descargas/
DIR_TEMPORAL=/home/simon/torrent-temporal/

rm -f /tmp/torrent_existentes
cd "$DIR_TORRENTS"
for torrent in *.torrent
do
nombre=$(basename "$torrent" .torrent | sed 's/\[/\\[/g; s/\]/\\]/g')
find "$DIR_TEMPORAL" "$DIR_DESCARGA" -maxdepth 2 -name "$nombre" -printf '%f.torrent\n' >> /tmp/torrent_existentes
done
for torrent in *.torrent
do
if ! grep -Fq "$torrent" /tmp/torrent_existentes
then
rm "$torrent"
fi
done

但是有什么方法可以更简单地编写这个脚本吗?

最佳答案

你需要逃避名字:

nombre='abc[def]ghi'
printf -v escaped_nombre "%q" "$nombre"
echo $escaped_nombre

关于linux - 在一个目录中查找不在另一个目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6032778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com