LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-15-2008, 10:04 AM   #1
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 666

Rep: Reputation: 39
nested , piped bash commands


hello everyone!
a quick question on bash.
say i have a directory with mp3 files. their filenames are of the short
[artistName] - trackTitle.mp3

i also have the corresponding originals in wave format as
[artistName] - trackTitle.wav

now i want to copy the mp3 files from a certain artist only, to a different folder...

how can i, in one line filter the filenames i want and do the copy...

here is my train of thought which is WRONG, it makes cp fail..

1. i tried doing the filter operation:
if i do ls -h , i get the filenames without attribute info:
Quote:
nass@stargaze:~/musicstuff/Mp3 to Wav$ ls -h
[Misuse]\ -\ Amanzi.mp3 [Misuse]\ -\ Loss\ in\ Action.mp3
[Misuse]\ -\ Amanzi.wav* [Misuse]\ -\ Loss\ in\ Action.wav*
[Misuse]\ -\ Desecid.mp3 [Misuse]\ -\ Progyria.mp3
[Misuse]\ -\ Desecid.wav* [Misuse]\ -\ Progyria.wav*
[Misuse]\ -\ Flat\ Line\ Evolution.mp3 [Misuse]\ -\ Way\ to\ the\ Seashore.mp3
[Misuse]\ -\ Flat\ Line\ Evolution.wav* [Misuse]\ -\ Way\ to\ the\ Seashore.wav*
[Misuse]\ -\ Genesis.mp3 [Misuse]\ -\ While\ it\ Lasts.mp3
[Misuse]\ -\ Genesis.wav* [Misuse]\ -\ While\ it\ Lasts.wav*
then if i pipe and grep 'mp3',

Quote:
nass@stargaze:~/musicstuff/Mp3 to Wav$ ls -h [Mis* | grep mp3
[Misuse]\ -\ Amanzi.mp3
[Misuse]\ -\ Desecid.mp3
[Misuse]\ -\ Flat\ Line\ Evolution.mp3
[Misuse]\ -\ Genesis.mp3
[Misuse]\ -\ Loss\ in\ Action.mp3
[Misuse]\ -\ Progyria.mp3
[Misuse]\ -\ Way\ to\ the\ Seashore.mp3
[Misuse]\ -\ While\ it\ Lasts.mp3
finally i want to cp all this list...
so i 'nested' the above command in a copy as:

Quote:
nass@stargaze:~/musicstuff/Mp3 to Wav$ cp `ls -h [Mis* | grep mp3` ~/winmx
cp: invalid option -- \
Try `cp --help' for more information.
so this fails. now while i was going for a long shot, and not entirely sure that bash can generate this list automatically, and use it to run the cp command... i went for it...
but it doesn't work...

is there a way to make it work however?
nass
 
Old 04-15-2008, 10:35 AM   #2
slimm609
Member
 
Registered: May 2007
Location: Chas, SC
Distribution: slackware, gentoo, fedora, LFS, sidewinder G2, solaris, FreeBSD, RHEL, SUSE, Backtrack
Posts: 430

Rep: Reputation: 67
give this a try

Code:
cp `ls -h "[Mis*.mp3"` ~/winmx
it should handle the escape char for spaces
 
Old 04-15-2008, 10:48 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Maybe I misunderstand, but I think this can be done with one command (cp):

cp [Mis*.mp3 ~/winmx

[Mis*.mp3 => all that starts with [Mis followed by anything (*) and ending with .mp3

Hope this helps.
 
Old 04-15-2008, 10:55 AM   #4
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 666

Original Poster
Rep: Reputation: 39
damn, this is quite right... i was looking for a complex way when it was so simple...

no comment please:P

hehe

btw (off topic),

slimm609, on your new pc,
-New machine-- Intel Q9650 quad-core 2.66 1333mhz FSB -
NVIDIA ultra SLI 790i - 4 GB patriot Low-latency ram DD3 1333mhz - 2x EVGA nvidia 512M 8800 GT running in SLI - 160 GB 10K raptor system drive with 2x 750GB storage drives - 24" samsung 1650x1200 5ms LCD.

are you running some linux distro?winXP?Vista? basically i'm curious if linux kernel fully utilizes things like the Quad-core, the SLI configuration...

thank you
 
Old 04-15-2008, 10:58 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Square backets in file names doesn't work very well because they are meta-characters. [Aa] matches A or a.
You need to escape them:
cp \[Andrew*.mp3 ~/Winmx

Putting the whole thing in quotes won't work because that will also escape the wild card "*".
 
Old 04-15-2008, 11:09 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@jschiwal: Is that really needed in this case? The shell will expand the [Mis*.mp3, which will put the escape codes in there where needed. cp will get the correctly escaped files from bash.

I must admit that you should use \[Mis, even if it is just(??) for form.
 
Old 04-15-2008, 11:44 AM   #7
slimm609
Member
 
Registered: May 2007
Location: Chas, SC
Distribution: slackware, gentoo, fedora, LFS, sidewinder G2, solaris, FreeBSD, RHEL, SUSE, Backtrack
Posts: 430

Rep: Reputation: 67
Quote:
Originally Posted by nass View Post
damn, this is quite right... i was looking for a complex way when it was so simple...

no comment please:P

hehe

btw (off topic),

slimm609, on your new pc,
-New machine-- Intel Q9650 quad-core 2.66 1333mhz FSB -
NVIDIA ultra SLI 790i - 4 GB patriot Low-latency ram DD3 1333mhz - 2x EVGA nvidia 512M 8800 GT running in SLI - 160 GB 10K raptor system drive with 2x 750GB storage drives - 24" samsung 1650x1200 5ms LCD.

are you running some linux distro?winXP?Vista? basically i'm curious if linux kernel fully utilizes things like the Quad-core, the SLI configuration...

thank you
I have run linux on it. The SLI is done on board so it is seen as 1 video card and the quad-core just shows as 4 CPUs. I run vista on it for gaming with dx 10 but it is dual-booted.

i plan on overclocking it in a few weeks
 
Old 04-15-2008, 12:01 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you don't have the closing bracket, then it would work. However:[Misuse]- will match M- or i- or s- or u- or e-.
One thing that you can do is use auto completion. If you type in [Misuse]- and press the TAB key, the first part will be replaced with \[Misuse\]-.

Last edited by jschiwal; 04-15-2008 at 12:06 PM.
 
Old 06-17-2008, 03:40 AM   #9
Gorgonos
LQ Newbie
 
Registered: Jun 2008
Posts: 1

Rep: Reputation: 0
Funniest thing nass, the album you are trying to rip is of my group! Hope you enjoy it
 
Old 06-17-2008, 06:33 AM   #10
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 666

Original Poster
Rep: Reputation: 39
What else could one open-developer geek ask for, than to be listening to open source aware musicians:-) regardless, i most definitely enjoy it!
 
  


Reply



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
Using piped input in a BASH script gnashley Programming 1 08-05-2007 10:50 AM
Few commands in bash c4nk Linux - General 10 12-18-2006 02:19 AM
Nested Bash Script downbound010 Programming 1 12-10-2005 02:37 PM
bash commands davatar Slackware 2 04-06-2005 03:05 AM
bash commands Obie Linux - Security 4 08-15-2004 02:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 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
Open Source Consulting | Domain Registration