First steps with Bash and Linux#

Bash is the most standard shell on Unix systems (Linux, MacOS, …). A shell is a basic programming language to interact with the computer and launch commands.

There are standard commands (corresponding to programs, i.e. to files) to do many basic tasks. This short document lists several important commands.

In the following, we are going to discover few commands and syntaxes. Let’s start by a command to get the current working directory:

pwd
/home/pierre/output/teach/py-training-2023/book/part0

We see that we ask pwd and we get an answer (as a string of characters).

We see that in Linux, the separator for the path is the slash / and the root of the disk is just /. Usually, when at the beginning of an interactive session, we start in

It’s very important to understand that in a terminal, you are at a particular level in the directory tree. You have to know in which directory you are. If you don’t know, use pwd!

Command cd to change the working directory#

Usually, we need to tell cd where we want to go to. One can use a full path (something like /home/pierre/output/teach/py-training-2023/book), a relative path (like py-training-2023/book) or few special cases (~ for the home directory, - for the directory previously visited, .. for the directory containing the directory where we are).

cd ~
pwd
/home/pierre
cd -
/home/pierre/output/teach/py-training-2023/book/part0
cd ..
pwd
/home/pierre/output/teach/py-training-2023/book
cd -
/home/pierre/output/teach/py-training-2023/book/part0
cd ../..
pwd
/home/pierre/output/teach/py-training-2023
cd book
pwd
/home/pierre/output/teach/py-training-2023/book
cd ..

Command ls to list the file in a directory#

After moving into a directory, it is very common that one wants to know what is in this directory. Just use

ls
book  LICENSE.txt  main_environment.yml  Makefile  old  requirements.txt
ls book/part0
fig  Gitlab_with_Mercurial.ipynb  hgrc4ipynb  install.md  intro_bash.ipynb
# list only a selection of files
ls book/part0/*.md
book/part0/install.md

With ls, we are going to learn the concept of options for commands. It’s possible to change the behavior of a command by adding options in the command line, written as -l. Let’s see what it gives:

ls
book  LICENSE.txt  main_environment.yml  Makefile  old  requirements.txt
ls -l
total 24
drwxrwxr-x 5 pierre pierre 4096 Sep 10 22:23 book
-rw-rw-r-- 1 pierre pierre 1499 Sep  6  2021 LICENSE.txt
-rw-rw-r-- 1 pierre pierre  376 Sep 10 22:20 main_environment.yml
-rw-rw-r-- 1 pierre pierre  135 Sep 10 22:20 Makefile
drwxrwxr-x 5 pierre pierre 4096 Sep 10 22:17 old
-rw-rw-r-- 1 pierre pierre  196 Sep 10 22:20 requirements.txt
ls -a
.   book        .hg          main_environment.yml  old               .vscode
..  .gitignore  LICENSE.txt  Makefile              requirements.txt
ls -la
total 44
drwxrwxr-x  6 pierre pierre 4096 Sep 10 22:20 .
drwxr-xr-x 23 pierre pierre 4096 Sep 10 22:19 ..
drwxrwxr-x  5 pierre pierre 4096 Sep 10 22:23 book
-rw-rw-r--  1 pierre pierre  264 Sep 10 22:19 .gitignore
drwxrwxr-x  6 pierre pierre 4096 Sep 10 22:25 .hg
-rw-rw-r--  1 pierre pierre 1499 Sep  6  2021 LICENSE.txt
-rw-rw-r--  1 pierre pierre  376 Sep 10 22:20 main_environment.yml
-rw-rw-r--  1 pierre pierre  135 Sep 10 22:20 Makefile
drwxrwxr-x  5 pierre pierre 4096 Sep 10 22:17 old
-rw-rw-r--  1 pierre pierre  196 Sep 10 22:20 requirements.txt
drwxrwxr-x  2 pierre pierre 4096 Sep  6  2021 .vscode

We can get the help for a command and the list of supported options with man. Here, we only plot the 20 first lines (with the command head):

man ls | head -20
LS(1)                            User Commands                           LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort  is  speci‐
       fied.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all

Commands to create (mkdir, touch) and remove (rm) directories and files#

mkdir tmp_dir
ls
book         main_environment.yml  old               tmp_dir
LICENSE.txt  Makefile              requirements.txt
touch tmp_dir/toto.txt
ls tmp_dir
toto.txt
rm -rf tmp_dir

Command which to tell which file corresponds to a command#

which ls
/usr/bin/ls

Command echo to print to the screen#

echo "toto"
toto

Environment variables#

There are several already defined environment variables. The syntax to get the value of an environment variable is $ followed by the variable name, for example (echo is a command that prints something):

echo $HOME
/home/pierre
echo $PATH
/data0/opt/mambaforge/condabin/app:/home/pierre/mambaforge/condabin/app:/data0/opt/miniconda3/condabin/app:/home/pierre/.pyenv/bin:/home/pierre/.pyenv/versions/3.9.6/bin:/home/pierre/.pyenv/libexec:/home/pierre/.pyenv/plugins/python-build/bin:/data0/opt/mambaforge/condabin/app:/home/pierre/mambaforge/condabin/app:/data0/opt/miniconda3/condabin/app:/data0/opt/mambaforge/condabin:/home/pierre/.pyenv/bin:/home/pierre/.pyenv/shims:/home/pierre/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/pierre/opt/env_xonsh/bin/:/data0/opt/mambaforge/condabin/app:/home/pierre/.cargo/bin:/home/pierre/.pyenv/bin:/home/pierre/Dev/Nek5000/bin

We can also define our own variables to change the working environment:

export MY_ENV_VAR="Bonjour"
echo $MY_ENV_VAR
Bonjour

Commands to add something in a file and to display the content of a file#

We are going to use echo plus some “redirection” syntaxes:

mkdir -p /tmp/tmp_intro_bash
cd /tmp/tmp_intro_bash
echo "toto" > tmp.txt
ls
tmp.txt
cat tmp.txt
toto
echo "titi" >> tmp.txt
cat tmp.txt
toto
titi

Command mv to rename or move a file and cp to copy a file#

mv tmp.txt tmp2.txt
cp tmp2.txt tmp3.txt
ls
tmp2.txt  tmp3.txt
mkdir other_dir
mv tmp2.txt other_dir
ls other_dir
tmp2.txt
rm -rf /tmp/tmp_intro_bash