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
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
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"
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
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
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 .
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
Usage π: create one or more folders
Example π₯:
$ mkdir folder
Create every non-existing folder in a path
$ mkdir -p folder0/folder1/folder2
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
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
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
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
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
$ tar -xvjf archive_name.tbz2
User management
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
Change multiple or one user password.
$ echo "username:password" | chpasswd
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
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
$ 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
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
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 lastn
days -
-cmin n
/-amin n
: same, but in the lastn
minutes -
-newer file
: modified afterfile
-
-size n
: a specific size such as4M
-
-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
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
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
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.
- modern-unix
- pretty CSV
-
mktemp
-
ranger
-
hexdump
-
cloc
,sloc
-
column -t
(t=as table)
-
getent
2: get an entry such as "passwd":getent passwd
(get entry) instead ofcat /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