LinuxQuestions.org
Help answer threads with 0 replies.
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 01-21-2008, 11:05 PM   #1
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Rep: Reputation: 15
To rename files in a directory should I use Bash script or a Perl Script ?


I am using Suse 10.2 on a HP Compaq NX6325 Notebook with 2gigs of ram. I have a USB Ext. drive 500G. with over 1,000 files that have to be renamed in succeeding order of numbers meaning 1,2,3,4, with a date (Date that the file was renamed).

My question is should I create a Perl script or a Bash Script to rename the files and should I rename them in blocks of 200 and move the renamed files to another directory?

The catch is I will have to use the system in question to do my day to day work like email creating docs and surfing the web.

So the script once running can not be taxing on the system and has to be very fast since the files in question has to be renamed with in a 3 day period.

I know this may start a Flame war.

Thank you for you help and advise in advance

Last edited by jamtech; 01-21-2008 at 11:19 PM.
 
Old 01-21-2008, 11:08 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Not quite sure what you mean by 'renamed in succeeding order of numbers meaning 1,2,3,4', but Perl is faster (compiled rather than interpreted) and it's better if the selection/matching criteria gets complicated.
Otherwise, use bash ... your choice.
 
Old 01-21-2008, 11:24 PM   #3
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
chrism01,

I should clarify my question I need to have the files renamed in sequence with a date added to the file as part or the rename process
 
Old 01-21-2008, 11:37 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Either lang should do want you want within the time limit.
Note that the mv cmd enables you to rename at the same same eg

mv afile.dat bfile.dat

iirc, this (mv) can only be used between dirs on the same (physical) disk partition, otherwise you have to copy (cp), then delete (rm) the original version.
There's no need to use blocks of 200, just do them one at a time as you go eg (pseudo-code)
Code:
for file in list
do
    newfile = create newfilename
    mv file newfilename
done
 
Old 01-22-2008, 05:21 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Renaming 1000 files will not put any stress on your system. If you are only renaming them, they are, well, renamed and not copies, no large data transfer involved. I routinely rename batches of 100 files on my system and the slowest is the verbose output to my screen. 5-10 seconds?

jlinkels
 
Old 01-22-2008, 05:35 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
a safety hint,

I usually write a script to echo the command to stdout, then capture and run it
through a shell, so you have a record of what you have done if you need
to reverse it:

Code:
for file in *; do
  echo mv $file $file.old
done
so it echoes to stdout like:
Code:
mv 1 1.old
mv 1.c 1.c.old
mv 1.pl 1.pl.old
mv 1.sh 1.sh.old
mv 11 11.old
so do somehting like:
Code:
move_script > output
# check the output file looks okay
sh output
# you have a record of what happened
 
Old 01-22-2008, 07:48 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,771

Rep: Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070
Quote:
Originally Posted by chrism01 View Post
iirc, this (mv) can only be used between dirs on the same (physical) disk partition, otherwise you have to copy (cp), then delete (rm) the original version.
This is not true. mv works fine regardless of filesystems. Of course behind the scenes the physical copying will take place if you mv across filesystems, but you don't have to do it manually.
 
Old 01-22-2008, 11:25 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
In the old days that wasn't always true... eg:

info mv
"Prior to version `4.0' of the fileutils, `mv' could move only regular files between file systems."

From perl docs re (Perl) rename cmd
"Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this."


guess i'm getting old
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script? how to rename files in random way? lefty.crupps Linux - Software 16 11-03-2015 02:07 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM

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

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