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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
12-04-2012, 02:31 PM
|
#1
|
|
Member
Registered: Oct 2012
Posts: 81
Rep: 
|
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.
|
|
|
|
12-04-2012, 03:57 PM
|
#2
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
For example 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
|
|
|
|
12-04-2012, 04:57 PM
|
#3
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by graphicsmanx1
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 ?
|
|
|
|
12-04-2012, 04:57 PM
|
#4
|
|
Member
Registered: Oct 2012
Posts: 81
Original Poster
Rep: 
|
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.
|
|
|
|
12-04-2012, 05:08 PM
|
#5
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by graphicsmanx1
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 ?
|
|
|
|
12-04-2012, 05:16 PM
|
#6
|
|
Member
Registered: Oct 2012
Posts: 81
Original Poster
Rep: 
|
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.
|
|
|
|
12-04-2012, 05:26 PM
|
#7
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
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.
|
|
|
|
12-04-2012, 05:30 PM
|
#8
|
|
Member
Registered: Oct 2012
Posts: 81
Original Poster
Rep: 
|
Quote:
Originally Posted by markush
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. 
|
|
|
|
12-04-2012, 05:32 PM
|
#9
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
Quote:
Originally Posted by graphicsmanx1
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
|
|
|
|
12-04-2012, 06:18 PM
|
#10
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by graphicsmanx1
... 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.
|
|
|
|
12-04-2012, 06:44 PM
|
#11
|
|
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: 2,901
|
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.
|
|
|
|
12-05-2012, 03:36 PM
|
#12
|
|
Member
Registered: Oct 2012
Posts: 81
Original Poster
Rep: 
|
Quote:
Originally Posted by frieza
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
|
|
|
|
12-05-2012, 11:45 PM
|
#13
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by graphicsmanx1
thank you
|
I do not see a spreadsheet in the proposed solution. Am I missing something obvious ?
|
|
|
|
12-06-2012, 12:49 AM
|
#14
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
Quote:
Originally Posted by Sergei Steshenko
... 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
|
|
|
|
12-06-2012, 01:50 AM
|
#15
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by markush
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".
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:24 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|