LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-01-2008, 03:46 PM   #1
xebix
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Add text to the beginning of file names


I tried doing a search on the forums to do what I want, but didn't have much luck.

I have a directory of files, and I want to prepend the same text string to each file.

example:
file1
file2
file3

renamed to:
this.file1
this.file2
this.file3

Thanks in advance for any help.
 
Old 10-01-2008, 04:00 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Code:
for looper in `ls <directory>`
do
    mv $looper "this.$looper"
done
 
Old 10-01-2008, 05:28 PM   #3
xebix
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks. That was exactly what I needed.
 
Old 10-01-2008, 11:50 PM   #4
jgallo
Member
 
Registered: Sep 2008
Posts: 39

Rep: Reputation: 15
i was already on here and needed the answer to this right now, and there it was!
thanks!

Last edited by Tinkster; 10-30-2010 at 04:58 PM.
 
Old 10-02-2008, 01:00 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by CRC123 View Post
Code:
for looper in `ls <directory>`
do
    mv $looper "this.$looper"
done
Not to be picky, but this has a little problem: quote the variables, unless you are certain that they will contain no special characters (like spaces, for example).

Code:
for looper in `ls <directory>`
do
    mv "$looper" "this.$looper"
done
Otherwise you are going to have problems if any files name has spaces.

Besides that, if you are using bash there's no need to use ls (and it can save you some pain sometimes to avoid it):

Code:
for looper in "/directory/"*
do
    mv "$looper" "this.$looper"
done
 
Old 10-02-2008, 01:43 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by xebix View Post
I tried doing a search on the forums to do what I want, but didn't have much luck.

I have a directory of files, and I want to prepend the same text string to each file.

example:
file1
file2
file3

renamed to:
this.file1
this.file2
this.file3

Thanks in advance for any help.
if you have Python, you can use the script in my sig called File Renamer.
eg usage
Code:
# ls -1
file1
file2
file3

# filerenamer.py -i "this." -l "file*"
==>>>>  [ /home/file1 ]==>[ /home/this.file1 ]
==>>>>  [ /home/file3 ]==>[ /home/this.file3 ]
==>>>>  [ /home/file2 ]==>[ /home/this.file2 ]

 filerenamer.py -i "this."  "file*" #remove -l to commit
/home/file1  is renamed to  /home/this.file1
/home/file3  is renamed to  /home/this.file3
/home/file2  is renamed to  /home/this.file2

# ls -1
this.file1
this.file2
this.file3
 
  


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
renaming file names beginning with "-" prashln Linux - General 5 05-01-2010 02:47 AM
How to add a text to first line of a text file? oskeewow Linux - Newbie 6 04-23-2008 01:40 PM
Add Text to Beginning of 200 Files dougp23 Linux - Newbie 3 10-30-2006 04:01 PM
writing text to the beginning of a file linuxmandrake Linux - Newbie 3 03-26-2006 11:55 PM
How to add data at file beginning in C? Nad0xFF Programming 8 04-17-2005 12:48 PM

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

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