Linux essential commands
If you’re new in Linux, these commands will help you to start learning the system, specially the command line.
Table of Contents
- Bash syntax
- Parameters
- Navigate through filesystem and list contents
- Copy, move and delete content
- Create and delete folders
- Processes
- Block devices and filesystems
- Hardware information
- File info
- File creation
- Users
- Permissions
- Network
- Keyboard shortcuts
- More
Bash syntax
Check my post: Bash syntax.
Parameters
A command usually has this structure:
<command_name> <options> <file>
In this case, <options>
and <file>
are parameters. Command options have an option name and an optional (or required) option value. Option names usually start with a single dash (-
) when is a one-letter, or two dashes (--
) when the option name is a word. Some GNU commands (like tar
) allow to specify one-letter option without typing the dash.
When you want to run a command where the <file>
parameter starts with a dash, you need to tell to the command that <file>
is not an option name. You can do this by typing --
before <file>
. In the following example, we are going to create a file called -testfile
:
touch -- -testfile
Navigate through filesystem and list contents
pwd
: show the working directory.cd <path>
: change the working directory (cd files/
). If you don’t add a<path>
, it will change to the “home” directory for the user (runecho $HOME
to see what is your home directory).cd ..
: change to the parent folder.cd -
: change to the previous working folder.
ls <path>
: list files and folders.<path>
is optional if you want to list files inside your working directory. Otherwise, you can use relative paths (relative to your working directory, like./folder/
,../folder/
) or absolute paths (like/home/user/
).- You can use wildcards to filter the list:
ls *.png
. ls -l
: show files and folders permissions.ls -a
: include hidden files (files that start with.
).ls -lt
: order by last modified time,ls -ltu
: order by last accessed time.ls --color=auto
: colorizes the output.
- You can use wildcards to filter the list:
du <path>
: similar tols
, list files/directories and their sizes. Unlikels
,du
shows folder size correctly (as a sum of the sizes of all their files).du -h
: use human-readable size format.du --max-depth=<number>
: limit the recursive listing to<number>
levels.du -a
: include files in the list (not only directories).du --apparent-size
: print apparent sizes rather than device usage (useful for small files). You can use it with--block-size=1
for printing bytes instead of kilobytes.
Copy, move and delete content
cp <source> <target>
: copy a file from a<source>
to a<target>
: For example,cp ./file.txt ./folder/
. You can change the filename by typing the new filename in<target>
(cp ./file.txt ./folder/file2.txt
).cp -r <folder> <target>
: copy files and folders recursively. Check the difference when the target folder exists (copies the folder itself an its content) or does not exist (only copies the content).cp --preserve=timestamps <source> <target>
: preserve file timestamps. You can also preserve other attributes, by default--preserve
preserves mode, ownership, timestamps.
mv <source> <target>
: move a file or folder from<source>
to<target>
. It moves recursively.rm <file>
: remove a file.rm -r <folder>
: remove files and folders recursively.rm -f <file>
: remove a write-protected file (you need to have write permissions on the folder).
Create and delete folders
mkdir <folder>
: create a folder.mkdir -p <folder>
: create parent folders if they don’t exist.
rmdir <folder>
: remove an empty folder.
Processes
<command> &
: run a command in the background (more info).pkill <process name>
: kill a process by its name.kill <process PID or job number>
: kill a process by its PID (Process ID) or job number (prepend ‘%’ in this case) It sends SIGTERM signal.kill -s <signal> <PID>
: kill a process with a signal (typekill -l
for a signals list). You can also type the signal name (SIGSTOP
) or type signal number after a dash (-9
).kill 1234 kill %1 kill -s 15 1234 kill -s SIGTERM 1234 kill -9 1234
killall <process name>
: kill all instances of a process.ps ax
: list running processes owned by any user.ps aux
: similar to the above, with more info. Same asps -ef
.ps -e -o pid,comm,%mem
: customize fields.ps --forest
,ps f
: display a process tree.
top
: similar tops ax
, but updates automatically. Most people usehtop
(an enhanced version oftop
) for this task.which <command name>
: find where a command is being run from.free
: provides information about physical memory and swap space. By default, displays numbers in kilobytes. Add-m
to show numbers in megabytes.echo $0
: shows the shell you are using (Bash, zsh, etc.). When using inside a script, displays script path (relative or absolute).pgrep <process name>
: find process ID.
Block devices and filesystems
lsblk
: list available block devices.lsblk -o <fields>
: customize the output by selecting the columns.lsblk -o name,uuid,label
df
: displays disk usage. Add-h
to show disk space in a human-readable format.du
: shows the amount of space that is being used by files in a directory. Add-h
to show disk usage in a human-readable format. Add-d <number>
to specify a maximum depth.
Hardware information
lspci
: list PCI devices. Add-v
to get more info.lsusb
: list USB devices.lscpu
: info about CPU.
File info
file <file>
: generic information about file type.stat <file>
: information about file permissions and date metadata (last accessed, last changed, etc.)stat -c %U <file>
: display only the file owner. You can also use%G
for group and%A
to show only file permissions.
File creation
touch <file>
: creates an empty file if<file>
does not exist.touch -m -d "2022-01-01 01:00:00" urls.txt
: change last modification time.touch -a -d "2022-01-01 01:00:00" urls.txt
: change last access time.touch -d "2022-01-02 01:00:00" urls.txt
: change modification and access time.
truncate <option> <file>
: shrink or extend the size of a file.truncate -s <size> <file>
:<size>
is an integer, with optional units: B,K,M,G,…truncate -s 0 some_file
: empty a file.
fallocate <option> <file>
: similar totruncate
.fallocate -l <size> <file>
mktemp [<template>]
: creates a temporary file or folder and prints their name. If<template>
is defined, must include at least three ‘X’ at the end. Those ‘X’ will be replaced by random characters. The file will be created inside the working directory (if a relative path is used). If<template>
is not defined,mktemp
will create a filetmp.XXXXXXXXXX
inside/tmp
(or$TMPDIR
if set).mktemp -p <DIR> <template>
: creates the file inside<DIR>
. If<DIR>
is not specified, uses$TMPDIR
if set, else/tmp
.mktemp -d <template>
: creates a temporary folder.
Users
- User Management on Linux
who
: show who is logged on.
Permissions
Network
wget <url>
: download a file.wget -O <filename> <url>
: specify a name for downloaded file.wget -P <path> <url>
: specify an output directory.wget -i <list file>
: specify a file with the URLs (one per line).wget -c <url>
: resume a stopped download (if server is compatible).
ip route
orip r
: display route table (shows local IP).
Keyboard shortcuts
- Ctrl + L or
clear
: clear the terminal. - Ctrl + +: increase the terminal font size.
- Ctrl + -: decrease the terminal font size.
- Ctrl + R: Search through command history. Type a search and press Enter to execute the found command, Ctrl + R to go to next match, or press the right arrow to edit the command. Ctrl + G to quit search.
- Ctrl + A: move to the beginning of the line.
- Ctrl + E: move to the end of the line.
- Ctrl + K: remove text from the current cursor position to the end of the line.
- Ctrl + U: remove text from the current cursor position to the beginning of the line.
- Ctrl + C: stop the current job.
- Ctrl + Z: suspend the current job.
- Alt + .: insert the last argument of the previous command.
!!
: repeat the previous command.$ pacman -Sy error: you cannot perform this operation unless you are root. $ sudo !! sudo pacman -Sy [sudo] password for ricardo:
More
head <file>
: show the first 10 lines of a file. You can specify any number of lines with-n <number>
.head -n -3
: show all except the last 3 lines.
tail <file>
: show the last 10 lines of a file. You can specify any number of lines with-n <number>
.tail -n +3
: show all except the first three lines.tail -f
: output appended data as the file grows, useful for viewing log files.
read -p "<prompt>" <variable>
: read from user input and assign to a variable.$ read -p "Confirm (y/n): " res; echo $res Confirm (y/n): y y
read -n 1 <variable>
: accepts only one character for input.man <page>
: show a reference manual about a topic (more info).man <section number> <page>
: show an specific section (e.g.: you can accesscrontab(5)
by typingman 5 crontab
)
date
: show date and time.date +FORMAT
: shows date/time with the specified format: For example,date +%s
shows seconds since the Epoch.date -d <date>
: display<date>
(date -d 20210101
,date -d "2 weeks ago"
).
run-parts <folder>
: execute all scripts inside a folder, sequentially.wc [options] <file>
: count words, lines, characters of a file.wc -l <file>
: count lines. It also displays the filename, to show only the number of lines, run:wc -l < <file>
.
watch <command>
: execute a program periodically, showing its output fullscreen. By default, the command is run every 2 seconds.watch -n <seconds> <command>
: specify the interval.watch -t <command>
: turn off the header.watch '<command> | <command>'
: you can use pipes.
lsof
: list open files.lsof <directory>
,lsof -p <PID>
,lsof -u <users>
lsof -i [46][protocol][@hostname|hostaddr][:service|port]
: e.g.:lsof -i TCP:https
+L1
: list open files with links fewer than 1, i.e. removed/unlinked files.+D <directory>
: search for any open instances of<directory>
and all files and directories it contains.
ldconfig -p | grep <lib name>
: find out if<lib name>
library is installed.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: