Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all I wanted to know the difference between these commands ,and when exactly they should be used? /usr/bin/cp <file1> <file2> cp <file1> <file2> I have noticed that the first ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    20

    [SOLVED] difference btw these cmds and where should they be used?

    Hi all

    I wanted to know the difference between these commands ,and when exactly they should be used?

    /usr/bin/cp <file1> <file2>
    cp <file1> <file2>

    I have noticed that the first method is exclusively used in boot scripts while the second is used predominantly once the terminal is up!
    is it because during the boot process the system cannot detect the path to these executables or something else?

    What is the main difference and where should these 2 types be used?

    Thanks

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    /usr/bin/cp is a self contained cp command that will work in any shell, whereas the cp command if not given the full path will default to the inbuilt cp of that shell. For example bash has its own cp and it will take priority over the external executable. If for instance you were to check the man page for for you would see that it refers to the C shell and uses syntax you may not be as familiar with. Bash again has its own inbuilt 'for' command.

    It's really a matter of preference most of the time for this type of basic command but there is also the issue of environmental variables. If the user executing the command did not have /usr/bin/ in their $PATH then they would not be able to execute cp if their default shell was sh for example.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    20
    oh ok,
    Thanks a lot ,now i got the entire picture...
    I was searching for such things on the web but efforts were in vain..

    Thanks a lot!!!1

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    the cp command if not given the full path will default to the inbuilt cp of that shell. For example bash has its own cp and it will take priority over the external executable.
    Um, not quite. True for bash and echo (for example), but not bash and cp. Here's how you can tell. Do this at the command line:
    Code:
    which echo
    That will tell you where the separate program "echo" is found on your system. Depending on your Linux distribution, it is usually found at /bin/echo or /usr/bin/echo. For this discussion, let's assume that it's at /usr/bin/echo. Now try these two commands:
    Code:
    echo --version
    /usr/bin/echo --version
    See how they respond differently? The first one is the bash builtin. The second one is the separate program.

    You can also do this:
    Code:
    builtin echo --version
    That forces bash to use the builtin. (Be assured that there are circumstances when you would want to spell this out.)

    Now let's assume that the separate program "cp" is also at /usr/bin/cp. (You'll want to do
    Code:
    which cp
    to tell for sure.) Try these three commands:
    Code:
    cp --version
    /usr/bin/cp --version
    builtin cp --version
    You'll find that the first two commands give exactly the same response, and the third complains that cp is not a shell builtin command.

    To answer your original question, do this at the command line:
    Code:
    echo $PATH
    This will give you not one path, but a list of them, separated from each other by a colon (":"). This tells bash where to look for programs which are to be run. But that's only if bash needs a separate program.

    Here's how that works. When you give a bash command, it does one of five things with it. It tries them in this order.
    1. If it's an alias, it will execute that alias. You can enter this command:
      Code:
      alias
      to find out which aliases you've set up (or which were set up for you automatically when you logged in). You can remove any of those, and add others. To find out how, do this at the command line:
      Code:
      help alias
    2. If it's a keyword, such as function, if, or for, it will handle that keyword.
    3. If it's a function, it will execute that function. You can enter this command
      Code:
      help function
      for more information.
    4. If it's (ta da!) a builtin, such as cd, echo, or type, it will execute that builtin.
    5. If it's an executable program that's found by searching the list of paths in your PATH environment variable, bash will execute that.

    And the reason you find program names fully spelled out with the paths at which they're located is that in boot scripts the PATH environment variable is not available.

    Questions?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Quote Originally Posted by wje_lf View Post
    Um, not quite. True for bash and echo (for example), but not bash and cp.
    Cheers wje_lf, I've been using a bash shell built for Windows so I guess it had included the extra commands. Good to know.

  6. #6
    Just Joined!
    Join Date
    May 2008
    Posts
    20
    Oh so since the PATH variable isnt set,we give the absolute pathname is it?
    Then how come commands like echo work without the actual path being specified

    eg : echo "this is a test" >/dev/msglog
    this logs an o/p subsequently,however its never stated as /usr/bin/echo <something>

    Here you mean to say that its accessed differently?
    Also this brings to an intriguing que, how about the looping statements?
    How are they accessed?

    Thanks?

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Oh so since the PATH variable isnt set,we give the absolute pathname is it?
    In boot scripts for commands which aren't builtin, yes.
    Then how come commands like echo work without the actual path being specified
    That will work in a boot script because it's a builtin. It will work at the command prompt because it's a builtin, and even if it weren't a builtin, at the command prompt you typically have the PATH variable set so that the separate "echo" program can be found.
    Also this brings to an intriguing que, how about the looping statements?
    How are they accessed?
    They are always accessible because they're an integral part of bash, just as the builtin commands are. (You can "hide" them by aliasing them to something else, but few people are that unconventional.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Just Joined!
    Join Date
    May 2008
    Posts
    20
    This clears all my queries...
    Thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...