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. |
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. |
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/ \; |
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? |
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 Code:
ls -al |grep USERNAME | awk '{print $9}' Code:
USER=`ls -al |grep USERNAME | awk '{print $9}'` Code:
cp -a $USER /your/destination |
Quote:
|
The second solution with the awk '{print $9}' works perfectly too. But both method I have to chown it manually.
Thanks to all gurus. |
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. |
… or, you could preserve ownership within cp if your implementation supports it:
Code:
find sourcedir/ -type f -user <username> -exec cp -p '{}' destdir/ ';' Code:
find sourcedir/ -type f -user <username> -exec cp '{}' destdir/ ';' -exec chown <username> destdir/'{}' ';' |
find ./ -user username -exec chown whatever {} \; -exec cp -p {} /destination \;
Edit - way too slow. |
Thanks billymayday. I had misread your post at first and rediscovered the same thing myself!
|
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. |