1.解压
#!/bin/bash
#解压如 *.zip *.tar *.tar.gz 等压缩文件
ftype="$(file "$1")"
case "$ftype" in
"$1: Zip archive"*)
unzip "$1"
;;
"$1: Gzip compressed"*)
gunzip "$1"
;;
"$1: POSIX tar"*)
tar xvf $1
;;
"$1: gzip compressed"*)
tar xzvf $1
;;
*)
echo "file $1 can not be uncompressed"
esac
2.对多个文件重命名的简单版本
#!/bin/bash
#we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ]; then
cat << HELP
ren--renames a number of files using sed regular expressions USAGE:ren 'regexp'
'replacement' files...
EXAMPLE rename all *.HTM files in *.html;
ren 'HTM' 'html" *.HTM
HELP
fi
old="$1"
new="$2"
#the shift command removes one arguments from the list of command line arguments.
shift
shift
# $@ new contains all the files
for file in "$@"; do
if [ -f "$file" ]; then
newfile=`echo "$file" | sed "s/${old}/${new}/g"`
if [ -f "$newfile" ]; then
echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile"
mv "$file" "$newfile"
fi
fi
done
3.对多个文件进行重命名,版本2
#!/bin/bash
#用途:重命名所有 *.jpg *.png
#格式为: image1.jpg image2.jpg image3.png等
count=1
for img in *.jpg *.png; do
new=image-${count}.${img##*.}
mv "$img" "$new" 2>/dev/null
if [ $? -eq 0 ]; then
echo "rename $img to $new"
#((count++))
let count++
fi
done