Delete files and folders not containing file ownd by a user - linux

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 {} \;

Related

Copy or move from nth level subfolder to another directory or folder

We have a software that adds a wav file and put it on a folder by its date which is buried under several subfolders.
For example:
home/user/music/group1/person1/todays date/wav file
home/user/music/group1/person1/yesterdays date/wav file
home/user/music/group1/person2/todays date/wav file
home/user/music/group1/person2/yesterdays date/wav file
Also, the person(n) folder is dynamic which means its created automatically if the software founds someone using that device and creates that folder. So for example, if a new user is using the software it will create home/user/music/group1/person3/.
How do I move or copy starting from the person(n) folder and move them to a new folder like home/user/new/person1.. home/user/new/person2..
Since the person(n) folder is dynamic I could not just do command like cp person1 newdirectory
What i did is find all wav files under group1 folder and cp to new folder but it copies the full path.
find /home/user/music/group1 -name "*.wav" -type f -exec cp --parents \{\} /home/user/new \;
If i remove --parents it will only copy the files to new folder. how do I copy starting from the person(n) folder to new folder?
thanks #RamanSailopal, i got it to work with find /home/user/music/group1/ -name "person*" -type d -exec cp -pR {}/ /home/user/new \;

delete all folders and files within a linux directory except one folder and all contents inside that folder

I have a directory structure as :-
/usr/testing/member/
---> public--->folder1--->file1
\----> file2
---> folder3:- contains files and folders
---> folder4:- contains files and folders
---> several files
I want to keep the public folder and all its contents (further folders and files within it) but want to delete everything else under the directory /usr/testing/member/. But that also means member folder is not deleted.
Is there any shell script or command that can be used to achieve this exactly as i stated.
Here's one way to do it:
(cd /usr/testing/member; find . -maxdepth 1 \( ! -name . -a ! -name public \) -exec echo rm -fr {} +)
That is: cd into /usr/testing/member, find all files and directories there, without going further below, and exclude the current directory (".") and any file or directory named "public", and execute a command for the found files.
This will print what would be deleted.
Verify it looks good, and then drop the echo.
I think below will do the work,
$ cd /usr/testing/member/
$ rm -rf $(ls | grep -v "public")
explanation:
we are passing everything inside /usr/testing/member/ but public to rm by making use of -v(exclude) option of grep

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

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

Deleting particular named directories from parent and all child folders

My folder hierarchy is like A/B/C/D.
So now each folder contains directory named CVS.
My purpose is I want to delete all CVS named directories from all folders.
I frequently tried to execute from parent folder(A Folder) rm -rf "CVS" , but it deletes CVS folder only from A folder and it's not fulfilling my needs.
I want to delete total 1200 folders named CVS.
If you can let me know appropriate command to delete CVS named directory recursively from parent to all sub folder it would be great help.
You can use the find command.
find pathname -type d -iname "CVS" -delete
In path name , You can give the path from which directory you have to delete.
Or else try this.
find pathname -type d -iname "CVS" -exec rm -rf \{\} \;

cp -r without hidden files

I have two directories and one is empty.
The first directory has many sub directories with hidden files. When I cp -r content from first directory to the second one, the hidden files gets copied too. Any solutions to escape them?
You can use rsync instead of cp:
rsync -av --exclude=".*" src dest
This excludes hidden files and directories. If you only want to exclude hidden directories, add a slash to the pattern:
rsync -av --exclude=".*/" src dest
You can do
cp -r SRC_DIR/* DEST_DIR
to exclude all .files and .dirs in the SRC_DIR level, but still it would copy any hidden files in the next level of sub-directories.
rsync has "-C" option
http://rsync.samba.org/ftp/rsync/rsync.html
Example:
rsync -vazC dir1 dir2
I came across the same need when I wanted to copy the files contained in a git repo, but excluding the .git folder, while using git bash.
If you don't have access to rsync, you can replicate the behavior of --exclude=".*" by using the find command along with xargs:
find ./src_dir -type f -not -path '*/.*' | xargs cp --parents -t ./dest_dir
To give more details:
find ./src_dir -type f -not -path '*/.*' will find all files in src_dir excluding the ones where the path contain a . at the beginning of a file or folder.
xargs cp --parents -t ./dest_dir will copy the files found to dest_dir, recreating the folder hierarchy thanks to the --parents argument.
Note: This will not copy empty folders. And will effectively exclude all hidden files and folders from being copied.
Link to relevant doc:
https://linux.die.net/man/1/cp
https://linux.die.net/man/1/find

Resources