发现:有什么基本名称和目录名了? [英] find: What's up with basename and dirname?

查看:238
本文介绍了发现:有什么基本名称和目录名了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是找到一个任务,我注意到,当我做这样的事情:

I'm using find for a task and I noticed that when I do something like this:

find `pwd` -name "file.ext" -exec echo $(dirname {}) \;

它会给你只圆点的每场比赛。当您在命令S /目录名/基名你得到这个全路径名。难道我搞砸的东西在这里,或者这是预期的行为?我已经习惯了BASENAME给你的文件的名称(在这种情况下,file.ext)和目录名给你路径的其余部分。

it will give you dots only for each match. When you s/dirname/basename in that command you get the full pathnames. Am I screwing something up here or is this expected behavior? I'm used to basename giving you the name of the file (in this case "file.ext") and dirname giving you the rest of the path.

推荐答案

请看下面的代码:

#!/bin/sh
set -x
find `pwd` -name "file.ext" -exec echo $(dirname {}) \;

设置-x 显示了扩建工程和最后的命令是什么。运行时,它提供了以下的输出:

set -x shows how the expansion works and what the final command is. When run, it gives the following output:

++ pwd
++ dirname '{}'
+ find /home/kibab -name file.ext -exec echo . ';'

所以,这是扩大第一件事是 PWD 。二是 $(目录名{})。然后这两个命令的结果被投进find命令。因此,你告诉查找到 -exec回声。,所以你看到预期的输出。

So, the first thing that is expanded is the pwd. Second is $(dirname {}). The result of those two commands is then dropped into the find command. Thus, you're telling find to -exec echo ., so you're seeing the expected output.

在替换基本名目录名,扩张仍然需要的地方,但扩张的结果是不同的

When you substitute basename for dirname, the expansion still takes places, but the results of the expansion are different:


  1. PWD 被扩展为当前路径。在上面我的例子中,结果是 /家庭/ kibab

  2. 执行基本名称{} 。这个命令的结果是 {}

  3. find命令与替代上述换人执行。执行最后一个命令是这样的:

  1. pwd is expanded to the current path. In my example above, the result is /home/kibab
  2. basename {} is executed. The result of this command is {}.
  3. The find command is executed with the above substitutions in place. The final command executed looks like this:

查找/主页/ -name kibab'*巴纽-exec回声'{}';

检查后上面的命令,你会发现,命令现在简单地echo任何文件被发现的。

Upon inspecting the above command, you'll notice that the command now simply echo's whatever file was found.

也许你想这样的事情?

find `pwd` -name "file.ext" -printf "%f\n"

这篇关于发现:有什么基本名称和目录名了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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