Linux commands

Many Linux commands are complexes and have multiple options or multiple ways to achieve the same purpose. This page describes some uses and some options of commands that are frequently used.

It's possible for each line of a command to become the arguments of another command using xargs:

$ echo "/" | xargs ls    # ls "/"

β›ͺ If there is a command that you don't understand, check out explain shell πŸš€.


Manual

Usage 🐚: man is a manual for many commands. It may have to be installed.

You can install additional man pages with packages such as manpages or manpages-dev.

Example πŸ”₯:

$ man man

➑️ man is a "less editor". See the less command.

Entries in the manual are split into sections. To access a command in a specific section, simply add the section number before the command.

$ man 1 man

linux1

Usage 🐚: Display a one-line manual page descriptions

Example πŸ”₯:

$ whatis man
man (1) - an interface to the system reference manuals

Usage 🐚: You can use apropos to search a command, or in which section a command is.

Example πŸ”₯:

$ apropos fopen
fopen (3)            - stream open functions
fopencookie (3)      - opening a custom stream
$ man 3 fopen

Usage 🐚: Alternative to man.

Example πŸ”₯:

$ sudo apt install info
$ info ls

Terminal-related

linuxfundamentalspart1 linux1

Usage 🐚: print text, usually for debugging or displaying instructions/results

Example πŸ”₯:

$ echo Hello, World
$ echo 'Hello, World'
$ echo "Hello, World"

-e πŸ§ͺ: to interpret special characters such as \n

$ echo -e "abc\nde"
abc
de

-n πŸ§ͺ: to remove the newline

$ echo -n "Hello, World"

You can also use printf "format" arguments:

$ printf "%s\n" "Hello, World"

linuxfundamentalspart1 linux1

Usage 🐚:

  • cat is commonly used display a file.
  • cat is intended to be used to merge multiple files in one, and display the output.

πŸ‘‰ tac is printing the file from the end to the start. And concatenating files after reversing their lines.

Example πŸ”₯:

$ cat file
the content here
# read for stdin
$ cat -

➑️ Concatenating files

$ cat f0 f1
f0
f1 Line 1
f1 Line 2
$ tac f0 f1
f0
f1 Line 2
f1 Line 1

Create a file f2 with input from f0, f1, and stdin.

$ cat f0 f1 - > f2

Usage 🐚: both are used to browse files/content from the CLI.

πŸ‘‰ more is rarely used as there is cat for short files, and less is more appropriate for large files.

Example πŸ”₯:

$ more file
$ less file

There are QUITE a lot of options. Simply press h to see all of them.

  • arrow up: to move up,
  • arrow down: to move down
  • q: to quit
  • /something: search "something"
    • Enter or n: go to the next match
    • N: go back to the previous match

Usage 🐚: open a file (txt, pdf...), a URL... with the default application associated with the file extension.

Example πŸ”₯:

$ xdg-open xxx.pdf
$ xdg-open URL

Usage 🐚: tee can be used to redirect one input to multiple sources. For instance, you can redirect the output to a file and stdout.

Example πŸ”₯:

Output to both files and the standard output.

$ tee output1 output2 < file

Use -a/--append to append content.

Usage 🐚: clear the terminal

Example πŸ”₯:

$ clear

πŸ‘‰ You may use the shortcut CTRL+L.

Usage 🐚: change the terminal behavior

Example πŸ”₯:

Enable an option (ex: nullglob)

$ shopt -ps nullglob

Disable an option (ex: nullglob)

$ shopt -pu nullglob

The nullglob option is replacing patterns with the null string if there are no matches.

Usage 🐚: create a command that is an alias of another commands with usually some arguments.

Example πŸ”₯:

Create a command la which is ls -la

$ alias la='ls -la'
$ la

See every defined alias

$ alias

⚠️ Aliases are temporary. You must load them every time you open a terminal, such as by adding them in a .bashrc in Bash.


Computer information

Usage 🐚: info about a user

Example πŸ”₯:

$ id          # current user
$ id username # some user

