重命名在bash的文件特定模式 [英] rename specific pattern of files in bash
本文介绍了重命名在bash的文件特定模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的文件和目录:
的/ tmp / JJ /
的/ tmp / JJ / ESE
的/ tmp / JJ /东南/ 2010
/tmp/jj/ese/2010/test.db
的/ tmp / JJ / dfhdh
的/ tmp / JJ / dfhdh / 2010
/tmp/jj/dfhdh/2010/rfdf.db
的/ tmp / JJ / ddfxcg
的/ tmp / JJ / ddfxcg / 2010
/tmp/jj/ddfxcg/2010/df.db
的/ tmp / JJ / ddfnghmnhm
的/ tmp / JJ / ddfnghmnhm / 2010
/tmp/jj/ddfnghmnhm/2010/sdfs.db
我想所有的 2010
目录重命名为它们的父目录,那么焦油
所有。分贝
文件...
我试过是:
#!/斌/庆典如果[$#-ne 1];然后
回声用法:`基本名$ 0`<绝对路径>中
1号出口
科幻如果[$(ID -u)=0!];然后
回声这个脚本必须以root身份运行1>和2
1号出口
科幻
RM / tmp / test目录找到$ 1 GT;> / tmp / test目录
在$线(CAT / tmp / test目录)
做
ARR = $((回声$线| awk的-F/'{为(i = 1; I< NF;我++),如果($ I ==2010)打印$(I-1)}') )
在指数$ {ARR [@]}
做
回声$指数#如何写MV命令,而不是echo命令?
DONE
DONE
1)的结果是:
ESE
dfhdh
ddfxcg
ddfnghmnhm
但是,应该是:
ESE
dfhdh
ddfxcg
ddfnghmnhm
2)我如何重新命名所有2010目录到它们的父目录?
我的意思是怎么做(我想这样做在循环
因为迪尔斯的LARG号码):
MV的/ tmp / JJ /东南/ 2010 / TMP / JJ /东南/ ESE
MV的/ tmp / JJ / dfhdh / 2010 / TMP / JJ / dfhdh / dfhdh
MV的/ tmp / JJ / ddfxcg / 2010 / TMP / JJ / ddfxcg / ddfxcg
MV的/ tmp / JJ / ddfnghmnhm / 2010 / TMP / JJ / ddfnghmnhm / ddfnghmnhm
解决方案
这应该是接近:
找到$ 1型Ð-name -print 2010 |
而IFS =读-r DIR
做
parentPath = $(目录名称$目录)
parentDir = $(基名$ parentPath)
回声MV$目录,$ parentPath / $ parentDir
DONE
测试后删除回声
。如果你的目录名称可以包含换行符然后寻找到 -print0
选项找到
和 -0
选项的xargs
。
I have the following files and directories:
/tmp/jj/
/tmp/jj/ese
/tmp/jj/ese/2010
/tmp/jj/ese/2010/test.db
/tmp/jj/dfhdh
/tmp/jj/dfhdh/2010
/tmp/jj/dfhdh/2010/rfdf.db
/tmp/jj/ddfxcg
/tmp/jj/ddfxcg/2010
/tmp/jj/ddfxcg/2010/df.db
/tmp/jj/ddfnghmnhm
/tmp/jj/ddfnghmnhm/2010
/tmp/jj/ddfnghmnhm/2010/sdfs.db
I want to rename all 2010
directories to their parent directories then tar
all .db
files...
What I tried is:
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: `basename $0` <absolute-path>"
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
rm /tmp/test
find $1 >> /tmp/test
for line in $(cat /tmp/test)
do
arr=$( (echo $line | awk -F"/" '{for (i = 1; i < NF; i++) if ($i == "2010") print $(i-1)}') )
for index in "${arr[@]}"
do
echo $index #HOW TO WRITE MV COMMAND RATHER THAN ECHO COMMAND?
done
done
1) The result is:
ese
dfhdh
ddfxcg
ddfnghmnhm
But it should be:
ese
dfhdh
ddfxcg
ddfnghmnhm
2) How can I rename all 2010 directories to their parent directory?
I mean how to do (I want to do it in loop
because of larg numbers of dirs):
mv /tmp/jj/ese/2010 /tmp/jj/ese/ese
mv /tmp/jj/dfhdh/2010 /tmp/jj/dfhdh/dfhdh
mv /tmp/jj/ddfxcg/2010 /tmp/jj/ddfxcg/ddfxcg
mv /tmp/jj/ddfnghmnhm/2010 /tmp/jj/ddfnghmnhm/ddfnghmnhm
解决方案
This should be close:
find "$1" -type d -name 2010 -print |
while IFS= read -r dir
do
parentPath=$(dirname "$dir")
parentDir=$(basename "$parentPath")
echo mv "$dir" "$parentPath/$parentDir"
done
Remove the echo
after testing. If your dir names can contain newlines then look into the -print0
option for find
, and the -0
option for xargs
.
这篇关于重命名在bash的文件特定模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文