LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-26-2010, 08:29 AM   #1
EchoPhyber
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Help with renaming files


Hi. Can someone tell me how to append an extension to all files in a folder.

I was thinking about writing a shell script that takes the folder name as an argument but I don't seem to get the regular expression right afer that.
 
Old 03-26-2010, 08:36 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
for i in *; do mv "$i" "`basename $i `.test"; done
 
Old 03-26-2010, 08:37 AM   #3
joe.beard
LQ Newbie
 
Registered: Apr 2006
Posts: 3

Rep: Reputation: 0
You can use find to do this, see the example below. The -exec option in find uses {} to replace the name of the file here so in each instance {} is changed to {}.ext (here 1 to 1.ext, 2 to 2.ext etc

oeb@cloudnext:~/test$ touch 1 2 3 4 5 6
joeb@cloudnext:~/test$ find . -exec mv {} {}.ext \;
mv: cannot move `.' to `..ext': Device or resource busy
joeb@cloudnext:~/test$ ls -lsa
total 8
4 drwxr-xr-x 2 joeb joeb 4096 2010-03-26 13:36 .
4 drwxr-xr-x 5 joeb joeb 4096 2010-03-26 13:35 ..
0 -rw-r--r-- 1 joeb joeb 0 2010-03-26 13:35 1.ext
0 -rw-r--r-- 1 joeb joeb 0 2010-03-26 13:35 2.ext
0 -rw-r--r-- 1 joeb joeb 0 2010-03-26 13:35 3.ext
0 -rw-r--r-- 1 joeb joeb 0 2010-03-26 13:35 4.ext
0 -rw-r--r-- 1 joeb joeb 0 2010-03-26 13:35 5.ext
0 -rw-r--r-- 1 joeb joeb 0 2010-03-26 13:35 6.ext
 
Old 03-26-2010, 08:38 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Sigh. Sorry to be snarky, but...

Did you ever think about using the search function? There must be literally hundreds of threads asking this same or similar question. The post creation page you used even had a function that would do it automatically for you. Even now you can just look to the bottom of the page and follow some of the "similar threads".

Not to mention that there's always Google.
 
Old 03-26-2010, 08:38 AM   #5
joe.beard
LQ Newbie
 
Registered: Apr 2006
Posts: 3

Rep: Reputation: 0
As a note, the find command will also do all sub file and directories unless you specify a depth, read the find man page for more details, maybe a more complete command would be:

find . -maxdepth 0 -type f -exec mv {} {}.ext \;

Last edited by joe.beard; 03-26-2010 at 08:41 AM.
 
  


Reply



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
Help renaming files nandelbosc Programming 2 11-26-2009 07:24 AM
Renaming files tekmann33 Linux - Newbie 6 05-22-2009 10:08 AM
Renaming files mashcaster Linux - General 3 05-08-2009 03:14 PM
Renaming files leupi Linux - General 5 09-16-2008 03:22 AM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 02:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration