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
- Navigate through filesystem and list contents
- Copy, move and delete content
- Create and delete folders
- Block devices and filesystems
- Hardware information
- File info
- File creation
- Keyboard shortcuts
Check my post: Bash syntax.
A command usually has this structure:
<command_name> <options> <file>
In this case,
<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
<file>. In the following example, we are going to create a file called
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 (run
echo $HOMEto 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/) or absolute paths (like
- You can use wildcards to filter the list:
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 to
ls, list files/directories and their sizes. Unlike
dushows 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
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=1for printing bytes instead of kilobytes.
Copy, move and delete content
cp <source> <target>: copy a file from a
<target>: For example,
cp ./file.txt ./folder/. You can change the filename by typing the new filename in
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
--preservepreserves mode, ownership, timestamps.
mv <source> <target>: move a file or folder from
<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.
<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 (type
kill -lfor a signals list). You can also type the signal name (
SIGSTOP) or type signal number after a dash (
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 as
ps -e -o pid,comm,%mem: customize fields.
ps f: display a process tree.
top: similar to
ps ax, but updates automatically. Most people use
htop(an enhanced version of
top) 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
-mto 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
-hto show disk space in a human-readable format.
du: shows the amount of space that is being used by files in a directory. Add
-hto show disk usage in a human-readable format. Add
-d <number>to specify a maximum depth.
lspci: list PCI devices. Add
-vto get more info.
lsusb: list USB devices.
lscpu: info about CPU.
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
%Gfor group and
%Ato show only file permissions.
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 to
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,
mktempwill create a file
mktemp -p <DIR> <template>: creates the file inside
<DIR>is not specified, uses
$TMPDIRif set, else
mktemp -d <template>: creates a temporary folder.
- User Management on Linux
who: show who is logged on.
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 r: display route table (shows local IP).
- 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:
head <file>: show the first 10 lines of a file. You can specify any number of lines with
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
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 access
man 5 crontab)
date: show date and time.
date +FORMAT: shows date/time with the specified format: For example,
date +%sshows seconds since the Epoch.
date -d <date>: display
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 -p <PID>,
lsof -u <users>
lsof -i [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.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles: