如何在目录中创建每个目录 tar 并保存到不同的位置
How to create each directory tar in a directory and save to different localtion
我在 /home/ 路径中有多个目录,我想制作脚本为每个文件夹制作 .tar
并保存到另一个位置。
谷歌搜索后我发现:
find /home/ -type d -maxdepth 1 -mindepth 1 -exec tar cvf {}.tar {} \;
但我的 问题 是在 /home
中创建 .tar
文件,然后我必须将它们移动到另一个位置,这需要很长时间.
我 想要 的是,tar
文件应该在另一个位置创建,而不是在 /home/
目录中。
感谢:
阿卡什
您尝试过这样指定目录吗?
find /home/ -type d -maxdepth 1 -mindepth 1 -exec tar cvf \path\to\directory\{}.tar {} \;
最后一个参数是 tar 命令。 {}
被 find
命令找到的名称替换。例如,如果 find
命令即将与 foo/
目录一起使用,则 {}
将被替换为 foo/
。这将允许您指定位置。
SEARCH_DIR=/home
OUTPUT_DIR=/tmp
cd "$SEARCH_DIR" && find . -maxdepth 1 -mindepth 1 -type d -exec tar cvf "$OUTPUT_DIR/{}.tar" {} \;
进一步分解:
cd "$SEARCH_DIR"
- 将当前目录更改为 OUTPUT_DIR
(在本例中为 /home
)1
&&
更改目录后执行以下操作...
find .
- 在当前目录中搜索东西1
-maxdepth 1 -mindepth 1
- 不要递归深入一层以上(顶层,与执行 ls
相同)
-type d
- 仅输出目录(忽略文件)
-exec
- 运行 find
根据之前的过滤器选项 遇到的每个实体上的以下命令
tar cvf /tmp/{}.tar {}
- 执行 tar 命令创建(详细)文件 /tmp/{}.tar
(其中 {}
被替换为实体 find
当前找到的)并且只包含 {}
的内容(find
找到的直接 /entity
\;
- 告诉 find
这是 -exec
命令的结尾
总之,此命令会将 tar $SEARCH_DIR
中的每个目录写入 $OUTPUT_DIR
中的一个文件,并适当命名。
当前目录很重要,因为 find
的输出将第一个参数作为其输出的前缀。如果您没有更改目录而是将目录传递给 find
本身,则输出看起来会大不相同:
$ find .
foo
bar
$ find /home
/home/foo
/home/bar
在这种情况下,我们需要前者,以便 tar
命令正确执行。
这个简单的脚本对我有用。
#!/bin/bash
DW=`date +%a`
DM=`date +%d`
cd /home/
for file in *;
do
tar -czvf /databackup_Allin/$DW/home/$file.tar.gz $file;
done
我在 /home/ 路径中有多个目录,我想制作脚本为每个文件夹制作 .tar
并保存到另一个位置。
谷歌搜索后我发现:
find /home/ -type d -maxdepth 1 -mindepth 1 -exec tar cvf {}.tar {} \;
但我的 问题 是在 /home
中创建 .tar
文件,然后我必须将它们移动到另一个位置,这需要很长时间.
我 想要 的是,tar
文件应该在另一个位置创建,而不是在 /home/
目录中。
感谢: 阿卡什
您尝试过这样指定目录吗?
find /home/ -type d -maxdepth 1 -mindepth 1 -exec tar cvf \path\to\directory\{}.tar {} \;
最后一个参数是 tar 命令。 {}
被 find
命令找到的名称替换。例如,如果 find
命令即将与 foo/
目录一起使用,则 {}
将被替换为 foo/
。这将允许您指定位置。
SEARCH_DIR=/home
OUTPUT_DIR=/tmp
cd "$SEARCH_DIR" && find . -maxdepth 1 -mindepth 1 -type d -exec tar cvf "$OUTPUT_DIR/{}.tar" {} \;
进一步分解:
cd "$SEARCH_DIR"
- 将当前目录更改为OUTPUT_DIR
(在本例中为/home
)1&&
更改目录后执行以下操作...find .
- 在当前目录中搜索东西1-maxdepth 1 -mindepth 1
- 不要递归深入一层以上(顶层,与执行ls
相同)-type d
- 仅输出目录(忽略文件)-exec
- 运行find
根据之前的过滤器选项 遇到的每个实体上的以下命令
tar cvf /tmp/{}.tar {}
- 执行 tar 命令创建(详细)文件/tmp/{}.tar
(其中{}
被替换为实体find
当前找到的)并且只包含{}
的内容(find
找到的直接 /entity\;
- 告诉find
这是-exec
命令的结尾
总之,此命令会将 tar $SEARCH_DIR
中的每个目录写入 $OUTPUT_DIR
中的一个文件,并适当命名。
当前目录很重要,因为
find
的输出将第一个参数作为其输出的前缀。如果您没有更改目录而是将目录传递给find
本身,则输出看起来会大不相同:$ find . foo bar $ find /home /home/foo /home/bar
在这种情况下,我们需要前者,以便
tar
命令正确执行。
这个简单的脚本对我有用。
#!/bin/bash
DW=`date +%a`
DM=`date +%d`
cd /home/
for file in *;
do
tar -czvf /databackup_Allin/$DW/home/$file.tar.gz $file;
done