有时候需要 linux 目录下文件分别打包,再分别解包,就需要用到 shell 中的 for 语句进行循环了。 当目录中有空格的时候,也容易发生报错,容易把一个目录名分隔为两个目录名,这时候就需要设置 IFS 这个值了。

具体测试可用的打包脚本如下:

#!/bin/bash

IFS=$'\n'

function tar_dir() {
for file in `ls -a $1`
        do
                if [ "$file" != "." -a "$file" != ".." ]; then
                        echo $file
                        tar cvf ${file}.tar $1"/"$file
                        echo "$file" >> tar_done.txt
                fi
        done
}

tar_dir $1

参考

https://blog.csdn.net/qq_40806289/article/details/114919068
https://blog.csdn.net/qq_28686911/article/details/115410830
https://blog.csdn.net/mr_leehy/article/details/76383091
https://blog.csdn.net/zhan570556752/article/details/80399154
https://blog.csdn.net/iamlihongwei/article/details/59114828

标签: shell

添加新评论