[Shell] Create directories in batches, modify directory names in batches

1. Create directories in batches

1.1 Create a directory under the current directory

1. Create a file in the current directory dirName.txt, which is used to store the directory name, and write a directory name in each line.

Note: There can be no spaces at the end of each line, use vscodeOpen, and select the line ending sequence asLF

insert image description here
2. Create a file in the current directory batch_mkdir.shwith the following content:

# 批量创建目录
for dirName in `cat  dirName.txt`
do
    echo $dirName
    mkdir $dirName
done

Execute the script again sh batch_mkdir.sh , that is, the directory test1, test2,test3

# 执行脚本
$ sh batch_mkdir.sh
test1
test2
test3

# 查看目录结构
$ tree
.
|-- batch_mkdir.sh
|-- dirName.txt
|-- test1
|-- test2
`-- test3

3 directories, 2 files

1.2 Create a multi-level directory

1. mkdir -pYou can create a multi-level directory, such as mkdir -p a/b/ccreating a multi-level directory under the current directory a -b -c.

# 创建多级目录
$ mkdir -p a/b/c

# 查看目录结构
$ tree a
a
`-- b
    `-- c

2 directories, 0 files

2. If you want to recursively create directories in batches, you only need to change the content format of each line mkdirin to mkdir -pformat .dirName.txta/b/c

# 批量创建多级目录
for dirName in `cat  dirName.txt`
do
    echo $dirName
    mkdir -p $dirName
done

Example: dirName.txtthe content is as follows:

# 查看dirName.txt内容
$ cat dirName.txt
test1/a
test2/a
test3/a

The result of executing the script is as follows:

# 执行脚本
$ sh batch_mkdir.sh
test1/a
test2/a
test3/a

# 查看目录结构
$ tree
.
|-- batch_mkdir.sh
|-- dirName.txt
|-- test1
|   `-- a
|-- test2
|   `-- a
`-- test3
    `-- a

6 directories, 2 files

2. Modify directory names in batches

2.1 Modify the directory name under the current directory

oldName.txt1. Create a file and in the current directory newName.txt, which are used to store the original directory name and the new directory name respectively:

Note: There can be no spaces at the end of each line, use vscodeOpen, and select the line ending sequence asLF

The original directory name is as follows:

# 查看oldName.txt内容
$ cat oldName.txt
test1
test2
test3

The new directory names are as follows:

# 查看newName.txt内容
$ cat newName.txt
test_1
test_2
test_3

2. Create a file in the current directory batch_rename_dir.shwith the following content:

# 批量修改目录名
lines=`sed -n '$=' oldName.txt` # oldName.txt的行数

for line in `seq 1 $lines`  
do  
    oldName=`sed -n "${line}p" oldName.txt`
    newName=`sed -n "${line}p" newName.txt`
    mv $oldName $newName
    echo "$oldName -> $newName"
done  

The result of executing the script is as follows:

# 执行脚本前的目录结构
$ tree
.
|-- batch_rename_dir.sh
|-- newName.txt
|-- oldName.txt
|-- test1
|-- test2
`-- test3

3 directories, 3 files

# 执行脚本
$ sh batch_rename_dir.sh
test1 -> test_1
test2 -> test_2
test3 -> test_3

# 执行脚本后的目录结构
$ tree
.
|-- batch_rename_dir.sh
|-- newName.txt
|-- oldName.txt
|-- test_1
|-- test_2
`-- test_3

3 directories, 3 files

2.2 Modify multi-level directory

Just change oldName.txtthe and newName.txtcontent format to a multi-level directory format, a multi-level directory format, such as a/b/c.

The original directory name is as follows:

$ cat oldName.txt
test1/a
test2/a
test3/a

The new directory names are as follows:

$ cat newName.txt
test1/b
test2/b
test3/b

The result of executing the script is as follows:

# 执行脚本前的目录结构
$ tree
.
|-- batch_rename_dir.sh
|-- newName.txt
|-- oldName.txt
|-- test1
|   `-- a
|-- test2
|   `-- a
`-- test3
    `-- a

6 directories, 3 files
 
 # 执行脚本
$ sh batch_rename_dir.sh
test1/a -> test1/b
test2/a -> test2/b
test3/a -> test3/b

# 执行脚本后的目录结构
$ tree
.
|-- batch_rename_dir.sh
|-- newName.txt
|-- oldName.txt
|-- test1
|   `-- b
|-- test2
|   `-- b
`-- test3
    `-- b

6 directories, 3 files

For other batch file operations, you can view my other article:
[Shell] Batch operation files: find file paths, delete files in batches, rename files in batches, move files to parent directories in batches

Guess you like

Origin blog.csdn.net/aidijava/article/details/127155100