Deleting particular named directories from parent and all child folders - linux

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

Related

How can I remove specific directories that all start with a common letter?

I have many EC2 instances in a folder that I need to delete. Using -delete doesn't work because the directories are not empty. I tried looking for a way to get -rmdir -f to work with no success. The instance folders are all started with "i-" which led me to add wildcard "i-*" like that to get it to delete all directories starting with those characters. How can I manage to do this? the directories will never be empty either.
Assuming your current dir is the folder in question, how about:
find . -type d -name 'i-*'
If that lists the directories you want to remove, then change it to:
find . -type d -name 'i-*' -exec rm -r {} \;
In the command line interface/shell/born again shell/etc...
rm -r i-*
will remove ANY and ALL contained file(s) or directory(s) with subfiles and sub directories (recursive = -r) where the name begins with "i-" .
To delete the directories matching the pattern graphene-80* directly under /tmp, use
rm -rf /tmp/graphene-80*/
Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted (or symbolic links to directories), and not files etc.
To find the matching directories elsewhere under /tmp and delete them wherever they may be, use
find /tmp -type d -name 'graphene-80*' -prune -exec rm -rf {} +
To additionally see the names of the directories as they are deleted, insert -print before -exec.
The two tests -type d and -name 'graphene-80*' tests for directories with the names that we're looking for. The -prune removes the found directory from the search path (we don't want to look inside these directories as they are being deleted), and the -exec, finally, does the actual removal by means of calling rm.

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

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).

find a file and copy it to another directory

I have a directory called main. This directory is located in the root I mean the path to this directory is like this: /HOME/main
Inside this directory there is a folder called f1 and inside it another folder called subf1, so the path is like this: /HOME/main/f1/subf1
I want to check if there is a file in subf1 with a name containing a special string and copy this file to the main directory. I use this:
find . -maxdepth 1 -name "*string*" -exec cp {} ../main \;
It's incomplete and I don't know how to check if condition here, but even when I execute this command in /HOME/main/f1/subf1 path, it doesn't work. what's wrong?
You may use it like this:
find /HOME/main/f1/subf1 -iname "*string*" -exec cp {} /HOME/main/ \;

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

Resources