加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

Bash:获取不同目录下的文件名

发布时间:2023-01-08 09:31:17 所属栏目:Unix 来源:互联网
导读: Bash:获取不同目录下的文件名
bashfile
Bash:获取不同目录下的文件名,bash,file,Bash,File,如何获取目录名和文件名?如果可能的话unix目录,如何在一个for循环中使用目录和文件名?如果您

Bash:获取不同目录下的文件名

bashfile

Bash:获取不同目录下的文件名,bash,file,Bash,File,如何获取目录名和文件名?如果可能的话unix目录,如何在一个for循环中使用目录和文件名?如果您想在子目录和文件名上循环,一个简单的示例将生成这些目录和文件名的列表# tree system/system/├── cat1│ └── id1├── cat5│ └── id42├── cat20│ └── 59593└── mumbry└── 39394 directories, 4 files#如果要将它们拆分为directory和basename,shell

如何获取目录名和文件名?如果可能的话,如何在一个for循环中使用目录和文件名?

如果您想在子目录和文件名上循环,一个简单的示例将生成这些目录和文件名的列表

# tree system/
system/
├── cat1
│?? └── id1
├── cat5
│?? └── id42
├── cat20
│?? └── 59593
└── mumbry
    └── 3939
4 directories, 4 files
#

如果要将它们拆分为directory和basename,shell中有内置的:

cd system
for file in */*; do
    echo "$file"
done

@tripleee中的目录和Basename替换在我的bash 4.2.25中不起作用,正确的for循环是:

for file in */*; do
    echo "Directory: ${file%/*}"
    echo "Basename: ${file#*/}"
done

这应该是一个评论。我不能对tripleee发表评论,我没有50点声望。

for file in */*; do
    # the match start from the end so you must invert the regexp
    echo "Directory: ${file%/*}" 
    # we must remove all the matches, not only the first
    echo "Basename: ${file##*/}" 
done

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章