linuxfundamentalspart1

Usage 🐚: username of the current user

Example πŸ”₯:

$ whoami
username

Usage 🐚: info about the machine

Example πŸ”₯:

$ uname # show OS name
$ uname -s # same
$ uname -rv # kernel
$ uname -m # architecture (x64, x86...)
$ uname -a # all

Usage 🐚: info about the machine

Example πŸ”₯:

$ date
Mon 12 Oct 2020 08:32:11 PM EDT

Computer management

Usage 🐚: Execute a command <command> with elevated privileges.

Example πŸ”₯:

Execute a command <command> as root.

# ex: ls /
$ sudo ls /

Elevate the shell. In an elevated shell, there is no need to add sudo before each command.

$ sudo -s

Usage 🐚: stop or reboot an operating system.

Example πŸ”₯:

To shutdown an operating system, use:

$ shutdown -h now

To reboot an operating system, use:

$ shutdown -r now
$ reboot

File system

linuxfundamentalspart1

Usage 🐚: echo the path to the working directory

Example πŸ”₯:

$ pwd
/home/listro

linuxfundamentalspart1 linux1

Usage 🐚: list all files in a directory. Both are the same, but dir is rarely used. tree display files in a tree-like format.

Example for ls/dir πŸ”₯:

$ ls # current directory
$ ls . # same as "ls"
$ ls folder
$ ls *.txt # pattern

-l: print more information about each file

$ ls -l file # info on file

-a: print hidden files

$ ls -a folder
$ ls -la folder # info + hidden

Other options

  • -R: recursive
  • -p: add a trailing "/" to repositories
  • -A: hide ".", and ".."
  • -s: show the size
  • --format="format": use a custom format
  • --hide="pattern": hide files matching "pattern"
  • -S: sort by size
  • -t: sort by last modified date
  • -u: sort by last access date

Example for tree πŸ”₯:

List recursively given a folder.

$ tree .

linuxfundamentalspart1

Usage 🐚: move to another directory

Example πŸ”₯:

$ cd folder

Move to the HOME folder

$ cd
$ cd ~

Move to the previous folder

$ cd -

The pushd and popd commands are quite useful when we are often moving between repositories:

$ cd /path/to/folderA
$ pushd .    # save
$ cd /path/to/folderB
$ pushd .    # save
$ cd /path/to/folderC
$ popd       # go back to folderB 
$ popd       # go back to folderA 

linuxfundamentalspart2 linuxstrengthtraining

Usage 🐚: create one or more folders

Example πŸ”₯:

$ mkdir folder

Create every non-existing folder in a path

$ mkdir -p folder0/folder1/folder2

linuxfundamentalspart2 linux1

Usage 🐚: there is no specific command to create a file, but there are multiple ways to achieve it: touch, cp, truncate, >, :>...

Example πŸ”₯:

πŸ‘‰ touch: ensure a file exists, but it may not be empty πŸ”₯. The command create a file or update the last modified date if it exists.

$ touch afile

πŸ‘‰ A trick is to copy /dev/null with cp

$ cp /dev/null file

πŸ‘‰ A popular way is to use a redirection of the output. Note that all of these syntax may not be supported everywhere.

$ echo -n "" > file
$ echo -n > file
$ > file
$ :> file

πŸ‘‰ You can use truncate to create an empty file or truncate an existing file.

$ truncate -s 0 file

Usage 🐚: create a symbolic/hard link

Example πŸ”₯:

Create a symbolic link at output linking to target:

$ ln -s target output

linuxfundamentalspart2 linuxstrengthtraining linux2

Usage 🐚: takes a list of folders/files to move, and a destination. Move all files/folders to the destination.

Example πŸ”₯:

$ mv toto ./all_toto/
$ mv toto -t ./all_toto/ # same
$ mv toto1 toto_2 ./all_toto/

Rename

$ mv toto toto0

linuxfundamentalspart2 linuxstrengthtraining

Usage 🐚: takes a list of folders/files to copy, and a destination. Copy all files/folders to the destination.

