Skip to main content
Basic Programming In C
Basic Programming In C
Easy 2h

Principles

:::info Basic Principle Under linux, everything is a file, organized in a single tree structure (whose root is named / and whose administrator is root). :::

Different Categories of Files

The different categories of files are:

  • Normal files:

    • text: mail, program sources, scripts, configuration files.
    • executables: programs in binary code
  • Directory files (referred to as directories): these are container files that contain references to other files — the true backbone of the tree structure, allowing files to be organized by category

  • Special files: located in /dev, these are the access points prepared by the system for devices. Mounting creates a correspondence between these special files and their directory.

  • Symbolic link files: these are files that contain only a reference (a pointer) to another file. This allows using the same file under multiple names without having to duplicate it on disk.

If you have a doubt, you can use the file command which gives you information about any element in the file system. Simply type file followed by the elements you want information about.

~ > file test.c Bureau tp5_gd.tgz a.out
test.c:      C source, ASCII text
Bureau:      directory
tp5_gdb.tgz: gzip compressed data, last modified: Sun Mar 10 20:11:18 2019, from Unix, original size 21504
a.out:       ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-.so1.2, for GNU/Linux 1.1.0, BuildID[sha1]=5c6a98b6e10bc1b991a57cb5374d4bf671d0ff8d, not stripped

Application

The ls command allows viewing part of the tree structure. For example on my personal account, the command ls -l (-l gives details on the content of the tree structure) gives the following result.

drwx------ 2 jdequidt imaEns  4096 Dec   8  2011 Desktop
drwxr-x--- 2 jdequidt imaEns  4096 Sep  28  2010 Documents
drwxr-x--- 2 jdequidt imaEns  4096 Dec  18  2018 Downloads
lrwxrwxrwx 1 jdequidt imaEns    29 Sep   9 14:09 mixed.csv -> ima2a/mixed_all_unordered.csv
drwxr-x--- 2 jdequidt imaEns  4096 Sep  28  2010 Music
drwxr-x--- 2 jdequidt imaEns  4096 Sep  28  2010 Pictures
-rwxr-xr-x 1 jdequidt imaEns 33024 Sep   9 14:05 prog1
drwxr-x--x 2 jdequidt imaEns  4096 Sep   9 14:08 Public
drwxr-x--x 2 jdequidt imaEns  4096 Jun   6 09:56 public_html
-rw-r--r-- 1 jdequidt imaEns   949 Sep   9 14:05 ReadMe.md
drwxr-xr-x 6 jdequidt imaEns  4096 Aug  27 09:51 tmp
drwxr-x--- 2 jdequidt imaEns  4096 Sep  28  2010 Videos

Using the following link https://www.garron.me/en/go2linux/ls-file-permissions.html, identify:

  • the directories
  • the text files
  • the executable file
  • the symbolic link and which real file it points to
  • the size in bytes of the ReadMe.md file
  • the directory that contains the most subdirectories

References

For a very concise overview of the main commands, I recommend consulting the following links: https://dev.to/awwsmm/101-bash-commands-and-tips-for-beginners-to-experts-30je (very well structured, in English) or https://bookmarks.ecyseo.net/?EAWvDw

Preamble

Difficulty: Easy
  1. Once your session is open, launch a terminal (Terminal, Konsole…)
  2. Launch a program (for example xeyes) in interactive mode xeyes & and in non-interactive mode xeyes. What is the difference? Type the command CTRL+C to kill the xeyes process.
Difficulty: Easy
  1. After launching the terminal, which directory are you in?

  2. Type (successively) the following commands:

    cd
    pwd
    cd /tmp
    pwd
    cd ~
    pwd
    cd /tmp
    cd /usr/local
    pwd
    cd -
    pwd
  3. Look at the result. What directory does ~ (tilde) correspond to? You can always go to your main/home directory (in system jargon) by simply doing cd or cd ~. Similarly, cd - allows going to the directory you were in previously.

  4. Create the directory ima3 in your home directory:

    mkdir ~/ima3
    cd ~/ima3

    or equivalently with:

    cd
    mkdir ima3
    cd ima3

1. Display the content of this directory with the `ls` command
1. Create an empty file (in the `ima3` directory) named `empty_file.txt` with the command `touch empty_file.txt`
1. Display the content of your `ima3` directory
1. `ls` can also be used as follows:

 ~~~bash
 cd /tmp
 ls ~/ima3
  1. Type cd to go back to your home

  2. What is the content of your home?

  3. Create a directory tmp. What is now the content of your home?

  4. Delete the directory tmp with the rm command.

  5. Display the content of your home

  6. Try to delete tmp again, what happens?

  7. Consider the following sequence of commands:

    cd /tmp
    ls
    ls -a
    ls -l
    ls -l -a
    ls -la #(equivalent to the previous)
  8. What is the difference in display? What do the options -l and -a do?

  9. Delete the directory ima3 along with its content via:

    cd ~
    rm -rf ima3

    Consult the rm help (via man rm) and explain why this command is dangerous.

Creating a Hierarchy

Difficulty: Easy
  1. Using the commands seen in the previous exercise, create the following directory hierarchy:

    \-- Algo/
    |   |-- TP1/
    |
    |-- algo/
    |   |-- TP1/
    |   |-- TP2/
    |
    |-- tmp/
  2. Verify that the created hierarchy matches the requirements using the tree command (if installed) or using the following command ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'. The output should be:

       .
       |-algo
       |---TP1
       |---TP2
       |-Algo
       |---TP1
       |-tmp
  3. Go to ~/algo/TP1 and create an empty file tp1.c there

  4. Delete tp1.c and the entire hierarchy

