LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Terminal ops: mv to affect multiple objects of container (https://www.linuxquestions.org/questions/linux-general-1/terminal-ops-mv-to-affect-multiple-objects-of-container-4175471289/)

ajaxStardust 07-29-2013 02:44 AM

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

goumba 07-29-2013 09:17 AM

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"

ajaxStardust 07-29-2013 01:50 PM

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

goumba 07-30-2013 07:27 AM

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).

chrism01 08-01-2013 06:14 AM

A good cli/bash intro http://rute.2038bug.com/index.html.gz

ajaxStardust 10-16-2013 02:25 AM

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.

goumba 10-18-2013 06:57 AM

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.

ajaxStardust 10-18-2013 12:29 PM

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!


All times are GMT -5. The time now is 10:25 AM.