First Steps with Linux
Principles
Different categories of files
The different categories of files are:
Normal files : text: mail, program sources, scripts, configuration… : executables: programs in binary code
Directory files (we talk about directory) : they are container files that contain references to other files true framework of the tree structure, they allow to organize files by categories.
Special files
: located in /dev, these are access
points prepared by the system to the devices. Mounting will perform a mapping of
these special files to their directories.
Link files : These are files that contain only a reference (pointer) to another file. This makes it possible to use the same file under several names without having to duplicate it on the disk.
Application
Difficulty: EasyThe command ls allows you to see a part of the tree structure. For example, on
my personal account, the command ls -l (-l gives details about the contents
of the tree) gives the following result.
drwx------ 2 jdequidt imaEns 4096 déc. 8 2011 Desktop
drwxr-x--- 2 jdequidt imaEns 4096 sept. 28 2010 Documents
drwxr-x--- 2 jdequidt imaEns 4096 déc. 18 2018 Downloads
lrwxrwxrwx 1 jdequidt imaEns 29 sept. 9 14:09 mixed.csv -> ima2a/mixed_all_unordered.csv
drwxr-x--- 2 jdequidt imaEns 4096 sept. 28 2010 Music
drwxr-x--- 2 jdequidt imaEns 4096 sept. 28 2010 Pictures
-rwxr-xr-x 1 jdequidt imaEns 33024 sept. 9 14:05 prog1
drwxr-x--x 2 jdequidt imaEns 4096 sept. 9 14:08 Public
drwxr-x--x 2 jdequidt imaEns 4096 juin 6 09:56 public_html
-rw-r--r-- 1 jdequidt imaEns 949 sept. 9 14:05 ReadMe.md
drwxr-xr-x 6 jdequidt imaEns 4096 août 27 09:51 tmp
drwxr-x--- 2 jdequidt imaEns 4096 sept. 28 2010 Videos
- What is the difference in display? What are the “the” and “a” options used for?
- Delete the
se3directory with its contents via:
cd ~
rm -rf se3
See the help of rm (via man rm) and tell why this command is dangerous.
Search in text
Difficulty: Easy- By always being in the
/etc/dictionaries-commondirectory - What does the command
wc <filename>do? What do the-and-coptions do? - What does the
grepcommand do with e.g. thegrep house wordsorgrep house wordscommands?
Edit / modify a file
Difficulty: Easy- In your home directory, create a text file using the
vimeditor via the commandvim test.txt. - Press the
ikey to switch toviminsertion mode, you can then type text (e.g. your first and last name). - Then press
ESCto exit insertion mode. - Then press
:wto save the file and then:qto exitvim(it is possible to combine the 2 commands into one by typing:wq) - Check with
lessthat the file contains what you wrote. - How big is this file?
Search for files in a directory
Difficulty: Rx- Consult the help of the search command with
man find. - Then type:
{% include snippets/000_find_examples.sh %}
- What is the meaning of the character
*here? - What is the purpose of the
-nameoption? - 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 (stdin)
Difficulty: EasyWe remind you that the standard input corresponds to what is typed on the keyboard.
- What does the
sortcommand do? Look specifically at what happens whensorthas no file as a parameter. - Try it:
sort
a
d
c
then type CTRL+D.
- Similarly, try to sort the numbers 2, 11, 1. Explain why 11 is considered to be smaller than 2. Use the appropriate option to get a numeric sort.
Standard Output
Difficulty: Easy- What does the
echocommand do? - Do the following (notice that the second call to
echooverwrites the contents offile):
echo test
echo test > file
cat file
echo toto
echo toto > file
cat file
- With
>>we add at the end without overwriting the file
echo test > file2
cat file2
echo toto >> file2
cat file2
- Using
sort, generate an .sorted file containing the numbers 2, 1, 11 sorted in ascending order. The result is displayed in the terminal? - Type the following commands and explain what happens:
sort < file.sorted
sort < file.sorted > file.sorted2
cat .sorted2 file
- Why are the
file.sortedandfile2.sortedfiles different? You can use thediffcommand to see the differences.
The pipe
Difficulty: Rx
The
pipeallows to connect the result of a command to the input of a new command.
- View the lines in
file.sortedthat contain the 1 character (look at thegrepcommand). - Count the number of lines in this file
- How do I count the number of lines that contain 1 in
file.sorted? - Try
grep 1 file.sorted | wcto check that this command answers the previous question and that it does not generate temporary files.
The .bashrc file
Difficulty: Rx
This file contains commands that are executed each time you open a terminal. This file usually contains shortcuts or options for the terminal.
- With a text editor1, create / open your
.bashrcfile and writealias ll='ls -l'in it. - Exit the editor and type
ll. What happens? Open a new terminal (without closing the first one) and typell. What happens? In the first terminal, typesource .bashrcand check that the commandllnow works. - Reload
.bashrcin the terminal - You can enrich your
.bashrcfile to customize your terminal with for example:
{% include snippets/bashrc.sh %}
If you wish to have more information on the possibilities of customization, 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 some commands that will be very useful for this year (and used very often in TP control for example):
- View / copy public files from another account. If you know the login of another person of Polytech Lille, you can consult the authorized files of this account via:
ls ~your_login
- Connect to the school’s machines/servers from the outside:
ssh votre_login@portier.polytech-lille.fr -p 2222
at Polytech Lille, you can connect to another machine with the following command
ssh your_login@machine_name
such as for example:
ssh your_login@gayant01
Useful shortcuts
The terminal uses a specific software library called
Readline to read the commands you
type. It provides several shortcuts to improve your productivity. These
shortcuts could be used either using emacs syntax or vi syntax. By default,
the emacs syntax is enabled. Below are the most useful shortcuts (the complete
list is detailed in the previous link).
Tab : auto-complete commands and files
Up Arrow_ or Down Arrow : allow to navigate through your command history (previous command or next command)
CTRL + r or CTRL + s : backward or forward search in your command history
CTRL + a / CTRL + f : moves the cursor to the beginning / end of the line
ALT + b / ALT + f : moves the cursor backward / forward one word
CTRL + l : clears the screen
CTRL + u : clears the line and copies it to the clipboard
CTRL + k : clears the line after the cursor and copies it to the clipboard
CTRL + w : remove one word backwards from cursor position
Back to file permissions
Difficulty: Rx- Based on the https://www.linuxtricks.fr/wiki/droits-sous-linux-utilisateurs-groupes-permissions page, change the read/write/execute permissions on your files.
- Create a text file in your
Publicdirectory and make it accessible and readable (but not editable) by all people from the groupima3.
Your first C program
Difficulty: Easy
- Write the following
Cprogram
{% include snippets/000_minimal.c %}
- Compile your program with the command
clang myfile.corgcc myfile.c. - Run your program with
./a.out.
Footnotes
-
nanoorgeditfor instance… we will usevimlater ↩