I’ve been looking for a code snippet to place in a .sh that removes a directory named after a file.
What I mean by this is I would like to move to specify a directory and then search it for a list of files of type “.YYY” and then remove any subdirectories (with contents) with the same name.
Pseudo code of what i want to do:
find names all files of type .YYY within a directory find and remove all subdirectories with names that equal the file names found
How would one best go about this? I have been looking at the find function, which I will be using before this function to unrar archives:
`find . -name "*.rar" -exec unrar x '{}' \;`
Answer
What’s not very clear here is that you can’t have a file and a directory with the same name. Also, since you mention uncompressing rar files, one could think that you want to remove the directory named after the rar file. This is strange because, why would you want to delete it after you uncompressed it?
Based on my poor understanding of the problem (which may improve if you clarify or, better yet, provide an example), this may work:
for i in *rar; do
(cd uncompressed && rm -rf $i)
done
so if you have something like
./file1.rar
./file2.rar
./file3.rar
./uncompressed/file1.rar/...
./uncompressed/file2.rar/...
./uncompressed/file3.rar/...
the mini-script I posted should delete all the file*.rar/ subdirectories under uncompressed.
Attribution
Source : Link , Question Author : sehesod , Answer Author : roadmr