TP n°1 Unix
Introduction
To practice on the Unix shell
You should have access to the Ubuntu session on the computer in the room, with your student number and student password. If you have a Unix-like OS (e.g., MacOS, GNU/Linux) on your laptop, you can also use it if you prefer. If you have Windows installed on your machine, you still have multiple options if you want to work on your machine. I listed some of these options on https://s.ortion.fr/w/course/mssb-computer-refresher/introduction.html
To take some notes
You should keep track of the commands you used for this practical exercise. You should avoid using a text processing application, such as LibreOffice or Word, because it might introduce unwanted character in the commands. Prefer using a plain text file, or, even better: use a standard plain text format for your notes such as a Markdown file or org-mode for instance.
Exercise 1. A first step in the Unix world
- Check your unix username in two different ways
- Use the command
whoami - Check the content of the environment variable
$USER
- Use the command
- Create a folder with
mkdirin your home directory for this practical exercise. - Move to this newly created directory with the
cdcommand - Check your current working directory with
pwd - Your Unix machine probably have the
curlorwgetcommand line included. This subject document should be available online at https://s.ortion.fr/w/course/mssb-computer-refresher/unix/tp/tp1/tp1.org and https://s.ortion.fr/w/course/mssb-computer-refresher/unix/tp/tp1/tp1.pdf if you prefer to get a PDF file. Usingwgetorcurl, download this document in your local folder. - Using
ls, check that this is correctly downloaded and present in your current directory. - Open the file you downloaded from the terminal. If you chose to download the PDF rather than the Org-mode plain text source, you will need to use a PDF viewer.
- Using
grepand a regular expression count how many questions there are in thetp1.orgfile. Each question has a number. The line of a question hence starts with the question number followed by a dot.
Exercise 2. A grep on grape
Not a long time ago took place the grape harvest. France is one of the main grape and wine producers worldwide.
The FranceAgriMer institution is a national organisation responsible of data analysis on French agricultural activities, and to apply the European common agricultural policy in France. We will use a reference document listing the grape varieties that we can find in the market. Unfortunately the reference table is stored in a inconvenient PDF format. To make your life easier, I extracted the first 100 lines of this table into a plain text file in the comma-separated values (CSV) format and translated the comment and table header: ./data/agrimer_grape.csv
Remember that with
grepyou can search for a pattern in a text file withgrep PATTERN FILE. For instance, I could write the following command to find all occurrences of the termChasselas(a well-known grape variety) in the file:grep Chasselas ./data/agrimer_grape.csv
I can also count how many lines have this pattern term with the grep option
-c:grep -c Chasselas ./data/agrimer_grape.csv
2
I kept the PDF file header in the csv in a “comment” section, starting with the character ’#’. With grep you can also show all lines that don’t match the pattern (we sort of invert the grep query, with
-v).How would you filter out the comment lines from the csv file with a grep command ?
If you used only “#” as a grep pattern, think of what would occur if a record line contained a hashtag.
With the grep option
-Eyou can specify a more advanced regular expression pattern (-Emeans extended regular expression support). What regular expression would match only the lines that starts with the hashtag ’#’ character ?Use this to fix your previous command.
- Count how many grape varieties are maintained by an institution in France (FR).
- Check whether there is a grape variety that is not maintained in France.
- In fact, while manually editing the file, I messed up with some lines… Those lines does not have the expected number of columns (4). Can you find these lines?
Exercise 3. An executable bash script
- Open your favorite text editor (not Word, but a plain text editor), and create a file with the extension
.sh. - Add a shebang in the header of this script to use the
bashshell interpreter. - The file
/proc/cpuinfocontains information on the CPU of your machine. Add a command to extract themodel nameof your CPU from this file. - Add a command to count how many available CPU of this kind there are on your system.
- Make your script executable for your user and group.
- Execute your script
- Add the folder that contains your script to the PATH variable.
- Check that you can execute your script as a command, from any folder.
Exercise 4. Getting help
A lot of information is available on the web on Linux and other Unix-like systems.
Most of command line tools comes with a --help option to show a description of the options available, e.g.
grep --help
Most of standard Unix commands also comes with an entry in the Manual.
To access the manual from the terminal, is often as easy as typing man <the name of the command>, e.g.,
man grep
- Check the manual entry of
cp. Whatcpoption can we use to recursively copy the content of a directory? To quit the manual, typeq.
Exercise 5. Internet and SSH
- Can you find the ip address of your current machine from the terminal?
- Knowing your Unix username and the address of your machine, and assuming that a SSH server is running on this machine, what would be the SSH command you would use to open a shell from a remote client to this machine?