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.mdfile - 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- Once your session is open, launch a terminal (
Terminal,Konsole…) - Launch a program (for example
xeyes) in interactive modexeyes &and in non-interactive modexeyes. What is the difference? Type the commandCTRL+Cto kill thexeyesprocess.
Navigating a Directory Hierarchy
Difficulty: Easy-
After launching the terminal, which directory are you in?
-
Type (successively) the following commands:
cd pwd cd /tmp pwd cd ~ pwd cd /tmp cd /usr/local pwd cd - pwd -
Look at the result. What directory does
~(tilde) correspond to? You can always go to your main/home directory (in system jargon) by simply doingcdorcd ~. Similarly,cd -allows going to the directory you were in previously. -
Create the directory
ima3in your home directory:mkdir ~/ima3 cd ~/ima3or 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
-
Type
cdto go back to your home -
What is the content of your home?
-
Create a directory
tmp. What is now the content of your home? -
Delete the directory
tmpwith thermcommand. -
Display the content of your home
-
Try to delete
tmpagain, what happens? -
Consider the following sequence of commands:
cd /tmp ls ls -a ls -l ls -l -a ls -la #(equivalent to the previous) -
What is the difference in display? What do the options
-land-ado? -
Delete the directory
ima3along with its content via:cd ~ rm -rf ima3Consult the
rmhelp (viaman rm) and explain why this command is dangerous.
Creating a Hierarchy
Difficulty: Easy-
Using the commands seen in the previous exercise, create the following directory hierarchy:
\-- Algo/ | |-- TP1/ | |-- algo/ | |-- TP1/ | |-- TP2/ | |-- tmp/ -
Verify that the created hierarchy matches the requirements using the
treecommand (if installed) or using the following commandls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'. The output should be:. |-algo |---TP1 |---TP2 |-Algo |---TP1 |-tmp -
Go to
~/algo/TP1and create an empty filetp1.cthere -
Delete
tp1.cand the entire hierarchy
Displaying Text
Difficulty: Easy- Go to the directory
/etc/dictionaries-common - Type
ls -lthencat words. What does thecatcommand do? Does it allow viewing large files? - Type the command
less wordsthen:
- press the
spacekey - press the
spacekey - press the
spacekey - press the
bkey - press the
bkey - press the up arrow key
- type
/fooand pressreturn - type
/zand pressreturn - press the up arrow key
- press the
hkey to access help - press
qto exit help - press
qto exitless
- What does
lessdo when you type/something? - Compare
lessandmore
Searching in Text
Difficulty: Easy- While still in the directory
/etc/dictionaries-common - What does the command
wc <filename>do? What do the options-land-cadd? - What does the command
grepdo, for example with commandsgrep house wordsorgrep maison words?
Editing / Modifying a File
Difficulty: Easy- In your home directory, create a text file using the
vimeditor with the commandvim test.txt - Press the
ikey to enter insertion mode invim, you can then type text (for example your name and surname) - Then press
ESCto exit insertion mode. - Then type
:wto save the file then:qto quitvim(it is possible to combine both commands in one by typing:wq) - Verify with
lessthat the file contains what you typed - What is the size of this file?
Searching for Files in a Directory
Difficulty: Rx-
Consult the help for the search command with
man find -
Then type:
find /etc find /etc -name "*.d" find /etc -name "*.cfg" -ls ls -R /etc -
What is the significance of the
*character here? -
What does the
-nameoption do? -
What is the difference between
ls -Randfind? -
Try the two commands
find /etc -exec wc '{}' +andfind /etc -name "*.cfg" -exec wc '{}' +. What does the-execoption do?
Standard Input
Difficulty: EasyRecall that standard input corresponds to what is typed at the keyboard
-
What does the
sortcommand do? Look in particular at what happens whensorthas no file as a parameter. -
Try:
sort a d cthen type
CTRL+D -
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
echocommand do? -
Do the following manipulations (note that the second call to
echooverwrites the content offile):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.sortedandfile.sorted2different? You can use thediffcommand to visualize the differences.
The pipe
Difficulty: Rx
The
pipeallows connecting the output of one command to the input of a new command
- Display the lines of
file.sortedthat contain the character 1 (look at thegrepcommand). - Count the number of lines in this file
- How to count the number of lines that contain 1 in
file.sorted? - Try
grep 1 file.sorted | wcand 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
- With a text editor1, create/open your
.bashrcfile and writealias ll='ls -l' - Exit the editor and type
ll. What happens? Open a new terminal (without closing the first) and typell. What happens? In the first terminal, typesource .bashrcand verify that thellcommand now works. - Reload
.bashrcin the terminal - You can enrich your
.bashrcfile 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: EasyYou will find below commands that will be very useful for this year (and used very frequently in practical exams for example):
-
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 -
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- Based on the page https://www.linuxtricks.fr/wiki/droits-sous-linux-utilisateurs-groupes-permissions, change the read/write/execute permissions on your files.
- Create a text file in your
Publicdirectory and make it accessible and readable (but not modifiable) by allima3students.
First C Program
Difficulty: Easy
-
Write the following
Cprogram:000_minimal.c#include <stdio.h> int main () { printf ("Hello World !\n"); return 0; } -
Compile your program with the command
clang monfichier.corgcc monfichier.c -
Execute your program with
./a.out
Footnotes
-
kateorgeditfor example… we will soon covervim↩