LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   linux rename multiple files to same suffix (http://www.linuxquestions.org/questions/linux-newbie-8/linux-rename-multiple-files-to-same-suffix-730689/)

v2010 06-04-2009 11:47 AM

linux rename multiple files to same suffix
 
hi all

I do have mutliple files want to be renamed with same suffix.
for example

files start with 1ab, 1ac, 1ad ....
and want them to rename them with same suffix like 1ab.dum 1ac.dum 1ad.dum...

Is it any way to do that in linux with one command or script.

Other question is to
split the huge files with same suffix, I did used split command to split the file but it does not give me the suffix option.

Thanks appreciate your help

venkat

Disillusionist 06-04-2009 11:50 AM

For rename:
Code:

for filename in $(ls 1a*)
do
  echo "Renaming file: $filename to ${filename}.dum"
  # mv $filename ${filename}.dum
done


v2010 06-05-2009 12:54 PM

Hi there

Thanks a lot, it works like a charm.

I do have one more help to ask,

I do have the 2000 files starting with random file prefix,
I would like to change the prefix like dum_1.x dum_2.x, dum_3.x
as a running numbers.

Please let me know how can I do that , really apprecite.

cheers
venkat

Disillusionist 06-05-2009 04:45 PM

I'm sorry, I don't entirely understand what your asking.

Please provide a sample list of five or six file names giving the before and after names that you are trying to achieve.

For example:
Code:

Before: dum_1.x
After : dum_0001.x


Uncle_Theodore 06-05-2009 05:05 PM

Something like this?

Before:
teddy@lappy~/test$ ls
halabuda.x yoda.y.x yoyo.x.y

Code:

teddy@lappy~/test$ i=1; for filename in $(ls); do mv $filename $i.${filename##*.}; i=$((i+1)); done
After:
teddy@lappy~/test$ ls
1.x 2.x 3.y

ghostdog74 06-05-2009 08:06 PM

Quote:

Originally Posted by Uncle_Theodore (Post 3564650)

Code:

teddy@lappy~/test$ i=1; for filename in $(ls); do mv $filename $i.${filename##*.}; i=$((i+1)); done

Quote:

Originally Posted by Disillusionist (Post 3563105)
For rename:
Code:

for filename in $(ls 1a*)
do
  echo "Renaming file: $filename to ${filename}.dum"
  # mv $filename ${filename}.dum
done


no need ls.. use shell expansion
Code:

for file in 1a*  #or for file in *

ghostdog74 06-05-2009 08:07 PM

Quote:

Originally Posted by v2010 (Post 3563101)
Other question is to
split the huge files with same suffix, I did used split command to split the file but it does not give me the suffix option.

Thanks appreciate your help

venkat

check out csplit. man csplit for help

ghostdog74 06-05-2009 08:15 PM

Quote:

Originally Posted by v2010 (Post 3564420)
I do have the 2000 files starting with random file prefix,
I would like to change the prefix like dum_1.x dum_2.x, dum_3.x
as a running numbers.

if you have Python, you can try the script in my sig called "File Renamer".
eg usage
Code:

# ls -1
XX_YY_010611.txt
XX_YY_010612.txt
XX_YY_010613.txt
XX_YY_010614.txt
log_0000118432_1.arc
log_0000118433_1.arc
log_0000118434_1.arc

# filerenamer.py -s  ".*" -e "dum_001:999.x" -l "*.*"
==>>>>  [ /test/images/log_0000118434_1.arc ]==>[ /test/images/dum_001.x ]
==>>>>  [ /test/images/log_0000118432_1.arc ]==>[ /test/images/dum_002.x ]
==>>>>  [ /test/images/XX_YY_010614.txt ]==>[ /test/images/dum_003.x ]
==>>>>  [ /test/images/XX_YY_010613.txt ]==>[ /test/images/dum_004.x ]
==>>>>  [ /test/images/XX_YY_010612.txt ]==>[ /test/images/dum_005.x ]
==>>>>  [ /test/images/log_0000118433_1.arc ]==>[ /test/images/dum_006.x ]
==>>>>  [ /test/images/XX_YY_010611.txt ]==>[ /test/images/dum_007.x ]

remove the "-l" option to commit changes

v2010 06-06-2009 08:07 AM

Thanks a lot for the reply.

My problem is,

I do have the files

xxx.txt
x12.txt
x32.txt
xaaa.txt
x123c.txt
x3432a.txt

like variable prefix, I want to rename them entirely in prefix

I want something like this

mod_1.txt
mod-2.txt
mod_3.txt
....
.....
....
mod_100000.txt

I do have redhat5.

Thanks for everyone trying to help me out

venk

ghostdog74 06-06-2009 10:28 AM

why do you post the same question again without even trying ? what have you tried???

amani 06-06-2009 10:36 AM

use grename or krename

v2010 06-10-2009 12:09 AM

Thanks a lot to every one. I did posted again because Disillunoist
need more details of what I need ?

I did tried other script, but it did not change the prefix the way I want. I did tried

$ i=1; for filename in $(ls); do mv $filename $i.${filename##*.}; i=$((i+1)); done

That changes the number but it did not replace the prefix the way I want. I am not good in python so not sure what I could change in that script for change the prefix.

Thanks a lot for the reply.

Again

I do have the files

xxx.txt
x12.txt
x32.txt
xaaa.txt
x123c.txt
x3432a.txt

like variable prefix, I want to rename them entirely in prefix

I want something like this

mod_1.txt
mod-2.txt
mod_3.txt
....
.....
....
mod_100000.txt



Here I did change the suffix with numbers for identification,

Thanks again for everyone trying to help me.

rikxik 06-10-2009 12:54 AM

Quote:

Originally Posted by ghostdog74 (Post 3564773)
no need ls.. use shell expansion
Code:

for file in 1a*  #or for file in *

Another advantage is, in case of too many files, ls fails with error like "/usr/bin/ls: arg list too long" but shell expansion succeeds.

chrism01 06-10-2009 01:06 AM

Code:

i=1
for filename in $(ls)
    do mv $filename mod_${i}.txt
    i=$((i+1))
done

See http://rute.2038bug.com/index.html.gz & http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

Disillusionist 06-10-2009 03:28 AM

Code:

i=1
for filename in *.txt
do
  mv $filename mod_${i}.txt
  i=$(expr $i + 1)
done



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