The basic commands
Basic commands
The basic Linux commands are pretty short:
Command | Description |
---|---|
ls | list files/directories (in the current directory) |
cd | change directory |
pwd | print working/current directory |
mkdir | create new directory |
rm | remove/delete file or directory |
cp | copy |
mv | move or rename |
ln | create link |
du | get file size (disk usage) |
df | get free disk space (disk free) |
See tool help about how to find the full documentation of these commands.
Common usage
Command | Common usage |
---|---|
ls |
|
cd |
|
rm |
|
cp |
|
mv |
|
du |
|
df | df -h to get an overview of all mounted partitions, including used and free space. Argument -h also exists here. |
The tilde sign (~) can be used in any command to indicate your home directory.
Special filenames
Linux is case sensitive, so “my_file” is different from “My_File”.
If a filename contains spaces or special characters 3), you need to use either of these methods:
- Use double quotes around the filename, e.g. du “my file”
- Use a backslash before the space or special character in the filename, e.g. du my\ file
Use Tab completion (see command line) to quickly enter longer and complicated filenames.
A less frequent problem is a filename that starts with a hyphen/minus. Commands will see that as an argument instead of as a filename. You need to put either -- (with a space afterwards) or ./ (glued to the filename) in front of such filenames. Example: rm -- -my_file or rm ./-my_file.
Wildcards
Two wildcards are supported by the shell, which replaces the expression with all matching filenames:
- The * (asterisk) character indicates any arbitrary string.
- A ? (question mark) stands for any single character.
Examples:
- ls c* will list all files that starts with the letter c
- ls c? will list all filenames with c and then one other letter
In addition to wildcards, the Bash shell also supports some simple regular expressions:
- [afz] to match any of the characters a, f or z
- [a-f] to match any character between a and f
Examples:
- ls a[abc] shows files aa, ab, and ac (if they exist)
- ls a[0-9] shows files a0, a1 through a9 (if they exist)
Symbolic and hard links
A symbolic link (also called symlink or soft link) is not a real file or directory, but points to a file or directory. You can create a symlink with ln -s real_file symlink_name. In the output of ls -l you can see where a symlink points to. If the symlink points to a directory, then argument -d prevents that ls shows the contents of the directory instead of the symlink name itself. Example:
ls -ld /*bin*
lrwxrwxrwx 1 root root 7 Jul 4 2024 /bin -> usr/bin
lrwxrwxrwx 1 root root 8 Jul 4 2024 /sbin -> usr/sbin
A hard link is less commonly used. You can create a hard link with ln original_file hard_link_name. By creating a hard link to a file, two filenames exist for the same file/data. It is only possible to create a hard link to another file on the same partition and hard links don't exist for directories.
Pipeline
The vertical bar | (also called pipe and entered with Shift+\) between commands causes the output of the first command to be sent to the second command as input. This is very commonly used. An example:
ps aux | grep foo
The first command outputs all running processes. The list becomes input for grep, which searches for the indicated pattern and only keeps those lines. The output of the whole command is a list of running processes with “foo” in their name.
The input of a command/program is called stdin (standard input), the output stdout. There is also stderr for error messages. Both stdout and stderr are output to the console/terminal, but you can filter them separately when redirecting.
Redirecting
With > you can redirect the output of a command to a file. Example that puts the output of the echo command in file my_file:
echo hi > my_file
Warning: If the file already exists, it will be overwritten! With >> you also redirect the output to a file, but instead of overwriting the file, you will append the output to the file (add the output to the end of the file):
echo hi >> my_file
You can get rid of unwanted output by redirecting it to /dev/null, the digital trash can:
echo hi > /dev/null
The stderr output is not included in this redirection. To include stderr you need to use either of these methods. Output 1 is stdout; 2 is stderr.
my_program &> my_file
my_program > my_file 2>&1
A related handy utility is tee. It allows you to simultaneously see the output and redirect it to a file:
echo hi | tee my_file
Redirecting input with < and << also exists, but is needed less often.
Command substitution
The backquote character ` (also called backtick) is located on the keyboard left of the 1 key. The output of a command between backquotes is filled in literally / ad verbatim at that location. Alternatively $(command) does the same. Example:
echo The date is `date`
The date is Mon 21 Apr 20:14:33 BST 2025
echo The date is $(date)
The date is Mon 21 Apr 20:14:33 BST 2025
Environment variables
You can show all current environment variables with export and show a single one with echo $MY_VAR. You can set/change one with export MY_VAR=some_value. It is not uncommon for Linux software to look at certain environment variables. (Which they look at should be in their documentation.)
The Bash shell itself also looks at some environment variables, such as PATH (explained below) and PS1 to customize the command prompt. I find it useful to show the current directory in the prompt:
export PS1="[\w]\\$ "
You can put export commands in ~/.bashrc. Those will be set every time a new Bash shell is started.
Source command
You can use either of these commands (they are equivalent) to execute commands from a file in your current shell. It will be the same as if you executed them directly in your shell.
source ./my_script
. ./my_script
This is different from running that script with either of these commands below. The bottom option works if the script contains a shebang line. This will start a new instance of the Bash shell under your current one (a child process), executes the script and when the script finishes the child process terminates.
bash ./my_script
./my_script
Trying to create a Bash script that sets environment variables that you want to get set in your current shell instance is therefore only possible by using the source method. If you would run the script, then the environment variables only get set in the new child process (shell) that runs the script, not in your current shell.
Using source is also a way to quickly get new settings applied in your current shell that you just added to ~/.bashrc. This is an alternative to starting a new shell:
source ~/.bashrc
Search path
The shell looks for binaries and scripts to execute in the paths that are included in environment variable PATH. The different directories are separated by colons:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Gotcha: Trying to execute a script or command that is in the current directory with my_script does not work. You will get an error response: “command not found”. This happens because the current directory is normally not in the PATH variable.
There are two solutions: execute it with ./my_script to explicitly indicate it's in the current directory, or include “.” (meaning current directory) in the PATH variable. You can do the latter with this extra line in ~/.bashrc: export PATH=$PATH:.
Aliases
Aliases are a convenience feature to reduce the amount of typing for commands. If you frequently type ls -l you could for example define an alias ll to do the same:
alias ll='ls -l'
You can put alias commands in ~/.bashrc. Those will be set every time a new Bash shell is started.
Tarballs
A common format for downloads is a .tar.gz file, sometimes also with extension .tgz instead. These are colloquially known as tarballs and are the same idea as a ZIP file. An advantage of a tarball is that Unix-specific things like permissions and owners are retained.
Tar is a program that allows you to archive files, meaning to put multiple files in a single archive file. Gzip is a program that can compress a file. Tar and gzip are used together to create a tarball. Example command to create a tarball:
tar zcvf my_tarball.tar.gz some_directory
You can unpack a tarball with:
tar zxvf my_tarball.tar.gz
The order of the arguments (zxvf) is not important, but this order is fastest to type. A hyphen/minus before the options is possible, but not necessary. The x stands for extract, the c for create and the v for verbose (shows the filenames) and z mean invoking gzip/gunzip.