【问题标题】:Bash: Creating subdirectories reading from a fileBash:创建从文件读取的子目录
【发布时间】:2013-07-06 18:20:30
【问题描述】:

我有一个包含一些关键字的文件,我打算使用 bash 脚本在同一关键字的同一目录中创建子目录。这是我正在使用的代码,但它似乎不起作用。 我不知道我哪里出错了。帮帮我

for i in `cat file.txt`
do
#        if [[ ! -e $path/$i ]]; then
            echo "creating" $i "directory"
            mkdir $path/$i
#       fi
            grep $i file >> $path/$i/output.txt
done
echo "created the files in "$path/$TEMP/output.txt

【问题讨论】:

  • 显示file.txt的内容。
  • 您是否收到错误消息?哪一个?
  • @anishsane file.txt 包含(每行中的每个 ABC、DEF、GHI、KJL)没关系,我明白了。这是我犯过的最愚蠢的错误之一。路径错了。感谢您的回答。 :) 美好的一天。
  • cat file.txt | xargs -n 1 -IDIR mkdir -p $path/DIR

标签: bash subdirectory


【解决方案1】:

你错了here,你错了here

while read i
do
  echo "Creating $i directory"
  mkdir "$path/$i"
  grep "$i" file >> "$path/$i"/output.txt
done < file.txt
echo "created the files in $path/$TEMP/output.txt"

【讨论】:

    【解决方案2】:

    78mkdir 将拒绝创建一个目录,如果它的一部分不存在。 例如如果没有/foo/bar 目录,则mkdir /foo/bar/baz 将失败。

    您可以通过使用-p 标志来放松这一点,它会在必要时创建父目录(在示例中,它可能会创建/foo/foo/bar)。

    您还应该使用引号,以防您的路径包含空格。

    mkdir -p "${path}/${i}"
    

    最后,确保您实际上被允许在$path 中创建目录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 2012-11-25
      • 2015-02-19
      • 2015-03-18
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多