-   Linux - General (
-   -   rename files on a command line? (

CrazyPilot 06-25-2004 12:33 AM

rename files on a command line?
I've been using linux for a quite a while but I have yet to figure out how to rename stuff in a terminal.


Shade 06-25-2004 12:35 AM

You use the mv command.

mv filename newname


Dark_Helmet 06-25-2004 12:54 AM

There is also a rename command. However, it's purpose is usually to rename multiple files that have a recognizeable pattern in their names. man rename for more info of course.

fluppi 06-25-2004 05:43 AM

Maybe your looking for a "Unix commands reference card":

CrazyPilot 06-25-2004 03:09 PM

as many times as ive moved files around with mv I'm suprized i didn't figure that out already. Thanks, Shade.

The command reference has been quite usefull aswell fluppl, thanx!

silmaril8n 06-30-2004 12:46 PM

Is it possible to go into subdirectories to rename files? Otherwise that would mean that I'd have to run it each time in each folder that I want to update!

Shade 06-30-2004 03:01 PM

I don't understand what you're asking...

You can't rename multiple files with mv.
You can rename multiple files with the rename command that Dark_Helmet mentioned.


meblost 06-30-2004 03:16 PM

Do you mean rename a file that isn't in the current directory?

$> mv /home/bob/files/junk /home/bob/files/oldjunk

silmaril8n 06-30-2004 04:09 PM

For instance:

I have a website with multiple directories to organize my .html files. At some point I think I want to change all the extensions from .html to .php. At this point I'm reduced to "cd"ing into each directory and then running "rename .html .php *.html".

Is there a way to run the "rename" command in the root web directory (public_html) and have it enter into all the subdirectories below it, making all the changes along the way?

Dark_Helmet 06-30-2004 05:10 PM

Create a shell script containing this:


if [ ! "$1x" = "x" ] ; then
  new_filename=`echo $1 | sed s/\.html//`
  mv -v "$1" "${new_filename}.php"

make the script executable: chmod u+x script_filename

execute this command: find /path/to/html/root -type f -name "*.html" -exec /path/to/script/script_filename {} \;

As I always suggest to anyone using a script to rename, move, or delete files, backup your data and try a test run. I'm not infallable, and there's a chance something could get mis-read when taking the script and typing it up (or the command) on your system. If the test run works like you want it to, then and only then, use the script on your real data.

All times are GMT -5. The time now is 08:48 AM.