我有很多文件需要保持同步,例如:
./regular/*.txt
./compressed/*.txt.bz2
当文件上传到 ./regular 时,我想制作一个脚本来定期检查和 bzip2 压缩尚未压缩的文件。
在我的脑海里,它就像......
ls ./regular/*.txt as A
ls ./compressed/*.txt* as B
for each in A as file
if B does not contain 'file' as match
bzip2 compress and copy 'file' to ./compressed/
有没有一个程序可以做到这一点,或者有人可以展示这种事情是如何在 coreutils / bash 中完成的?
答案1
用zsh
而不是bash
:
regular=(regular/*.txt(N:t))
compressed=(compressed/*.txt.bz2(N:t:r))
print -r Only in regular: ${regular:|compressed}
print -r Only in compressed: ${compressed:|regular}
然后你可以这样做:
for f (${regular:|compressed}) bzip2 -c regular/$f > compressed/$f.bz2
这是使用${A:|B}
数组减法运算符(扩展到元素A
酒吧(不包括)B
)的那些。
使用bash
GNU 工具:
(
export LC_ALL=C
shopt -s nullglob
comm -z23 <(cd regular && set -- *.txt && (($#)) && printf '%s\0' "$@") \
<(cd compressed && set -- *.txt.bz2 && (($#)) &&
printf '%s\0' "${@%.bz2}")
) |
while IFS= read -rd '' f; do
bzip2 -c "regular/$f" > "compressed/$f.bz2"
done
然后由命令执行减法comm
。这里使用 NUL 分隔符能够处理任意文件名,就像 zsh 解决方案中一样。
答案2
只需更改目录路径,末尾不带“/”,希望有帮助!
#!/bin/bash
path_input=/tmp/regular
path_compressed=/tmp/compressed
compressed_ext='.bz2'
echo "Starting backup..."
#List files and check if they are compressed
for file in `ls -1 $path_input/*.txt`; do
#Compressed file search
search_compressed=`sed "s;"$path_input";"$path_compressed";g" <<< $file$compressed_ext`
if [[ ! -f $search_compressed ]]
#Compress file
then
echo "Compressing $file"
bzip2 --keep --force "$file"
mv -f "$file$compressed_ext" "$path_compressed"
fi
done
echo "Done"