In the previous exercises we used
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 ...
Make sure you're in the appropriate folder (or adjust
ROOT_DIR accordingly), then
find level0 -name *.bat
-type dflag selects directories only
-namecan be specified together
ROOT_DIR, which is level
-maxdepth flag too, you will have guessed what it does by now.
-size +10c flag
+is short for 'larger than' (can you guess what
crefers to 'character', which as you will recall is of length 1 byte (for ASCII)
G('kilo', 'Mega', 'Giga')
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
PATTERN is the string to find.
exercises, then issue
-rflag for recursive
One 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':
COMMAND > output # this overwrites COMMAND >> output # this appends
man grepand 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:
ls /usr/bin | less
There is a subtle but important caveat when piping together utilities. Some utilities, including
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
grep isn't quite as simple as you probably expect:
construct argument list(s) and execute utility
The environment variable
PATH determines the locations (and search order) the shell looks for commands when you execute a line.
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
Where is the
python-executable located in the two cases? How is this reflected in the
PATH environment variable?