HowTo: Bash Extract Filename And Extension In Unix / Linux
来源:互联网 发布:spring源码深度百度云 编辑:程序博客网 时间:2024/04/30 06:15
在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.tgztempfile="${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 sysloglogger "$_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 /rootmv -f /backcup/data/server42/latest.tar /backcup/data/server42/old.latest.tarcp -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 /rootmv -f /backcup/data/server42/latest.tgz /backcup/data/server42/old.latest.tgzcp -f /tmp/latest.tgz /backcup/data/server42/latest.tgz
转载自:http://www.cyberciti.biz/faq/unix-linux-extract-filename-and-extension-in-bash/
0 0
- HowTo: Bash Extract Filename And Extension In Unix / Linux
- HowTo: Use cat Command In Linux / UNIX
- filename extension
- HowTo: Use grep Command In Linux / UNIX [ Examples ]
- HowTo: Use grep Command In Linux / UNIX – Examples
- HowTo: Run the .sh File Shell Script In Linux / UNIX
- HowTo: Use grep Command In Linux / UNIX – Examples
- Some Point in learning Linux And Unix
- HowTo hide Password in shell Scripting using Bash
- 《去哪网编程题》filename extension
- 去哪儿笔试:filename extension
- Filename parsing in batch file and more idioms
- Howto Compile and Install Linux Kernel 3.3rc / 3.2 / 3.1 / 3.0 in Ubuntu 11.10, 11.04, 10.10 and 10.
- zombie and orphan process in Linux-Unix systems
- Bash Shell in Linux
- Howto - Cross Compiling in Linux using MingW32
- Linux Advanced Routing and Traffic Control HOWTO
- HowTo: Add Jobs To cron Under Linux or UNIX?
- 正则表达式
- a configuration error occured during startup
- Appium键盘处理
- 高效同步数据的方法及效率测试--边打包边压缩边传输边解压20150105
- C# Datatable导出CSV算法
- HowTo: Bash Extract Filename And Extension In Unix / Linux
- 把图片切成小片(IOS)
- 最简单的Curl实例
- 系统架构设计师考试
- C++11 auto and decltype
- MFC实现打开、保存文件对话框和浏览文件夹对话框
- TestBird果伦:手游开发前期的准备
- NSString特性分析学习
- 控制台 算术运算