bash Unix 脚本查找目录中的所有文件夹,然后 tar 并移动它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3995047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unix script to find all folders in the directory, then tar and move them
提问by Shenna
Basically I need to run a Unix script to find all folders in the directory /fss/fin, if it exists; then I have tar it and move to another directory /fs/fi.
基本上,我需要运行一个 Unix 脚本来查找目录 /fss/fin 中的所有文件夹(如果存在);然后我将它 tar 并移动到另一个目录 /fs/fi。
This is my command so far:
到目前为止,这是我的命令:
find /fss/fin -type d -name "essbase" -print
Here I have directly mentioned the folder name essbase
. But instead, I would like to find all the folders in the /fss/fin
and use them all.
这里我直接提到了文件夹名称essbase
。但相反,我想找到 中的所有文件夹/fss/fin
并全部使用它们。
How do I find all folders in the /fss/fin
directory & tar them to move them to /fs/fi
?
如何找到/fss/fin
目录中的所有文件夹并将它们压缩以将它们移动到/fs/fi
?
Clarification 1:
说明 1:
Yes I need to find only all folders in the directory
/fss/fin
directory using a Unix shell script and tar them to another directory/fs/fi
.
是的,我只需要
/fss/fin
使用 Unix shell 脚本查找目录目录中的所有文件夹,然后将它们 tar 到另一个目录/fs/fi
。
Clarification 2:
说明二:
I want to make it clear with the requirement. The Shell Script should contain:
- Find all the folders in the directory
/fss/fin
- Tar the folders
- Move the folders in another directory
/fs/fi
which is located on the servers11003232sz.net
- On user requests it should untar the Folders and move them back to the orignal directory
/fss/fin
我想把要求说清楚。Shell 脚本应包含:
- 查找目录中的所有文件夹
/fss/fin
- 压缩文件夹
- 移动
/fs/fi
位于服务器上的另一个目录中的文件夹s11003232sz.net
- 根据用户请求,它应该解压缩文件夹并将它们移回原始目录
/fss/fin
回答by Chris Hough
here is an example I am working with that may lead you in the correct direction
这是我正在使用的一个示例,它可能会引导您走向正确的方向
BackUpDIR="/srv/backup/"
SrvDir="/srv/www/"
DateStamp=$(date +"%Y%m%d");
for Dir in $(find $SrvDir* -maxdepth 0 -type d );
do
FolderName=$(basename $Dir);
tar zcf "$BackUpDIR$DateStamp.$FolderName.tar.gz" -P $Dir
done
回答by Jonathan Leffler
Since tar
does directories automatically, you really don't need to do very much. Assuming GNU tar:
由于tar
目录会自动执行,因此您实际上不需要做太多事情。假设GNU tar:
tar -C /fss/fin -cf - essbase |
tar -C /fs/fi -xf -
The '-C
' option changes directory before operating. The first tar
writes to standard output (the lone '-') everything found in the essbase
directory. The output of that tar
is piped to the second tar
, which reads its standard input (the lone '-'; fun isn't it!).
' -C
' 选项在操作前更改目录。第一个tar
写入标准输出(单独的“-”)在essbase
目录中找到的所有内容。其输出通过tar
管道传输到第二个tar
,后者读取其标准输入(唯一的“-”;不是很有趣!)。
Assuming GNU find, you can also do:
假设 GNU 找到,您还可以执行以下操作:
(cd /fss/fin; tar -cf - $(find . -maxdepth 1 -type d | sed '/^\.$/d')) |
tar -xf - -C /fs/fi
This changes directory to the source directory; it runs 'find' with a maximum depth of 1 to find the directories and removes the current directory from the list with 'sed'; the first 'tar' then writes the output to the second one, which is the same as before (except I switched the order of the arguments to emphasize the parallelism between the two invocations).
这会将目录更改为源目录;它运行最大深度为 1 的 'find' 以查找目录并使用 'sed' 从列表中删除当前目录;然后第一个 'tar' 将输出写入第二个,这与之前相同(除了我切换了参数的顺序以强调两次调用之间的并行性)。
If your top-level directories (those actually in /fss/fin) have spaces in the names, then there is more work to do again - I'm assuming none of the directories to be backed up start with a '.':
如果您的顶级目录(实际上在 /fss/fin 中的目录)在名称中有空格,那么还有更多工作要做 - 我假设要备份的目录都没有以“.”开头:
(cd /fss/fin; find * -maxdepth 0 -type d -print 0 | xargs -0 tar -cf -) |
tar -xf - -C /fs/fi
This weeds out the non-directories from the list generated by '*', and writes them with NUL '\0' (zero bytes) marking the end of each name (instead of a newline). The output is written to 'xargs', which is configured to expect the NUL-terminated names, and it runs 'tar' with the correct directory names. The output of this ensemble is sent to the second tar, as before.
这会从由 '*' 生成的列表中清除非目录,并用 NUL '\0'(零字节)标记每个名称的结尾(而不是换行符)写入它们。输出被写入'xargs',它被配置为期望以NUL 结尾的名称,并使用正确的目录名称运行'tar'。和以前一样,这个集合的输出被发送到第二个 tar。
If you have directory names starting with a '.' to collect, then add '.[a-z]*
' or another suitable pattern after the '*'; it is crucial that what you use does not list '.' or '..'. If you have names starting with dashes in the directory, then you need to use './*
' and './.[a-z]*
'.
如果您的目录名称以“.”开头 收集,然后.[a-z]*
在 '*' 后添加 ' ' 或其他合适的模式;至关重要的是,您使用的内容不要列出“。” 或者 '..'。如果目录中有以破折号开头的名称,则需要使用“ ./*
”和“ ./.[a-z]*
”。
If you've got still more perverse requirements, enunciate them clearly in an amendment to the question.
如果您还有更不正当的要求,请在问题的修正中清楚地阐明它们。
回答by Max Doronin
find /fss/fin -d 1 -type d -name "*" -print
The above command gives you the list of 1st level subdirectories of the /fss/fin. Then you can do anything with this. E.g. tar them to your output directory as in the command below
上面的命令为您提供了 /fss/fin 的第一级子目录列表。然后你可以用它做任何事情。例如,将它们压缩到您的输出目录,如下面的命令所示
tar -czf /fss/fi/outfile.tar.gz `find /fss/fin -d 1 -type d -name "*" -print`
Original directory structure will be recreated after untar-ing.
解压后将重新创建原始目录结构。
回答by kenorb
Here is a bash example (change /fss/fin
, /fs/fi
with your paths):
下面是一个bash例子(其他城市/fss/fin
,/fs/fi
你的路径):
dirs=($(find /fss/fin -type d))
for dir in "${dirs[@]}"; do
tar zcf "$dir.tgz" "$dir" -P -C /fs/fi && mv -v "$dir" /fs/fi/
done
which finds all the folders, tar them separately, and if successful - move them into different folder.
找到所有文件夹,分别对它们进行 tar,如果成功 - 将它们移动到不同的文件夹中。
回答by Ankur Agarwal
This should do it:
这应该这样做:
#!/bin/sh
list=`find . -type d`
for i in $list
do
if [ ! "$i" == "." ]; then
tar -czf ${i}.tar.gz ${i}
fi
done
mv *.tar.gz ~/tardir