0
!#/bin/bash
#
#
for filename in `ls`
do
filename_new=`echo $filename | sed 's/.bak//g'`
mv $filename $filename_new
done
echo "complete!"
#
#
for filename in `ls`
do
filename_new=`echo $filename | sed 's/.bak//g'`
mv $filename $filename_new
done
echo "complete!"
0
本帖最后由 ocean 于 2014-5-5 15:28 编辑
#!/bin/bash
#
#批量把这些文件都还原,也就是把.bak的后缀都去掉。
#
for i in `ls /root/*.bak`
do
newname=`echo test.bak.bak |sed -n 's/.bak.bak//p'`
# mv $i ${i%.*}
mv $i $newname
done
#!/bin/bash
#
#批量把这些文件都还原,也就是把.bak的后缀都去掉。
#
for i in `ls /root/*.bak`
do
newname=`echo test.bak.bak |sed -n 's/.bak.bak//p'`
# mv $i ${i%.*}
mv $i $newname
done
0
#! /bin/bash
cd /root
for i in `ls|grep 'bak'`;do
a=`echo $i|awk -F'.bak' '{print $0}'`
b=`echo $i|awk -F'.bak' '{print $1}'`
mv $a $b
done
cd /root
for i in `ls|grep 'bak'`;do
a=`echo $i|awk -F'.bak' '{print $0}'`
b=`echo $i|awk -F'.bak' '{print $1}'`
mv $a $b
done
0
#!/bin/bash
cd /root
for i in `ls *bak`
do
newfile=`echo $i | sed 's/\.bak//g' `
mv $i $newfile
done
cd /root
for i in `ls *bak`
do
newfile=`echo $i | sed 's/\.bak//g' `
mv $i $newfile
done
0
#!/bin/bash
## This script is for change file name back.
## Writed by Louis on 2014/08/29 17:30
for f1 in `ls .`; do
n=`echo $f1|grep '.bak'`
if [ ! -z $n ];then
f2=`echo $f1|cut -d. -f1,2`
mv $f1 $f2
fi
done
#!/bin/bash
## This script is for change file name back.
## Writed by Louis on 2014/08/29 17:30
for f1 in `ls .`; do
n=`echo $f1|grep '.bak'`
if [ ! -z $n ];then
f2=`echo $f1|cut -d. -f1,2`
mv $f1 $f2
fi
done
0
#! /bin/bash
ls -l test > test.txt # 我是把.bak文件放在/tmp/test目录下进行操作
awk '{print $9}' test.txt |grep -v '^$' |sed 's/.bak//g'
ls -l test > test.txt # 我是把.bak文件放在/tmp/test目录下进行操作
awk '{print $9}' test.txt |grep -v '^$' |sed 's/.bak//g'
0
#!/bin/bash
for file in `ls /root/baktest/`
do
if [ -f $file ]
then
name=`ls $file|grep bak`
echo $name
if [ ! -z $name ]
then
newname=`ls $file| sed 's/\.bak//g'`
mv $file $newname
fi
fi
done
for file in `ls /root/baktest/`
do
if [ -f $file ]
then
name=`ls $file|grep bak`
echo $name
if [ ! -z $name ]
then
newname=`ls $file| sed 's/\.bak//g'`
mv $file $newname
fi
fi
done
0
本帖最后由 程城 于 2014-9-9 23:42 编辑
#!/bin/bash
for i in `find /chengcheng/*.bak -type f`;do
b=`echo $i | awk -F '.' '{print $1}'`
mv $i $b
done
或者改为.txt文件
#!/bin/bash
for i in `find /chengcheng/*.bak -type f`;do
b=`echo $i | awk -F '.' '{print $1}'`
mv $i $b.txt
done
#!/bin/bash
for i in `find /chengcheng/*.bak -type f`;do
b=`echo $i | awk -F '.' '{print $1}'`
mv $i $b
done
或者改为.txt文件
#!/bin/bash
for i in `find /chengcheng/*.bak -type f`;do
b=`echo $i | awk -F '.' '{print $1}'`
mv $i $b.txt
done
0
#! /bin/bash
for filename in `ls | grep '.bak\>$'` ;do
file=`echo "$filename" |sed 's/.bak\>$//'`
mv $filename $file
done
~
~
~
for filename in `ls | grep '.bak\>$'` ;do
file=`echo "$filename" |sed 's/.bak\>$//'`
mv $filename $file
done
~
~
~
0
- #!/bin/bash
- # This is a batch delete file name suffix scripts.
- # Writen by Wangxiaoqiang 2014-11-24.
- DIR=~
- FILE=$(cd ${DIR} ; ls *.bak)
- for i in ${FILE}
- do
- KEY=`echo ${i} | awk -F . '{NF-=2 ; OFS="." ; print $0}'`
- mv ${i} ${KEY}
- done
- ls ${DIR}
- # End
0
for i in `ls /root/ | grep ".bak"`;do newname="`echo $i | sed -n 's/\.bak//gp'`";mv $i $newname;done
0
#!/bin/bash
#author:GY
#Date:2014-12-3
dir=/root
for i in `ls $dir |grep '.bak'`; do
mv $i `echo $i | awk -F '.bak' '{print $1}'`
done
#author:GY
#Date:2014-12-3
dir=/root
for i in `ls $dir |grep '.bak'`; do
mv $i `echo $i | awk -F '.bak' '{print $1}'`
done
0
#!/bin/bash
# -------------------------------------------------------------------------------
# Filename: amingtest5.sh
# Revision: 1.0
# Date: 2014/12/17
# Author: FUQIANG LI
# Email: 317377106@qq.com
# Notes: aminglinux shell exercises of 2013-09-29
# -------------------------------------------------------------------------------
for file in `find -type f -name "*.bak.bak"|awk -F / '{print $2}'`
do
mv $file `echo $file |sed -r 's/.bak.bak//g'`
done
# -------------------------------------------------------------------------------
# Filename: amingtest5.sh
# Revision: 1.0
# Date: 2014/12/17
# Author: FUQIANG LI
# Email: 317377106@qq.com
# Notes: aminglinux shell exercises of 2013-09-29
# -------------------------------------------------------------------------------
for file in `find -type f -name "*.bak.bak"|awk -F / '{print $2}'`
do
mv $file `echo $file |sed -r 's/.bak.bak//g'`
done
0
#! /bin/bash
# 批量去除文件的扩展名
filepath=/tmp/
for file in `find $filepath -name "*_bak"`
do
mv $file ${file%.*}
done
${file%.*}是bash中字符处理“掐头去尾法”的去尾法。
# 批量去除文件的扩展名
filepath=/tmp/
for file in `find $filepath -name "*_bak"`
do
mv $file ${file%.*}
done
${file%.*}是bash中字符处理“掐头去尾法”的去尾法。
0
本帖最后由 寻@轨迹 于 2015-2-6 17:56 编辑
#! /bin/bash
# Written by yuhui
cd /root/bak
for sf in `ls`;
do
re=`ls $sf | tr '.bak' ' '`
mv $sf $re
done
#! /bin/bash
# Written by yuhui
cd /root/bak
for sf in `ls`;
do
re=`ls $sf | tr '.bak' ' '`
mv $sf $re
done
0
#/bin/bash
for file in *; do
nf=`ls $file |awk -F '.' '{print NF}'`
first_nf=`ls $file |awk -F '.' '{print $1}'`
second_nf=`ls $file |awk -F '.' '{print $2}'`
filename=`ls $file |awk -F '.' '{print $1"."$2}'`
if (($nf==1)); then
continue
else
if [ "$second_nf" == "bak" ]; then
mv $file $first_nf
else
mv $file $filename
fi
fi
done
for file in *; do
nf=`ls $file |awk -F '.' '{print NF}'`
first_nf=`ls $file |awk -F '.' '{print $1}'`
second_nf=`ls $file |awk -F '.' '{print $2}'`
filename=`ls $file |awk -F '.' '{print $1"."$2}'`
if (($nf==1)); then
continue
else
if [ "$second_nf" == "bak" ]; then
mv $file $first_nf
else
mv $file $filename
fi
fi
done
0
本帖最后由 wuhen 于 2015-2-17 21:45 编辑
find /root -type f -name "*.bak.bak"|sed 's/\.bak\.bak/\.bak/g' 变成*.bak
find /root -type f -name "*.bak.bak"|sed 's/\.bak.bak//g' 变成文件没有bak
find /root -type f -name "*.bak.bak"|sed 's/\.bak\.bak/\.bak/g' 变成*.bak
find /root -type f -name "*.bak.bak"|sed 's/\.bak.bak//g' 变成文件没有bak
0
#!/bin/bash
#
filename=`ls /root | grep '.bak'`
for i in $filename ; do
newfilename=`ls $i | sed 's/.bak//g'`
mv $i $newfilename
done
#
filename=`ls /root | grep '.bak'`
for i in $filename ; do
newfilename=`ls $i | sed 's/.bak//g'`
mv $i $newfilename
done
0
以下脚本实现把/emma目录下所有文件加上.bak
#!/bin/bash
#writen by alex on 2014.11.19 about add".bak"
cd /emma
for i in `ls`;do #列出/test 目录下的所有文件
mv $i $i.bak #在文件名的后面加上.bak
done
以下脚本实现把/emma目录下所有文件名后的.bak 去掉
#!/bin/bash
#writen by alex.zhu on 2014.11.19 about "delete .bak"
cd /emma
for i in `ls`;do
mv $i `echo $i |sed 's#.bak##' ` #mv 为重命名命令, sed 's#.bak##' 为替换命令。把每次循环得到得文件名(如a.bak) 后面的.bak 替换为空 。所以就实>现了 把文件名后面的.bak去掉的目的。
done
#!/bin/bash
#writen by alex on 2014.11.19 about add".bak"
cd /emma
for i in `ls`;do #列出/test 目录下的所有文件
mv $i $i.bak #在文件名的后面加上.bak
done
以下脚本实现把/emma目录下所有文件名后的.bak 去掉
#!/bin/bash
#writen by alex.zhu on 2014.11.19 about "delete .bak"
cd /emma
for i in `ls`;do
mv $i `echo $i |sed 's#.bak##' ` #mv 为重命名命令, sed 's#.bak##' 为替换命令。把每次循环得到得文件名(如a.bak) 后面的.bak 替换为空 。所以就实>现了 把文件名后面的.bak去掉的目的。
done
0
#!/bin/bash
# delete bank
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
# delete bank
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
0
#!/bin/bash
# delete bank
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
# delete bank
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
for file in `find . -name "*.bank"`
do
mv $file ${file%.*}
done
0
本帖最后由 ヾSun 于 2015-12-20 19:26 编辑
- #!bin/bash
- for f in $(find ./ -name "*.bak"|sed 's/.bak/ /g')
- do
- mv ${f}.bak ${f}
- done
0
#!/bin/bash
ls -l | awk '{print $9}' | grep -v '^$' | grep '_bak$' > /tmp/1.txt
for i in `cat /tmp/1.txt`
do
b=`grep "$i" /tmp/1.txt | sed "/$i/s/_bak$//"`
mv $i $b
done
rm -rf /tmp/1.txt
ls -l | awk '{print $9}' | grep -v '^$' | grep '_bak$' > /tmp/1.txt
for i in `cat /tmp/1.txt`
do
b=`grep "$i" /tmp/1.txt | sed "/$i/s/_bak$//"`
mv $i $b
done
rm -rf /tmp/1.txt
0
- #!/bin/bash
- find /root -type f -iname "*.bak"|while read line
- do
- newname=`echo -n $line|sed 's/.bak//g'`
- mv $line $newname
- done
0
#!/bin/bash
##在你的/root目录下有.bak文件,批量把这些文件都还原,也就是把.bak的后缀都去掉。##write by 2016-01-12
for i in `ls *.bak`;do
a=${i%%.*}
#echo $a
mv $i $a
done
##在你的/root目录下有.bak文件,批量把这些文件都还原,也就是把.bak的后缀都去掉。##write by 2016-01-12
for i in `ls *.bak`;do
a=${i%%.*}
#echo $a
mv $i $a
done
0
如图,在你的/root 目录下有如下文件,请写一脚本批量把这些文件都还原,也就是把.bak的后缀都去掉。
#!/bin/bash
#find /root/ -name "*.bak.bak" -type f > /tmp/1.txt
#sed -i.bak -r "s/.bak.bak//g" /tmp/1.txt
nn=$(cat /tmp/1.txt|sed -n ""p|wc -l)
for i in $(seq 1 $nn)
do
if [ "$(sed -n "$i"p /tmp/1.txt.bak)" == "$(sed -n "$i"p /tmp/1.txt).bak.bak" ]
then
cat $(sed -n "$i"p /tmp/1.txt.bak) > $(sed -n "$i"p /tmp/1.txt)
fi
done
#!/bin/bash
#find /root/ -name "*.bak.bak" -type f > /tmp/1.txt
#sed -i.bak -r "s/.bak.bak//g" /tmp/1.txt
nn=$(cat /tmp/1.txt|sed -n ""p|wc -l)
for i in $(seq 1 $nn)
do
if [ "$(sed -n "$i"p /tmp/1.txt.bak)" == "$(sed -n "$i"p /tmp/1.txt).bak.bak" ]
then
cat $(sed -n "$i"p /tmp/1.txt.bak) > $(sed -n "$i"p /tmp/1.txt)
fi
done
0
本帖最后由 gxp2008 于 2016-3-1 22:26 编辑
- #!/bin/bash
- 4
- 5 while :
- 6 do
- 7 a=`ls |sed 's/.bak//' |awk '/.bak/'`
- 8 if [-z "$a"]
- 9 then
- 10 exit
- 11 else
- 12 echo $a
- 13 fi
- 14 done
0
之前的,发现给for循环,传不进参数。没法删除bak后缀。使用rename,只能替换文件名,删除后缀,使用perl正则无效。百思不得其解,网上搜到了这个,这个还是比较有效的
- for i in $(ls .)
- do
- mv $i $(echo $i|sed 's/\.bak//')
- done
之前的,发现给for循环,传不进参数。没法删除bak后缀。使用rename,只能替换文件名,删除后缀,使用perl正则无效。百思不得其解,网上搜到了这个,这个还是比较有效的
0
- #!/bin/bash
- dir=/root/lianxi
- for file in $dir/*
- do
- newFile=$(echo $file|sed 's/\(.bak\)\+//')
- echo $newFile
- mv $file $newFile
- done
0
#/bin/bash
#批量改后缀(去掉.bak)
for i in `ls /root | grep .bak.bak$ |sed 's/.bak.bak//g'`
do
mv /root/$i.bak.bak /root/$i
done
#批量改后缀(去掉.bak)
for i in `ls /root | grep .bak.bak$ |sed 's/.bak.bak//g'`
do
mv /root/$i.bak.bak /root/$i
done
0
#!/bin/bash
dir=/root
for file in $dir/*;do
echo $file
newFile=$(echo $file |sed 's/\(.bak\)\+/ /')
mv $file $newFile
done
dir=/root
for file in $dir/*;do
echo $file
newFile=$(echo $file |sed 's/\(.bak\)\+/ /')
mv $file $newFile
done
0
本帖最后由 xebszw 于 2016-8-18 20:13 编辑
dir=/root/
ext=".bak.bak"
cd $dir
for file in $(ls $dir |grep $ext);do
name=$(ls $file |cut -d. -f1)
mv $file $name
done
dir=/root/
ext=".bak.bak"
cd $dir
for file in $(ls $dir |grep $ext);do
name=$(ls $file |cut -d. -f1)
mv $file $name
done
0
#!/bin/bash
#在你的/root目录下有.bak.bak后缀名文件,请写一脚本批量把这些文件都还原,也就是把.bak的后缀都
去掉。
for file in `ls /root/aa/*.bak`;do
b=`ls $file |sed 's/.bak//g'`
mv $file $b
done
#在你的/root目录下有.bak.bak后缀名文件,请写一脚本批量把这些文件都还原,也就是把.bak的后缀都
去掉。
for file in `ls /root/aa/*.bak`;do
b=`ls $file |sed 's/.bak//g'`
mv $file $b
done
0
#!/bin/bash
a=`ls /tmp/bak`
cd /tmp/bak
for i in $a
do
newname=`ls $i |sed 's/.bak//'`
mv $i $newname
done
a=`ls /tmp/bak`
cd /tmp/bak
for i in $a
do
newname=`ls $i |sed 's/.bak//'`
mv $i $newname
done
0
#!/bin/bash
cd /root
for file in `ls /root|grep '.bak.bak'`;
do
newfile=`echo $file|sed 's#.bak.bak##g'`
mv $file $newfile
done
cd /root
for file in `ls /root|grep '.bak.bak'`;
do
newfile=`echo $file|sed 's#.bak.bak##g'`
mv $file $newfile
done
0
#!/bin/bash
#Description: This script is to batch rename file names
#Author: Jiazhi Yang
#Date: 14/11/2016
#Script Name: batchrename.sh
#定义文件的目录
PWD='/tmp/exercise'
#进入到文件的目录
cd $PWD
#首先通过ls将.bak.bak结尾的文件列出来,然后通过NF(文件的域个数,在这里相当于几个bak),OFS就是域的分隔符
for i in `ls |grep '.bak.bak$'`;do
/bin/mv $i `ls $i |awk -F '.' 'NF-=2' OFS='.'`
done
#Description: This script is to batch rename file names
#Author: Jiazhi Yang
#Date: 14/11/2016
#Script Name: batchrename.sh
#定义文件的目录
PWD='/tmp/exercise'
#进入到文件的目录
cd $PWD
#首先通过ls将.bak.bak结尾的文件列出来,然后通过NF(文件的域个数,在这里相当于几个bak),OFS就是域的分隔符
for i in `ls |grep '.bak.bak$'`;do
/bin/mv $i `ls $i |awk -F '.' 'NF-=2' OFS='.'`
done
0
#!/bin/bash
file=`ls |grep 'bak'`
for i in $file
do
filename=`ls $i |sed 's/.bak//g'`
mv $i $filename
done
0
[root@aiker01 rename]# vim rename.sh
#!/bin/bash
#by Aiker
for i in `ls | grep .bak`; do mv $i `echo "$i" | awk -F '.bak' '{print $1}'`;done
[root@aiker01 rename]# ll
total 16
-rw-r--r--. 3 aiker aiker 0 Dec 2 16:11 11.txt.bak.bak.bak
-rwxrwxrwx. 1 root root 10 Dec 4 01:16 120301.txt.bak.bak.bak
-rw-r--r--. 3 aiker aiker 0 Dec 2 16:11 1.txt.bak.bak.bak
-rw-r--r--. 1 root root 0 Dec 2 23:01 2.txt.bak
-rw-r--r-- 1 root root 0 Dec 7 02:28 {3...5}.txt.bak
-rw-r--r-- 1 root root 0 Dec 7 02:27 3.txt.bak
-rw-r--r-- 1 root root 0 Dec 7 02:27 4.txt.bak
-rw-rw-r--. 1 root root 0 Dec 7 02:27 5.txt.bak
-rw-rw-r--. 1 root root 0 Dec 2 22:44 6.txt.bak
-rw-rw-r--. 1 root root 0 Dec 2 22:48 7.txt.bak
-rw-------. 1 root root 7668 Dec 2 16:25 a.txt.bak
-rw-r----- 1 root root 0 Dec 7 00:51 file1.txt.bak
-rw-r----- 1 root root 0 Dec 7 00:51 file2.txt.bak
-rw-r----- 1 root root 0 Dec 7 00:51 file3.txt.bak
----r----- 1 root root 0 Dec 7 00:51 file4.txt.bak
----r----- 1 root root 0 Dec 7 00:51 file5.txt.bak
-rw----r-- 1 root root 0 Dec 7 00:51 file[6-8].txt.bak
-rw-r--r-- 1 root root 0 Dec 7 00:13 mycprogram.txt.bak
-rw-r--r-- 1 root root 0 Dec 7 00:13 MyCProgram.txt.bak
-rw-r--r-- 1 root root 0 Dec 7 00:13 Program.txt.bak
-rw-r--r-- 1 root root 104 Dec 12 01:23 rename.sh
[root@aiker01 rename]# sh -x rename.sh
[root@aiker01 rename]# ll
total 16
-rw-r--r--. 3 aiker aiker 0 Dec 2 16:11 11.txt
-rwxrwxrwx. 1 root root 10 Dec 4 01:16 120301.txt
-rw-r--r--. 3 aiker aiker 0 Dec 2 16:11 1.txt
-rw-r--r--. 1 root root 0 Dec 2 23:01 2.txt
-rw-r--r-- 1 root root 0 Dec 7 02:28 {3...5}.txt
-rw-r--r-- 1 root root 0 Dec 7 02:27 3.txt
-rw-r--r-- 1 root root 0 Dec 7 02:27 4.txt
-rw-rw-r--. 1 root root 0 Dec 7 02:27 5.txt
-rw-rw-r--. 1 root root 0 Dec 2 22:44 6.txt
-rw-rw-r--. 1 root root 0 Dec 2 22:48 7.txt
-rw-------. 1 root root 7668 Dec 2 16:25 a.txt
-rw-r----- 1 root root 0 Dec 7 00:51 file1.txt
-rw-r----- 1 root root 0 Dec 7 00:51 file2.txt
-rw-r----- 1 root root 0 Dec 7 00:51 file3.txt
----r----- 1 root root 0 Dec 7 00:51 file4.txt
----r----- 1 root root 0 Dec 7 00:51 file5.txt
-rw----r-- 1 root root 0 Dec 7 00:51 file[6-8].txt
-rw-r--r-- 1 root root 0 Dec 7 00:13 mycprogram.txt
-rw-r--r-- 1 root root 0 Dec 7 00:13 MyCProgram.txt
-rw-r--r-- 1 root root 0 Dec 7 00:13 Program.txt
-rw-r--r-- 1 root root 104 Dec 12 01:23 rename.sh
编辑回复