linux - linux下如何自动创建目录?

标签 linux mkdir

我有一个名为 temp.txt 的文件,其中包含以下内容 https://abcdef/12345-xyz

https://ghifdfg/5426525-abc

我需要仅使用文件中每一行的数字部分在 Linux 中自动创建一个目录。 因此输出应该类似于创建的 123455426525 目录。

任何有关如何执行此操作的方法都可能有所帮助。

这是我从网上搜索得到的代码,在这段代码中,将以 BR 和 W0 开头的文件名创建新目录。

for file in {BR,W0}*.*; do
dir=${file%%.*}
mkdir -p "$dir"
mv "$file" "$dir"
done

最佳答案

假设每个 URL 的形式为

http[s]://any/symbols/some_digits-some_letters

那么你确实可以在 shell 变量扩展中使用简单的前缀和后缀修饰符。

${x##*/} 扩展为 x 的后缀部分,从最后一个斜杠 / 开始。

${y%%-*} 扩展为第一个 - 之前 y 的前缀部分。

while read x ; do
  y=${x##*/} 
  z=${y%%-*} 
  mkdir $z
done < temp.txt

关于linux - linux下如何自动创建目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463828/

相关文章:

c++ - 在 Linux 中为 C++ 应用程序指定静态库的位置

rsync - 为什么 rsync 使用没有 p 选项的 mkdir

php - mkdir() : Permission problems

linux - bash/linux - touch/mkdir 来自文件中的字符串

c++ - 共享库未在函数中链接

linux - 根据文件扩展名过滤 git diff-tree 的输出

c - Linux C : error of redefinition of in include headers

linux - Unix 删除最后两个标记

file - 如果存在同名文件夹,则无法创建文件

linux - mkdir "max name length"与 pathconf MAX_PATH & MAX_NAME