在 CentOS(或一般 Linux)里,“extract”通常不是系统自带命令,而是大家自己写的一个解压脚本/函数,用来自动识别压缩格式并解压。
下面给你几种常见写法,从简单脚本到常用函数都有。
vim /usr/local/bin/extract
#!/bin/bash
if [ $# -eq 0 ]; then
echo "用法: extract 文件名"
exit 1
fi
for file in "$@"; do
if [ ! -f "$file" ]; then
echo "文件不存在: $file"
continue
fi
case "$file" in
*.tar) tar xvf "$file" ;;
*.tar.gz) tar xzvf "$file" ;;
*.tgz) tar xzvf "$file" ;;
*.tar.bz2) tar xjvf "$file" ;;
*.tar.xz) tar xJvf "$file" ;;
*.gz) gunzip "$file" ;;
*.bz2) bunzip2 "$file" ;;
*.xz) unxz "$file" ;;
*.zip) unzip "$file" ;;
*.rar) unrar x "$file" ;;
*.7z) 7z x "$file" ;;
*) echo "不支持的格式: $file" ;;
esac
done
chmod +x /usr/local/bin/extract
extract test.tar.gz
extract a.zip b.tar.xz
如果你不想建文件,可以直接加到:
~/.bashrc
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.tar.xz) tar xJf "$1" ;;
*.tar) tar xf "$1" ;;
*.zip) unzip "$1" ;;
*.gz) gunzip "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.xz) unxz "$1" ;;
*) echo "无法识别: $1" ;;
esac
else
echo "文件不存在: $1"
fi
}
生效:
source ~/.bashrc
有些格式需要额外安装:
yum install -y unzip bzip2 xz unrar p7zip
如果你愿意,我可以帮你写一个**“解压到指定目录”的版本或企业级脚本**。
你是想用在服务器运维还是个人方便使用?