grep
and find
#
grep
#
grep
finds and prints lines in files that match a regular expression. In the following example, we search for the string Python
:
[1]:
!grep Python ../index.rst
IPython
`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
advanced Python interpreter that has now grown into an extensive project
Today, IPython is not only an interactive interface to Python, but also offers a
number of useful syntactic additions for the language. In addition, IPython is
* `Miki Tebeka - IPython: The Productivity Booster
The option -w
limits the matches to the word boundaries so that IPython
is ignored:
[2]:
!grep -w Python ../index.rst
`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
advanced Python interpreter that has now grown into an extensive project
Today, IPython is not only an interactive interface to Python, but also offers a
-n
shows the line numbers that match:
[3]:
!grep -n -w Python ../index.rst
4:`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
5:advanced Python interpreter that has now grown into an extensive project
7:Today, IPython is not only an interactive interface to Python, but also offers a
-v
inverts our search
[4]:
!grep -n -v "^ " ../index.rst
1:IPython
2:=======
3:
4:`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
5:advanced Python interpreter that has now grown into an extensive project
6:designed to provide tools for the entire life cycle of research computing.
7:Today, IPython is not only an interactive interface to Python, but also offers a
8:number of useful syntactic additions for the language. In addition, IPython is
9:closely related to the `Jupyter project <https://jupyter.org/>`_.
10:
11:.. seealso::
14:
15:.. toctree::
19:
grep
has lots of other options. To find out what they are, you can type:
[5]:
!grep --help
usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
In the following example we use the -E
option and put the pattern in quotes to prevent the shell from trying to interpret it. The ^
in the pattern anchors the match to the start of the line and the .
matches a single character.
[6]:
!grep -n -E "^.Python" ../index.rst
1:IPython
find
#
find .
searches in this directory whereby the search is restricted to directories with -type d
.
[7]:
!find .. -type d
..
../mypackage
../unix-shell
../unix-shell/.ipynb_checkpoints
../.ipynb_checkpoints
With -type f
the search ist restricted to files.
[8]:
!find . -type f
./index.rst
./sorted-length.txt
./create-delete.ipynb
./length.txt
./dvc.list
./file-system.ipynb
./pipes-filters.ipynb
./shell-variables.ipynb
./.ipynb_checkpoints/create-delete-checkpoint.ipynb
./.ipynb_checkpoints/grep-find-checkpoint.ipynb
./.ipynb_checkpoints/pipes-filters-checkpoint.ipynb
./.ipynb_checkpoints/file-system-checkpoint.ipynb
./grep-find.ipynb
With -mtime
the search is limited to the last X
days, in our example to the last day:
[9]:
!find . -mtime -1
.
./sorted-length.txt
./create-delete.ipynb
./length.txt
./dvc.list
./file-system.ipynb
./pipes-filters.ipynb
./.ipynb_checkpoints
./.ipynb_checkpoints/create-delete-checkpoint.ipynb
./.ipynb_checkpoints/grep-find-checkpoint.ipynb
./.ipynb_checkpoints/pipes-filters-checkpoint.ipynb
./.ipynb_checkpoints/file-system-checkpoint.ipynb
./grep-find.ipynb
With -name
you can filter the search by name.
[10]:
!find .. -name "*.rst"
../index.rst
../unix-shell/index.rst
../extensions.rst
../start.rst
Now we count the characters in the files with the suffix .rst
:
[11]:
!wc -c $(find .. -name "*.rst")
833 ../index.rst
450 ../unix-shell/index.rst
2097 ../extensions.rst
1145 ../start.rst
4525 total
It is also possible to search for a regular expression in these files:
[12]:
!grep "ipython.org" $(find .. -name "*.rst")
../index.rst:`IPython <https://ipython.org/>`_, or *Interactive Python*, was initially an
Finally, we filter out all results whose path contains ipynb_checkpoints
:
[13]:
!find . -name "*.ipynb" | grep -v ipynb_checkpoints
./create-delete.ipynb
./file-system.ipynb
./pipes-filters.ipynb
./shell-variables.ipynb
./grep-find.ipynb