Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Recursive shell script - most elegant implementation?

Posted on 2002-05-30
11
1,211 Views
Last Modified: 2010-05-18
I've been working on a shell script to rename files in a directory tree.  The purpose is to remove spaces from the file names of some photos so that they can be more easily put on the web (fewer %20 instances).  I'm working in the Bourne shell, and have mastered the task of renaming all the files in the current directory, using a "for Original in *" type of construct to look at each file the directory, squeeze the spaces out of it, rename it.  The current implementation looks something like this (from memory, may not be perfect):

#!/bin/sh

for Original in *
do
  Squeezed = `echo $Original | sed 's/ //g'`
  if [ "$Original" != $Squeezed ]
  then
    mv \"$Original\" $Squeezed
  fi
done


The next advancement I'd like to make is to recursively run the script - test each file with [ -d $Squeezed ] and then call the script to work on the new directory.

The trouble is how to refer to the script itself in order to run it successively from various places within the directory tree.  The solutions that come to mind are:

1. copy the script to tmp and execute it from that fixed location, then delete it when done, a la
cp $0 /tmp/$$.sqzit
<do recursive stuff>
rm /tmp/$$.sqzit

2. Put the whole working part of the script into a Here document, then copy that into each directory as we go and execute it from there (whew, the conceptual image of that is interesting).  Kind of makes it into a two part script.

Usually when I find myself proposing a solution this complicated I later discover that Unix has some obscure but elegant way to accomplish what I'm trying to do... does anyone have any simple way to run a new instance of the script that is currently running, in a new directory and with a new command line?

Suggestions and pointers to good scripting tutorials/resources on the web welcome.
0
Comment
Question by:ckurt99
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7045465
Any reason you want to do the tree traversal in shell rather than simply calling find?

find . -type f -print | while read pathname
0
 
LVL 3

Author Comment

by:ckurt99
ID: 7045496
Thank you for responding...

Initially, I did intend to use find; however, I ran into trouble with the pathnames interacting (read: failing) with the mv when the dealing with directories.  As I recall, I was generating commands like this that didn't work:

mv "/usr/~kurt/My Directory" /usr/~kurt/MyDirectory

After experimenting a bit it seemed like the only way to rename a directory was to be in the directory and do a simple rename:

mv "My Directory" MyDirectory

Hence the traversal in the shell... but I'm open to suggestions.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 7045770
You do have to use recursive call for the job:

you can modify your script as a ksh script, and make your original script as a function of the main script, and use function call inside the script to do the job.

eg.

#==========================================
#!/bin/ksh

#define your vars here.


#put your script here as a function, use dir as arg
mvfile()
{

cd $1           #move to the dir you want to change
for Original in *
do
 Squeezed = `echo $Original | sed 's/ //g'`
 if [ "$Original" != $Squeezed ]
 then
   mv \"$Original\" $Squeezed
 fi
done

}

# main body of your script.
  #do whatever you want to create a list of dir to be cahnge

  for dir in `cat MYDIRLIST` ; do

   mvfile ${dir}

  done

.........
 exit 0

# Note you can have 2 arg for your function if your what to move file from one dir to another


0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:ecw
ID: 7046438
find . -type d | while read dir ; do
  (
  cd $dir
  for Original in *
  do
   Squeezed=`echo "$Original" | sed 's/ //g'`
   if [ "$Original" != "$Squeezed" ]
   then
     mv "$Original" "$Squeezed"
   fi
  done
  )
done


0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 100 total points
ID: 7046750
Since directories may also be renamed...

 find . -type d -depth | while read dir ; do
 (
 cd $dir
 for Original in *
 do
  Squeezed=`echo "$Original" | sed 's/ //g'`
  if [ "$Original" != "$Squeezed" ]
  then
    mv "$Original" "$Squeezed"
  fi
 done
 )
done
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7050391
find . -exec echo echo {} @@ {} \;|awk -F@ '{print $1 "`mv \""$3"\"|tr -d '"'"' '"'"'`" }'|sh

# above asumes that there're no filenames containing @
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7050398
oops, last suggestion is wrong, please use following instead:

find . -exec echo mv {} @@ {} \;|awk -F@ '{print $1 "`echo \""$3"\"|tr -d '"'"' '"'"'`" }'|sh
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7051140
Interesting, but you'll still want -depth on the find.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7051259
somebody asked for -depth ?
or even existance of "MyDirectory" when "My Directory" exists also ?-)
0
 
LVL 3

Author Comment

by:ckurt99
ID: 7051460
Whew, it's getting deep now!  Thanks to all who responded.  I've learned something from each of you... well, OK, I haven't learned ahoffman's yet, but given time I will.  I wouldn't have dreamed that this could be reduced to a one-liner, but then I'm forgetting that All Things Are Possible With Awk (ATAPWA?).

I've chosen Chris Calabrese' second comment as the answer, and will post sharing points questions for yuzh, ecw, and ahoffman.
0
 
LVL 3

Author Comment

by:ckurt99
ID: 7074721
Just for the record, I want to post a small correction to this script, caused by the fact that find reports files with relative pathnames.  This required me to go back to the starting directory before jumping to the directory to be processed.  The corrected script is shown below.  There is a further refinement left as an exercise for the reader: handle the error case where a new file name already exists.  Thanks to all who responded.

#!/bin/sh

StartDir=`pwd`

find . -type d -depth -print | while read MyDir ; do
(
  cd "$MyDir"

  for OriginalFile in *
  do

    ShortName=`echo $OriginalFile | sed 's/ //g'`

    if [ $ShortName != "$OriginalFile" ]
    then
      mv "$OriginalFile" "$ShortName"
    fi

  done

  cd "$StartDir"
)
done
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question