Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write this shell script to create in the parent directory a copy of the folders found in the current directory. So far, I wrote this:

for folder in *; do

    mkdir ../$folder;

I'd like to do a "find and replace" operation on the folder name, so that, for instance, folder named graphics-HD becomes copied as graphics-SD

Sorry if it's simplistic, but I am absolutely not experienced with shell script / unix.

Thanks a lot! J.

share|improve this question
Can you edit and add more details about the find and replace operations you want to cover (unless the answer below covers them all) – Paul Nov 21 '12 at 11:46
up vote 2 down vote accepted

Adding a trailing slash to the wildcard restricts the output to directories

for dir in */; do ...

You'll want to read about bash parameter expansion -- you can do find and replace within the shell:

share|improve this answer
What is the percent sign for? – Ярослав Рахматуллин Nov 21 '12 at 13:14
See the documentation linked in my answer: If pattern begins with ‘%’, it must match at the end of the expanded value of parameter. – glenn jackman Nov 21 '12 at 14:54

there is nothing wrong with what you have, except it will make folders out of files as well as folders. You can either check that every folder is a directory with [ -d $folder ], or use find. From the folder where you have folders that you wish to copy:

find -type d -maxdepth 1 -exec mkdir "../{}" \+ 

To search and replace, there is rename from you can rename all folders somewhere like this:

renamexm -s/-HD/-SD/ `find . -type d -maxdepth 1`
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .