How can i copy only files which are owned by some user in Linux/unix - linux

How can i only copy files which are owned by some user. is there a way to specify user name in copy command?
i have few files in my home direct which are owned by some other user, i want to copy only those files to some other directory.How to filter it.

You can find the files owned by user with find command and then use cp to copy your files.
Example:
find all .txt files from user:
find /path/to/directory -user <username> -name "*.txt"
You can pipe it with the cp command to copy.
Or a one liner with find:
find /var/www -user vivek -name "*.pl" -exec cp -f source dest

Related

Linux Move txt files not start with String to Another folder

The situation is
I have a directory A, I have a bunch of files and folders in the foldler.
Such as folder B , foler C , tmp1.txt , Hello.txt , tmp3.txt , okay.txt.
And in folder B,there are also a bunch of files in it.
So I want to move all txt files recrusively to another folder such as /home.
Here is my code.
find . -name "*.txt"| grep -v [\s\S]*tmp[\s\S]* -exec mv {} /home \;
I can only select these files,however it won't execute move operation.
because linux find has path in result.So it annoy me a lot.
To move only regular files, add -type f and exclude the files you don't want with \!:
find . -type f -name '*.txt' \! -name '*tmp*' -exec mv -i -t /home {} +
The -i asks for permission to overwrite a file if it already exists and the + instead of the \; is used to move as many files as possible with one invocation of mv (and thus we need to specify the target directory with the -t option as {} contains more than one file).

List all user directories and look for specific file

I'm working on a script which will check for a specific file in ~/ of all users with home directory.
I tried ls /home and cd into users into their home directories but it gives too many arguments error.
username=$(ls /home)
cd /home/$username
cat file.json
I except the output of json file but it doesn't gives output of json file even user have a json file.
Edit:
Now I need to extract username of users with file file.json I have tried to do this with grep but it didn't worked.
files=$(find /home -name tilde.json -print)
echo "$files" >> jsons.txt
cat jsons.txt | grep /*/
This will find and list all files called file.json under the /home directory:
find /home -name file.json -print
You may want to redirect errors to /dev/null in the event you don't have access to all users' home dirs.
If you want to print out the contents of all these files, try:
find /home -name file.json -print -exec cat {} \;
To limit the search to only the directories under /home (i.e. not /home itself, and no sub directories in the user home), use:
find /home -mindepth 2 -maxdepth 2 -type f -name file.json -print -exec cat {} \;
I also added the -type flag there to limit the search to files and exclude any dirs that may happen to share the name.
This'll do:
cat /home/*/file.json
It'll print
cat: '/home/*/file.json': No such file or directory
on standard error if it can't find any.
What about:
cd /home
find . -name file.json -maxdepth 1 -exec cat {} \;
Suppose /home contains user1 and user2.
Then your cd command is invoked as
cd /home/user1 user2
That's not what you wanted, and isn't valid syntax for cd, which accepts only a single argument. You probably wanted a for loop instead.
If you can't predict such expansions, set -x enables tracing, which may provide insight into what commands are actually run, and would show your problem here. set +x to turn it off again.
Finally, note that not all users' home directories are necessarily in home. You might want to use getent or similar to find all user home directories.

Recursive find and copy to other directory

I need to find all files in a directory and it's subdirectories, but I need to keep directory structure. For example there is a file
/media/subdir1/subdir2/file.jpg
and I want to copy it to
/new-media/subdir1/subdir2/file.jpg
and the same to all files inside /media/ directory. And by the way, directories inside /new-media/ must be created if not exist.
if I use
find /media/ -name '*.jpg' -exec cp /new-media/ ????? {} \;
how can I get all subdirectories inside /media/?
The above will get you everything in /media, but to get exactly what you want you probably want to use something like:
Method 1: Copy only what you need recursively, per your requirements:
mkdir ../media2; find . -name "*.jpg" -exec cp -r --parents {} ../media2 \;
I ran this from inside the directory you want to search recursively. It does a couple things:
1 - create the new destination directory
mkdir ../media2
2 - then finds all files ending with ".jpg" recursively.
find . -name "*.jpg"
3 - uses -exec to pass the copy command to each file returned to find as a match, and subs that in as the first argument (which with the syntax of cp, is going to be your source file):
-exec cp -r --parents {} ../media2 \;
the "--parents" flag retains existing directory structure and recursively creates subsequent parent directories. Super useful right?
Method 2: there might be a better way to do this with xargs, but the above ended up being the most simple method imho. That said, if you want to think outside the box, you could simply copy the entire directory over recursively, then remove anything NOT ending with ".jpg" with something like:
cp -r media media2; find ./media '!'-name "*.jpg" -type f | xargs rm
I think this is what you want:
cp -r /media /new-media
-R, -r, --recursive
copy directories recursively
Hope this helps.

Delete files and folders not containing file ownd by a user

Hi I want help with a rm command that can remove all files and folders not containing any files created by a specific user
so say i copy a "public" folder where lots of users store there files and this "user1" wants a copy of all his files and folders (not the empty folders)
Try to copy only the files of user1.
find publicdir -user user1` -exec cp {} somedir \;
When you have used cp -p you can still remove the files using
find user1dir ! -user user1 -exec rm {} \;

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.

Resources