批量修改文件后缀名

Bash批量修改文件后缀名

Mv files suffix in batch in one dir

 

一、# and %

#! /bin/bash

FILE=dir1/dir2/dir3/my.file.txt

echo "The source string"

echo ${FILE}     #dir1/dir2/dir3/my.file.txt

echo "Cut the first / and the characters left beside it"

echo ${FILE#*/}  #dir2/dir3/my.file.txt

echo "Cut the last / and the characters left beside it"

echo ${FILE##*/} #my.file.txt

echo "No function,just put out the source string"

echo ${FILE#/*}

echo "Cut the first / and the characters right beside it"

echo ${FILE%/*}  #dir1/dir2/dir3

echo "Cut the last / and the characters right beside it"

echo ${FILE%%/*}  #dir1

echo "No effect, just put out the source string"

echo ${FILE%*/}

#Remember like this

## left

#% right

#one first

#two last

 

FILE2=my.file.txt

echo "file name"

echo ${FILE2%%.*}

echo "suffix"

echo ${FILE2##*.}

 

Result

 The source string

dir1/dir2/dir3/my.file.txt

Cut the first / and the characters left beside it

dir2/dir3/my.file.txt

Cut the last / and the characters left beside it

my.file.txt

No function,just put out the source string

dir1/dir2/dir3/my.file.txt

Cut the first / and the characters right beside it

dir1/dir2/dir3

Cut the last / and the characters right beside it

dir1

No effect, just put out the source string

dir1/dir2/dir3/my.file.txt

file name

my

suffix

txt

 

二、Realize

[braveyly@m-net ~/bakfilerename]$ ls

1.c            2.c            3.c             4.c          

 

 

#!/bin/bash

for file in$(find . -name "*.c" -type f)

do

echo $file

echo "${file%%.*}" # print null ?

echo "${file%%.*}.o"

echo "${file%.c}.o" # cut the first . and the c right beside it ????

mv "$file" "${file%.c}.o"

done

 

[braveyly@m-net ~/bakfilerename]$ ./rename.sh

./1.c

.o

./1.o

./2.c

.o

./2.o

./3.c

.o

./3.o

./4.c

.o

./4.o

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值