递归BASH重命名

时间:2010-11-22 20:43:47

标签: bash

编辑:好的,对不起,我应该指定我在Windows上,并使用基于bash 1.14.2的win-bash以及gnuwin32工具。这意味着遗憾的是,所有解决方案都没有帮助。它不包含许多高级功能。不过我终于明白了。这是一个丑陋的剧本,但它确实有效。

#/bin/bash
function readdir
{
  cd "$1"
  for infile in *
  do
    if [ -d "$infile" ]; then
      readdir "$infile"
    else
      renamer "$infile"
    fi
  done
  cd ..
}

function renamer
{
  #replace " - " with a single underscore.
  NEWFILE1=`echo "$1" | sed 's/\s-\s/_/g'`
  #replace spaces with underscores
  NEWFILE2=`echo "$NEWFILE1" | sed 's/\s/_/g'`
  #replace "-" dashes with underscores.
  NEWFILE3=`echo "$NEWFILE2" | sed 's/-/_/g'`
  #remove exclamation points
  NEWFILE4=`echo "$NEWFILE3" | sed 's/!//g'`
  #remove commas
  NEWFILE5=`echo "$NEWFILE4" | sed 's/,//g'`
  #remove single quotes
  NEWFILE6=`echo "$NEWFILE5" | sed "s/'//g"`
  #replace & with _and_
  NEWFILE7=`echo "$NEWFILE6" | sed "s/&/_and_/g"`
  #remove single quotes
  NEWFILE8=`echo "$NEWFILE7" | sed "s/’//g"`

  mv "$1" "$NEWFILE8"
}

for infile in *
do
  if [ -d "$infile" ]; then
    readdir "$infile"
  else
    renamer "$infile"
  fi
done

ls

我正在尝试创建一个bash脚本来递归目录并重命名文件,删除空格,破折号和其他字符。我已经让脚本正常工作,除了它的递归部分。我还是新手,所以它没有应有的效率,但是它有效。任何人都知道如何使这个递归?

#/bin/bash
for infile in *.*;
do 
#replace " - " with a single underscore.
NEWFILE1=`echo $infile | sed 's/\s-\s/_/g'`; 
#replace spaces with underscores
NEWFILE2=`echo $NEWFILE1 | sed 's/\s/_/g'`; 
#replace "-" dashes with underscores.
NEWFILE3=`echo $NEWFILE2 | sed 's/-/_/g'`; 
#remove exclamation points
NEWFILE4=`echo $NEWFILE3 | sed 's/!//g'`; 
#remove commas
NEWFILE5=`echo $NEWFILE4 | sed 's/,//g'`; 
mv "$infile" "$NEWFILE5";
done;

5 个答案:

答案 0 :(得分:3)

find是能够显示文件系统层次结构中所有元素的命令。您可以使用它在每个找到的文件上执行命令,或将结果传递给xargs,它将处理执行部分。

请注意for infile in *.*对包含空格的文件不起作用。查看-print0的{​​{1}}选项,该选项与find的{​​{1}}选项相关联。

答案 1 :(得分:2)

所有这些分号都是多余的,没有理由使用所有这些变量。如果你想将sed命令放在不同的行上并散布详细的注释,你仍然可以这样做。

#/bin/bash
find . | while read -r file
do
    newfile=$(echo "$file" | sed '
        #replace " - " with a single underscore.
        s/\s-\s/_/g 
        #replace spaces with underscores
        s/\s/_/g
        #replace "-" dashes with underscores.
        s/-/_/g
        #remove exclamation points
        s/!//g
        #remove commas
        s/,//g')
    mv "$infile" "$newfile"
done

这要短得多:

#/bin/bash
find . | while read -r file
do
    # replace " - " or space or dash with underscores
    # remove exclamation points and commas
    newfile=$(echo "$file" | sed 's/\s-\s/_/g; s/\s/_/g; s/-/_/g; s/!//g; s/,//g')
    mv "$infile" "$newfile"
done

更短的时间:

#/bin/bash
find . | while read -r file
do
    # replace " - " or space or dash with underscores
    # remove exclamation points and commas
    newfile=$(echo "$file" | sed 's/\s-\s/_/g; s/[-\s]/_/g; s/[!,]//g')
    mv "$infile" "$newfile"
done

答案 2 :(得分:0)

在bash 4中,设置globstar选项允许递归通配。

shopt -s globstar
for infile in **
 ...

否则,请使用find

while read infile
do
 ...
done < <(find ...)

find ... -exec ...

答案 3 :(得分:0)

我过去使用'find'来查找文件,然后让它执行另一个应用程序。 见'-exec'

答案 4 :(得分:0)

rename 's/pattern/replacement/' glob_pattern