LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-30-2017, 05:53 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,129

Rep: Reputation: 42
Lightbulb Quick: is there a single command to copy a file and give it a different owner?


Quick question: is there a single command to copy a file and give it a different owner? It is easy to do both with cp and chmod, but I am imagining something like this in my terminal:

Code:
$ # cpc is "copie cuidadosamente" or "cp carefully"
$ su
# cpc /usr/share/anything/some.file.1 /home/caxi/
# exit
$ # Instead of:
$ su
# ls -l /usr/share/anything/   # check owner: unnecessary
root:root 755 /usr/share/anything/some.file
# cp /usr/share/anything/some.file /home/caxi/
# chown caxi:caxi /home/caxi/some.file
# exit
$
Possible wishes:

- make the file have owner:group of destination dir

- make the file have 755 permisions (is this necessary?)

- get file owner:group and permissions from a config file in ~ (of root, in the above example!), which overrides /etc/cpc, if it exists

- optional arguments for 1) owner:group; 2) permission

- option to force using owner:group of destination dir

- anything else?

This idea was born because sometimes I copy files that I find as root to my normal user folder and, after that, I must chown - or, if I forget the chown, do a "sudo chown me:me file" in the future to read or edit that file.

Comments are also welcome.
 
Old 07-30-2017, 06:03 PM   #2
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 748

Rep: Reputation: 323Reputation: 323Reputation: 323Reputation: 323
Lightbulb Cp: my Custom CoPy script

Often forgotten install: http://www.linuxnix.com/linuxcopy-ch...-in-a-command/ ?
But no config file. Toss in a clever alias maybe. Clever name: capitalC: Cp
Yea, sounds like a job for a script. Anyone know of such, already written? Enjoy

Last edited by !!!; 07-30-2017 at 06:18 PM.
 
1 members found this post helpful.
Old 07-30-2017, 06:10 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917Reputation: 2917
If I copy, as user not root, a root-owned file to one of my own user-owned directories then it auto changes the owner and group to mine.

Code:
$ ls -l /usr/local/bin/fruho.bin

-rwxr-xr-x 1 root root 5698668 Feb  8 14:50 /usr/local/bin/fruho.bin

$ cp /usr/local/bin/fruho.bin ./

$ ls -l fruho.bin

-rwxr-xr-x 1 hydrurga hydrurga 5698668 Jul 31 00:02 fruho.bin
Have a quick search on the net for linux copy with ownership change. The thread https://stackoverflow.com/questions/...le-during-copy for example suggests using the cpio command.
 
2 members found this post helpful.
Old 07-30-2017, 06:16 PM   #4
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,129

Original Poster
Rep: Reputation: 42
Quote:
[...] then it is time consuming task and more over keeping these commands in ansible play-book is not a great option.
Reading that page (from !!!'s post), but what is "ansible play-book"? Dictionary + general web search = confusion to me.

It surely sounds like "I need a script for that" problem! hahahaha

Last edited by dedec0; 07-30-2017 at 06:19 PM.
 
Old 07-30-2017, 06:23 PM   #5
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 748

Rep: Reputation: 323Reputation: 323Reputation: 323Reputation: 323
Oh dear! More duckduckgo.gling of that quoted string...
Don't get sidetracked by the 99% of "Linux in Devops" etc that we don't ...!!!

Edit: oh, ooops, I see...sorry... Just ignore that article's mention of ansible:
install(1) is an old Unix command, even in busybox:
Code:
install [-cdDsp] [-o USER] [-g GRP] [-m MODE] [source] dest|directory

Copy files and set attributes

Options:

        -c      Just copy (default)
        -d      Create directories
        -D      Create leading target directories
        -s      Strip symbol table
        -p      Preserve date
        -o USER Set ownership
        -g GRP  Set group ownership
        -m MODE Set permissions
Even "$6figure ansible gurus" had the same basic desire as you have in #1

Last edited by !!!; 07-30-2017 at 07:38 PM.
 
1 members found this post helpful.
Old 07-30-2017, 06:38 PM   #6
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 748

Rep: Reputation: 323Reputation: 323Reputation: 323Reputation: 323
Cp src dst owner:group mode
The Utopian Cp, suid so anyone can 'stick' anyone else, with anything, as anything

+1 to shortest ... ... Edit hmmmm... something like:
alias Cp 'su - me -c cp'
in/as/for root, so root switches back to user me, to get behavior in post#3, maybe?
(I haven't tested this, so you can still earn my +1 by posting The Solution)

Last edited by !!!; 07-30-2017 at 07:33 PM.
 
Old 08-02-2017, 02:24 AM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
I'd say write a script

cp $1

chown $2 $1

Then run
Code:
$ script file user:user
 
Old 08-02-2017, 07:49 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,425

Rep: Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054
Quote:
Originally Posted by AwesomeMachine View Post
I'd say write a script

cp $1

chown $2 $1

Then run
Code:
$ script file user:user
Agreed.

Or...don't "push" from root; "pull" with destination user (see #3 from hydrurga)
 
Old 08-03-2017, 04:36 AM   #9
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 553

Rep: Reputation: 277Reputation: 277Reputation: 277
Hi,

Pretty inspiring thread. Take a look at this for a start:
Code:
#!/bin/sh

# Script to copy and change ownership of file depending on
# owners of destination.
# TO BE USED WITH CARE.

set -e

if [ -e "$1" -a ! -d "$1" ]; then
# Verify that source file exists and is not a directory.

  if [ -d "$2" -a -w "$2" ]; then
  # Verify that destination exists and that write permission is granted.
    UserID=$(stat -c '%U' "$2")
    GroupID=$(stat -c '%G' "$2")
    DESTFILE="${2}${1#"${1%/*}"/}"

    # Copying and changing ownership accordingly
    cp --verbose "$1" "$2"
    chown --verbose $UserID:$GroupID "$DESTFILE"
  else
    echo "$0: $2 does not exist or write permission is not granted."
    exit 2
  fi

else

  echo "$0: $1 does not exist."
  exit 1

fi

exit 0
It copies a file to a certain destination and changes the ownership to that of the directory where the file ended up. You run the script as:
Code:
./SCRIPTNAME FILEPATH DESTINATION
Please tell if there are mistakes and you are free to modify the code to change permissions and other things. Check out the man page for the stat command. I say "use with care" as the chown and chmod commands are usually used as root, and if you're copying files as root to other users' directories, just make sure sensible files do not end up in the wrong hands.

Last edited by aragorn2101; 08-06-2017 at 06:53 AM.
 
  


Reply


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
How do you copy a single file or directory into a multiple locations? Alexkeinjas4 Linux - Newbie 1 11-09-2014 08:14 AM
copy multiple directories into another one directory with single cp command zeusys Linux - Newbie 2 08-17-2011 10:23 PM
[SOLVED] How to copy a file to all subfolders in a directory using a single command? SirTristan Linux - Newbie 7 07-18-2010 11:19 PM
Copy single file to multiple locations - What am I doing wrong decartes Linux - Newbie 7 04-26-2008 04:09 PM
copy all of one filetype found by locate command to a single directory socratesone Linux - Software 3 06-22-2005 01:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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