LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 06-24-2004, 11:33 PM   #1
CrazyPilot
Member
 
Registered: Jan 2004
Location: Somewhere...
Distribution: Fedora Core 4, Slackware 10.1
Posts: 61

Rep: Reputation: 15
Question 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.
 
Old 06-24-2004, 11:35 PM   #2
Shade
Senior Member
 
Registered: Mar 2003
Location: Burke, VA
Distribution: RHEL, Slackware, Ubuntu, Fedora
Posts: 1,418
Blog Entries: 1

Rep: Reputation: 46
You use the mv command.

mv filename newname

--Shade
 
Old 06-24-2004, 11:54 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
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.
 
Old 06-25-2004, 04:43 AM   #4
fluppi
Member
 
Registered: Oct 2003
Location: Switzerland (Europe)
Distribution: OpenSuSE, RedHat, Knoppix, IRIX + MacOSX
Posts: 198

Rep: Reputation: 30
Maybe your looking for a "Unix commands reference card":
http://www.indiana.edu/~uitspubs/b017/
 
Old 06-25-2004, 02:09 PM   #5
CrazyPilot
Member
 
Registered: Jan 2004
Location: Somewhere...
Distribution: Fedora Core 4, Slackware 10.1
Posts: 61

Original Poster
Rep: Reputation: 15
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!
 
Old 06-30-2004, 11:46 AM   #6
silmaril8n
Member
 
Registered: May 2004
Location: Vancouver, WA
Distribution: Mint, Debian, Slackware
Posts: 123

Rep: Reputation: 15
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!
 
Old 06-30-2004, 02:01 PM   #7
Shade
Senior Member
 
Registered: Mar 2003
Location: Burke, VA
Distribution: RHEL, Slackware, Ubuntu, Fedora
Posts: 1,418
Blog Entries: 1

Rep: Reputation: 46
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
 
Old 06-30-2004, 02:16 PM   #8
meblost
Member
 
Registered: May 2004
Location: At Keyboard
Distribution: Mandrake 10.0, SuSE 9.0
Posts: 114

Rep: Reputation: 15
silmaril8n,
Do you mean rename a file that isn't in the current directory?

$> mv /home/bob/files/junk /home/bob/files/oldjunk
 
Old 06-30-2004, 03:09 PM   #9
silmaril8n
Member
 
Registered: May 2004
Location: Vancouver, WA
Distribution: Mint, Debian, Slackware
Posts: 123

Rep: Reputation: 15
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?
 
Old 06-30-2004, 04:10 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to rename a file from command line ??? waelaltaqi Linux - Software 1 11-17-2005 01:49 PM
How to rename a file from command line ??? waelaltaqi Linux - Software 2 11-17-2005 01:43 PM
How can I rename a lot of files with a single command? pujolasdf Linux - Software 4 07-20-2005 05:27 AM
Command line: Rename several dist files at the same time ExCIA Linux - Newbie 2 04-28-2005 10:12 AM
rename with command line shanenin Linux - Software 1 09-25-2003 07:03 PM


All times are GMT -5. The time now is 04:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration