Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a root folder with a text file in it called pairs.txt.

Within that root folder are other folders with text files called pairs.txt in them.

Is there a simple way to remove them using rm?

I know that there I could use find . -name 'pairs.txt' -exec rm {} \; but I would like to know of other ways, perhaps using * or some other wildcard?

I tried using rm -rf pairs.txt but it seems to only remove the pairs.txt in the current directory.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

With bash 4+:

shopt -s globstar dotglob
rm -- **/pairs.txt

The globstar option makes ** match any number of directory levels. The dotglob option makes it include directories whose name begins with . (dot files).

With ksh93, use set -o globstar instead of shopt -s globstar. To get the effect of dotglob, use FIGNORE=.

With zsh, use the second line directly. To include dot files, run setopt glob_dots first or make the second line rm -- **/pairs.txt(D).

Note that bash's ** follows symbolic links to directories. Ksh's and zsh's don't.

share|improve this answer
    
That would be the equivalent of find -L . -name '.?*' -prune -o -name pairs.txt -exec rm {} + though. That is, it doesn't follow hidden dirs, but follows symlinks. bash got it from ksh93 which got it from zsh. zsh and ksh93 don't have the issue with following symlinks. –  Stephane Chazelas Aug 1 '13 at 8:55
    
Is there another solution that doesn't require setting some sort of global variable like globstar? What I need to do is put this command in a makefile, and I was told by my boss that it'd be more portable if we didn't have to change the globstar option –  ishikun Aug 5 '13 at 0:38
    
@ishikun You already gave the more portable option in your question -- using find. –  Chris Down Aug 5 '13 at 6:00
add comment

ls -1R / | grep -e "/pairs.txt$" | xargs rm

share|improve this answer
2  
That will break if something in the path contains whitespace - not advisable. Using ls is usually not good advice for this type of thing. –  Mat Aug 1 '13 at 7:09
2  
That will not work at all since ls only outputs file names, not paths. whitespaces (TAB, NL, SPC) are not the only problematic characters, quoting characters are as well. –  Stephane Chazelas Aug 1 '13 at 8:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.