LinuxQuestions.org
Review your favorite Linux distribution.
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-06-2012, 09:58 AM   #16
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by Sergei Steshenko View Post
I do not see a spreadsheet in the proposed solution. Am I missing something obvious ?
I think you are fixed on the "spreadsheet". My goal is to rename 100s of files with a desired new name that must be exact that has no relation to one or another. I used the spreadsheet to record the existing names. To the right of the existing name I typed in the new name they need to be. I was interested in developing a script that will run through that sheet or as someone else put it CSV since it is txt based.

Id be happy if I could run a script that read:
file A rename from 12345.jpg to 1458979.jpg.

Last edited by graphicsmanx1; 12-06-2012 at 10:10 AM.
 
Old 12-06-2012, 10:16 AM   #17
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 think you are fixed on the "spreadsheet". My goal is to rename 100s of files with a desired new name that must be exact that has no relation to one or another. I used the spreadsheet to record the existing names. To the right of the existing name in the I typed in the new name they need to be. I was interested in developing a script that will run through that sheet or as someone else put it CSV since it is txt based.

Id be happy if I could run a script that read:
file A rename from 12345.jpg to 1458979.jpg.
I think you are missing the point(s) already given.

The very first point to understand is to realize that the 'rename' function requires two inseparable arguments, i.e. a pair (of file names).

The second point is that you need to define rules using which destination name can be derived from source name.

If and when you define the rules and after some thinking come to the conclusion that the rules are OK, you code the rules in whatever language you feel comfortable with.

If the rules are hard coded human chosen names, associative arrays come to mind: https://en.wikipedia.org/wiki/Associative_array .
 
Old 12-06-2012, 10:34 AM   #18
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
I understand what some are saying BUT my argument is how can several hundreds of files be renamed with something like rename or mv IF there isnt a common element you are using?? Everything I have read and searched has a common entity in regards to something like "I have files but need a date added" and yes that can be done with rename and mv I get that. My issue is with several hundreds of files how do you properly and safely pipe in a random name and rename that name to something exact in the end.

frieza understood what I was trying to accomplish with his post. Thats why I keep posting examples like

Old name alpha.ai renamed to omega.ai
old name pie32.ai renamed to apple.ai
old name xmas.ai renamed to santa.ai

EDIT:
and the spreadsheet came into play with the whole thing when I used that to fill in my old names and new names. I thought it would be easier to use something I already filled to read from. Ive been reading sed all morning and maybe thought of some sed command that I could do sed alpha.ai,omega.ai.

I searched and found all these but they wont work
here is the same as here
here

Last edited by graphicsmanx1; 12-06-2012 at 11:05 AM.
 
Old 12-06-2012, 11:08 AM   #19
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,111

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
i believe you guys are tripping up on the word 'spreadsheet'
all the OP is looking to do is create a data file that contains original names of existing files, and new names for the files, a way of passing that as arg(1), arg(2) of the mv command, and a 'spreadsheet' program provides a nice columnar interface for editing that data hence my suggestion of a CSV file, it can be edited with a spreadsheet, is columnar and is plain text.

so basically a file
Code:
column a, column b
old name, new hame
old name, new hame
old name, new hame
old name, new hame
old name, new hame
then run
Code:
for line file.txt
do
mv $column a  $column b
done
note that above example is extremely simplified pseudo code and won't work

Last edited by frieza; 12-06-2012 at 11:14 AM.
 
1 members found this post helpful.
Old 12-06-2012, 11:15 AM   #20
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by graphicsmanx1 View Post
...
Old name alpha.ai renamed to omega.ai
old name pie32.ai renamed to apple.ai
old name xmas.ai renamed to santa.ai

EDIT:
and the spreadsheet came into play with the whole thing when I used that to fill in my old names and new names. I thought it would be easier to use something I already filled to read from. Ive been reading sed all morning and maybe thought of some sed command that I could do sed alpha.ai,omega.ai.
...
I would do this with Perl, you may take a look at the script I posted above. If you save your spreadsheet document as csv (comma separated value) file you will have lines in the form
Code:
old filename,new filename
In Perl this can be done with something like:
Code:
foreach $line (<CSVFILE>) {
    ( $old, $new ) = split /,/, $line ;
    move ( $old, $new ) ;
}
You will have to check that the filename $new doesn't already exist!

