LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-29-2013, 02:44 AM   #1
ajaxStardust
LQ Newbie
 
Registered: Aug 2012
Location: GMT -5 (aka. EST)
Distribution: Mint, Cygwin
Posts: 26

Rep: Reputation: Disabled
Question Terminal ops: mv to affect multiple objects of container


I believe the best way that the reader and I might quickly develop the understanding I wish to communicate, would be to preview the screen cap featuring a tedious, albeit successful operation (Cygwin, but i doubt it matters):
http://whatsonyourbrain.com/a_genera...uery_re_mv.png

Identifying a problem: need to shorten file names, i resorted to that which i presumed would do the job: Cygwin term. And, so it did.

MY QUESTION:
Certainly there is a more efficient way to have done this, through a more-- perhaps, forgive my poor rhetoric-- /scripted/ approach? E.g. find all files containing the string "... It's the Infectious Grooves ", and remove that string. (vs, as i did, with each individual file, where it was necessary to copy / paste each variation of file names).

Thank you for your advisory.
Best regards
J Sabarese

@ajaxStardust
www.NoviceNotes.Net
 
Old 07-29-2013, 09:17 AM   #2
goumba
Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Wheezy (7.0), Jessie (future 8) and Sid (unstable)
Posts: 511
Blog Entries: 2

Rep: Reputation: 55
Somebody may (likely) have better, but to get you started...

Code:
#!/bin/bash

for filename in *.flac
do
   mv "$filename" "${filename//$1/}"
done
$1 holds the string you want to remove from the filenames. When you run the script, this string must be enclosed in quotes.

Code:
rename-files "Remove this string"

Last edited by goumba; 07-29-2013 at 09:22 AM.
 
Old 07-29-2013, 01:50 PM   #3
ajaxStardust
LQ Newbie
 
Registered: Aug 2012
Location: GMT -5 (aka. EST)
Distribution: Mint, Cygwin
Posts: 26

Original Poster
Rep: Reputation: Disabled
Unhappy Great Help. Couple Questions:

Thanks so much for your thoughtful reply, and attention to my concerns. It does not go w/out appreciation.

First: When I look at this, i think... okay,he's doing some sort of regular expression evaluation here. But, i'm unfamiliar w/ the syntax. E.G. $filename, this is a pseudo-name, you've placed for example, yes? Could you please be a bit more specific as to how i would setup the code?

Second. Your code-- it is meant for-- something like rename.sh, yes? a shell executable script? assuming i get that far w/ the code i want, is it as simple as [ $> exec rename.sh ] ??

(i'm cheating, i know... but figure i might as well brush-up on my syntax, etc. while i've got your attention)

EDIT: OH! wait, /filename/ in your code becomes a variable, referenced subsequently however using the /dollar sign/. it's coming back to me, but i've written only nominal batch scripts for BASH. (uh, this is BASH, yes?)

Thanks again, and for your patience. Sincerely.
-JS
www.ChordsAndScales.Info
@ajaxStardust

Last edited by ajaxStardust; 07-29-2013 at 01:53 PM. Reason: realization
 
Old 07-30-2013, 07:27 AM   #4
goumba
Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Wheezy (7.0), Jessie (future 8) and Sid (unstable)
Posts: 511
Blog Entries: 2

Rep: Reputation: 55
This is a shell script.

You save that code to a file, whatever you want to use. I used rename-files as an example.

Quote:
Second. Your code-- it is meant for-- something like rename.sh, yes? a shell executable script? assuming i get that far w/ the code i want, is it as simple as [ $> exec rename.sh ] ??
No exec needed from the shell prompt if you're running from within the Cygwin shell environment,

Code:
$ ./rename.sh
should be all you need.

Code:
c:\cygwin\bin\bash.exe rename-sh <arguments>
May be possible as well, but it's been a while since I've used Cygwin and not sure of it's shell.

Quote:
EDIT: OH! wait, /filename/ in your code becomes a variable, referenced subsequently however using the /dollar sign/. it's coming back to me, but i've written only nominal batch scripts for BASH. (uh, this is BASH, yes?)
Yes, and in this case the shell is going to store a filename that matches (the extension .flac) upon each iteration of the loop until there are no more. "${filename//$1/}" is a shell built-in and removes the string you want to (actually it replaces it will an empty string).

$1 is a special variable, the first parameter of the script ($2 the second, $3, and so on). You want to enclose this in strings, to ensure that any string containing spaces is included in the first argument (otherwise the shell parses each peach as a separate string and therefore separate argument, likely not what you want).
 
