LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 12-04-2012, 02:31 PM   #1
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Rep: Reputation: Disabled
best way to rename files


I wanted to redo my files on my server. What would be the best way to take the existing names and put them in column A of a spreadsheet and in column B will be the desired name. I asked a question about echoing and was suggested awk but Im unsure what its called or where to.
 
Old 12-04-2012, 03:57 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
For example
Code:
ls | sed '/./p'
would work.

But are you sure that it wouldn't be better to rename the files with a shellscript? Could you please explain how you want to rename the files? do you have an example?

Markus
 
Old 12-04-2012, 04:57 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graphicsmanx1 View Post
I wanted to redo my files on my server. What would be the best way to take the existing names and put them in column A of a spreadsheet and in column B will be the desired name. I asked a question about echoing and was suggested awk but Im unsure what its called or where to.
And what does a spreadsheet application have to do with renaming files ?
 
Old 12-04-2012, 04:57 PM   #4
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
well I have a bunch of files that are .psds and other formats but I wanted to pull the names in a .xls file and then place them in column A and in column B would be the output. The spreadsheet was just an idea of a way to store the needed name changes.

Old
Goes in column A
1. design.psd
2. invoice.xml
3. proof.pdf
4. template.ai

New
Goes in column B
1. date_client_design.psd
2. date_client_invoice.xml
3. client_proof.pdf
4. project_template.ai

Just an example trying to organize my workflow

Last edited by graphicsmanx1; 12-04-2012 at 05:00 PM.
 
Old 12-04-2012, 05:08 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graphicsmanx1 View Post
well I have a bunch of files that are .psds and other formats but I wanted to pull the names in a .xls file and then place them in column A and in column B would be the output. The spreadsheet was just an idea of a way to store the needed name changes.

Old
Goes in column A
1. design.psd
2. invoice.xml
3. proof.pdf
4. template.ai

New
Goes in column B
1. date_client_design.psd
2. date_client_invoice.xml
3. client_proof.pdf
4. project_template.ai

Just an example trying to organize my workflow
It doesn't answer my question. I.e. I still do not understand what a spreadsheet application has to do with file renaming.

There is an application called 'rename': http://linux.die.net/man/1/rename . The application is, of course, based on 'rename' system call: http://www.manpagez.com/man/2/rename/ .

There are scripting languages which have wrappers around the system call, e.g.: http://perldoc.perl.org/functions/rename.html .

So, again, what does a spreadsheet application have to do with file renaming ?

...

A mandatory car analogy: suppose you need to burn 1 liter of gasoline. Do you necessarily need to buy a car, fill the tank and ride until the 1 liter of gasoline is consumed ?
 
Old 12-04-2012, 05:16 PM   #6
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
think you're over examining the purpose of the spreadsheet. The spreadsheet was a means to be able to identify the ending goal of the file naming convention. If another way could be recommended I would use it. I was suggesting the spreadsheet because I would record the current name in one column and in the other column the name goal within the same row so why not use it? I have used rename but how can you securely rename a large number of files without having to worry about any issues??


Quote:
A mandatory car analogy: suppose you need to burn 1 liter of gasoline. Do you necessarily need to buy a car, fill the tank and ride until the 1 liter of gasoline is consumed?
no I already own a scooter and it can get me farther and since I currently USE the scooter why do I need to buy a car or use another means of transportation? lol.

Last edited by graphicsmanx1; 12-04-2012 at 05:18 PM.
 
Old 12-04-2012, 05:26 PM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
I've once written a little perlscript, I called it rename.pl and it resides in /usr/local/bin on my computer
Code:
#!/usr/bin/perl

use strict ;
use warnings ;
use File::Copy ;

my ($from, $to, $in) = @ARGV ;

opendir THISDIR, "." ;
my @files = readdir THISDIR ;
my ( $old, $new ) ;
foreach $old ( @files ) {
        next if $old !~ /$in/ ;
        $new = $old ;
        $new =~ s/$from/$to/ ;
        my $subst = $1 ;
        $new =~ s/\$1/$subst/ ;
        next if $old eq $new ;
        if ( -e $new ) {
            print "file $new exists, cannot overwrite\n" ;
        }
        move ( $old, $new ) ;
}
I haven't used it for some month but you try out how it works and if you can use it. Other than the rename tool it can handle wildcards for digits and numbers. Also it will not overwrite an existing filename.

Markus

Last edited by markush; 12-04-2012 at 05:27 PM.
 
Old 12-04-2012, 05:30 PM   #8
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
I've once written a little perlscript, I called it rename.pl and it resides in /usr/local/bin on my computer
Code:
#!/usr/bin/perl

use strict ;
use warnings ;
use File::Copy ;

my ($from, $to, $in) = @ARGV ;

