LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-19-2009, 04:48 PM   #1
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
Renaming files


I have a long list of jpg files that people have submitted:

cat.jpg
dog.jpg
bird.jpg
etc....

I need to append a two letter code in front of each file to designate where it came from:

wacat.jpg
wadog.jpg
wabird.jpg
etc...

Can I do this with 'sed' ? The only time I have ever used sed was to use it for substitution for the contents of a file, and not the file itself.
 
Old 05-19-2009, 05:00 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
man rename


Cheers,
Tink
 
Old 05-19-2009, 07:15 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by tekmann33 View Post
I have a long list of jpg files that people have submitted:

cat.jpg
dog.jpg
bird.jpg
etc....

I need to append a two letter code in front of each file to designate where it came from:

wacat.jpg
wadog.jpg
wabird.jpg
etc...

Can I do this with 'sed' ? The only time I have ever used sed was to use it for substitution for the contents of a file, and not the file itself.
if you have Python
Code:
#!/usr/bin/env python
import os
for files in os.listdir("."):
    if os.path.isfile(files) and files.endswith(".jpg"):
        os.rename(files,"wc"+files)
 
Old 05-19-2009, 08:50 PM   #4
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
Shell script:
Code:
#!/bin/sh
for file in *.jpg
do
   mv "$file" "wa$file"
done
 
Old 05-22-2009, 09:57 AM   #5
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by burninGpi View Post
Shell script:
Code:
#!/bin/sh
for file in *.jpg
do
   mv "$file" "wa$file"
done
What would the login look like if I already had files that had "wa" appended to them and some of them without and I only want to change the filename that do not have "wa". Also, it would do the same for both jpeg and jpg files. For instance:

cat.jpg
wadog.jpg
bird.jpg
wafish.jpg

So, I guess I want this in pseudocode:

for every file that has the extension "jpg" or "jpeg"
if file does not already have "wa" in front of file
 
Old 05-22-2009, 10:01 AM   #6
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by burninGpi View Post
Shell script:
Code:
#!/bin/sh
for file in *.jpg
do
   mv "$file" "wa$file"
done
What would the logic look like if I already had files that had "wa" appended to them and some of them without "wa" and I only want to change the filenames that do not have "wa"? Also, there is a possiblity that I might have jpegs submitted instead of jpgs. For instance, my directory might look like this:

cat.jpg
wadog.jpg
bird.jpg
wafish.jpeg
ferrit.jpeg

So, I guess what I want in pseudocode is this:

Code:
for every file that has the extension "jpg" or "jpeg"
       if file does not already have "wa" at beginning of the file
               change "file" to "wafile"
        else
               skip file

Last edited by tekmann33; 05-22-2009 at 10:08 AM.
 
Old 05-22-2009, 10:08 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
for file in $(find .  \( -iname \*.jpg -o -iname \*.jpeg \) ! -name wa\*)
do
  mv ${file##./} wa${file##./}
done
 
  


Reply



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 files mashcaster Linux - General 3 05-08-2009 03:14 PM
Renaming files Viablade Linux - Newbie 11 10-10-2008 01:54 PM
Renaming files leupi Linux - General 5 09-16-2008 03:22 AM
help renaming files please balistic Linux - Newbie 2 07-29-2007 03:35 PM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 01:16 PM

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

All times are GMT -5. The time now is 04:36 AM.

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