将_添加到文件夹中的每个文件/目录

时间:2012-09-18 09:46:32

标签: bash

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

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

怎么样:

for f in *
do
  mv "$f" _"$f"
done

答案 1 :(得分:0)

ls -1 | while read i; do mv "$i" "_$i"; done

答案 2 :(得分: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)