I wanna list out all the sub directory in a main directory with a serial numbers.
我想用序列号列出主目录中的所有子目录。
Example :
If a directory A contains B,C,D and E as a sub directory then the output should be like
如果目录A包含B,C,D和E作为子目录,那么输出应该是这样的
1 B
2 C
3 D
4 E
4 个解决方案
#1
You could use a loop:
你可以使用一个循环:
i=0
for f in A/*/; do
echo "$((++i)) $f"
done
The pattern matches all directories in A. $((++i))
increments the variable $i
by 1 for each directory that is found.
该模式匹配A中的所有目录。$((++ i))为找到的每个目录将变量$ i递增1。
#2
use this:
find * -type d | nl
find * -type d
: print name of all directories in current path
find * -type d:打印当前路径中所有目录的名称
nl
: add line number to output
nl:添加行号到输出
#3
It depends what you want to do with the number. If you plan to use it further/later, and anyone could make or delete directories between looking at the list and using the number, you will be in trouble.
这取决于你想用数字做什么。如果您计划进一步/稍后使用它,并且任何人都可以在查看列表和使用该号码之间建立或删除目录,那么您将遇到麻烦。
If that is the case, you can use the inode numbers of the directories like this as they are constant and unique across the entire filesystem:
如果是这种情况,您可以使用这样的目录的inode编号,因为它们在整个文件系统中是常量且唯一的:
ls -di */
19866918 f/ 19803132 other/ 19705681 save/
On a Mac, you can also do
在Mac上,您也可以这样做
stat -f '%i %N' */
19866918 f/
19803132 other/
19705681 save/
and I believe the Linux equivalent is
我相信Linux等价物
stat -c '%i %N' */
#4
ls | nl ; where ls is for listing the directory files and nl is for numbered line
ls | nl;其中ls用于列出目录文件,nl用于编号行
#1
You could use a loop:
你可以使用一个循环:
i=0
for f in A/*/; do
echo "$((++i)) $f"
done
The pattern matches all directories in A. $((++i))
increments the variable $i
by 1 for each directory that is found.
该模式匹配A中的所有目录。$((++ i))为找到的每个目录将变量$ i递增1。
#2
use this:
find * -type d | nl
find * -type d
: print name of all directories in current path
find * -type d:打印当前路径中所有目录的名称
nl
: add line number to output
nl:添加行号到输出
#3
It depends what you want to do with the number. If you plan to use it further/later, and anyone could make or delete directories between looking at the list and using the number, you will be in trouble.
这取决于你想用数字做什么。如果您计划进一步/稍后使用它,并且任何人都可以在查看列表和使用该号码之间建立或删除目录,那么您将遇到麻烦。
If that is the case, you can use the inode numbers of the directories like this as they are constant and unique across the entire filesystem:
如果是这种情况,您可以使用这样的目录的inode编号,因为它们在整个文件系统中是常量且唯一的:
ls -di */
19866918 f/ 19803132 other/ 19705681 save/
On a Mac, you can also do
在Mac上,您也可以这样做
stat -f '%i %N' */
19866918 f/
19803132 other/
19705681 save/
and I believe the Linux equivalent is
我相信Linux等价物
stat -c '%i %N' */
#4
ls | nl ; where ls is for listing the directory files and nl is for numbered line
ls | nl;其中ls用于列出目录文件,nl用于编号行