Displaying Text

Difficulty: Easy
  1. Go to the directory /etc/dictionaries-common
  2. Type ls -l then cat words. What does the cat command do? Does it allow viewing large files?
  3. Type the command less words then:
  • press the space key
  • press the space key
  • press the space key
  • press the b key
  • press the b key
  • press the up arrow key
  • type /foo and press return
  • type /z and press return
  • press the up arrow key
  • press the h key to access help
  • press q to exit help
  • press q to exit less
  1. What does less do when you type /something?
  2. Compare less and more

Searching in Text

Difficulty: Easy
  1. While still in the directory /etc/dictionaries-common
  2. What does the command wc <filename> do? What do the options -l and -c add?
  3. What does the command grep do, for example with commands grep house words or grep maison words?

Editing / Modifying a File

Difficulty: Easy
  1. In your home directory, create a text file using the vim editor with the command vim test.txt
  2. Press the i key to enter insertion mode in vim, you can then type text (for example your name and surname)
  3. Then press ESC to exit insertion mode.
  4. Then type :w to save the file then :q to quit vim (it is possible to combine both commands in one by typing :wq)
  5. Verify with less that the file contains what you typed
  6. What is the size of this file?

Searching for Files in a Directory

Difficulty: Rx
  1. Consult the help for the search command with man find

  2. Then type:

    find /etc
    find /etc -name "*.d"
    find /etc -name "*.cfg" -ls
    ls -R /etc
  3. What is the significance of the * character here?

  4. What does the -name option do?

  5. What is the difference between ls -R and find?

  6. Try the two commands find /etc -exec wc '{}' + and find /etc -name "*.cfg" -exec wc '{}' +. What does the -exec option do?

Standard Input

Difficulty: Easy

Recall that standard input corresponds to what is typed at the keyboard

  1. What does the sort command do? Look in particular at what happens when sort has no file as a parameter.

  2. Try:

    sort
    a
    d
    c

    then type CTRL+D

  3. Similarly, try to sort the numbers 2, 11, 1. Explain why 11 is considered less than 10? Use the appropriate option to obtain a numeric sort.

Standard Output

Difficulty: Rx
  • What does the echo command do?

  • Do the following manipulations (note that the second call to echo overwrites the content of file):

    echo test
    echo test > file
    cat file
    echo toto
    echo toto > file
    cat file
  • With >> content is appended without overwriting the file:

    echo test > file2
    cat file2
    echo toto >> file2
    cat file2
  • Using sort, generate a file.sorted that contains the numbers 2, 1, 11 sorted in ascending order. Is the result displayed in the terminal?

  • Type the following commands and explain what happens:

    sort < file.sorted
    sort < file.sorted > file.sorted2
    cat file.sorted2
  • Why are the files file.sorted and file.sorted2 different? You can use the diff command to visualize the differences.

The pipe

Difficulty: Rx

The pipe allows connecting the output of one command to the input of a new command

  1. Display the lines of file.sorted that contain the character 1 (look at the grep command).
  2. Count the number of lines in this file
  3. How to count the number of lines that contain 1 in file.sorted?
  4. Try grep 1 file.sorted | wc and verify that this command answers the previous question without generating temporary files

The .bashrc File

Difficulty: Rx

This file contains commands that are executed every time you open a terminal. This file generally contains shortcuts or options for the terminal

  1. With a text editor1, create/open your .bashrc file and write alias ll='ls -l'
  2. Exit the editor and type ll. What happens? Open a new terminal (without closing the first) and type ll. What happens? In the first terminal, type source .bashrc and verify that the ll command now works.
  3. Reload .bashrc in the terminal
  4. You can enrich your .bashrc file to customize your terminal, for example with:
# Example .bashrc configuration
# You can customize your terminal with these options

If you want more information about customization possibilities, you can consult the following link https://www.howtogeek.com/307701/how-to-customize-and-colorize-your-bash-prompt/

Very Useful Commands for These 3 Years

Difficulty: Easy

You will find below commands that will be very useful for this year (and used very frequently in practical exams for example):

  1. Viewing/copying public files from another account. If you know the login of another person at Polytech Lille, you can view the permitted files of that account via:

    ls ~login_of_account
  2. Connecting to school machines/servers from outside:

    ssh your_login@portier.polytech-lille.fr -p2222

at Polytech Lille, you can connect to another machine with the following command:

ssh your_login@machine_name

for example:

ssh your_login@gayant01

Back to File Permissions

Difficulty: Rx
  1. Based on the page https://www.linuxtricks.fr/wiki/droits-sous-linux-utilisateurs-groupes-permissions, change the read/write/execute permissions on your files.
  2. Create a text file in your Public directory and make it accessible and readable (but not modifiable) by all ima3 students.

First C Program

Difficulty: Easy
  1. Write the following C program:

    000_minimal.c
    #include <stdio.h>
    
    int main ()
    {
        printf ("Hello World !\n");
        return 0;
    }
    
  2. Compile your program with the command clang monfichier.c or gcc monfichier.c

  3. Execute your program with ./a.out

Footnotes

  1. kate or gedit for example… we will soon cover vim