1 members found this post helpful.
Old 08-01-2013, 06:14 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
A good cli/bash intro http://rute.2038bug.com/index.html.gz
 
1 members found this post helpful.
Old 10-16-2013, 02:25 AM   #6
ajaxStardust
LQ Newbie
 
Registered: Aug 2012
Location: GMT -5 (aka. EST)
Distribution: Mint, Cygwin
Posts: 26

Original Poster
Rep: Reputation: Disabled
Cool Nearly there, but a still snagging...

per the URL ref'd by @chrism01 , I focused on the following section, albeit a cursory review of the greater resource. http://rute.2038bug.com/node10.html....00000000000000

I believe this /might/ be the section I wish to study, but herein lies my problem; lack of understanding, which goes back to
@goumba 's thoughtful, veritable tutorial for me, RE how to process multiple items using MV (or otherwise) as a BATCH script.

I am-- at least-- stuck on this concept of
Code:
$1
and the relationship to
Code:
$filename
. The "$1" being the key component of my confusion. Is this not unlike typical regex backreferences (e.g. where: "(atom_1)(atom_2)(atom_3)" are referenced as $1, $2, $3, respectively? If so, I am confused per the continuing use of the same backref. Wait... no, that doesn't necessarily make sense, as one would typically WANT to reference the same atom [e.g. (\.flac)], assuming that is referenced via $1, where $1 would change, per consecutive iterations, per loop.

:sigh: perhaps it's the unfamiliar syntax. Begging your pardon, if a most concrete example were provided (e.g. Filenames VS the BATCH example), I might most easily understand why $filename\\$1 is going to get me what i want to achieve.

Sure, I could copy paste your tutorial into my own BATCH file, but that's imitation, not /assimilation/, and I prefer to learn through assimilation, rather than guessing at what's really going on.

I suspect the understanding I seek is likely within the reference cited by @chrism01 , but it's a large volume of text which I don't mind reading, but having the advantage of interactive communication, perhaps someone might cite the particular section of that resource which is most applicable to my particular problem.

Or, do whatever. I'm easy going, and I truly appreciate your attention to my queries.
Many thanks, Sincerely,
@ajaxStardust

Best regards.
 
Old 10-18-2013, 06:57 AM   #7
goumba
Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Wheezy (7.0), Jessie (future 8) and Sid (unstable)
Posts: 511
Blog Entries: 2

Rep: Reputation: 55
Code:
mv "$filename" "${filename//$1/}"
Ok, let me see if this helps, using the script as an example...

This line uses bash's own substitution method to move a file (therefore renaming it). "${variable/replacement string/string to find/}".

Here, tt does this by taking the variable $filename, a shell script, which contains for example "01 - My Collection - My Artist - My Song 1.flac", and the first parameter passed to the shell script $1 which we'll say contains "My Collection -". The replacement string is empty, so we are replacing "My Collection -" as found in $filename with an empty string, "".

So now the new string is "01 - My Artist - My Song 1.flac". And the file is moved to one with the new filename.
 
1 members found this post helpful.
Old 10-18-2013, 12:29 PM   #8
ajaxStardust
LQ Newbie
 
Registered: Aug 2012
Location: GMT -5 (aka. EST)
Distribution: Mint, Cygwin
Posts: 26

Original Poster
Rep: Reputation: Disabled
Cool EXTRA! EXTRA! Today's Headline: @goumba proves the Simian capable of symbolic Logic!

@goumba,

perhaps not momentarily, but I mean to reference your thoughtful tutorial; your patient itemization of the issues i cited which troubled me.

I suspect you're one of the /good ones/. Thank you. I guess one might define himself a geek or a nerd, a dork, whatever if he finds himself eager and excited to try this? Oh, lord! I am a Nerd! hehe...
best wishes, friends!

Sincerely,
Twitter ID: @ajaxStardust

Author: ChordsAndScales.Net

Be well. Stay well!
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating multiple objects grob115 Programming 7 02-26-2012 06:02 AM
How to pass multiple objects into Perl script? AST Programming 1 02-07-2012 07:41 AM
count multiple objects in shell script raggmopp1961 Linux - General 1 07-22-2010 08:56 AM
Changing bash profile doesn't affect new terminal windows... SAFX Linux - Newbie 5 10-12-2004 11:51 AM
Do multiple versions of Apache on one machine affect each other? mlhammer Linux - Software 0 11-28-2003 06:33 PM


All times are GMT -5. The time now is 11:18 AM.

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