HowTo: Bash Extract Filename And Extension In Unix / Linux

在Unix / Linux 下输出文件名


Find out filename

The syntax is as follows to remove the pattern (front of $VAR):

${var#Pattern}          
${var##Pattern} 

To get file name, enter:

dest="/nas100/backups/servers/z/zebra/mysql.tgz"
echo "${dest##*/}"

OR

dest="/nas100/backups/servers/z/zebra/mysql.tgz"
f="${dest##*/}"
echo "${f}"

Sample outputs:

mysql.tgz

Find out file extension

The syntax is as follows to remove the pattern from back of $VAR:

dest="/nas100/backups/servers/z/zebra/mysql.tgz"
echo "${dest##*.}"

OR

dest="/nas100/backups/servers/z/zebra/mysql.tgz"
e="${dest##*.}"
echo "${e}"

Sample outputs:
tgz

Extract filename i.e. filename without extension

The syntax is as follows to remove the pattern from back of $VAR:

${var%pattern}
${var%%pattern}

To get filename without an extension, enter:

dest="/nas100/backups/servers/z/zebra/mysql.tgz"
## get file name i.e. basename such as mysql.tgz
tempfile="${dest##*/}"
 
## display filename 
echo "${tempfile%.*}"

Sample outputs:

mysql

Putting it all together

#!/bin/bash
## A sample shell script to demo concept of shell parameter expansion
## Usage: backup.bash /path/to/backup.tar.gz 
## Author: nixCraft <www.cyberciti.biz> under GPL v2.x+
## -------------------------------------------------------------------
 
## Get our script name ##
_me="${0##*/}"
 
## get filename from cmd arg $1
_backuppath="$1"
 
## Failsafe 
[[ $# -ne 1 ]] && { echo -en "Usage:\t$_me /path/to/file.tar\n\t$_me /path/to/file.tgz\n"; exit 1; }
 
## Backup these dirs 
_what="/etc /home /root"
 
## Get dirname 
_dirname="${_backuppath%/*}"
 
# Get filename 
_filename="${_backuppath##*/}"
 
# Get file extension 
_extesion="${_filename##*.}"
 
# Set tar options
_opt=""
 
# Old backup file name starts with
_oldsuff="old"
 
 
## Okay log data to syslog
logger "$_me backup job started at $(date)@${HOSTNAME}"
 
## make decision based upon file extension
[[ "$_extesion" == "tgz" ]] && { _opt="zcvf"; _oldpref="tgz"; }
[[ "$_extesion" == "tar" ]] && { _opt="cvf"; _oldpref="tar";  }
 
 
## Just display commands for demo purpose ##
echo "tar $_opt /tmp/${_filename} $_what"
echo "mv -f ${_backuppath} ${_dirname}/${_oldsuff}.${_filename%.*}.${_oldpref}"
echo "cp -f /tmp/${_filename} ${_backuppath}"
 
logger "$_me backup job ended at $(date)@${HOSTNAME}"

Run the script as follows:

backup.bash /backcup/data/server42/latest.tar

Sample outputs:

tar cvf /tmp/latest.tar /etc /home /root
mv -f /backcup/data/server42/latest.tar /backcup/data/server42/old.latest.tar
cp -f /tmp/latest.tar /backcup/data/server42/latest.tar

Again run as follows:

backup.bash /backcup/data/server42/latest.tgz

Sample outputs:

tar zcvf /tmp/latest.tgz /etc /home /root
mv -f /backcup/data/server42/latest.tgz /backcup/data/server42/old.latest.tgz
cp -f /tmp/latest.tgz /backcup/data/server42/latest.tgz

转载自:http://www.cyberciti.biz/faq/unix-linux-extract-filename-and-extension-in-bash/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值