如何循环在linux目录? [英] How to loop over directories in Linux?

查看:134
本文介绍了如何循环在linux目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Linux在bash脚本,并需要经过所有子目录的名称在给定的目录中。我如何通过这些目录循环(并跳过常规文件)?

例如:结果
给定的目录为的/ tmp / 结果
它具有以下子目录:的/ tmp / A,/ tmp目录/ B,/ tmp目录/ C

我要找回A,B,C。


解决方案

  CD / tmp目录
找 。 -maxdepth 1 -mindepth 1型ð-printf'%F \\ N'

一个简短的说明:找到查找文件(很明显)


  • 。是当前目录,CD后,它的 / tmp目录(恕我直言,这是比拥有更灵活 / tmp目录直接在找到命令,只有一个地方,在 CD ,要改变,如果你想要更多的行动采取此文件夹中的地方)


  • -maxdepth 1 -mindepth 1 确认,即找到真的,只是会在当前目录,不包括在结果


  • 型ð仅查找目录


  • -printf'%F \\ n 每个命中只打印找到的文件夹的名称(外加一个换行符)。


电子瞧!

I am writing a script in bash on Linux and need to go through all subdirectory names in a given directory. How can I loop through these directories (and skip regular files)?

For example:
the given directory is /tmp/
it has the following subdirectories: /tmp/A, /tmp/B, /tmp/C

I want to retrieve A, B, C.

解决方案

cd /tmp
find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'

A short explanation: find finds files (quite obviously)

  • . is the current directory, after the cd it's /tmp (IMHO this is more flexible than having /tmp directly in the find command. You have only one place, the cd, to change, if you want more actions to take place in this folder)

  • -maxdepth 1 and -mindepth 1 make sure, that find really, only looks in the current dir and doesn't include '.' in the result

  • -type d looks only for directories

  • -printf '%f\n prints only the found folder's name (plus a newline) for each hit.

E voila!

这篇关于如何循环在linux目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