unix Shell脚本用于存档超过2天的同名目录[重复]

tcbh2hod  于 5个月前  发布在  Unix
关注(0)|答案(2)|浏览(66)

此问题在此处已有答案

How do I set a variable to the output of a command in Bash?(16个回答)
20天前关闭。
我是新的shell脚本,试图写一个脚本存档目录相同的名称在同一路径这是老了2天.请帮助

示例-Log 15122023应该像Log15122023.tar.gz一样存档

for x in 'find /apps/logs/* -mtime +2'; do
  echo "Tarring $x.."
  tar cvzf $x.tar.gz $x
done

字符串
我得到下面的错误,当我尝试执行此脚本。
tar find /apps/logs/* -mtime +2.. tar:您不能指定多个“-Acdtrux”、“--delete”或“--test-label”选项尝试“tar --help”或“tar --usage”以获取更多信息。

w8biq8rn

w8biq8rn1#

#!/bin/bash
dir_path="/apps/logs"
find "$dir_path" -maxdepth 1 -type d -mtime +2 -print0 | while IFS= read -r -d '' dir; do
    dir_name=$(basename "$dir")
    tar cvzf "${dir_path}/${dir_name}.tar.gz" -C "$dir_path" "$dir_name" && rm -rf "$dir"
done
exit 0

字符串

5anewei6

5anewei62#

#!/bin/bash
set -o noglob
IFS=$'\n'; for x in $(find /apps/logs/ -name "*" -mtime +2); do
echo "Tarring $x.."
tar cvzf "${x##*/}.tar.gz" "$x"
done
set +o noglob

对于来自windows的人,他们的文件名中从来没有\n,并且让文件名以空格结尾。

相关问题