Example πŸ”₯:

$ cp toto ./all_toto/
$ cp toto1 toto_2 ./all_toto/

To create a copy

$ cp toto toto0 # ⚠️ toto still exists

-r ⚠️: required to copy a folder and its content

$ cp -r all_toto/ all_toto_copy

linuxfundamentalspart2 linux2

Usage 🐚: use rm to remove files/folders. rmdir is usually not used.

Example πŸ”₯:

$ rm file
$ rm file0 file1

πŸ‘‰ rm by default is asking for confirmation.

$ rm -f file # do not ask (f=force)
$ rm -i file # ask

πŸ‘‰ Delete a folder

$ rm -d folder # if the folder is empty
$ rm -r folder # -R is doing the same

πŸ‘‰ Delete a folder and to not ask for confirmation

$ rm -rf folder # usual f + r

linuxfundamentalspart2

Usage 🐚: file is widely used for images or to find the type of file. stat is useful to extract the metadata of a file.

Example (file) πŸ”₯:

$ file folder
folder: directory
$ file image.jpg
image.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, comment: "CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90", baseline, precision 8, 1080x1350, components 3

Example (stat) πŸ”₯:

$ stat folder
$ stat file
$ stat toto -c "Size: %s -- Name: %n -- Perms: %a / %A -- type: %F"
Size: 4096 -- Name: toto -- Perms: 755 / drwxr-xr-x -- type: directory

Usage 🐚: you can examine the difference between two files using diff. You can use patch to fix some differences.

Example πŸ”₯:

Diff only.

$ diff f0 f1 -q
Files f0 and f1 differ
$ diff f0 f1
# one below the other diff
$ diff f0 f1 -y
# side-by-side diff

Generate a patch

$ diff f0 f1 -u
# ...
$ diff f0 f1 -u > f0.patch 

Then, you can use patch to apply the differences with f1 to f0.

$ patch < f0.patch
$ patch -d path/to/xxx [...]  # folder to apply patch on
$ patch -R [...]              # reverse patch
$ patch -p0 [...]             # n slashes skipped

Usage 🐚: compute a folder/file size

Example πŸ”₯:

  • s to summarize the result (one result per argument)
  • h to use a human-readable format (add units...)
$ du -sh folder
8.0K folder

Usage 🐚: get the filename from a path.

Example πŸ”₯:

$ basename /etc/passwd
passwd

Usage 🐚: get the parent folder of a file.

Example πŸ”₯:

$ dirname /etc/passwd
/etc
$ dirname /etc
/

Usage 🐚: get the absolute path to the given argument. Patterns are evaluated and symoblic links are resolved.

Example πŸ”₯:

$ realpath ~
/home/folder

Usage 🐚: zip/unzip a .zip.

Example πŸ”₯:

$ zip toto.zip f0 f1 f2
$ unzip toto.zip
$ gunzip -S .zip toto.zip

View the contents of a zip

$ zipinfo toto.zip
$ unzip -l toto.zip

Usage 🐚: archive/unarchive tar.gz, gz, tgz...

Example πŸ”₯:

Compress (c=create, v=verbose, z=compress, f=archive name)

$ tar -cvzf archive_name.tar.gz file0 file1 # ...

Decompress (x=decompress, v=verbose, f=archive name)

$ tar -xvf archive_name.tar.gz

User management

linux1

Usage 🐚: swap/log in as another user

Example πŸ”₯:

$ su root

πŸ‘‰ Log in as another user (move the their home...)

$ su -l root
$ su - root # same

Usage 🐚: add a user

Example πŸ”₯:

$ useradd username

Create a username, give them a group, and define their home folder.

# -m = create home
$ useradd username -g usergroup -b /path/to/user/home -m
$ adduser username # create everything at once

Usage 🐚: add users to a group... Don't forget to log back in for the changes to be applied.

Example πŸ”₯:

Add user yyy to group xxx

$ sudo usermod -a -G xxx yyy

Add "username" to sudoers

