当前位置: 首页 > news >正文

Linux 压缩制定目录下指定类型的多个文件

编写一个 Shell 脚本,将指定目录下的指定类型的文件打成一个压缩包

这里主要会涉及到文件查找find和打包tar命令

这是第一版,里面存在着很大的问题

if ["$#" -ne 3]; then # 这里是运行报错的第1行echo "用法:$0 <目录> <文件类型> <输出压缩包>"exit 1
fidir=$1
file_type=$2
compose_file_path=$3# 检查文件夹是否存在
if [! -d "$dir" ]; then # 这里是运行报错的第11行echo "error:目录:'$dir'不存在."exit 1
fi
# 符合要求的文件是否大于等于1个
target_files=$(find "$dir" -type f -name "$file_type")
if [ -z "$target_files"]; then # 这里是运行报错的第17行echo "error:没有找到:'$filee_type'对应类型的文件."exit 1
fi# 打包符合要求的文件tar -czvf "$compose_file_path" "$target_files"echo "打包成功."

我们来看看运行报错

root@hcss-ecs-c2b8:/var/test# vim compose_file.sh
root@hcss-ecs-c2b8:/var/test# chmod +x compose_file.sh 
# 这里我们要做的是将 ./目录下的以".sh"结尾类型的文件进行打包放在test.tar.gz中
root@hcss-ecs-c2b8:/var/test# ./compose_file.sh ./ "*.sh" test.tar.gz
./compose_file.sh: line 1: [3: command not found
./compose_file.sh: line 11: [!: command not found 
./compose_file.sh: line 17: [: missing `]' # 这三个个相同的报错是因为没有在中括号和条件之间空一格导致的
tar: ./compose_file.sh\n./dir1/param_trans.sh\n./dir1/hello.sh\n./dir1/helloworld.sh\n./file_move.sh: Cannot stat: No such file or directory # 这个报错是因为 在最后打包命令使用时,$target_files 需要没有引号,以便传递多个文件名,否则会将其识别为一个文件,这当然就找不到对应文件和目了
tar: Exiting with failure status due to previous errors
打包成功.

这里是修改后的脚本

if [ "$#" -ne 3 ]; thenecho "用法:$0 <目录> <文件类型> <输出压缩包>"exit 1
fidir=$1
file_type=$2
compose_file_path=$3# 检查文件夹是否存在
if [ ! -d "$dir" ]; thenecho "error:目录:'$dir'不存在."exit 1
fi
# 符合要求的文件是否大于等于1个
target_files=$(find "$dir" -type f -name "$file_type")
if [ -z "$target_files" ]; thenecho "error:没有找到:'$filee_type'对应类型的文件."exit 1
fi# 打包符合要求的文件tar -czvf "$compose_file_path" $target_filesecho "打包成功."

以及运行结果

root@hcss-ecs-c2b8:/var/test# ./compose_file.sh ./ "*.sh" test.tar.gz
./compose_file.sh: line 1: [3: command not found
./compose_file.sh
./dir1/param_trans.sh
./dir1/hello.sh
./dir1/helloworld.sh
./file_move.sh
打包成功.
root@hcss-ecs-c2b8:/var/test# ll
total 24
drwxr-xr-x  3 root root 4096 Sep 23 23:14 ./
drwxr-xr-x 14 root root 4096 Sep 21 08:28 ../
-rwxr-xr-x  1 root root  549 Sep 23 23:02 compose_file.sh*
drwxr-xr-x  2 root root 4096 Sep 22 10:07 dir1/
-rwxr-xr-x  1 root root  473 Sep 22 10:07 file_move.sh*
# 这个就是打包后产生的文件
-rw-r--r--  1 root root  931 Sep 23 23:13 test.tar.gz
root@hcss-ecs-c2b8:/var/test# ./compose_file.sh /var/test "*.sh" test.tar.gz
# 当我们使用绝对路径时这里会有一个提示
tar: Removing leading `/' from member names
/var/test/compose_file.sh
tar: Removing leading `/' from hard link targets
/var/test/dir1/param_trans.sh
/var/test/dir1/hello.sh
/var/test/dir1/helloworld.sh
/var/test/file_move.sh
打包成功.

当使用 tar 命令创建归档时,如果看到 tar: Removing leading ‘/’ from hard link targets 的消息,表示你在归档中使用了绝对路径。tar 会自动去掉绝对路径的前导斜杠,以便在解压时能够保持相对路径。


http://www.mrgr.cn/news/34684.html

相关文章:

  • YOLO V10简单使用
  • 0-1开发自己的obsidian plugin DAY 1
  • C++的哲学思想
  • iOS 顶级神器,巨魔录音机更新2.1正式版
  • 一看就会!PS2024下载安装教程详解
  • 在 Java 中,你如何实现不可变对象?不可变对象有哪些好处?
  • 【Godot4.3】三角形类
  • JS的链判断符有几种写法,有哪些用法?
  • # 深度学习笔记(9)huggingface 构建数据集
  • kubernetes网络(三)之bird的路由反射器的使用
  • 大数据新视界 --大数据大厂之 Reactjs 在大数据应用开发中的优势与实践
  • npm、yarn、pnpm 最新国内镜像源设置和常见问题解决
  • Axure精选各类组件案例集锦:设计灵感与实战技巧
  • 速盾:网页游戏部署高防服务器有什么优势?
  • Spring MVC 执行流程
  • FortiGate 硬盘格式化指南
  • C++11新特性和扩展(1)
  • LLMs之Qwen:Qwen2.5的简介、安装和使用方法、案例应用之详细攻略
  • 防护装备穿戴与否检测系统源码分享
  • Algo-Lab 2 Stack Queue ADT