Markus

Last edited by markush; 12-06-2012 at 11:16 AM. Reason: typo
 
Old 12-06-2012, 12:05 PM   #21
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 understand what some are saying BUT my argument is how can several hundreds of files be renamed with something like rename or mv IF there isnt a common element you are using?? Everything I have read and searched has a common entity in regards to something like "I have files but need a date added" and yes that can be done with rename and mv I get that. My issue is with several hundreds of files how do you properly and safely pipe in a random name and rename that name to something exact in the end.

frieza understood what I was trying to accomplish with his post. Thats why I keep posting examples like

Old name alpha.ai renamed to omega.ai
old name pie32.ai renamed to apple.ai
old name xmas.ai renamed to santa.ai

EDIT:
and the spreadsheet came into play with the whole thing when I used that to fill in my old names and new names. I thought it would be easier to use something I already filled to read from. Ive been reading sed all morning and maybe thought of some sed command that I could do sed alpha.ai,omega.ai.

I searched and found all these but they wont work
here is the same as here
here

"My issue is with several hundreds of files how do you properly and safely pipe in a random name and rename that name to something exact in the end." - What is the rule for creating that "exact" ? Why to rename into something random ? Why not use 'f0000', 'f0001', 'f0002', etc names ?
 
Old 12-06-2012, 01:18 PM   #22
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
"My issue is with several hundreds of files how do you properly and safely pipe in a random name and rename that name to something exact in the end." - What is the rule for creating that "exact" ? Why to rename into something random ? Why not use 'f0000', 'f0001', 'f0002', etc names ?
1. I dont know "foo".
2. I think your way over complicating the entire thread.
3. Im just going to do the following several hundred times in excel, yes I said excel and paste into notepad++. I didnt want to do it this way, I wanted to learn a cool way and try to learn Linux and its fundamentals.

In excel I opened a new .xls file.
Column A Leave Blank
Column B mv
Column C "old_name"
Column D "new_name"
  • In Column A write =CONCATENATE(B1,C1,D1) and pull that down to the last name.
  • Copy the entire column and paste into a notepad++ file.
  • First line in notepad++ #!/bin/bash
  • Save file out as name.sh.
  • Might need to convert Dos file so run dos2unix name.sh
  • In Unbuntu point terminal to location of images and name.sh.
  • Type bash name.sh.
  • Done.

Wanted to learn how to do it better than this but I guess it proves more complicated in trying to explain it. Thanks for the help of others.

EDIT:
sorry meant mv not rm

Last edited by graphicsmanx1; 12-06-2012 at 01:26 PM.
 
Old 12-06-2012, 01:26 PM   #23
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by graphicsmanx1 View Post
1... I wanted to learn a cool way and try to learn Linux and its fundamentals.

In excel I opened a new .xls file.
Column A Leave Blank
Column B rm
Column C "old_name"
Column D "new_name"

In Column A write =CONCATENATE(B1,C1,D1) and pull that down to the last name. Copy the entire column and paste into a notepad++ file. First line in notepad++ #!/bin/bash enter. Save file out as name.sh. Might need to convert Dos file so run dos2unix name.sh Unbuntu point terminal to location of images and name.sh. Type bash name.sh. Done. Wanted to learn how to do it better than this but I guess it proves more complicated in trying to explain it. Thanks for the help of others.
That's not cool neither a Linux way. I can understand that you want to have a csv file with old and new filenames in columns, but please read the above posts again and pick some of the answers which you can apply to your csv file.
In order to avoid problems with DOS/Linux converting, why don't you install openoffice or libreoffice or any spreadsheet for Linux and do the whole thing in Linux.
BTW: you should consider if the rm command in column B is really what you want

Markus
 
Old 12-06-2012, 01:31 PM   #24
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
That's not cool neither a Linux way. I can understand that you want to have a csv file with old and new filenames in columns, but please read the above posts again and pick some of the answers which you can apply to your csv file.
In order to avoid problems with DOS/Linux converting, why don't you install openoffice or libreoffice or any spreadsheet for Linux and do the whole thing in Linux.
BTW: you should consider if the rm command in column B is really what you want

