LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-10-2004, 04:03 PM   #1
BabaKali
LQ Newbie
 
Registered: Nov 2004
Posts: 9

Rep: Reputation: 0
rename all files in my directory


Hi again,

Can someone tell me what is the best way to rename all files in my directory?

I'm trying to rename all the files to end with .txt using a for do loop, like:

for i in `ls`;
do
mv $i $i.txt;
done

i get an error that mv can not start, what's wrong?

Thanks in advance
 
Old 11-10-2004, 04:18 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
Code:
for i in *; do mv "$i" "$i.txt"; done
double quotes just incase you have spaces in the names etc...
 
Old 11-10-2004, 04:27 PM   #3
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
That looks fine and works for me. (eg. renames foo.bar --> foo.bar.txt)
Do you have spaces in your filenames? If so, easiest way is to
use program named "rename", eg. command
Code:
rename 's/$/.txt/' *
does the same as your command above.
The syntax of the command is "rename regexp files", where regexp is a sedlike regular expression, in this case it 's/$/.txt/' where "s" is for search and replace, $ matches end of the string and ".txt" replaces it (end of the string/filename) with .txt (eg. adds .txt extension).
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - rename directory to overwrite existing LarsWestergren Linux - Newbie 18 04-04-2013 02:07 PM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM
rename files allelopath Linux - General 5 07-05-2005 03:00 AM
rename all files in a directory merlin23 Linux - Newbie 4 03-24-2005 11:02 AM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 01:36 PM


All times are GMT -5. The time now is 01:11 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