opendir THISDIR, "." ;
my @files = readdir THISDIR ;
my ( $old, $new ) ;
foreach $old ( @files ) {
        next if $old !~ /$in/ ;
        $new = $old ;
        $new =~ s/$from/$to/ ;
        my $subst = $1 ;
        $new =~ s/\$1/$subst/ ;
        next if $old eq $new ;
        if ( -e $new ) {
            print "file $new exists, cannot overwrite\n" ;
        }
        move ( $old, $new ) ;
}
I haven't used it for some month but you try out how it works and if you can use it. Other than the rename tool it can handle wildcards for digits and numbers. Also it will not overwrite an existing filename.

Markus
thanks for the script but I wanted to learn how to do my own. Only way I seem to learn from trial and error.
 
Old 12-04-2012, 05:32 PM   #9
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by graphicsmanx1 View Post
thanks for the script but I wanted to learn how to do my own. Only way I seem to learn from trial and error.
That's a very good idea, good luck, I'm curious which solution you'll find.

Markus
 
Old 12-04-2012, 06:18 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graphicsmanx1 View Post
... I was suggesting the spreadsheet because I would record the current name in one column and in the other column the name goal within the same row so why not use it? ...
Look at the "column" word and ask yourself a question: "will anything change if instead of column I use row ?".

The answer is no, and that alone makes spreadsheet irrelevant for the task - exactly because it introduces redundant for the task entities: columns (or rows). Remember Occam's razor: "Don't create entities beyond the necessary.

Last edited by Sergei Steshenko; 12-04-2012 at 06:59 PM.
 
Old 12-04-2012, 06:44 PM   #11
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,104

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
i presume what you are attempting to do is automatically generate a list of files in a certain directory into column A, then use a spreadsheet program and manually input new names for the file into column B, then run a script that parses the file and performs the name changes from column A to B?

perhaps the word 'spreadsheet' should be substituted for 'CSV file', which can be
opened and edited in a spreadsheet program, as well as parsed by a shell script.

since you said you wanted to build your own i would look into
for next loops in bash
sed
cut
mv

a simple renaming script (this example moves files with extension JPG to jpg) might look like
Code:
#!/bin/bash
for file in *.JPG #list all the files named .JPG
do #begin loop
filename_raw =`echo $file | cut -f1 -d.` #cut off the file extension
mv $file $filename_raw.jpg move the original file name to the new extension
done #end loop
your script would replace the *.JPG with the output of running CAT on your file, and the cut would be cutting each row on the ,
then use sed to remove the " (unless you saved the CSV without quotes)

Last edited by frieza; 12-04-2012 at 06:45 PM.
 
Old 12-05-2012, 03:36 PM   #12
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by frieza View Post
i presume what you are attempting to do is automatically generate a list of files in a certain directory into column A, then use a spreadsheet program and manually input new names for the file into column B, then run a script that parses the file and performs the name changes from column A to B?

perhaps the word 'spreadsheet' should be substituted for 'CSV file', which can be
opened and edited in a spreadsheet program, as well as parsed by a shell script.

since you said you wanted to build your own i would look into
for next loops in bash
sed
cut
mv

a simple renaming script (this example moves files with extension JPG to jpg) might look like
Code:
#!/bin/bash
for file in *.JPG #list all the files named .JPG
do #begin loop
filename_raw =`echo $file | cut -f1 -d.` #cut off the file extension
mv $file $filename_raw.jpg move the original file name to the new extension
done #end loop
your script would replace the *.JPG with the output of running CAT on your file, and the cut would be cutting each row on the ,
then use sed to remove the " (unless you saved the CSV without quotes)
thank you
 
Old 12-05-2012, 11:45 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graphicsmanx1 View Post
thank you
I do not see a spreadsheet in the proposed solution. Am I missing something obvious ?
 
Old 12-06-2012, 12:49 AM   #14
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by Sergei Steshenko View Post
... Am I missing something obvious ?
I understood that the OP needs the spreadsheet for the overview and to decide which new filenames to use. So the spreadsheet is not directly included when the files once are renamed.

Markus
 
Old 12-06-2012, 01:50 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by markush View Post
I understood that the OP needs the spreadsheet for the overview and to decide which new filenames to use. So the spreadsheet is not directly included when the files once are renamed.

Markus
I do not know OP's line of thinking. Anyway, in order to review and change, if necessary, some text there is a simple and dedicated class of tools - called "text editors".
 
  


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
Application to eliminate doubles in files and rename changed files with date? S. Chapelin Linux - Software 6 01-16-2011 02:02 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
get files from ftp and rename those files.. amaulana Linux - Newbie 2 11-15-2008 10:14 PM
how to rename files bkcreddy17 Programming 7 09-14-2008 09:22 AM
rename files allelopath Linux - General 5 07-05-2005 03:00 AM


All times are GMT -5. The time now is 01:16 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration