如何使用 bash 单独压缩超过 x 天的文件? (有用户输入)
How do I zip files individually older than x days using bash? (with user input)
嘿,我希望有人能帮助我解决我的问题(我是新手)
我想压缩超过 x 天的多个文件。 我已经实现了 读取天数并使用查找运算符将它们放入 $Files。
但我想问一下 bash 脚本的用户是否要将文件添加到存档(或不)然后删除原始文件。
唯一的问题是这个 yes/no 部分,因为我不知道 如何单独添加(或不添加)每个文件。
它应该看起来像:压缩 到 然后删除原来的? (y/n)(对于每个文件)
感谢大家帮助我:D
我的代码:
echo "How old should the file(s) be?"
read anmount_days
Files=$(find -mtime +$anmount_days)
#echo -e "\nZip <xyz> to <zip.zip> and delete the original then? (y/n)"
#read input
#if [ "$input" == "" ]; then
#
# echo -e "\nNothing was entered by the User\n"
#
#if [["$input" == y]]; then
#
# zip files
#
#else
#
# skip
#
# zip -r zip.zip $Files -rm
echo -e "\nxy file(s) older than $anmount_days day(s) from current ${PWD} archieved in xyz"e
编辑 1:
在将文件压缩到 zip.zip
之前,代码要求每个文件输入“y/n”
编辑 2:
显示压缩文件的数量和已压缩的文件。
echo "How old should the file(s) be?"
read anmount_days
Files=$(find -mtime +$anmount_days)
# Create blank array
list=$@
newlist=()
for file in $Files
do
echo -e "\nZip $file to <zip.zip> and delete the original then? (y/n)"
read input
if [[ $input = "y" ]]
then
echo "Zipping $file to zip.zip"
#Append $file in array
newlist+=($file)
zip zip.zip $file
#rm $file
fi
done
echo "Files that were zipped: ${newlist[*]}"
echo "Total number of files zipped: ${#newlist[@]}"
当输入为3,且全为“y”时:
全“y”时的压缩文件:
当输入是3, 2"y" 和rest 是"n":
当 2“y”其余部分为“n”时的 Zip 文件:
显示文件数和文件名:
嘿,我希望有人能帮助我解决我的问题(我是新手)
我想压缩超过 x 天的多个文件。 我已经实现了 读取天数并使用查找运算符将它们放入 $Files。
但我想问一下 bash 脚本的用户是否要将文件添加到存档(或不)然后删除原始文件。
唯一的问题是这个 yes/no 部分,因为我不知道 如何单独添加(或不添加)每个文件。
它应该看起来像:压缩
感谢大家帮助我:D
我的代码:
echo "How old should the file(s) be?"
read anmount_days
Files=$(find -mtime +$anmount_days)
#echo -e "\nZip <xyz> to <zip.zip> and delete the original then? (y/n)"
#read input
#if [ "$input" == "" ]; then
#
# echo -e "\nNothing was entered by the User\n"
#
#if [["$input" == y]]; then
#
# zip files
#
#else
#
# skip
#
# zip -r zip.zip $Files -rm
echo -e "\nxy file(s) older than $anmount_days day(s) from current ${PWD} archieved in xyz"e
编辑 1: 在将文件压缩到 zip.zip
之前,代码要求每个文件输入“y/n”编辑 2: 显示压缩文件的数量和已压缩的文件。
echo "How old should the file(s) be?"
read anmount_days
Files=$(find -mtime +$anmount_days)
# Create blank array
list=$@
newlist=()
for file in $Files
do
echo -e "\nZip $file to <zip.zip> and delete the original then? (y/n)"
read input
if [[ $input = "y" ]]
then
echo "Zipping $file to zip.zip"
#Append $file in array
newlist+=($file)
zip zip.zip $file
#rm $file
fi
done
echo "Files that were zipped: ${newlist[*]}"
echo "Total number of files zipped: ${#newlist[@]}"
当输入为3,且全为“y”时:
全“y”时的压缩文件:
当输入是3, 2"y" 和rest 是"n":
当 2“y”其余部分为“n”时的 Zip 文件:
显示文件数和文件名: