Folders

Batch Rename Folders

Problem:I’ve been trying to rename 1000+ folders to template_XXXX where the XXXX is a number. I want to rename all the folders, but don’t want the subfolders to be changed, only the parents.

Solution:

num=1; 
for dir in `find -maxdepth 1 -type d` ; 
  do mv $dir template_$num ; 
  num=$((num + 1)) ; 
done
  • “-type d” tells it to find only directories
  • “-maxdepth 1” says don’t recurse into sub dirs
  • the “for” loop goes through every directory that the find command spits out, moves (“mv”) the “$dir” to the new name: “template_$num.”
  • “num” starts at 1 and is incremented each time.
By practic on August 3, 2011 | | A comment?