Every developer should learn how to use the command line and include it in their regular activities. Bash is the default command interpreter on most Linux distributions. Once you know about 20 basic Bash commands, you can find your way around a server, manage files, search logs and stop runaway processes without a control panel.
This guide is for beginners: developers opening a terminal for the first time, students, and anyone who just got SSH access to a Linux VPS. You'll learn how Bash commands are put together (command, options, arguments), the essential commands for files, text search, permissions and processes, with examples, and how pipes, redirection and environment variables tie the commands together.
โ What is Bash?
Bash stands for Bourne Again Shell, a witty term that pays homage to Bourne Shell (invented by Steven Bourne). Bash is a shell project designed by Brian Fox as an update to the Bourne Shell application "sh." It's an open-source GNU project first launched in 1989 and now one of the most popular GNU/Linux shell distributions. Its manual page describes it as an sh-compatible command language interpreter that runs commands read from standard input or from a file.
Bash is more functional than the Bourne Shell in terms of programming and interactive use. It comes with command-line editing, key bindings, a complete command history, and more. A shell script is a sequence of these commands contained within a single file, and these scripts can be read and executed using Bash.
Bash vs shell vs terminal
These three get mixed up all the time. The terminal is the window program you type into (GNOME Terminal, iTerm2, PuTTY over SSH). The shell is the program inside that window that reads your commands and runs them. For a deeper explanation, see what is shell in Linux. Bash is one particular shell. Others include sh, zsh and fish. On a typical Ubuntu or Debian server, you get Bash as soon as you log in over SSH.
macOS has used zsh as its default shell since Catalina. Most of the commands below work the same there, though some options vary because macOS ships BSD versions of tools like ls and sed.
โ๏ธ How Bash Commands Work
Nearly every command follows the same pattern:
command [options] [arguments]
For example, ls -la /var/log runs ls with the options -l (long format) and -a (show hidden files) on the argument /var/log. In grep -i "error" app.log, the -i option makes the match case-insensitive, and the two arguments are the pattern and the file.
Paths come in two kinds. An absolute path starts at the root, as in /home/alice/site. A relative path starts from your current working directory, so site/index.html only works if you're already in /home/alice.
Some commands are builtins, meaning Bash runs them itself (cd, echo, exit, export). Others are separate programs on disk (ls, grep, find). Run type ls or type cd to see which kind you're dealing with.
๐ File and Directory Navigation
The navigation commands below are the same across most Linux distributions, including Kali Linux commands for beginners. For a broader reference, see basic Linux commands.
pwd โ print the current directory
pwd shows the absolute path of the directory you're in. If you have many terminals open and need to remember where you are, pwd tells you instantly.
pwd
ls โ list directory contents
ls is arguably the most commonly used command. You'll frequently be working in a directory and need to know what files it contains.
ls # list files
ls -lh /var/www # long listing, human-readable sizes
ls -a # include hidden files (names starting with a dot)
Options available: -a, -l, -h.
cd โ change directory
cd changes the directory you're in, allowing you to access, manipulate, and read files across the system.
cd /etc/nginx # go to an absolute path
cd .. # go up one level
cd ~ # go to your home directory
cd - # go back to the previous directory
โ๏ธ File Creation, Moving, and Deletion
These commands follow the same pattern across Unix-like systems. For more examples, see best Unix commands with examples.
touch โ create files
touch is the simplest way to create new files, but it may also update the timestamps on existing files and directories. You can make as many files as you like in one command.
touch notes.txt
Options available: -a, -m, -r, -d.
mkdir โ create directories
The mkdir command creates folders. A large number of directories can be created at once, which speeds up the process significantly.
mkdir -p project/src/css # create nested directories in one go
Options available: -m, -p, -v.
mv โ move or rename
mv moves or renames files and directories. Without it, you'd have to rename each file separately, which is time-consuming. You can rename batch files with mv, saving a lot of time.
mv notes.txt project/ # move a file
mv old.conf new.conf # rename a file or directory
Options available: -i, -b.
cp โ copy files and directories
cp config.php config.php.bak # copy a file
cp -r project project-backup # copy a directory recursively
rm โ delete files
Bash has no trash can. rm deletes immediately, and mv or cp will silently overwrite a file with the same name at the destination.
rm file.txt
rm -r oldproject # delete a directory and everything in it
Add -i (rm -i) for a confirmation prompt. Before running rm -r with a wildcard, run ls with the same pattern first so you can see what it matches.
rmdir โ remove empty directories
Empty directories get removed using rmdir. The difference between rm and rmdir is that rmdir only deletes empty directories, whereas rm deletes all directories and files, whether or not they contain data.
rmdir emptyfolder
Options available: -p.
echo โ print text to the terminal
Echo writes text to the terminal window and is commonly used to publish status text in shell scripts and batch files. It's also handy for displaying the contents of environmental variables.
echo "Hello, world"
echo $HOME
Options available: -e, -n.
๐ File Viewing and Text Search
cat โ read, create, and concatenate files
Cat is one of the more versatile commands, with three primary uses: showing files, combining copies, and creating new ones.
cat a.txt b.txt # join two files
cat -n file.txt # number every line
Options available: -n.
less โ view the contents of a file
You can view files without opening an editor with less. It's easier to use, and there's no risk of accidentally changing the file. Scroll with arrows, search with /, and quit with q.
less /var/log/syslog
Options available: -e, -f, -n.
head โ read the start of a file
head displays the first 10 lines of a file by default.
head -n 20 access.log
Options available: -n.
tail โ read the end of a file
tail displays the last 10 lines of a file by default. tail -f keeps the file open and prints new lines as they're written, which is handy while you reload a broken site. Press Ctrl+C to stop it.
tail -f error.log
Options available: -n.
grep โ search text
Grep searches for patterns in text that the user specifies. It's one of the most useful and effective commands available when you need to find a specific string or pattern within a file but don't know where to start looking.
grep -i "error" /var/log/nginx/error.log # case-insensitive search
grep -rn "DB_HOST" /var/www/ # search recursively, show line numbers
Options available: -i, -c, -n, -r.
find โ search for files
find /var/www -name "*.php" -mtime -1 # PHP files changed in the last day
find . -type f -size +100M # files larger than 100 MB
find walks the filesystem in real time, so it's slower but always current.
locate โ locate a file or directory
This is the most straightforward method of locating a file or directory. If you're not sure what you're looking for, you can keep your search broad or narrow it down with wildcards or regular expressions.
locate nginx.conf
locate queries a prebuilt index, so it's very fast but won't show files created since the last updatedb run. On many minimal server images it isn't installed at all.
Options available: -q, -n, -i.
wc โ count lines, words, and bytes
wc counts lines, words, and bytes in a file. It's useful for quick file size checks or counting matches from another command. For a deeper walkthrough, see Linux wc command.
wc notes.txt # lines, words, bytes
wc -l notes.txt # lines only
๐ Permissions and Process Management
man, help and --help
Run man ls for the full manual page of an external command, help cd for a Bash builtin, and ls --help for a quick summary of options.
man grep # read the full manual
help cd # help for a builtin
ls --help # quick summary of options
Options for man: -w, -f, -b.
chmod โ change file permissions
There will be times when you're trying to upload a file or alter a document and get an error message because you don't have permission. Using chmod is an easy fix. ls -l shows permissions as strings like -rwxr-xr-x: read, write and execute for the owner, the group and everyone else. chmod changes them using numbers or symbols.
chmod 644 index.html # numeric
chmod u+x deploy.sh # symbolic: give the owner execute permission
| Mode | Owner | Group / others | Typical use |
| 755 | read, write, execute | read, execute | Directories, scripts |
| 644 | read, write | read | Regular web files |
| 700 | read, write, execute | none | Private folders such as ~/.ssh |
Don't "fix" a permission error with chmod 777. It lets every user on the system write to the file. Work out who should own it and use chown instead.
Options available: -f, -v.
chown โ change file ownership
sudo chown www-data:www-data -R /var/www/site
ps โ list running processes
ps lists running processes with their PIDs. For a deeper dive, see Linux ps command.
ps aux | grep php # find PHP processes and their PIDs
For live monitoring, top shows CPU and memory in real time. For broader system monitoring, see Linux monitoring.
kill โ terminate stalled processes
You can kill a program from the command line with the kill command. This is done by supplying the process ID (PID), which you find with ps aux.
kill 12345 # ask process 12345 to stop (SIGTERM)
kill -9 12345 # force it to stop (SIGKILL)
A plain kill sends SIGTERM, which gives the program a chance to clean up and exit. kill -9 sends SIGKILL, which the program can't catch, so unsaved data is lost. Try the plain version first.
Options available: -p.
๐ง Bash Features Every Beginner Should Know
Pipes and redirection
A pipe (|) sends the output of one command into the next as its input. Redirection sends output to a file, or reads input from one.
grep "error" app.log | less # page through matches
ls > files.txt # write output (overwrites the file)
echo "done" >> run.log # append to the end of a file
sort < names.txt # read input from a file
command 2> errors.txt # send standard error to a file
Be careful with > because it empties the target file before writing to it.
Environment variables and $PATH
$HOME holds your home directory and $PWD holds your current one. $PATH is a colon-separated list of directories that Bash searches when you type a command name. Run echo $PATH to see it, and export MYVAR=value to set a variable for the current session and any programs it starts.
Wildcards and quoting
* matches any characters and ? matches exactly one, so rm *.tmp deletes every file ending in .tmp. Put names that contain spaces in quotes: cd "My Files". Single quotes stop variable expansion, so echo '$HOME' prints the text literally, while double quotes expand variables.
Exit codes and exit
Every command returns an exit status. 0 means success and anything else means failure. Run echo $? straight after a command to check it, which is how scripts and if statements make decisions. For a full walkthrough on conditional logic, see Bash if statement. exit closes the current shell or script (it ends your SSH session if you run it there). It has nothing to do with leaving a directory.
Once you're comfortable with these Bash commands, the next step is connecting to remote servers with SSH commands.
๐จ Common Errors and What They Mean
command not found: the program isn't installed, you mistyped it, or its directory isn't in$PATH. Check withtype name.No such file or directory: the path is wrong. Runpwdandlsto check where you are, and remember that Linux treatsFile.txtandfile.txtas different files.Permission denied: your user lacks rights. Check withls -l, then usesudoor fix ownership.
๐ Bash Command Cheat Sheet
| Command | What it does | Example |
pwd |
Show the current directory | pwd |
ls |
List files | ls -la |
cd |
Change directory | cd /var/www |
touch |
Create an empty file | touch app.log |
mkdir |
Create a directory | mkdir -p a/b |
cp |
Copy | cp -r src dst |
mv |
Move or rename | mv a.txt b.txt |
rm |
Delete | rm -i old.txt |
cat / less |
View a file | less big.log |
head / tail |
First or last lines | tail -f error.log |
grep |
Search text | grep -rn "TODO" . |
find |
Search for files | find . -name "*.conf" |
wc |
Count lines, words, bytes | wc -l file.txt |
chmod |
Change permissions | chmod 755 run.sh |
ps / kill |
List or stop processes | kill 12345 |
man |
Read a command's manual | man grep |
clear |
Clear the terminal window | clear |
compgen |
List commands, aliases, functions | compgen -a |
๐งฉ Conclusion
By this stage, you might have a strong idea of how to navigate around using the command line, move raw files, and make modest changes in Linux Bash. This beginner-level lesson on Bash commands will help you gain basic fluency and confidence. You now know the 20 most crucial commands, how they're put together (command, options, arguments), and the features that tie them together โ pipes, redirection, environment variables, wildcards, and exit codes. To practice on a real Linux environment with full root access, explore Linux VPS hosting from 1GBits โ full control and dedicated resources.


Leave A Comment