Quick unix question

I hope it’s ok to post this unix question here, don’t know where else to post it… I need to rename various files, as follows:

/photos/section1/page1/photos.jsp
/photos/section1/page2/photos.jsp

/photos/section5/page7/photos.jsp
etc…

I need to rename all ‘photos.jsp’ files to ‘photos.php’… if I’m at the root of webapp (/photos/) how do I do this in unix with one command?

I also need to rename all .jsp files at the root (/photos/) to .php… how would I do that?

would appreciate help…

thank you…

You could probably use the ‘find’ command. I need to check exact syntax. The command can execute another command on all found files matching a criteria. Something kinda like this

$ find /photos -name ‘photos.jsp’ -exec mv {} photos.php ;

I have a feeling you have to escape the parenthesis in the shell also.

Sent from my XT316 using Tapatalk 2

hmmm… ‘mv’ is move, right? and what is option -exec? am searching, can’t find it…

thank you…

Hi maya,

mv can be used to rename a file.

mkdir test
cd test
echo "a" > a.html
ls
> a.html
mv a.html b.html
ls
> b.html

-exec is a parameter passed to the find program in the form of -exec command ;
As you might imagine, it executes the specified command on each matched file.
You can read more here: http://www.computerhope.com/unix/ufind.htm (do an in page search for “exec”).

HTH

thank you very much for your help…

so I typed command:

find /photos -name ‘photos.jsp’ -exec mv {} photos.php ;

I get error:

find: -exec: no terminating ";" or "+"

(tried with both space and no space before ending ‘;’)

looked it up… found I can quote the final ‘;’ or escape it with a '' but in both cases still get that same error…

(don’t know what unix shell I’m on… am on on a mac, 10.6.8…)

thank you…

PS: why do you need a ‘;’ at the end of this command??
normally I don’t have to type ‘;’ at the end of unix commands…

everywhere I look it says to either escape the ‘;’, or quote it in single quotes… but in both cases still get that error…:frowning:

thank you…

Hi,

On Linux you would do it like this:

find -name 'photos.jsp' -execdir mv {} photo.php \;

This assumes you are in the photos directory (the root of your webapp).
If you just use exec, it’ll create one single file in your root dir named “Photo.php”, whereas execdir preserves the context.

I’m not sure how that works on a Mac.
Try it out and let us know how you get on.

A -exec command must be terminated with a ; (so you usually need to type ; or ‘;’ to avoid interpretion by the shell)

http://stackoverflow.com/questions/2961673/find-missing-argument-to-exec

IMPORTANT
Please make a backup of your web app before running any commands such as find on it.
If you get it wrong, it is quite possible to delete stuff.

yes, but as I mentioned, if i either quote the final ‘;’ or escape it with a '' I still get that same error…

find /photos -name 'photos.jsp' -exec mv {} photos.php\;

find /photos -name 'photos.jsp' -exec mv {} photos.php';'

I also tried double quotes; same thing…

(yes I did make a backup…:wink: thank you…)

What happens if you run this command from the root of your project? (just copy and paste it)

find -name 'photos.jsp' -execdir mv {} photo.php \;

Note the use of execdir (not exec) and the space between photos.php and the backslash semi-colon.

thank you Pullo…

ran the command, this is what happens:

$ find -name 'photos.jsp' -execdir mv {} photo.php \;
find: illegal option -- n
find: illegal option -- a
find: illegal option -- m
find: illegal option -- e
find: photos.jsp: No such file or directory

it can’t find the file b/c it’s not there… it’s buried two dirs down… as I mentioned in my OP, this is the file-structure:

/photos/section1/page1/photos.jsp
/photos/section1/page2/photos.jsp

/photos/section5/page7/photos.jsp

etc…

a total of 11 sections, with an average of about six pages per section, for a total of approx 80 photos.jsp files I need to rename to photos.php… all of them in

section<no.>/page<no.>/photos.jsp…

thank you very much for your help…

find will recurse through any directories that it encounters.
Assuming a similar directory structure as yours and that I am in the project root:

jack@friendly ~/Desktop/photos $ find -name 'photos.jsp'
./section1/page2/photos.jsp
./section1/page1/photos.jsp
./section5/page2/photos.jsp
./section5/page1/photos.jsp
./section2/page2/photos.jsp
./section2/page1/photos.jsp

However, there seem to be some differences between Mac and Linux, so maybe we can approach this another way.

Do you have Ruby installed? If so, which version?
To find out open a terminal and run ruby -v and post the results back here.

ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin10.0]

oh man… I have never used Ruby…:wink:

(funny you should mention Ruby… I was thinking of maybe writing a java class to do this…)

thank you very much…

Hi,

Create a new file on your computer.
Name it “rename_photos.rb” or something similar.
Copy the following into the newly created file:

def rename_file(old_path)
  new_path = old_path.sub("photos.jsp", "photos.php")
  puts "Old path: #{old_path}"
  puts "New path: #{new_path}"
  puts
  #File.rename(old_path, new_path)
end

def build_tree(start_path)
  Dir.open(start_path).entries.each do |file_name|
    full_path = File.join(start_path, file_name)
    next if /^\./ =~ file_name
    if File.directory?(full_path)
      build_tree(full_path)
    elsif (file_name == "photos.jsp")
      rename_file(full_path)
    end
  end
end

build_tree("/photos")

Ensure the start path is pointing to the root of your app.
Then run the file with ruby rename_photos.rb
Check that the output is correct.
If so, un-comment this line: #File.rename(old_path, new_path) (by removing the #).
Run again.

And that should hopefully work.
Let us know how you get on.

hi,

thank you very much for your help… I appreciate you taking the time…

this is what I get when I run the .rb file:

$ ruby rename_photos.rb

rename_photos.rb:10:in `open': No such file or directory - /photos (Errno::ENOENT)
from rename_photos.rb:10:in `build_tree'
from rename_photos.rb:21

question is: where does this file rename_photos.rb have to be? I put it at root of webapp (/photos/)

“start path”… you mean in the terminal I need to be at that location, right? yes that’s where I am, and where rename_photos.rb is…

(I added a trialing ‘/’ at the end of ‘/photos’ on last line of rename_photos.rb, get exact same thing…)

again, thank you very much…

here’s screenshot of file-structure of photos webapp…

Hi,

You are passing the location of the folder containing the files you want to rename as an argument to the method build_path

For me:

Ruby Script is located at /home/pullo/Desktop/rename.rb (this is unimportant).

Folder containing the files to be renamed is is /home/pullo/sitepoint/photos.

It is structured as previously discussed:

/home/pullo/sitepoint/photos/section1/page1/photos.jsp
/home/pullo/sitepoint/photos/section1/page2/photos.jsp
......
/home/pullo/sitepoint/photos/section5/page7/photos.jsp
etc...

To run things, I change the last line of the ruby script to build_tree("/home/pullo/sitepoint/photos")

Does that make sense at all?

woo hoo!!!

that worked!!! :smile:

thank you very very much… I really appreciate your help…

(so I put rename_photos.rb in /Library/WebServer/Documents (which contains /photos/…)
and changed that last line in ruby file (class? script? :wink: to:

build_tree(“/Library/WebServer/Documents/photos”)

hadn’t realized the path to /photos/ had to be from root of computer…:wink:

again, thank you very much…

No probs. Glad we got there in the end :smile: