2009-07-30 167 views
1

我需要删除的扩展名 “.tex”:子目录删除扩展

./1-aoeeu/1.tex 
./2-thst/2.tex 
./3-oeu/3.tex 
./4-uoueou/4.tex 
./5-aaa/5.tex 
./6-oeua/6.tex 
./7-oue/7.tex 

请下面一些工具做到这一点:

  1. 桑达并找到

  2. 红宝石

  3. Python

我可怜的尝试:

$find . -maxdepth 2 -name "*.tex" -ok mv `sed '[email protected]@@g' {}` {} + 

回答

4

Python脚本做同样的:

import os.path, shutil 

def remove_ext(arg, dirname, fnames): 
    argfiles = (os.path.join(dirname, f) for f in fnames if f.endswith(arg)) 
    for f in argfiles: 
     shutil.move(f, f[:-len(arg)]) 

os.path.walk('/some/path', remove_ext, '.tex') 
1

的一种方式,不一定是最快的(但至少最快的开发):

 
    pax> for i in *.c */*.c */*/*.c ; do 
    ...> j=$(echo "$i" | sed 's/\.c$//') 
    ...> echo mv "$i" "$j" 
    ...> done 

因为你的maxdepth是2,所以它是等效的。脚本只是在回应mv命令(用于测试目的)和处理C文件(因为我没有tex文件进行测试)。

或者,您可以用找到的所有因而功率:

 
    pax> find . -maxdepth 2 -name '*.tex' | while read line ; do 
    ...> j=$(echo "$line" | sed 's/\.tex$//') 
    ...> mv "$line" "$j" 
    ...> done 

+0

+1,对于“读线”,我搞乱了'-exec` – nik 2009-07-30 06:20:02

+0

同意。 “find ... | while read”是我最喜欢的bash单线模板之一。 – Cascabel 2009-07-30 14:54:54

0

使用bash,从基本目录findmv

for i in $(find . -type f -maxdepth 2 -name "*.tex"); 
do 
    mv $i $(echo "$i" | sed 's|.tex$||'); 
done 

基于这里其他的答案变2

find . -type f -maxdepth 2 -name "*.tex" | while read line; 
do 
    mv "$line" "${line%%.tex}"; 
done 

PS:我没有得到这个角色有关转义 '.' 由pax ...

0

“在我的” 使用可能会导致 “太多的参数” errrors

更好的方法是将管道找到下一个过程。

例子:

find . -type f -name "*.tex" | while read file 
do 
    mv $file ${file%%tex}g 
done 

(注:惯于处理文件用空格)

0

有一个很好的Perl脚本重命名附带的一些发行,否则你可以找到它在网络上。 (我不确定它在哪里正式居住,但是this is it)。检查您的重命名是否由Larry Wall撰写(作者部分man rename)。它可以让你做这样的事情:

find . [-maxdepth 2] -name "*.tex" -exec rename 's/\.tex//' '{}' \; 

使用-exec是最简单的,因为在这里,只有一个执行操作,并且它不是调用重命名多次太贵了。如果你需要做多件事情,用“而读”的形式:

find . [-maxdepth 2] -name "*.tex" | while read texfile; do rename 's/\.tex//' $texfile; done 

如果你有什么要一次调用:

find . [-maxdepth 2] -name "*.tex" | xargs rename 's/\.tex//' 

这最后一个使重命名清楚是多么有用 - 如果所有东西都在同一个地方,那么你有一个快速的正则表达式重命名器。