I run command to search for phrase in all files:
cat *.* | grep blablaIt works fine but I got problem with hidden files and directories. Command simply not deals with them. Ho to solve this problem?
93 Answers
By default, hidden files (i.e. those starting with a period) are excluded from the bash shell's glob expansion. However you can alter that using the dotglob setting e.g.
$ mkdir dir
$ touch dir/.hidden dir/visible
$ echo dir/*
dir/visible
$ shopt -s dotglob
$ echo dir/*
dir/.hidden dir/visibleYou can unset the option afterwards with shopt -u dotglob
Use find command with logical OR flag (-o ) and -exec . . .\+ flag
find . -maxdepth 1 \( -iname "*.*" -o -iname ".*" \) -exec grep "MySearchTerm" {} \+ Explanation:
findis a recursive command that searches files in specified directory. In this case , it is.the current working directory.-maxdepthflag tells us to stay only in current directory. If you want to go recursivelly or specify how many subdirectories to descent, change1to number of levels you wanna go.\( . . .\)part prevents shell of treating that as subshell, rather treating it as grouping of arguments tofind.-inameflags allow specifying for which filenames to search.-oflag will tell find to search for files*.*or files that start with leading dot , the hidden files.-exec . . .{}structure allows running specific command to operate on files found.\+will tellfindto take all the files as arguments for the command you want to run, in this casegrep.
Here's a small example, where you can see SEARCHFILE.txt and .SEARCHFILE.txt are both found:
DIR:/xieerqi
skolodya@ubuntu:$ find . -maxdepth 1 \( -iname "*.*" -o -iname ".*" \) -exec grep "HelloWorld" {} \+ 2>/dev/null
./SEARCHFILE.txt:HelloWorld ! I'm found
./localDir.txt:HelloWorld.so
./localDir.txt:HelloWorld.c
Binary file ./2015-05-05-raspbian-wheezy.img matches
./.SEARCHFILE.txt:HelloWorld ! I'm found "Hidden files" are simply files whose name starts with a dot. In GUIs applications these files are usually not shown, whence their name.
You can use shell globbing:
cat {*,.*} | grep blablaThe previous command include all files with no dot (*) and all files that start with a dot (.*).
By the way, this is an useless use of cat, and you should instead write your command as:
grep blabla {*,.*} 3