目录的递归列表
我有下面的代码,它必须递归列出所有文件和目录。 我不确定它是否工作正常。
#!/usr/bin/sh
recur_fun()
{
for i in `ls -ltr | awk '{print $9}'`
do
echo $i;
cd $i;
ls
pwd
recur_fun
cd ..
pwd
done
}
recur_fun
我需要复制文件名,然后在clearcase中使用它。
I have the below code which has to do a recursive listing of all the files and directories.
I am not sure if it is working fine.
#!/usr/bin/sh
recur_fun()
{
for i in `ls -ltr | awk '{print $9}'`
do
echo $i;
cd $i;
ls
pwd
recur_fun
cd ..
pwd
done
}
recur_fun
I need to copy the name of the file and then use it in clearcase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议将其替换为:
find 。 -打印
I suggest replacing this with:
find . -print
ls
已经有一个递归选项:ls
already has a recursive option:注意:我宁愿建议使用cleartool 命令,而不是特定于操作系统的 shell 命令。
例如:
将 递归列出所有文件,无论是否私有。
请参阅 命令行删除所有 ClearCase view-private文件了解更多信息。
Note: I would rather recommend going with cleartool command than OS-specific shell commands.
For instance:
would list all files recursively, private or not.
See Command line to delete all ClearCase view-private files for more.
如果目的是完成工作,则使用“
find
”或“ls -R
”等预构建工具来可靠地完成工作。假设目的是学习递归和 shell 脚本,而不是完成工作,那么它由于多种原因而无法正常工作:
cd
最多只会产生一条错误消息。cd
是否有效,所以您很容易看到一遍又一遍地列出相同的文件(给定目录中的每个文件一次)。此外,两个分号是多余的。
如果您确实需要在脚本中使用 cd,通常最好在子 shell 中执行此操作:
当子 shell 完成时,您知道父 shell 仍然位于完全相同的位置它是之前的 - 它不依赖于 cd 命令在符号链接上执行的变幻莫测的操作。现代 shell(意思是
bash
)似乎认为您通常应该需要一个“逻辑cd
”操作,这让我很烦恼,因为我几乎从来不想要这种行为。If the purpose is to get the job done, then use pre-built tools such as '
find
' or 'ls -R
' to do the job reliably.Assuming that the purpose is to learn about recursion and shell scripting, rather than to get the job done, then it is not working fine for a number of reasons:
cd
will at best produce an error message.cd
works, you're apt to see the same files listed over, and over, and over again (once per file in a given directory).Additionally, the two semi-colons are superfluous.
If you do need to use
cd
in a script, it is usually a good idea to do that in sub-shell:When the sub-shell completes, you know the parent is still in exactly the same place it was in before - it doesn't depend on the vagaries of what the
cd
command does across symlinks. Modern shells (meaningbash
) seem to think that you should normally want a 'logicalcd
' operation, which bugs the hell out of me because I almost never do want that behaviour.