LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-03-2009, 09:14 PM   #1
allancth
Member
 
Registered: Mar 2009
Posts: 37

Rep: Reputation: 15
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.
 
Old 07-03-2009, 09:17 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 07-03-2009, 09:18 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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/ \;
 
Old 07-03-2009, 09:29 PM   #4
allancth
Member
 
Registered: Mar 2009
Posts: 37

Original Poster
Rep: Reputation: 15
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?

Last edited by allancth; 07-03-2009 at 09:47 PM. Reason: chown I mean, not chmod
 
Old 07-03-2009, 09:31 PM   #5
internalkernel
LQ Newbie
 
Registered: Jun 2009
Location: /dev/null
Distribution: Jaunty 9.04
Posts: 15

Rep: Reputation: 0
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.
 
Old 07-03-2009, 09:40 PM   #6
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 69
Quote:
Originally Posted by allancth View Post
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.
 
Old 07-03-2009, 09:49 PM   #7
allancth
Member
 
Registered: Mar 2009
Posts: 37

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

Thanks to all gurus.
 
Old 07-03-2009, 10:52 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
Old 07-03-2009, 11:09 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
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/'{}' ';'
 
Old 07-03-2009, 11:15 PM   #10
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

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

Edit - way too slow.
 
Old 07-03-2009, 11:19 PM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Thanks billymayday. I had misread your post at first and rediscovered the same thing myself!
 
Old 07-04-2009, 12:09 AM   #12
internalkernel
LQ Newbie
 
Registered: Jun 2009
Location: /dev/null
Distribution: Jaunty 9.04
Posts: 15

Rep: Reputation: 0
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...
 
  


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
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-08-2013 12:34 AM
copy files containing specific words in a specified line abenmao Linux - Newbie 5 08-28-2008 10:04 AM
How to copy a specific no of files from a group of files biswa_nayak Programming 2 05-11-2008 01:21 PM
BASH script to copy specific files ScottReed Linux - Software 5 07-20-2006 12:08 PM
copy specific files with directory stucture rincewind Linux - Software 1 06-22-2006 08:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:09 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration