压缩多个文件的脚本

压缩多个文件的脚本

我有一个脚本,它可以生成备份并查找超过 30 天的文件并压缩它们。现在我面临的问题是,如果文件不止一个,我该如何编辑脚本以便它也压缩所有这些文件。下面是我使用的查找功能。请帮忙。

Time=+30
PATH_TO_DUMP=/home/tarun/Desktop/Backup

#Find any Backup File defined by the time constraint
file="$(find $PATH_TO_DUMP -type f -mtime $Time)"

#To verify if $file is empty or has some value
if [ ! -n "$file" ]; then
echo "No Earlier Backups were found to compress" >> $PATH_TO_LOG
else
echo "Earlier Backups $file will be compressed" >> $PATH_TO_LOG
gzip $file
fi

答案1

只需稍加修改即可

(管道寻找输出到尽管循环执行

Time=+30
PATH_TO_DUMP=/home/tarun/Desktop/Backup
#Find any Backup File defined by the time constraint

find $PATH_TO_DUMP -type f -mtime $Time | while read file
do

  #To verify if $file is empty or has some value
  if [ ! -n "$file" ]; then
    echo "No Earlier Backups were found to compress" >> $PATH_TO_LOG
  else
    echo "Earlier Backups $file will be compressed" >> $PATH_TO_LOG
    gzip "$file"
  fi

done

答案2

tar使用而不是 来压缩所有文件gzip

tar jcvf new_file.tar.bz2 $file

tar 将文件存储
-jbziptarred 文件中 (-z使用gzip)
-c创建 (-x提取)
-v详细
-f文件以创建 (或打开)

提取:

tar jxvf new_file.tar.bz2

否则,如果您想要每个文件的存档:

echo $file | xargs gzip

xargs 从标准输入读取项目,以空格(可以用双引号或单引号或反斜杠保护)或换行符分隔,并使用任何初始参数后跟从标准输入读取的项目执行命令(默认为 /bin/echo)一次或多次。

相关内容