Remove directories when specific file is in directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove directories when specific file is in directory?
# 1  
Old 08-26-2016
Remove directories when specific file is in directory?

Hello Smilie
I need little help i have following cmd:
this only able to delete files but not folder which contain that file
Code:
find /home/test/*  -name "*.testfile" -type f -exec rm -i {} \;

how to delete whole directory if file called somethingblahblah.testfile is there?
Thanks you Smilie
# 2  
Old 08-26-2016
Code:
find /home/test  -name "*.testfile" -type f -exec rm -irf $(dirname {}) \;

This User Gave Thanks to Ivo Breeden For This Post:
# 3  
Old 08-27-2016
Quote:
Originally Posted by Ivo Breeden
Code:
find /home/test  -name "*.testfile" -type f -exec rm -irf $(dirname {}) \;

Hi Ivo did you test this?

Please be very careful when suggesting the use ofrm -rf without fully knowing the consequences!

In this case before the find command is even being executed, the shell sees a command substitution $(dirname {}).

$(dirname {}) evaluates to a single dot (.), namely the current directory!

So then it executes the find command, which now becomes:
Code:
find /home/test  -name "*.testfile" -type f -exec rm -irf . \;

So for every match that find finds it executes this command:
Code:
rm -irf .

Luckily, on my system in a controlled setting, this leads to the following message..
Code:
rm: "." and ".." may not be removed

But who knows if on a different OS you may not be so lucky ...

** edit **
The Posix specification prescribes this behavior.
Code:
The rm utility is forbidden to remove the names dot and dot-dot in order to avoid the consequences of inadvertently doing something like:

rm -r .*

rm: Application Usage


--
Also: the rm option -i is diametrically opposed to the option -f, using both in the same command gives unspecified results. On my system -f overrules -i so suddenly, by adding the -f option to the OP's -i in the original post, rm does not ask before deleting a file, it just does it.

Last edited by Scrutinizer; 08-27-2016 at 03:16 AM.. Reason: Added posix spec reference
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 08-27-2016
thx both Smilie

got some help on other forum, so this is final cmd:

Code:
 find /home/test/* -type f -name '*.testfile' -printf '%h\n' | sort -u | xargs rm -rf

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Solaris

Giving read write permission to user for specific directories and sub directories.

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. This is for Solaris. Please help. (1 Reply)
Discussion started by: blinkingdan
1 Replies

3. Shell Programming and Scripting

Remove all files with specific file names in directory

If I have 5 files in a directory, what is the best way to remove specific files in it? For example, snps.ivg probes.ivg Desired output probes.ivg probes.txt all.txt Basically, removing those files with "snp" in the filename regardless of extension. Thank you :). (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

5. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

6. Shell Programming and Scripting

Find specific files only in current directory...not sub directories AIX

Hi, I have to find specific files only in the current directory...not in the sub directories. But when I use Find command ... it searches all the files in the current directory as well as in the subdirectories. I am using AIX-UNIX machine.Please help.. I am using the below command. And i am... (2 Replies)
Discussion started by: aakishore
2 Replies

7. Shell Programming and Scripting

Compressing all directories inside a directory and remove the uncompressed version

hi pls give me a script to compress all directories inside a directory and remove the original uncompressed version... >> please also tell the single commmand to uncompress all the directories back...whemn needed (2 Replies)
Discussion started by: dll_fpga
2 Replies

8. UNIX for Advanced & Expert Users

allow user to use sudo cp on a specific directory and only a specific file

Is there a way to allow a user to use sudo cp on a specific directory and only a specific file? (6 Replies)
Discussion started by: cokedude
6 Replies

9. Shell Programming and Scripting

Untar specific directory and strip leading directories

Ok so I know the title was probably confusing so here goes: I have a tarball (gzipped) that has a nested directory structure . For example: my.tar.gz (contents) --- ------ --------- ------------ --------------- ... (2 Replies)
Discussion started by: DC Slick
2 Replies
Login or Register to Ask a Question