$ sudo usermod -a -G sudo username # Debian...
$ sudo usermod -a -G wheel username # Fedora...

Change the login username

$ sudo usermod -l newname oldname

Change user UID. You can't change the current user UID.

$ sudo usermod -u newUID oldUID

Lock Account

$ sudo usermod --lock [...]

Usage 🐚: delete a user

Example πŸ”₯:

$ deluser username

Usage 🐚: change a user's password

Example πŸ”₯:

Change the current user's password:

$ passwd

Delete the current user's password:

$ passwd -d # automatically logged/no password prompt

Change another user's password:

$ passwd username

Usage 🐚: create a group

Example πŸ”₯:

$ groupadd group_name

Usage 🐚: edit a group information.

Example πŸ”₯:

Change group GID.

$ sudo groupmod -g newGID oldGID

Change the name of the group.

$ sudo groupmod -n newname oldname

Usage 🐚: delete a group

Example πŸ”₯:

$ delgroup group_name

linux2

Usage 🐚: manage a file/folder permissions

Example πŸ”₯:

Use + to grant perms..

$ chmod u+x target
$ chmod g+rw target
$ chmod ug+r target
$ chmod g+x,o+rx target

Grant to a (all, alias of ugo)

$ chmod +x target
$ chmod a+x target # same
$ chmod ugo+x target # same

Use - instead of + to revoke permissions.

$ chmod -x target
$ chmod u-x target
$ chmod ug-rw target

Grant "perms" using the shortcut number.

# u=rwx, g=rx, o=x
$ chmod 751 target
# u=rwx, g=, o=
$ chmod 700 target

linux2

Usage 🐚: change the owner of a file/folder

Example πŸ”₯:

$ ls -l toto.txt
-rw-r-xr-x 1 n1 n [...] toto.txt 
$ chown n2 toto.txt
-rw-r-xr-x 1 n2 n [...] toto.txt
$ chown n2:m toto.txt
-rw-r-xr-x 1 n2 m [...] toto.txt

You may use -R (recursive), and -h (do not deference symbolic links).

Usage 🐚: A call to umask return the missing permissions with a leading 0.

Example πŸ”₯:

$ umask
0026 # meaning 751 by default
$ umask -s
u=rwx,g=rx,o=r
$ umask 0026
$ umask u=rwx,g=rx,o=r

Search

Usage 🐚: find a command

Example πŸ”₯:

Find the executable for a command

$ which find
/usr/bin/find

Find any executable for a command in the PATH

$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

linuxfundamentalspart1

Usage 🐚: find a file/folder. There are a ton of options.

Example πŸ”₯:

The search is done recursively starting from a folder (ex: /). We can indicate a pattern (ex: toto*).

$ find /
$ find / -name "toto*" # toto toto0...
$ find / -name "toto*" -type f # files
$ find / -name "toto*" -type d # folders

You can apply an operation on every file found. For instance, you can copy them somewhere.

# {} is the path to the match
$ find [...] -exec cp '{}' /tmp/found \;

When using find, we may get many errors. We usually redirect them to the "trash".

$ find [...] 2> /dev/null

Other options

  • -empty: empty folders/files only
  • -execdir: works the same as -exec, but in the directory of the match
  • -quit: exit when one result is found
  • -printf: change the output format
  • -mindepth value: minimum depth (default=0)
  • -maxdepth value: maximum depth

These are rarely used:

  • -mtime n/-atime n: changed/accessed in the last n days
  • -cmin n/-amin n: same, but in the last n minutes
  • -newer file: modified after file
  • -size n: a specific size such as 4M
  • -perms 0744/-perms a=x/-writable/.../-perm -o w/-222: files matching the given perms
  • -user xxx: files belonging to the user xxx
  • -group xxx: files belonging to users in xxx

πŸ‘‰ You can add a + or - such as -size +4M.

πŸ‘‰ You can use !/-not to negate an option. For example, you can use -not -name.

πŸ‘‰ You can group conditions too: \( -name xxx -o -name yyy \). Use -o for OR and -a for AND.

