LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add text to the beginning of file names (https://www.linuxquestions.org/questions/linux-newbie-8/add-text-to-the-beginning-of-file-names-673600/)

xebix 10-01-2008 02:46 PM

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.

CRC123 10-01-2008 03:00 PM

Code:

for looper in `ls <directory>`
do
    mv $looper "this.$looper"
done


xebix 10-01-2008 04:28 PM

Thanks. That was exactly what I needed.

jgallo 10-01-2008 10:50 PM

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

i92guboj 10-02-2008 12:00 AM

Quote:

Originally Posted by CRC123 (Post 3297359)
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


ghostdog74 10-02-2008 12:43 AM

Quote:

Originally Posted by xebix (Post 3297349)
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



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