APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS














(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



decrement filenames in unix







Author: anonymous
Date: Thu Aug 25 11:12:26 2005
Subject: decrement filenames in unix

Hi, I need a help regrading renaming the file names with the follwoing scenario: I have thousand of files with following name:


134532565.txt 
134532566.txt 
134532567.txt 
134532568.txt 
 

I want a script to decrement the file names with last 4 digits of current files names as following:

2565.txt 
2567.txt 
2568.txt 
2569.txt 
 

Any help regaridng this, ll appreciate very much.

Thankning You in advance.

Nick




More Articles by anonymous




Click here to add your comments





Thu Aug 25 11:44:03 2005: Subject:   TonyLawrence

gravatar
Please SEARCH before you post.

If you had typed "rename" into the search box at the top of every page here, you would have got http://aplawrence.com/SCOFAQ/FAQ_scotec1rename.html as the first hit and had your answer instantly, and also saved me the time of pointing this out to you.

Search first. If you don't find what you want or don't understand it, post. But search first.



Fri Aug 26 05:45:06 2005: Subject: rename (remove leading didgets)   anonymous


I would try something like this: Make a directory called scripts with another directory called test then save the contents of this file as rename.pl, and copy some of your files into the test directory. Notice i said COPY. You don't want to lose the originals, but this worked for me when I had to rename a bunch of spyware images one time... if it doesn't, we can use perl's substr() function....

#!/usr/bin/perl
$dir="/scripts/test/";
opendir (DH1,"$dir") || die "Cannot open directory\n";
@ARRAY1=readdir(DH1);
$cnt="0";


foreach $file (@ARRAY1)
{
next if $file=~/\^./;
if ($file =~/^\d/)
{
$file2 = $file;
$file2 =~ s/^\d[4]//; # This d[4] item is what you would change for the
# number of didgets to remove...
`cp $dir/$file $dir/$file2`;
# `rm -f $dir/$file`; use for cleanup with caution ;)
$cnt++;
}
}
print "$cnt files handled\n";

close DH1;







Sat Aug 27 13:28:26 2005: Subject: rename (remove leading didgets)   anonymous


Many Thanks for your help,
I tried to do as per your instruction, but scipt gave me the error:

source $PATH and file names with source $PATH file names are identical.

Any suggestion on it..




Fri Sep 9 20:43:56 2005: Subject:   anonymous


Where did you save the script? It should be in the scripts directory and not the test directory. If that is not the issue, then I would want to look at putting the copied files into another directory.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



/Forum/anonymous24.html copyright August 2005 anonymous All Rights Reserved

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server






More:
       - Forum


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress