编写一个shell脚本 实现批量压缩 (1)第一个位置参数 给定一个绝对路径 (2)对于该绝对路径下的每一个文件,判定其是否为一个目录,如果为一个目录则压缩,否则输出警告信息 (3)统计压缩的目录数量
时间: 2024-06-03 19:09:11 浏览: 9
#!/bin/bash
# 批量压缩脚本
# 参数1:要压缩的目录的绝对路径
# 统计压缩的目录数量
count=0
# 判断参数是否存在
if [ ! $1 ]; then
echo "缺少参数1:要压缩的目录的绝对路径"
exit 1
fi
# 判断目录是否存在
if [ ! -d $1 ]; then
echo "目录不存在:$1"
exit 1
fi
# 遍历目录下的文件
for file in `ls $1`
do
# 判断是否为目录
if [ -d "$1/$file" ]; then
# 压缩目录
echo "正在压缩目录:$1/$file"
tar -czvf "$1/$file.tar.gz" "$1/$file"
count=$(expr $count + 1)
else
# 输出警告信息
echo "警告:$1/$file 不是一个目录"
fi
done
echo "压缩完成,共压缩了$count 个目录"
相关问题
编写一个shell脚本,实现批量压缩,第一个位置参数给定一个绝对路径,对于该绝对路径下的每一个文件,判定其是否为一个目录,如果为一个目录则压缩,否则输出警告信息,统计压缩的目录数量
#!/bin/bash
count=0 # 统计压缩的目录数量
if [ $# -ne 1 ]; then
echo "Usage: $0 <absolute_path>"
exit 1
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory"
exit 1
fi
for file in $1/*; do
if [ -d $file ]; then
tar -czvf $file.tar.gz $file
echo "$file has been compressed successfully"
count=$((count+1))
elif [ -f $file ]; then
echo "$file is not a directory, skipping..."
fi
done
echo "Total directories compressed: $count"
写一个Windows下批量获取文件绝对路径的shell脚本
好的,以下是一个能够批量获取指定目录下所有文件的绝对路径的Windows Shell脚本:
```
@echo off
setlocal enableextensions enabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
for /r "%folder%" %%f in (*) do (
set "filePath=%%~ff"
echo !filePath!
)
endlocal
```
该脚本接受一个参数,用于指定要扫描的目录。如果未提供参数,则默认为当前目录。脚本使用循环遍历指定目录下的所有文件,并使用`%%~ff`语法获取每个文件的绝对路径,最后将其打印到控制台上。
请注意,该脚本仅适用于Windows操作系统。如果您需要在其他操作系统上运行,请相应地进行更改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)