gpt4 book ai didi

linux - 创建目录结构的 Bash 脚本

转载 作者:IT王子 更新时间:2023-10-29 00:08:10 25 4
gpt4 key购买 nike

我整晚都在谷歌上搜索,试图找到一种创建目录结构脚本的方法。看起来像这样:

/sharedshared/projectsshared/seriesshared/moviesshared/movies/action

You get the point.

The file that the script reads from look like this:

shared backupshared datashared projects shared projcets seriesshared projects moviesshared projects movies action

I want to create a script that reads each line in the file and run the following for each line:If the directory exist, it places itself in the directory and create the structure from there, ifThe directory doesn’t exist, create it.
When all entries in the row have been preceded by, go back to original directory and read the next line.

My system is Ubuntu 10.10.

So far I’ve done this, but it doesn’t work.

#!/bin/bash

pwd=$(pwd)

for structure in ${column[*]}
do
if [ $structure ]
then
cd $structure
else
mkdir $structure
fi
done

cd $pwd

最佳答案

您可以使用 mkdir -p shared/projects/movies/action 创建整个树:它将创建 shared,然后是 shared/projects,然后是 shared/projects/moviesshared/projects/movies/action

所以基本上您需要运行 mkdir -p $dir 的脚本,其中 $dir 是目录树的叶目录。

关于linux - 创建目录结构的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5374382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com