Usage 🐚: search a file in a local database of files. The database must be updated manually sudo updatedb.

Example πŸ”₯:

$ locate find

File manipulation

This sections does NOT include file editors.

Usage 🐚: show the $n$ first/last lines of a file. Both a working the same, head is for the first lines, tail is for the last lines.

Example πŸ”₯:

# first 10 lines
$ head file
# first 5 lines
$ head file -n 5
$ head file -n +5
# all lines, aside from the last 5 lines
$ head file -n -5
$ tail -c nchars file

linuxfundamentalspart1 adventofcyber4

Usage 🐚: search files based on their content.

Example πŸ”₯:

List files having 'toto' in them

$ grep "toto" *
$ grep --color "toto" * # highlight match

Best options

  • -r recursive (-R to follow links too)
  • -i Ignore case
  • -v Inverse pattern
  • -c: number of matches per file
  • -n: add line number before each match
  • -o show only the matched part
  • -H show the file before every match
  • -w words-only ("XxtotoxX" won't match "toto")

regex mode πŸ§ͺ

By default, if you are using ?, (, or any character used by regexes, it won't be interpreted (eg. ? won't be considered as "optional").

$ grep "opt?" * # ❌ match "opt?"
$ grep "opt\?" * # βœ… "t" is optional
$ grep -E "opt?" * # βœ… same as grep -E
$ egrep "opt?" * # βœ… same as grep -E

Less frequently used options

  • -L: stop when match found, show files without matches
  • -l: stop when match found, show files with matches
  • -q: no output, use the exit code to indicate if a match was found (0), or not (1).

Usage 🐚: sed has many uses. You can apply simple modification on the file, or search and replace something.

sed does not modify the file, but output on stdout the modified content, which you can redirect.

Example - syntax 1 πŸ”₯:
sed '<range><letter><args>' <file>.

  • i: add a line with "XXX" before (i) every line
  • a: add a line with "XXX" after (a) every line
  • p: duplicate every line
  • d: delete every line
  • c: replace every line with "XXX"
  • e: execute the command before every line (you may add parenthesis to make things cleaner)
$ sed 'iXXX' file
$ sed 'cXXX' file
$ sed 'aXXX' file
$ sed 'p' file
$ sed 'e(echo hello)' file

Instead of applying a command to every line, you can pick some lines

$ sed '1iXXX' file # line 1
$ sed '1,3iXXX' file # line 1 to 3
$ sed '$iXXX' file # last line
$ sed '1p' file # duplicate first
$ sed '1d' file # delete first
# ...

Example - syntax 2 πŸ”₯:
sed 's/<pattern>/<replacement>/<flag>' <file>.

  • pattern: a regex
  • flag:
    • none: first match of each line
    • g: every match is replaced
    • n: replace the nth match of each line
  • replacement: what to replace the section with

Replace every "e" with "E"

$ sed "s/e/E/g" file

Replace the first "e" of each line with "E"

$ sed "s/e/E/" file
$ sed "s/e/E/1" file

Comment out every line starting with "S". We use a capture group 🚩 which is referenced using \1.

$ sed "s/^\(S.*\)/# \1./" file

Usage 🐚: most of the time, there are other solutions that are more appropriate such as sed. awk is a mix of sed, cut, tr...

πŸ‘‰ See also: The_AWK_Programming_Language & To awk or not

awk is considering space (-F to change) as a separator for columns. The first column is $1, the nth column is $n. $0 means every column.

The syntax is awk 'target {action}' file.

  • target: filter where the action is applied
  • action: print...

Example πŸ”₯:

By default, awk prints every column.

$ awk '{print}' file
$ awk -F' ' '{print $0}' file # same

Only print the first column of lines having AT LEAST 6 columns (NF)

$ awk 'NF > 6 {print $1}' file

Print the first, and the third column, if the file has at least 3 lines (NR)

$ awk 'NR > 3 {print $1 $3}' file

Usage 🐚: extract "fields" from a table For instance, given the following file:

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash

For every line, we got values separated by ":". These are what we call "fields". For cut, the row look like this: f1:f2:f3:f4:f5:f6:f7.

Example πŸ”₯:

$ cat /etc/passwd | cut -d: -f1
root
$ cat /etc/passwd | cut -d: -f1-3
root:x:0
$ cat /etc/passwd | cut -d: -f1,3
root:0

Usage 🐚: replace a character with another, or delete a character.

Example πŸ”₯:

Replace whitespaces with the newline character

$ cat file | tr ' ' '\n'

Delete every character W

$ cat file | tr -d 'W'

Usage 🐚: number lines of files

Example πŸ”₯:

Number non-empty lines

$ nl file
$ nl -bt file

Number all lines

$ nl -ba file

Usage 🐚: count words/lines/characters

Example πŸ”₯:

Use -l for lines, -w for words, and -c for characters.

$ cat file.txt | wc -l
7560 # lines

You can read something from stdin. Use CTRL+D to indicate the end of the input.

$ wc -w
Hello
World # <CTRL+D>
2

Usage 🐚: output the sorted file

Example πŸ”₯:

$ sort
$ sort -d
  • -b: ignore leading blanks
  • -r: reverse (z to a)

You can define a separator (:), and only sort a column.

$ sort s -t: -k2 # sort by second column
3:a
1:b
2:c
$ sort s -t: -k2.0 # column.nth character
$ sort s -t: -k2.0,2.1 # range

Usage 🐚: remove lines or indicate how many duplicates there are for each unique line.

Example πŸ”₯:

Remove duplicates

$ uniq < file

Add the number of duplicates for each unique line

$ uniq -c < file
  1: XXX
  1: YYY
  5: ZZZ

Utilities

linuxfundamentalspart3

Usage 🐚: download files/folders.

Example πŸ”₯:

$ wget https://path/to/file [...]
$ wget -r [...]          # download a folder
$ wget -O xxx [...]      # output name
$ wget -P xxx [...]      # output location

Other helpful options:

$ wget -d [...]         # debug
$ wget --no-parent [...] # maxdepth 1
$ wget -t 2 [...]        # number of tries
$ wget -T 30 [...]       # timeout
$ wget --no-check-certificate [...] # no SSL

webrequests

Usage 🐚: do GET/POST/... requests.

Example πŸ”₯:

GET request.

$ curl https://example.com    # GET an URL
$ curl -u username:pass [...] # HTTP Basic Auth
$ curl -i [...]               # request headers (any request)
$ curl -k [...]               # ignore SSL errors
$ curl -O [...]               # save ; use remote filename
$ curl -o xxx [...]           # save ; use custom filename
$ curl --silent [...]         # -s | only print the output

Craft an HTTP request:

$ curl -X GET [...]           # same as "--request"
$ curl -H "Name: Value" [...] # same as "--header"
$ curl -d "key=value" [...]   # same as "--data"
$ curl -d '{"key":"value"}' [...] # ex: json
$ curl -A "xxx" [...]         # set user-agent
$ curl -b "NAME=VALUE" [...]  # set cookie
$ curl -T file [...]          # upload file using PUT

POST request.

$ curl -X POST 'URL' -d 'key=value' -H 'Content-Type: application/x-www-form-urlencoded'

Usage 🐚: track the time a command takes.

Example πŸ”₯:

$ time sleep 5
real    0m5.002s
user    0m0.001s
sys     0m0.000s

Usage 🐚: set the size, orientation, brightness of the screen...

Example πŸ”₯:

Set the brightness to "0.5"

$ xrandr --output DP-2 --brightness 0.5

πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.

  • getent 2: get an entry such as "passwd": getent passwd (get entry) instead of cat /etc/passwd
  • dd if=/dev/zero of=bigfile bs=1M
  • jq '.x.y ','a,b'
lsblk==block devices
lsusb==USB devices
lsof==opened files (ex: see if device files are in-use)
lspci==opened PCI devices