Markus
I will probably do this but I keep bouncing back and forth from reading awk, sed, bash, and perl. I need to focus on one area and feel comfortable. I like the perl idea but Im trying to prevent burn-out. I want to only focus on two but Im not sure which two.
 
Old 12-06-2012, 01:42 PM   #25
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,974

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
sed is relatively complicated.

Here a complete Perl solution:
Code:
#!/usr/bin/perl

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

open CSVFILE, "filenames.csv" ;
my $line ;
foreach $line (<CSVFILE>) {
    my ( $old, $new ) = split /,/, $line ;
    if ( -e $new ) {
        print "file $new exists, cannot overwrite\n" ;
        next ;
    }
    move ( $old, $new ) ;
}
One thing that doesn't work is that when the new filename is checked for existence, it gives an errormessage if the file is not there and this is the default.

Hope this helps.

Markus
 
1 members found this post helpful.
Old 12-07-2012, 02:37 AM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graphicsmanx1 View Post
1. I dont know "foo".
2. I think your way over complicating the entire thread.
3. Im just going to do the following several hundred times in excel, yes I said excel and paste into notepad++. I didnt want to do it this way, I wanted to learn a cool way and try to learn Linux and its fundamentals.

In excel I opened a new .xls file.
Column A Leave Blank
Column B mv
Column C "old_name"
Column D "new_name"
  • In Column A write =CONCATENATE(B1,C1,D1) and pull that down to the last name.
  • Copy the entire column and paste into a notepad++ file.
  • First line in notepad++ #!/bin/bash
  • Save file out as name.sh.
  • Might need to convert Dos file so run dos2unix name.sh
  • In Unbuntu point terminal to location of images and name.sh.
  • Type bash name.sh.
  • Done.

Wanted to learn how to do it better than this but I guess it proves more complicated in trying to explain it. Thanks for the help of others.

EDIT:
sorry meant mv not rm

"a cool way" - utter nonsense. And probably the root cause of your problems. I.e. forget about cool. Think about what needs to be done and what actually happens when the task is being performed.


"yes I said excel and paste into notepad++" - nonsense. I.e. however much I dislike Windows, the OS has all the necessary tools to do the job, and more convenient tools can be easily installed.

Windows has BAT ( .bat files I mean) scripting language, and there is pretty decent Power shell for Windows - free as in beer, developed by Microsoft.

A lot of UNIX tools exist for Windows too, as well as more convenient Perl/Python/Ruby.


Excel is the problem - not that it doesn't work, but the way it influenced your thinking. And in Linux there is 'gnumeric' ( https://en.wikipedia.org/wiki/Gnumeric , http://projects.gnome.org/gnumeric/ ), and both tools are equally mind poisoning for the task at hand.


Stop thinking in terms of GUI, start thinking in terms of data (structures) and operations which are to be performed on data. Regardless of OS.


Have you decided on the file naming rules ?
 
Old 12-12-2012, 11:15 AM   #27
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
@sergio.. You know I appreciate a lot of what people have done to help me grow in Linux but dude you over analyze in more threads that I read and over complicate everything. Again, in my post you OVER ANALYZE what I said. Do you not read what people post before commenting???

Quote:
I wanted to learn a cool way and try to learn Linux and its fundamentals.

IE: I wanted to actually apply Linux to what I know, but as you take it...
Quote:
"a cool way" - utter nonsense. And probably the root cause of your problems. I.e. forget about cool. Think about what needs to be done and what actually happens when the task is being performed.

Last edited by graphicsmanx1; 12-12-2012 at 11:22 AM.
 
Old 12-13-2012, 02:53 AM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by graphicsmanx1 View Post
...dude you over analyze ...
In this thread I easily observed what is said in Russian as "забивать гвозди микроскопом" - can be translated as "to use microscope to hammer nails".


The microscope being the combination of "spreadsheet" and "cool".

The true hammer head being system 'rename' function.

The hammer handle being any scripting language wrapping the 'rename'.
 
  


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


All times are GMT -5. The time now is 10:55 PM.

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