In the previous exercises we used ls
and cp
with wildcards to select a bunch of files for manipulation. For deeply-nested directory structures, we need the utility find
, which has a great number of parameters (flags) that you can read all about at man find
. The basic syntax is:
find ROOT_DIR -flag1 something_flag1_related -flag2 ...
.bat
in any of the subdirectories to level0
Make sure you're in the appropriate folder (or adjust ROOT_DIR
accordingly), then
find level0 -name *.bat
3B
in them-type d
flag selects directories only-type
and -name
can be specified togetherlog
level3
-mindepth N
flagN
includes the ROOT_DIR
, which is level 1
Try the -maxdepth
flag too, you will have guessed what it does by now.
use the -size +10c
flag
+
is short for 'larger than' (can you guess what -
represents?)c
refers to 'character', which as you will recall is of length 1 byte (for ASCII)k
, M
, G
('kilo', 'Mega', 'Giga')in the notebooks/imgs
-directory, find all files larger than 200 kilobytes
Finding stuff in files can be achieved using grep
. The basic syntax is:
grep -flags PATTERN FILE
where PATTERN
is the string to find.
notebooks/fddhs.py
notebooks/fddhs.py
exercises
, then issue grep
with the -r
flag for recursiveFILE
-argument be?level0
-l
flagOne of the fundamental design principles of Unix is having lots of small utilities that do one thing (and do it well), whereas more complicated tasks are achieved by chaining them together.
The most common (and useful) redirection is that of the standard output. By default, all utilities send their output to the shell that prints them on the terminal. We can use the >
('greater than') sign to redirect output to a file instead.
NB: If the target file exists, it will be overwritten without warning!
If you wish to append to the end of an existing file use 'double greater than': >>
grep
-command, sending output to file grep.out
COMMAND > output # this overwrites
COMMAND >> output # this appends
find
for files larger than 10 bytes; send output to find.out
Files
-tabcat
grep
find it?grep
-command to find the 'missing' fileman grep
and scroll down to the -i
-flag (this time it's not for 'interactive'!)What if we could send the output of one utility to the input of another? Chains would emerge. The metaphor for doing this is 'putting a pipe in between utilities', or simply 'piping'. The special character for the operation is |
('vertical bar'; Danish keyboards on Windows have it behind a key-combo involving AltGr
, on Macs it's even better hidden: Alt-i
!)
/usr/bin
and pipe the output to less
ls /usr/bin | less
/usr/bin
containing the word zip
ls
and grep
zip
-containing files are there?ls
and grep
and wc -l
!xargs
There is a subtle but important caveat when piping together utilities. Some utilities, including ls
, rm
and cat
actually ignore standard input, which is where the pipe redirects to. Instead, they only accept input as command-line arguments. So sending the output of find
and grep
isn't quite as simple as you probably expect:
find
or grep
to ls -l
xargs
construct argument list(s) and execute utility
xargs
before the ls
!The environment variable PATH
determines the locations (and search order) the shell looks for commands when you execute a line.
foobar
We'll talk about variables in the context of computer programs (our next topic).
To demonstrate what conda
is doing when you 'activate' an environment, and why indeed they are called environments, temprorarily deactivate the current ('fddhs') environment:
source deactivate # linux/mac
deactivate # windows
which
and echo
commands from aboveWhere is the python
-executable located in the two cases? How is this reflected in the PATH
environment variable?
fddhs