Some basics on Bash
Survival kit
Open a Linux terminal (or a Git Bash for Windows) and type:
$ lsAs we can see, “ls” lists files and folders which are in the directory where we are when we execute the command.
Let’s type mkdir to create a new folder (in the same directory):
$ mkdir testfolderI want now to enter in this folder (change directory)
$ cd testfolderWe can check the complete directory with ‘pwd’ (print working directory):
$ pwdThis folder is for now empty. Let’s create a file:
$ touch testfileThe file is empty. To write in this file, we can use the ‘echo’ command (we will task later about the operator >):
$ echo "hello world" > testfileLet’s visualize the content of this file:
$ cat testfileLet’s try to add another content:
$ echo "new line" > testfileThe previous content has been suppressed. To add a content to a file, use ‘>>’:
$ echo "new line" >> testfileParameters for commands
Most of the commands accept parameters to specify their behaviors. We will illustrate this on two commands: ‘ls’ and ‘du’ (size of file).
Parameters of ls
- “-a” displays all the hidden files and folders
$ ls -a- ‘-l’ gives details about each file.
$ ls -lThe following information are provided: 1. File permission (later subject) 2. Number of physical links 3. Owner of the file 4. Group owner 5. Size of the file (octets) 6. Last update 7. Name - ‘h’ enables to have readeable size (K,Mo,…)
$ ls -lh- ‘t’ arranges the files according to the date of the last change:
$ ls -ltThe command cd
To return to the parent folder:
$ cd ..If you want to return to the home folder:
$ cd ~The command du (optional)
$ dugives the size of all the folders in the current directory.
$ du -hgives readeable size (K,Mo,…).
$ du -agives the size of files and folders since ‘du’ only displays size of folders by default.
$ du -sdisplays the size of the whole folder without detailing each sub-folder.
Introduction to Vim
Vim is one of the text editor available in the terminal. We will see how to use it basically. To open vim on “testFile”:
$ vim testFileDifferent modes
Vim has three different modes:
- interractive mode where you cannot write in your file, but you can move, do copy past, cancel out your actions.
- insertion mode where you can write your text, code. To activate this mode, use
i. To escape this mode, useEchap. - command mode where you can execute some commands: register, quit, activate some options like the numbering. To activate this mode, use
:from the interractive mode.
Basics
We sum up some commands to move efficiently in the file and register your modifications:
0and$enable to move at the beginning or the end of the file (interractive model).:w nameFileto save your file (insertion mode).:qto quit the file (insertion mode).:wqto save and quit the file (insertion mode).xto suppress a letter.(number) xto suppress a certain number of letters.(number)ddto suppress/cut a certain number of lines.dwto suppress a word.d0ord$to suppress the beginning or the end of the line.yyto copy a line.(number)pto paste.uto cancel the modifications.(n)Gto move to the line n.:set numberto activate the numbering.