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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
06-04-2009, 11:47 AM
#1
Member
Registered: Feb 2005
Location: us
Posts: 98
Rep:
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
06-04-2009, 11:50 AM
#2
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
For rename:
Code:
for filename in $(ls 1a*)
do
echo "Renaming file: $filename to ${filename}.dum"
# mv $filename ${filename}.dum
done
06-05-2009, 12:54 PM
#3
Member
Registered: Feb 2005
Location: us
Posts: 98
Original Poster
Rep:
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
06-05-2009, 04:45 PM
#4
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
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
06-05-2009, 05:05 PM
#5
Member
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896
Rep:
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
06-05-2009, 08:06 PM
#6
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
Uncle_Theodore
Code:
teddy@lappy~/test$ i=1; for filename in $(ls); do mv $filename $i.${filename##*.}; i=$((i+1)); done
Quote:
Originally Posted by
Disillusionist
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 *
06-05-2009, 08:07 PM
#7
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
v2010
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
06-05-2009, 08:15 PM
#8
Senior Member
Registered: Aug 2006
Posts: 2,697
Quote:
Originally Posted by
v2010
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
06-06-2009, 08:07 AM
#9
Member
Registered: Feb 2005
Location: us
Posts: 98
Original Poster
Rep:
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
06-06-2009, 10:28 AM
#10
Senior Member
Registered: Aug 2006
Posts: 2,697
why do you post the same question again without even trying ? what have you tried???
06-06-2009, 10:36 AM
#11
Senior Member
Registered: Jul 2006
Location: Kolkata, India
Distribution: Debian 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,766
Rep:
use grename or krename
06-10-2009, 12:09 AM
#12
Member
Registered: Feb 2005
Location: us
Posts: 98
Original Poster
Rep:
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.
06-10-2009, 12:54 AM
#13
Member
Registered: Dec 2007
Posts: 88
Rep:
Quote:
Originally Posted by
ghostdog74
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.
06-10-2009, 01:06 AM
#14
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359
06-10-2009, 03:28 AM
#15
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
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 12:12 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News