LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   rename files on a command line? (http://www.linuxquestions.org/questions/linux-general-1/rename-files-on-a-command-line-197451/)

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.

anybody?
thanx.

Shade 06-25-2004 12:35 AM

You use the mv command.

mv filename newname

--Shade

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":
http://www.indiana.edu/~uitspubs/b017/

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.

--Shade

meblost 06-30-2004 03:16 PM

silmaril8n,
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:
Code:

#!/bin/bash

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

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 01:38 PM.