class: center, middle ## A very short list of some useful commands 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. There is also short/condensed version of this page
here
. --- class: center, middle ## General ### man / tldr / explainshell.com / history --- ### **man** : an interface to the reference manuals It's still the absolute reference : **RTFM** & **RTMF** > man [command] Example : ```bash [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) ```bash [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:
[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 https://explainshell.com/
💡 **NOTE** > 90% of this presentation were prepared using tldr and explainshell --- ### **history** : Command-line history. ```bash - 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** (`export HISTTIMEFORMAT='%F %T '`) * !*numero de command* : relaunch the command --- class: center, middle ## Files ### cd / ls / pwd / chmod / find / less / cat / tail / du / df / which --- ### cd *path/to/directory* : **c**hange the current (working) **d**irectory * `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) * or even `ls -alh` ### pwd : print name of current/working directory (where am I ?) ```bash [paul@localhost ~]$ cd git [paul@localhost git]$ pwd /home/paul/git ``` --- ### chmod : change file (*everything is a file*) rights (mode bits) * `chmod 777 file` : 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 * `chmod -R 740 directory` : set rights for directory and all files inside (owner=all, group=read, others=nothing) * bits mode : rwx = 7 = 4(r) + 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!!!**) ### grep : print lines matching a pattern * `grep "pattern" file` : search pattern in file * `grep -Ri "pattern"` : search (ignore-case) in all files in current directory (and sub-directories) * `grep -Ril "pattern"` : as above, but just print files with match * `grep -in "pattern" file` : search pattern in file and print line-numbers * `pcregrep` : a grep with Perl-compatible regular expressions * `ripgrep` : grep written in Rust : faster and powerful (but not everywhere) --- ### less : Open a file for interactive reading, allowing scrolling and search. 😉 *less is opposite of more* ```bash - Open a file: less source_file - Page down / up:
(down), b (up) - Go to end / start of file: G (end), g (start) - Forward search for a string (press `n`/`N` to go to next/previous match): /something ``` ### cat : print file on the standard output (and con
cat
enate files) 😉 *dog is not opposite of cat*, *opposite of cat is tac* ```bash - 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* (catch cat by tail; never more cat ; cat ; cat ;) ### head : output the first part (first N lines) of files * `head -n num file` : show first 'num' lines of a file * `head -c num file` : show first 'num' bytes of a file --- ### du : **d**isk **u**sage (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 ```bash [user@localhost ~]$ which df /usr/bin/df ``` --- class: center, middle ## Processus ### top / htop / ps / kill --- ### top/htop : display live current system state (tasks) / modern version of top
--- ### 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
--- ### kill : send a signal to a process (usually to kill/stop it) ```bash - 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) --- ## Some others very useful commands without explanation * cp, mv, rm, cut, sed * tar, gzip/gunzip * ip a, ping, wget/curl