LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to copy files that belong only to a specific user (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-copy-files-that-belong-only-to-a-specific-user-737604/)

allancth 07-03-2009 08:14 PM

how to copy files that belong only to a specific user
 
Hi,

In a directory, I have thousands of files owned by different users. How to specifically copy all the files that belong to one specific user in this directory to another directory without changing the ownership of the newly copied files? Thanks in advance.

pixellany 07-03-2009 08:17 PM

Take a look at the "find" command (man find). There are options for just about everything.

The "exec" feature is useful for whatever action is to be taken on what is found.

jschiwal 07-03-2009 08:18 PM

Use the find command to locate these files. The -user option can filter the files to those owned by the user. The -exec option can be used to copy the files.

example
find sourcedir/ -type f -user <username> -exec cp '{}' destdir/ \;

allancth 07-03-2009 08:29 PM

Yes thanks. Thanks for the command.

But I have to run it twice. First on cp, then on chown.
Anyway, what is the {} symbols for? Is it the array of items found after the executing the find command?

internalkernel 07-03-2009 08:31 PM

So... there may be an easier way for this - but it's what I came up with...

We start with:
Code:

ls -al |grep USERNAME
Which will return a listing of all files in the directory from the specified user. Now we have to hack off that extraneous stuff so that we have just a file name.
Code:

ls -al |grep USERNAME | awk '{print $9}'
Lets turn this command into a variable that we can easily call:
Code:

USER=`ls -al |grep USERNAME | awk '{print $9}'`
Then it's a simple:

Code:

cp -a $USER /your/destination
Try it out first obviously... but it should do the trick.

Uncle_Theodore 07-03-2009 08:40 PM

Quote:

Originally Posted by allancth (Post 3596060)
Anyway, what is the {} symbols for? Is it the array of items found after the executing the find command?

No, find works one file at a time. {} is the placeholder for the file that it has found on each step.

allancth 07-03-2009 08:49 PM

The second solution with the awk '{print $9}' works perfectly too. But both method I have to chown it manually.

Thanks to all gurus.

jschiwal 07-03-2009 09:52 PM

If the destination files are in one place, you can chmod them en-mass. Or a second find command with the -perm option could locate just files with incorrect permissions.

You could also use the find command to supply the names of file to a for loop. Inside the loop you could perform your cp & chmod operations in two separate lines.

osor 07-03-2009 10:09 PM

… or, you could preserve ownership within cp if your implementation supports it:
Code:

find sourcedir/ -type f -user <username> -exec cp -p '{}' destdir/ ';'
Or, you could use find to do both separately:
Code:

find sourcedir/ -type f -user <username> -exec cp '{}' destdir/ ';' -exec chown <username> destdir/'{}' ';'

billymayday 07-03-2009 10:15 PM

find ./ -user username -exec chown whatever {} \; -exec cp -p {} /destination \;

Edit - way too slow.

jschiwal 07-03-2009 10:19 PM

Thanks billymayday. I had misread your post at first and rediscovered the same thing myself!

internalkernel 07-03-2009 11:09 PM

cp -a is the same as -dpR; which is to say... -d=same as --no-dereference --preserve=links, -p=same as --preserve=mode,ownership,timestamps, and -R=recursive. The -p or --preserve should maintain consistent attributes after a copy, at least it's always worked for me...


All times are GMT -5. The time now is 11:49 PM.