[SOLVED] remove directories that only contain .txt and .log files?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As a quick method of finding out if a directory contains files that are not just .txt or .log... "ls -a | grep -v .txt | grep -v .log". there might be an easier way to do both in a single grep but -a is important since there might be hidden files to consider, however they may also be common hidden files, if you don't care to bother to check for them or of them then just do a normal ls with the grepping.
a very quick bash script I think would be
Directory = <some directory>
Filecheck = 'ls ${Directory} | grep -v .*\.txt$ | grep -v .*\.log$'
# more advanced version of the greps.. I am still unable to merge these into a single grep =/.
if [ ${Filecheck} = "" ]
then
rm ${Directory}
fi
however I only tentatively know bash scripting so this likely could use a lot of improvement, I am more of a c, php, vb, javascript, java... kinda man.
Last edited by r3sistance; 08-08-2010 at 03:35 AM.
I tried to get this to work in the while loop:
[[ -n $(ls $dir/*{txt,log} &> /dev/null) ]] || echo rm -r $dir
It seemed to pick every directory though, and I don't understand the syntax enough to figure out what the problem was.
Thanks -- I played around with putting quotes, but couldn't find the right place until your suggestion.
Code:
while read dir
do
[[ -n $(ls "$dir/*{txt,log}" 2> /dev/null) ]] && echo rm -r "$dir"
done< <(find $HOME -mindepth 3 -maxdepth 4 -type d)
Doesn't print anything now -- I'll have to play around with it.
With the quote changes for directories containing spaces, the following works well, thanks!
(I had to subtract 1 from the depth arguments because of the ls in the while loop)
Code:
while read dir
do
Filecheck=$(ls "${dir}" | egrep -e ".txt$" -e ".log$")
if [ -z ${Filecheck} ]
then
echo rm -r "${dir}"
fi
done< <(find $HOME -mindepth 2 -maxdepth 3 -type d)
Doesn't print anything now -- I'll have to play around with it.
Quoting protects all special characters(1), so your *, {, and } also become literal parts of the string here. So be careful to quote only the parts you need to protect, usually the variable and other literal strings, and not those characters the shell needs to process as commands.
Code:
ls "$dir"/*{txt,log}
(1) There's a difference between double and single quotes here. Double-quotes do not protect $, `, and \, which means you can still use variables, embedded commands, and escape sequences inside them. Single quotes, however, protect *everything* (except for another single quote, of course).
Correct quoting is a very important part of scripting; one small mistake can cost you many hours in debugging a large script. More on it here: http://www.tldp.org/LDP/abs/html/quoting.html
Last edited by David the H.; 08-09-2010 at 12:20 AM.
Reason: Corrected my own "quoting"
will print out the rm commands for the depth 3 directories that contain no *.txt or *.log files anywhere underneath.
The find "-depth" option tells find to run depth-first, so it prints everything it finds in a directory before printing that directory. So I'm telling it to print any reason to keep a directory before printing that directory, then telling awk to print directories it doesn't see any reason to keep.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.