Linux Class Week Oct 29 - Nov 2 2018

The PATH variable

How does Linux know where a command is?

Let's create our own command, that will just count how many files exist in the current folder.

echo "ls -l | grep '^-' | wc -l" > nls
chmod +x nls 

Exercise 1:

Explain the above commands

How can we run the previous commands?

The first way is to just type

## 4

Exercise 2:

Create a folder named “example2”. Create the files f1.txt, f2.txt, f3.txt inside the example2. Run the nls command to find out how many files are in example2.

A complication that arises from the above practice, is that we need executables in all folders that we are running something. This is not very practical and it's also error prone (WHY?)

In Linux it's very simple to overcome this problem by exploiting the PATH variable. The PATH variable defines the locations of the directories where bash may find some command. Thus, when you are typing a command, e.g., ls the system will go to visit the directories in the order provided by the PATH variable. If the command will be found in one of these directories then it will be executed. Otherwise, an error will be shown: “Command not found”

The variable PATH can be modified by editing the .bashrc file, or directly in the terminal (This is a temporary solution). Be careful: you always need to append the new directory to the existing PATH… Otherwise, the PATH will be erased.

How to add locations in the PATH

##Exercise 3: Create a new command that will make a directory and inside this directory a file with the same name as the directory and the suffix “.txt”

##Exercise 4: Create a directory inside your home folder called bin2. Create a second directory called bin3. Add these directories in the PATH