Thread: OT:force overwrite with cp

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    OT:force overwrite with cp

    Can anyone please tell me how to force overwrite with the cp command [on linux] on the command-line.

    The following do not work:

    -f
    --force
    --remove-destination

    Any ideas?

    rotis23

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    According to the man page, -f should work. What's the entire command you're using?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Try this to recreate:

    Code:
    touch /tmp/file
    touch file
    cp -f file /tmp

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try doing this:
    Code:
    touch /tmp/file
    touche file
    cp -f file /tmp/
    Does it work if you include the trailing slash? I'm at work and don't have access to a *NIX box.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    No difference with trailing slashes.

    It's a permissions thing.

    If I create the file as a user in /tmp I can cp without a confirmation (even without a -f).

    But if you su to root, confirmation is required.

    I still confused.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    example:
    Code:
    [rotis23@cabbage rotis23]$ touch /tmp/file
    [rotis23@cabbage rotis23]$ cp file /tmp
    [rotis23@cabbage rotis23]$ su
    Password:
    [root@cabbage rotis23]# cp file /tmp
    cp: overwrite `/tmp/file'? y
    [root@cabbage rotis23]# exit
    exit
    [rotis23@cabbage rotis23]$ cp file /tmp
    [rotis23@cabbage rotis23]$

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    mlawrence@xanthus mlawrence $ touch /tmp/file
    mlawrence@xanthus mlawrence $ touch file
    mlawrence@xanthus mlawrence $ cp file /tmp
    mlawrence@xanthus mlawrence $ su -
    Password:
    xanthus root # touch file
    xanthus root # cp file /tmp
    xanthus root # exit
    logout
    mlawrence@xanthus mlawrence $ cp file /tmp
    mlawrence@xanthus mlawrence $
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Weird - I tried 'su -' but still no difference.

    I've tested using redhat 8 and 9. What unix are you using XSquared.

    BTW vegetables are the new mythological creatures!

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Show us an "ls -al" for that file before and after.
    Check your umask settings for each user. These will affects the default file permissions for new files.
    >>cp -f source target
    should do you. Can you show the output for it failing?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I'm runnin' Gentoo with a 2.6.5 kernel. Just for reference, I've included the ls -al before and after.

    Code:
    Before:
    -rw-r--r--  1 mlawrence users 0 Jul  5 20:42 /tmp/file
    
    After:
    -rw-r--r--  1 mlawrence users 0 Jul  5 20:42 /tmp/file
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Found the problem - alias!!

    cp is aliased to 'cp -i'!!
    Code:
    [root@cabbage cmasters]# alias
    alias cp='cp -i'

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by rotis23
    Found the problem - alias!!

    cp is aliased to 'cp -i'!!
    Code:
    [root@cabbage cmasters]# alias
    alias cp='cp -i'
    yeah Redhet does that, would have never remembered that.

  13. #13
    .
    Join Date
    Nov 2003
    Posts
    307
    Several Linux distros do that to prevent overwriting files in places like '/' with commands like 'cp * /' issued by newbies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using the cp command in a program in os x
    By peateargriffin in forum C Programming
    Replies: 22
    Last Post: 04-08-2006, 11:52 PM
  2. how to overwrite the text in *.txt
    By SuperNewbie in forum C# Programming
    Replies: 4
    Last Post: 11-10-2003, 09:01 AM
  3. Add not overwrite to a file
    By Bones in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 09:56 AM
  4. Overwrite data in a file
    By SMurf in forum Windows Programming
    Replies: 5
    Last Post: 12-16-2002, 01:40 PM
  5. File opens, but won't overwrite
    By Brown Drake in forum C++ Programming
    Replies: 0
    Last Post: 11-23-2001, 08:31 AM