bash 不带斜杠“/”的目录列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9044465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:26:49  来源:igfitidea点击:

LIst of dirs without trailing slash "/"

linuxbashsedawk

提问by abkrim

I'd like a list (one item per line) of a dir. But the last character is a "/"

我想要一个目录的列表(每行一个项目)。但最后一个字符是“/”

 ls /var/lib/mysql/ | grep -v "\."

This shows:

由此可见:

 wachuwar_funkfow/
 wachuwar_prueba/
 webdeard_arde/

And I'd like

我想要

 wachuwar_funkfow
 wachuwar_prueba
 webdeard_arde

I'd appreciate help

我很感激帮助

回答by Basile Starynkevitch

Probably, your lsis aliased or defined as a function in your .bashrcor /etc/profileor elsewhere.

也许,你ls的别名或您定义为一个函数.bashrc/etc/profile或其他地方。

Try the full path to lslike

尝试完整路径ls喜欢

/bin/ls /var/lib/mysql/ 

回答by kenorb

Probably you've defined alias for lswhich is doing that.

可能您已经为ls它定义了别名。

To run lsdirectly, run as \ls, e.g.

ls直接运行,请作为 运行\ls,例如

\ls /var/lib/mysql/


To see what's aliased to, run: type ls.

看到发生了什么别名,运行:type ls

If so, remove alias by:

如果是这样,请通过以下方式删除别名:

unalias ls


Otherwise, here is the workaround:

否则,这是解决方法:

for dir in `ls -1 .`; do echo $dir; done

回答by ДМИТРИЙ МАЛИКОВ

You should check your aliases for ls. For example,

你应该检查你的别名ls。例如,

$> alias ls
alias ls='ls --color=auto'

-Fflag appends directory indicator, so you should remove it from your lsaliases if you don't need it.

-F标志附加目录指示符,因此ls如果您不需要它,应将其从别名中删除。

About one item by line. lshave usable -1flag, so it should works like

大约一行一行ls有可用的-1标志,所以它应该像

ls -1 /var/lib/mysql/
wachuwar_funkfow
wachuwar_prueba
webdeard_arde

回答by Abhijeet Rastogi

ls /var/lib/mysql/ | grep -v "\." | sed 's/\/$//'

The last sed commands searches for a line which has an ending / (which is backslashed) and replaces it with empty string.

最后一个 sed 命令搜索具有结尾 /(反斜杠)的行并将其替换为空字符串。

回答by glenn Hymanman

if you have a command that is also an alias, you can bypass the alias and execute the command directly with these methods:

如果您有一个也是别名的命令,您可以绕过别名并使用以下方法直接执行命令:

ls            # runs the alias
command ls    # runs the command
\ls           # runs the command

回答by Paused until further notice.

To exclude files that have a dot in their name, instead of using grep -vyou can use:

要排除名称中带有点的文件,grep -v您可以使用:

shopt -s extglob
\ls -d !(*.*)

The backslash bypasses the alias which is likely similar to one of:

反斜杠绕过别名,它可能类似于以下之一:

alias ls=ls -F
alias ls=ls -p
alias ls=ls --classify
alias ls=ls --file-type
alias ls=ls --indicator-style=WORD

Where "WORD" is one of slash, file-typeor classify.

其中“WORD”是slash,file-type或 之一classify