Mombu the GNU Linux Forum sponsored links

Go Back   Mombu the GNU Linux Forum > GNU_Linux > Adding a prefix to multiple filenames.
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 22nd December 09:39
andy j.
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.



I've got a folder with numerous files in which I'd like to add the same
prefix to each.
For example on command line what would I enter to add "ADJ-" to the
beginning of each filename? I want two copies of each, one with and one
without the prefix and I don't fancy cp'ing each one manually.

Andy J.
  Reply With Quote


  sponsored links


2 22nd December 09:40
de kameel
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.



[sander@multia speel]$ ll
total 8
-rw-rw-r-- 1 sander sander 9 Dec 13 21:50 bla
-rw-rw-r-- 1 sander sander 9 Dec 13 21:50 blabla
[sander@multia speel]$

[sander@multia speel]$ find * -exec cp {} ADJ-{} \;

[sander@multia speel]$ ll
total 16
-rw-rw-r-- 1 sander sander 9 Dec 13 22:42 ADJ-bla
-rw-rw-r-- 1 sander sander 9 Dec 13 22:42 ADJ-blabla
-rw-rw-r-- 1 sander sander 9 Dec 13 21:50 bla
-rw-rw-r-- 1 sander sander 9 Dec 13 21:50 blabla
[sander@multia speel]$


HTH

De Kameel
  Reply With Quote
3 22nd December 09:40
lew pitcher
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


for file in * ;
do
mv "$file" "ADj-$file"
done


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDn0WSagVFX4UWr64RAhh3AJwIZJvxDWMpjr5Gzzn+Q/o7vV90IACg0iyK
8FhshXZxb16FhqZu6EMCx4U=
=lB+P
-----END PGP SIGNATURE-----
  Reply With Quote
4 22nd December 09:40
eric
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


This wont work because he wants "two copies of each, one with and one
without the prefix "

This should work:

for file in * ;
do
cp "$file" "ADj-$file"
done

Eric
  Reply With Quote
5 22nd December 09:40
james d. beard
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


This is faster than using find, but the OP asked for a copy
of each file with the modified name, not a change to the
modified name.

for i in *
do
cp $i ADJ-$i
done

Cheers!

jim b.

--
Unix is not user-unfriendly; it merely
expects users to be computer-friendly.
  Reply With Quote
6 22nd December 09:40
lew pitcher
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Oops... missed that requirement

My counter proposal...

for file in * ;
do
ln "$file" "ADJ-$file"
done


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDn22wagVFX4UWr64RAs2uAJ9Hhd7YsSO6E7kgDgZ/hdNqhITZnwCg9sAW
8UZ0nwwzv5u1g3hh3uQeXGA=
=PsoE
-----END PGP SIGNATURE-----
  Reply With Quote
7 22nd December 09:40
robert marshall
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


and doesn't operate on subdirectories you need to use maxdepth to
prevent unintentionally altering other files

Robert
--
La grenouille songe..dans son château d'eau
  Reply With Quote
8 22nd December 09:40
dave
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


Also, it'll hit *all* files in that directory (iirc). Replace...:
for file in * ;
.... with...:
for file in $1 ;
.... if it's for a batchfile. If the for-done loop is being typed in at a
terminal window, replace...:
for file in * ;
.... with...:
for file in *.ext ;
.... to do the operation on files with a .ext extension.

--
Linux: because I work with Windows, and that's bad enough.
AOLM FAQ - http://blinkynet.net/comp/faq_aolm.html
RLU #300033 - MDK 10.2 - WindowMaker 0.92.0
  Reply With Quote
9 22nd December 09:41
andy j.
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


Thanks Guys, for all the answers. I'm really going to have to get a
good book about trickier command line usage; anyone got any recommendations?

Andy J.
  Reply With Quote
10 22nd December 09:41
de kameel
External User
 
Posts: 1
Default Adding a prefix to multiple filenames.


Recommendation: ask in this newsgroup how to solve a certain problem
using a script and you will get a lot of answers and instructions.

And don't worry: people like to post their (knowlegde about) scripts.
Making up *nix oneliners is even more fun than solving Sudoku's. ;-)

Please don't forget some awk and grep questions: fun guaranteed.

De Kameel
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666