Unix 脚本查找目录中的所有文件夹,然后 tar 并移动它们
基本上我需要运行一个 Unix 脚本来查找目录 /fss/fin 中的所有文件夹(如果存在);然后我将其 tar 并移动到另一个目录 /fs/fi。
到目前为止,这是我的命令:
find /fss/fin -type d -name "essbase" -print
这里我直接提到了文件夹名称 essbase
。但相反,我想找到 /fss/fin
中的所有文件夹并使用它们。
如何查找 /fss/fin
目录中的所有文件夹? tar 将它们移动到 /fs/fi
?
澄清1:
是的,我只需要使用 Unix shell 脚本查找
/fss/fin
目录中的所有文件夹,并将它们压缩到另一个目录/fs/fi
。< /p>
澄清2:
我想把要求说清楚。 Shell 脚本应包含:
- 查找目录
/fss/fin
中的所有文件夹- 压缩文件夹
- 将文件夹移动到位于服务器
s11003232sz.net
上的另一个目录/fs/fi
中- 根据用户请求,它应该解压文件夹并将其移回原始目录
/fss/fin
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.
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.
How do I find all folders in the /fss/fin
directory & tar them to move them to /fs/fi
?
Clarification 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
.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我正在使用的一个示例,它可能会引导您走向正确的方向
here is an example I am working with that may lead you in the correct direction
由于
tar
自动创建目录,因此您实际上不需要做太多事情。假设 GNU tar:'
-C
' 选项在操作之前更改目录。第一个tar
将essbase
目录中找到的所有内容写入标准输出(唯一的“-”)。该tar
的输出通过管道传输到第二个tar
,后者读取其标准输入(唯一的“-”;很有趣不是吗!)。假设 GNU find,您还可以执行以下操作:
将目录更改为源目录;它运行最大深度为 1 的“find”来查找目录,并使用“sed”从列表中删除当前目录;然后,第一个“tar”将输出写入第二个“tar”,这与以前相同(除了我切换了参数的顺序以强调两个调用之间的并行性)。
如果您的顶级目录(实际上位于 /fss/fin 中的目录)名称中包含空格,那么还有更多工作要做 - 我假设要备份的目录都不以“.”开头
:从 '*' 生成的列表中清除非目录,并用 NUL '\0'(零字节)标记每个名称的结尾(而不是换行符)写入它们。输出被写入“xargs”,它被配置为期望以 NUL 结尾的名称,并且它使用正确的目录名称运行“tar”。像以前一样,该集成的输出被发送到第二个 tar。
如果您的目录名称以“.”开头收集,然后在“*”后添加“
.[az]*
”或其他合适的模式;至关重要的是,您使用的内容不会列出“.”。或者 '..'。如果目录中的名称以破折号开头,则需要使用“./*
”和“./.[az]*
”。如果您还有更不正当的要求,请在问题的修正案中清楚地阐明它们。
Since
tar
does directories automatically, you really don't need to do very much. Assuming GNU tar:The '
-C
' option changes directory before operating. The firsttar
writes to standard output (the lone '-') everything found in theessbase
directory. The output of thattar
is piped to the secondtar
, which reads its standard input (the lone '-'; fun isn't it!).Assuming GNU find, you can also do:
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).
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 '.':
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.
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]*
'.If you've got still more perverse requirements, enunciate them clearly in an amendment to the question.
上面的命令为您提供了 /fss/fin 的第一级子目录列表。
然后你就可以用这个做任何事情了。例如,将它们 tar 到您的输出目录,如下面的命令所示
原始目录结构将在解压后重新创建。
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
Original directory structure will be recreated after untar-ing.
这是一个 bash 示例(使用您的路径更改
/fss/fin
、/fs/fi
):它找到所有文件夹,分别压缩它们,如果成功 - 移动将它们放入不同的文件夹中。
Here is a bash example (change
/fss/fin
,/fs/fi
with your paths):which finds all the folders, tar them separately, and if successful - move them into different folder.
这应该可以做到:
This should do it: