使用bash找到包含字符串的文件夹名称 [英] Use bash to find a folder name that contains a string

查看:137
本文介绍了使用bash找到包含字符串的文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为此在击:


  • 在当前目录中,找到包括富的名字
  • 第一个文件夹
  • in the current directory, find the first folder that contains "foo" in the name

我一直在用find命令打转转,但有点困惑。有什么建议?

I've been playing around with the find command, but a little confused. Any suggestions?

编辑:真棒建议,但我怎么能结果存储在变量

awesome suggestions, but how can I store the result in a variable?

编辑#2:该死的,你们是伟大的。但是还有一个问题 - 怎样才能通过在富到子shell变量? (没有必要不使用子shell的建议,我会尝试,现在。)谢谢

EDIT #2: damn you guys are great. But one more question - how can I pass in a variable for "foo" into the subshell? (Not necessary in the suggestion that doesn't use subshell, I'll try that for now.) THANK YOU

推荐答案

更新:用于从谷歌的旅客。最好是使用它是使用命令,其目的是为下面的命令行,只显示第一行(由@ jm666的回答):

Update For travellers from google. The best would be using the following command line which is using the head command, which is intended for that, to display only the first line (as answered by @jm666) :

find -maxdepth 1 -type d -name '*foo*'| head -n1


不过,这才是我的前 AWK 的尝试:

find -maxdepth 1 -type d -name '*foo*'| awk '{if(NR==1) print}'

什么可以简化为(感谢@Kevin):

what can be simplified to (thanks @Kevin):

find -maxdepth 1 -type d -name '*foo*'| awk 'NR==1'

据认为在当前文件夹中的所有文件夹(非递归),并使用awk将只打印结果的第一行到stdout

It finds all folders in the current folder (not recursively) and uses awk to print only the first line of the result to stdout

这篇关于使用bash找到包含字符串的文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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