LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2005, 09:02 AM   #1
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Rep: Reputation: 15
command line newbie help! -renaming files.


I've been using KDE and Gnome for a while and am pretty comfortable with desktop and mouse clicking, but when it comes to command lines and writing scripts I'm mostly lost.

My question is: I have sequential files in a folder and I'd like to rename them from file1.jpg, file2.jpg to file 001.jpg file 002.jpg, etc. there are a lot of files, so I was hoping there is a faster way to rename files in a folder sequentially.

i tried : rename file1.jpg file001.jpg, and also renum, but it didn't work how I thought...

Also, I am planning to go to borders this weekend and purchase a book on learning linux. My time is somewhat constrained, though, so I'd like advice on which book would be able to give me a good overview of general linux commands and beginning scripting (i'm hoping to learn to make scripts so I can stream line work as well as hopefully to become adept at using linux), in a somewhat easier to understand format.
 
Old 08-31-2005, 09:19 AM   #2
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi
i am not sure, but you can try out this
"rename file file00 file*.jpg" .

Mehul.

Last edited by mehuljv; 08-31-2005 at 09:27 AM.
 
Old 08-31-2005, 09:22 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Try :
Code:
$ ls
file1.jpg  file278.jpg  file2.jpg  file3.jpg

$ rename file file0 file?.jpg
$ rename file file0 file??.jpg

$ ls
file001.jpg  file002.jpg  file003.jpg  file278.jpg
 
Old 08-31-2005, 09:35 AM   #4
mifan
Member
 
Registered: Apr 2004
Distribution: mandrake
Posts: 116

Original Poster
Rep: Reputation: 15
$ ls
file1.jpg file278.jpg file2.jpg file3.jpg

$ rename file file0 file?.jpg
$ rename file file0 file??.jpg

$ ls
file001.jpg file002.jpg file003.jpg file278.jpg

that worked great!
 
Old 08-31-2005, 09:37 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
And the rename man page is great too
 
Old 09-01-2005, 06:06 AM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Or, for a more generalised version:
Code:
#!/bin/bash
for file in $(ls -1 file*.jpg); do
    number=$(echo $file | sed -e "s/file//" -e "s/\.jpg$//")
    mv $file file$(printf "%03d" $number).jpg
done
hth --Jonas
 
  


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
Command line to sort files satimis Programming 6 06-28-2005 02:50 AM
newbie questions about the command line sether *BSD 12 09-04-2004 01:40 PM
Newbie: Stuck at command line. BoardinBum Slackware 9 08-14-2004 02:40 PM
Another Newbie Question - Command Line Shortcuts? AtomiCTheGr8 Linux - General 9 08-14-2002 09:02 PM
Command Line Newbie mrozkan Linux - General 6 01-27-2002 12:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 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