使用shell将zip, rar压缩包压缩到同目录的文件夹中

今天在研究压缩包解压的时候,发现批量化处理相关的脚本还是挺少的。和大家分享下如何快速用shell识别不同压缩包并在同名目录下解压缩。

#unzipp.sh

# echo "${FILE%%.*}"
# # => example
 
# echo "${FILE%.*}"
# # => example.tar
 
# echo "${FILE#*.}"
# # => tar.gz
 
# echo "${FILE##*.}"
# # => gz


cd {这里放入你的路径}
for file in $(ls)
do

    if [ "${file#*.}" = "zip" ]; then
        
        eval sudo unzip -o "${file%.*}" -d "${file%%.*}/"
    fi
    if [ "${file#*.}" = "rar" ]; then
        eval sudo unrar x "${file%.*}" " ${file%%.*}/"
    fi
done

在运行的时候,可以选择用source命令防止建立子bash执行

source ./unzipp.sh

参考:
https://blog.csdn.net/ken2232/article/details/132020162
https://www.cnblogs.com/kuangsyx/p/12595888.html

你可能感兴趣的:(unix)