LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 10-13-2012, 11:48 AM   #1
eNux eLin
LQ Newbie
 
Registered: Oct 2012
Posts: 9

Rep: Reputation: Disabled
cp command wildcard asterisk usage at the end of the destination path


Yeah I'm new to LINUX and loving it.

But it is an infinite matrix of possibilities with a humongous learning curve in the eyes of someone new to it.

On to my first question . . .

Quote:
5. Copy all files with 3 characters then an x to main from /usr/lib (when you pipe it to wc properly you get 70 lines or files)
I believe this to be one solution
Quote:
cp /usr/lib/???x* /home/MyUserName/Final/Main
but I have found a similar post with a solution that has a second asterisk and I do not understand the purpose of the second asterisk in the destination path for the cp command
Quote:
/usr/lib/???x* Final/Main/*
Googling cp examples has been fruitless for me in finding an asterisk example in the destination path. Also, I keep getting hits for the sofware product "Asterisk"


Off topic ? :
Can anyone explain how to google the characters /*
I've tried "/*" without success


Thank you very much
 
Old 10-13-2012, 01:17 PM   #2
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 424

Rep: Reputation: 18
Quote:
Originally Posted by eNux eLin View Post
Yeah I'm new to LINUX and loving it.

But it is an infinite matrix of possibilities with a humongous learning curve in the eyes of someone new to it.

On to my first question . . .



I believe this to be one solution

but I have found a similar post with a solution that has a second asterisk and I do not understand the purpose of the second asterisk in the destination path for the cp command

Googling cp examples has been fruitless for me in finding an asterisk example in the destination path. Also, I keep getting hits for the sofware product "Asterisk"


Off topic ? :
Can anyone explain how to google the characters /*
I've tried "/*" without success


Thank you very much
Let's see how it behaves in each case, i.e.
Code:
~ $ mkdir a b
~ $ > a/cccx
~ $ > a/ccdx
~ $ > a/ccex
~ $ > b/ccfx
~ $ > b/ccgx
~ $ ls a
cccx  ccdx  ccex
~ $ ls b
ccfx  ccgx
~ $ cp a/???x* b
~ $ ls b
cccx  ccdx  ccex  ccfx  ccgx
~ $ rm b/cccx b/ccdx b/ccex
~ $ ls b
ccfx  ccgx
~ $ cp a/???x* b/*
cp: target `b/ccgx' is not a directory
~ $
It seems to be substituting the names it finds in directory b for the second * for each file it tries to move from a. As there are only files in b, bash complains because it is not a directory. But apparently not
Code:
~ $ mkdir b/h b/i b/j
~ $ cp a/???x* b/*
cp: omitting directory `b/h'
cp: omitting directory `b/i'
~ $ ls b/j
cccx  ccdx  ccex  ccfx  ccgx
~ $ ls b
ccfx  ccgx  h/  i/  j/
~ $
Which seems to suggest that it used b/* both as a source of files to copy, and as a destination directory b/j. But as to why it does that, I don't have a clue.

I doubt that anyone would be likely to make an entry of '/*' in their web pages' metadata, so google would never find such, even if it would actually search for such a short entry.
 
Old 10-13-2012, 01:34 PM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,212

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
Quote:
Originally Posted by porphyry5 View Post
long post...


Which seems to suggest that it used b/* both as a source of files to copy, and as a destination directory b/j. But as to why it does that, I don't have a clue.
It's because the names matched by the * are expanded before running cp. cp's syntax is to copy all of the files/dirs passed on the command line to the last file/dir passed on the command line. There are a few ways this can work:
a) two names passed, second one is non-existant or an existing file
b) two names passed, second one is an existing directory
c) more than two names passed, last one is non-existant or an existing file
d) more than two names passed, last one is an existing directory

The results will be:
a) first file/dir is copied to the second file name
b) first file/dir is copied INTO the directory
c) error, you can't copy multiple files/dirs into a single file
d) the first N-1 files/dirs are copied INTO the last directory

Now remember that your * and ? are expanded FIRST, before running cp. With that in mind, your first example failed because you were doing example C, copying multiple files/dirs into a single file. The second example did what it did because it was example D, copying multiple files/dirs into a directory (only it won't copy directories over unless you set the recursive flag, which is why you got the two "cp: omitting directory `xxx'" messages).


You should generally only use a * in your destination if you KNOW that there is only going to be one match, otherwise it will copy the first N-1 matches into the last match, in addition to copying all of your source files/dirs into the last match.

Last edited by suicidaleggroll; 10-13-2012 at 01:38 PM.
 
1 members found this post helpful.
Old 10-13-2012, 01:56 PM   #4
eNux eLin
LQ Newbie
 
Registered: Oct 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
SuicidalEggRoll,

I vaguely follow your detailed explanation being that I am a beginner.

What is the value of the destination asterisk for question 5?
Quote:
5. Copy all files with 3 characters then an x to main from /usr/lib (when you pipe it to wc properly you get 70 lines or files)
Should the solution be (and why) . . .
Quote:
cp /usr/lib/???x* Final/Main/*
Or should the solution be (and why) . . .
Quote:
cp /usr/lib/???x* Final/Main/*
or something else (and why)?

Thank you very much
 
Old 10-13-2012, 02:03 PM   #5
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
Your two options are identical, unless I'm reading wrong,... and you're leaving something important out...

Hint,... there's no Final mount point...
 
Old 10-13-2012, 02:04 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,212

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
Quote:
Originally Posted by eNux eLin View Post
SuicidalEggRoll,

I vaguely follow your detailed explanation being that I am a beginner.

What is the value of the destination asterisk for question 5?


Should the solution be (and why) . . .


Or should the solution be (and why) . . .


or something else (and why)?

Thank you very much
I assume you mean, should the solution be:
Code:
cp /usr/lib/???x* /home/MyUserName/Final/Main
or
Code:
cp /usr/lib/???x* Final/Main/*
(the two you posted were identical)?

The first one is correct. The star at the end of "Final/Main/*" in the second solution will not work unless it matches something, and even then it will only work if it matches ONE directory. If it matches more than one file/directory, it will try to copy the first N-1 matches into the last match, which I assume is not what you want. The relative path "Final/Main/" will only work correctly if your CWD is "/home/MyUserName" as well.

Last edited by suicidaleggroll; 10-13-2012 at 02:05 PM.
 
Old 10-13-2012, 02:11 PM   #7
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
Suicidaleggroll caught my point...

If you want that (the last) command to work from anywhere in the system it should read;

Code:
cp /usr/lib/???x* ~/Final/Main/*
Oh,... and by the way,... it will yield its result in different places depending on just who you are... or if you are using sudo, which switch you are using the command with...

Last edited by JaseP; 10-13-2012 at 02:12 PM.
 
Old 10-13-2012, 02:19 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
The usual term for this is globbing, not "wildcards".

Remember first of all that the shell parses the line first, substituting variables and commands, and in this case, substituting globbing patterns. Then once the final command has been built, it executes the command.

Here's a quick, but important, introduction to http://mywiki.wooledge.org/Arguments.


If an unquoted globbing pattern matches one or more filenames in the current directory, the shell replaces the glob with those filenames before the command is executed. If the glob matches nothing, it generally gets passed as a literal text string (which will probably result in an error).


To illustrate, try adding an echo in front of the line. This will show you what the final expanded command looks like:

Code:
$ echo cp *
cp file1 file2 file3 ... etc
So we can now see that...

Code:
cp *
is executed as...

Code:
cp file1 file2 file3
Since cp treats its final argument as the target directory, it would attempt to copy file1 and file2 into file3, and probably fail. Unless "file3" was actually the name of a directory, that is, in which case it would succeed.

I hope this clears things up.


For this reason I always recommend in scripts that you use the -t option in cp and mv, to explicitly define the target directory.
 
Old 10-13-2012, 03:01 PM   #9
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by David the H. View Post
The usual term for this is globbing, not "wildcards".
Nice history reference,... But I guess that using that term depends on when you started learning terminology... I was introduced to the term "Wildcards" probably sometime around the time of Unix V6 (mid 1970s). The folks who introduced me to the term were among some of the first generation Unix system admins that were not part of Bell Labs out there. Since glob was a separate program at the time (located in /etc/glob), I don't think they were using the term "globbing," at that point...

I was only in the single digits, in terms of years old, at that point,... mind you... But referring to the asterisk, etc. as "Wildcards" kinda stuck with me for when I was re-introduced to the *nix world in the late 1990s...
 
Old 10-13-2012, 03:15 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Sorry. I really only meant to point out that it's the term most commonly used in the *nix world today. I know that other names are used elsewhere. The stripped-down version used in that other operating system in particular hardly qualifies as real globbing anyway.
 
Old 10-13-2012, 05:26 PM   #11
eNux eLin
LQ Newbie
 
Registered: Oct 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
The second asterisk . . . is it needed for question 5 . . . and what does it do in this case?

Yikes! I messed it up. The second and third quotes were not suppose to be identical so I've corrected below

SuicidalEggRoll,

I vaguely follow your detailed explanation being that I am a beginner.

What is the value of the destination asterisk for question 5?
Quote:
5. Copy all files with 3 characters then an x to main from /usr/lib (when you pipe it to wc properly you get 70 lines or files)
Should the solution be (and why) . . .
Quote:
cp /usr/lib/???x* Final/Main/*
Or should the solution be (and why) . . .
Quote:
cp /usr/lib/???x Final/Main/*
the second asterisk's purpose is hard for me to comprehend.
 
Old 10-13-2012, 05:48 PM   #12
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,212

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
Quote:
Originally Posted by eNux eLin View Post
Yikes! I messed it up. The second and third quotes were not suppose to be identical so I've corrected below

SuicidalEggRoll,

I vaguely follow your detailed explanation being that I am a beginner.

What is the value of the destination asterisk for question 5?
the second asterisk's purpose is hard for me to comprehend.
I believe this has already been answered in post #6.

Your corrected syntax still puzzles me, as you're referring to the presence of the second asterisk in text, yet the difference between the two commands is the presence of the first asterisk.

Last edited by suicidaleggroll; 10-13-2012 at 05:50 PM.
 
Old 10-13-2012, 07:33 PM   #13
eNux eLin
LQ Newbie
 
Registered: Oct 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
SuicidalEggRoll,

I messed it up a second time. I've corrected again the second and third quotes.

Is the second asterisk in quote 2 incorrect or does it serve a purpose? Quote three is how I thought it show be done.


What is the value of the destination asterisk for question 5?
Quote:
5. Copy all files with 3 characters then an x to main from /usr/lib (when you pipe it to wc properly you get 70 lines or files)
Should the solution be (and why) . . ."with second asterisk as I found it previously posted in this forum
Quote:
cp /usr/lib/???x* Final/Main/*
Or should the solution be (and why) . . ."no second asterisk"
Quote:
cp /usr/lib/???x* Final/Main/
the second asterisk's purpose is hard for me to comprehend in quote 2. Is the second asterisk incorrect or does it serve a purpose?


Too many posts on my part. Thank you very much
 
Old 10-13-2012, 07:36 PM   #14
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,212

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
As I said in post #6:
Quote:
The star at the end of "Final/Main/*" in the second solution will not work unless it matches something, and even then it will only work if it matches ONE directory. If it matches more than one file/directory, it will try to copy the first N-1 matches into the last match, which I assume is not what you want.
You should re-read post #8 carefully, and examine his example. That should explain what will/will not happen with and without that asterisk on the end.
 
Old 10-13-2012, 08:51 PM   #15
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
Definition of the target directory...

Think,... portability of the the command to script form... and... modifications to make the original command recursive.. What would that change?!?!
 
  


Reply

Tags
asterisk, path, wildcard


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
[SOLVED] cp: Create any directories necessary to open the destination path entz Linux - Software 10 01-10-2012 06:12 AM
Find command to search wildcard in path? markdjones82 Linux - General 12 12-11-2009 02:02 AM
get soft link destination path frado Linux - Software 5 05-19-2007 07:05 PM
how to unzip all files with a wildcard path? adamrosspayne Linux - Newbie 12 06-20-2006 02:16 PM
Kmail links open to wrong destination path tigerflag Slackware 2 12-30-2003 11:43 PM


All times are GMT -5. The time now is 03:32 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