0

是否有一个脚本(bash)在每个文件和目录名称的开头添加一个“_”?

谢谢你的帮助。

4

3 回答 3

1

关于什么:

for f in *
do
  mv "$f" _"$f"
done
于 2012-09-18T09:48:44.867 回答
0
ls -1 | while read i; do mv "$i" "_$i"; done
于 2012-09-18T09:49:12.797 回答
0

如果目录是递归的,你需要一些更复杂的东西:

#!/bin/bash
#Requires BASH and a find with -print0

#The source    
SRC=/tmp/writ

unset a i
#Read in the file names
while IFS= read -r -d '' file; do
  # Get the file name, rip off the directory name
  fname=$(basename "$file")

  # Get the directory name, without the file name
  dname=$(dirname "$file")

  #I used 'cp' instead of 'mv' for testing reasons
  # Note how I put the underscore in.
  cp "$file" "${dname}/_${fname}"

  # This just finds files.  You can repeat the loop with
  # type -d for directories.
done < <(find $SRC -type f -print0)
于 2012-09-18T12:27:19.517 回答