Main shell commands

General

Now we will present you small bunch of Linux commands and how to use them.
There is a lot of… so don’t hesitate to search yourself.

man : an interface to the reference manuals

It’s still the absolute reference : RTFM & RTMF

man [command]

Example :

[user@localhost ~]$ man more
MORE(1) User Commands MORE(1)
NAME
       more - file perusal filter for crt viewing
SYNOPSIS
       more [options] file...
DESCRIPTION
       more  is  a  filter for paging through text one screenful at a time.
...

But sometimes

  • it’s not so easy

  • even if you read the manuel : you are not sure how to use the command

  • some new commands/software come event without man (–help option)

  • GIYF/GETA : Google is your friend/Google est ton ami

tldr : Modern days man

tldr-pages : online collection of community-maintained help pages, that aims to be a simpler complement to traditional man pages. tldr : a client to use tldr-pages directly from shell (command line)

[user@localhost ~]$ tldr more
  more
  Open a file for interactive reading, allowing scrolling and search. More information:
  https://manned.org/more.

  - Open a file:
    more path/to/file

  - Open a file displaying from a specific line:
    more +line_number path/to/file

  - Display help:
    more --help

  - Go to the next page:
    <Space>
[skip]
  • online (or offline cache)

  • much more simple and with very useful example

  • sometimes is not available

  • just a simple version for the man page, it’s NOT an alternative. Sometimes, you should read the man pages patiently ;)

Another useful site/tool

Note

90% of this presentation were prepared using tldr and explainshell

history : Command-line history.

  • display the commands history list with line numbers:

    history
    
  • display the last 20 commands (in zsh it displays all commands starting from the 20th):

    history 20
    
  • check $HISTSIZE, $HISTFILESIZE

  • check $HISTTIMEFORMAT (e.g. export HISTTIMEFORMAT='%F %T ')

  • `!: replay the command

Files

cd path/to/directory : change the current (working) directory

  • cd : Go to the home directory of the current user

ls : list directory contents

  • ls -a : list all files, including hidden files

  • ls -l : long format list (permissions, ownership, size, and modification date)

  • ls -lh : same as “-l” but size displayed using human-readable units (KiB, MiB, GiB)

  • ou even ls -alh

pwd : print name of current/working directory (where am I ?)

[paul@localhost ~]$ cd git
[paul@localhost git]$ pwd
/home/paul/git

chmod : change file (everything is a file) rights (mode bits)

  • chmod 777 : give all rights for owner, his group and all others

  • chmod u+x file : give the [u]ser who owns a file the right to e[x]ecute it

  • chmod u+rw file : give the [u]ser rights to [r]ead and [w]rite to a file/directory

  • chmod g-x file : remove e[x]ecutable rights from the [g]roup

  • bits mode : rwx = 7 = 4® + 2(w) + 1(x)

find : search for files in a directory hierarchy

  • find . -name '*.ext' find files by extension in current directory (.)

  • find root_path -type d -iname '*lib*' find directories matching given name, in case-insensitive mode in root_path

  • find /home/user/tmp/ -type f -empty -delete find empty (0 byte) files and delete them in /home/user/tmp (Be Careful!!!)

cat : print file on the standard output (and concatenate files)

  • print the contents of a file to the standard output

    cat file
    
  • concatenate several files into the target file

    cat file1 file2 > target_file
    
  • number all output lines

    cat -n file
    

tail : output the last part (last N lines) of files

  • tail -n num file : show last ‘num’ lines in file

  • tail -c num file : show last ‘num’ bytes in file

  • tail -f file : keep reading file until Ctrl + C (never more cat ; cat ; cat ;)

du : disk usage (estimate file space usage)

  • du -h path/to/directory : list the sizes of a directory and any subdirectories, in human-readable form

df : gives an overview of the filesystem disk space usage

  • df -h : in human-readable form

which : locate a command/programm in the user’s path

  • which executable : search $PATH variable matching executable

  • which -a executable : if there are multiple executables which match, display all

Processus

  • top / htop / ps / kill

top/htop : display live current system state (tasks) / modern version of top

../_images/top.png

Fig. 21 Example of the top output.

../_images/htop.png

Fig. 22 Example of htop output for comparison with Fig. 21.

ps : report a snapshot of the current processes

  • ps aux : list all running processes (BSD syntax)

    • ~= ps aux

    • ~= ps -aux (Unix syntax)

  • ps -aef : almost the same (list all running processes), but change format

../_images/ps.png

Fig. 23 Example of the ps output.

kill : send a signal to a process (usually to kill/stop it)

  • terminate a program using the default SIGTERM (terminate) signal

    kill [process_id]
    
  • terminate a background job

    kill %*[{]job_id]*
    
  • signal to immediately terminate a program (no chance to capture the signal)

    kill -s 9 [process_id]
    
  • terminate a program using the SIGINT (interrupt) signal

    kill -2 [process_id]
    

Some useful codes :

  • 9 = SIGKILL

  • 15 = SIGTERM (default)

  • 2 = SIGINT (Ctrl-C)