概述
本文翻译自:Listing only directories using ls in bash: An examination
This command lists directories in the current path: ls -d */
此命令列出当前路径中的目录: ls -d */
What exactly does the pattern */
do? */
模式到底是做什么的?
And how can we give the absolute path in the above command (eg ls -d /home/alice/Documents
) for listing only directories in that path? 以及如何在上述命令中给出绝对路径(例如ls -d /home/alice/Documents
)以仅列出该路径中的目录?
#1楼
参考:https://stackoom.com/question/yDgY/在bash中仅使用l列出目录-检查
#2楼
*/
is a pattern that matches all of the subdirectories in the current directory ( *
would match all files and subdirectories; the /
restricts it to directories). */
是与当前目录中所有子目录匹配的模式( *
将与所有文件和子目录匹配; /
将其限制为目录)。 Similarly, to list all subdirectories under /home/alice/Documents, use ls -d /home/alice/Documents/*/
同样,要列出/ home / alice / Documents下的所有子目录,请使用ls -d /home/alice/Documents/*/
#3楼
Four ways to get this done, each with a different output format 完成此操作的四种方法,每种方法具有不同的输出格式
1. Using echo
1.使用echo
Example: echo */
, echo */*/
示例: echo */
, echo */*/
Here is what I got: 这是我得到的:
cs/ draft/ files/ hacks/ masters/ static/
cs/code/ files/images/ static/images/ static/stylesheets/
2. Using ls
only 2.仅使用ls
Example: ls -d */
示例: ls -d */
Here is exactly what I got: 这正是我得到的:
cs/
files/
masters/
draft/
hacks/
static/
Or as list (with detail info): ls -dl */
或作为列表(包含详细信息): ls -dl */
3. Using ls
and grep
3.使用ls
和grep
Example: ls -l | grep "^d"
示例: ls -l | grep "^d"
ls -l | grep "^d"
Here is what I got: ls -l | grep "^d"
这是我得到的:
drwxr-xr-x
24 h
staff
816 Jun
8 10:55 cs
drwxr-xr-x
6 h
staff
204 Jun
8 10:55 draft
drwxr-xr-x
9 h
staff
306 Jun
8 10:55 files
drwxr-xr-x
2 h
staff
68 Jun
9 13:19 hacks
drwxr-xr-x
6 h
staff
204 Jun
8 10:55 masters
drwxr-xr-x
4 h
staff
136 Jun
8 10:55 static
4. Bash Script (Not recommended for filename containing spaces) 4. Bash脚本(不建议将文件名包含空格)
Example: for i in $(ls -d */); do echo ${i%%/}; done
示例: for i in $(ls -d */); do echo ${i%%/}; done
for i in $(ls -d */); do echo ${i%%/}; done
Here is what I got: 这是我得到的:
cs
draft
files
hacks
masters
static
If you like to have '/' as ending character, the command will be: for i in $(ls -d */); do echo ${i}; done
如果您希望以'/'作为结束字符,则命令将为: for i in $(ls -d */); do echo ${i}; done
for i in $(ls -d */); do echo ${i}; done
cs/
draft/
files/
hacks/
masters/
static/
#4楼
I use: 我用:
ls -d */ | cut -f1 -d'/'
This creates a single column with no trailing slash - useful in scripts. 这将创建不带斜杠的单列-在脚本中很有用。
My two cents. 我的两分钱。
#5楼
I partially solved with : 我部分解决了:
cd "/path/to/pricipal/folder"
for i in $(ls -d .*/); do sudo ln -s "$PWD"/${i%%/} /home/inukaze/${i%%/}; done
ln: «/home/inukaze/./.»: can't overwrite a directory
ln: «/home/inukaze/../..»: can't overwrite a directory
ln: accesing to «/home/inukaze/.config»: too much symbolics links levels
ln: accesing to «/home/inukaze/.disruptive»: too much symbolics links levels
ln: accesing to «/home/inukaze/innovations»: too much symbolics links levels
ln: accesing to «/home/inukaze/sarl»: too much symbolics links levels
ln: accesing to «/home/inukaze/.e_old»: too much symbolics links levels
ln: accesing to «/home/inukaze/.gnome2_private»: too much symbolics links levels
ln: accesing to «/home/inukaze/.gvfs»: too much symbolics links levels
ln: accesing to «/home/inukaze/.kde»: too much symbolics links levels
ln: accesing to «/home/inukaze/.local»: too much symbolics links levels
ln: accesing to «/home/inukaze/.xVideoServiceThief»: too much symbolics links levels
Well , this reduce to me , the mayor part :) 好吧,这减少了我,市长的一部分:)
#6楼
One-liner to list directories only from "here". 一线式仅从“这里”列出目录。
With file count. 随着文件数。
for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done
最后
以上就是爱笑西牛为你收集整理的在bash中仅使用l列出目录:检查的全部内容,希望文章能够帮你解决在bash中仅使用l列出目录:检查所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复