在目录名称前面删除斜线

发布于 2025-02-13 13:19:41 字数 453 浏览 0 评论 0原文

我正在尝试重新夺回C壳和TCSH的一些简单性。我有一个简单的别名,让我列出目录(别名lsdd'ls | grep /'< /code>)。我找到了一个带有几种解决方案的帖子,没有一个特别令人满意。例如,

ls -d */

除非没有子目录,否则效果很好,在这种情况下,您会收到错误消息 - 不是完全优雅。

echo */

没有给出该错误,但列表不像单列那样容易读取。

因此,我一直在/etc中进行路由,以查找bash定义其ls命令的位置,以便它使用颜色,以便它剥离/遵循目录名称。这似乎是做一些芽的好地方。 ls命令中的目录名称中的bash strashs在哪个启动文件中?

I'm trying to recapture some of the simplicity of the c-shell and tcsh. I had a simple alias that allowed me to list directories (alias lsdd 'ls | grep /'). I found a post with several solutions, none of which were particularly satisfying. For instance,

ls -d */

works well unless there are no sub-directories, in which case you get an error message--not exactly elegant.

echo */

doesn't give that error, but the list is not as easily readable as a single column.

So, I have been routing around in /etc to find where bash defines its ls command so that it uses color, and so that it strips the / following the directory name. That seems to be a great place to do some bud nipping. In what startup file does bash strip slashes from directory names in an ls command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-02-20 13:19:42

如果您只想列出而没有落后斜线的目录,并且在单列中ls不一定是最佳实用程序。别名在某种程度上过时了。如今建议使用功能。

lsdd() {
  local -a list=( */ )
  printf '%s\n' "${list[@]%/}"
}

如果默认情况下没有子目录,则打印单个*。为了摆脱它,我们可以暂时设置nullglob选项。在下文中,我们记录该选项的初始状态,然后将其恢复:

lsdd() {
  local tmp=$(shopt -p nullglob)
  shopt -s nullglob
  local -a list=( */ )
  printf '%s\n' "${list[@]%/}"
  eval "$tmp"
}

If you want to list only directories without the trailing slash and in a single column ls is not necessarily the best utility. And aliases are somehow obsolete; functions are recommended nowadays.

lsdd() {
  local -a list=( */ )
  printf '%s\n' "${list[@]%/}"
}

If there are no sub-directories by default a single * is printed. To get rid of it we can temporarily set the nullglob option. In the following we record the option's initial state and restore it afterwards:

lsdd() {
  local tmp=$(shopt -p nullglob)
  shopt -s nullglob
  local -a list=( */ )
  printf '%s\n' "${list[@]%/}"
  eval "$tmp"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文