Essential Linux Commands: A Beginner's Guide

By 

Updated on

17 min read

Essential Linux Commands

New Linux converts from the Windows world may find working with the command line somewhat intimidating. However, it’s not that difficult to use. All you need to get started with the command line is to learn a few basic commands.

While most Linux distributions are user-friendly and come with an easy-to-use graphical interface, knowing the command line can be very useful. The command line gives you greater control over your system and access to features not available through a graphical interface.

In this article, we’ll look at some of the most common Linux commands that system administrators use every day.

Getting Information About the Command

Memorizing command options is usually not necessary and may be a waste of time. Usually, if you don’t use a command frequently, you can easily forget its options.

Most commands have a --help option, which prints a short message about how to use the command and exits:

sh
command_name --help

The man command

Almost all Linux commands come with man pages. A man page is documentation that explains what the command does, how to use it, and what arguments it accepts.

The man command displays the manual page for a given command.

sh
man command_name

For example, to open the man page of the cd command, you would type:

Terminal
man cd

To navigate the man pages, use the Arrow, Page Up, and Page Down keys. You can also press the Enter key to move one line at a time, the Space bar to move to the next screen, and the b key to go one screen back. To exit the man page, press q.

In Linux, every file and directory is under the root directory, the first or top-most directory in the directory tree. The root directory is referred to as a single leading slash /.

When navigating the file system and operating on files, you can use either the absolute or the relative path to the resource.

An absolute path (or full path) starts from the root directory (/), while a relative path starts from your current directory.

Current Working Directory (pwd command)

The current working directory is the directory the user is currently in. Each time you interact with your command prompt, you are working within a directory.

Use the pwd command to find out what directory you are currently in:

Terminal
pwd

The command displays the path of your current working directory:

output
/home/linuxize

Changing directory (cd command)

The cd (“change directory”) command changes the current working directory in Linux and other Unix-like operating systems.

If you use cd without any arguments, it takes you to your home directory:

Terminal
cd

To switch to a different directory, you can use either its absolute path or its relative path.

Assuming that the directory Downloads exists in the directory from which you run the command, you can navigate to it by using the relative path to the directory:

Terminal
cd Downloads

You can also navigate to a directory by using its absolute path:

Terminal
cd /home/linuxize/Downloads

Two dots (..), one after the other, represent the parent directory or, in other words, the directory immediately above the current one.

If you’re in the /usr/local/share directory and want to move up one level to /usr/local, type:

Terminal
cd ../

To move two levels up:

Terminal
cd ../../

To return back to the previous working directory, use the dash (-) character as an argument:

Terminal
cd -

If the directory name has spaces, you can put the path in quotes or use a backslash (\) before each space:

Terminal
cd Dir\ name\ with\ space

Working with Files and Directories

Listing directory contents (ls command)

The ls command shows information about files and directories within a directory.

If you run ls with no options, it lists all the files in your current directory in alphabetical order:

Terminal
ls

To list files in a specific directory, pass the path to the directory as an argument:

Terminal
ls /usr

By default, ls only shows the names of files and folders. Use the -l option to see more details in a long listing format:

Terminal
ls -l /etc/hosts

The output includes the file type, permissions, number of hard links, owner, group, size, date, and filename:

output
-rw-r--r-- 1 root root 337 Oct  4 11:31 /etc/hosts

By default, ls doesn’t show hidden files. A hidden file is any file that starts with a period (.).

To see all files, including hidden ones, use the -a option:

Terminal
ls -a ~/

Displaying file contents (cat command)

The cat command prints the contents of one or more files and merges (concatenates) files by appending one file’s contents to the end of another file.

To show a file’s contents on the screen, type cat followed by the file name:

Terminal
cat /etc/hosts

Viewing file beginning and end (head and tail commands)

The head command shows the beginning of a file. By default, it displays the first 10 lines:

Terminal
head /var/log/syslog

To display a specific number of lines, use the -n option:

Terminal
head -n 20 /var/log/syslog

The tail command shows the end of a file. By default, it displays the last 10 lines:

Terminal
tail /var/log/syslog

Use the -f option to watch a log file update in real time:

Terminal
tail -f /var/log/syslog

Creating files (touch command)

The touch command updates the timestamps of existing files and directories, and creates new, empty files.

To create a file , specify the file name as an argument:

Terminal
touch file.txt

If the file already exists, touch updates its last access and modification times to the current time.

Creating directories (mkdir command)

In Linux, you can create new directories (also known as folders) with the mkdir command.

To create a directory, type mkdir followed by the directory name:

Terminal
mkdir /tmp/newdirectory

mkdir can take one or more directory names as its arguments.

If the argument is a directory name, without the full path, the new directory is created in the current working directory.

To create parent directories, use the -p option:

Terminal
mkdir -p Projects/linuxize.com/src/assets/images

This command creates the entire directory structure in a single step.

When mkdir is invoked with the -p option, it creates the directory only if it doesn’t exist.

A symbolic link (or symlink) is a special type of file that points to another file or directory.

To make a symbolic link to a file, use the ln command with the -s option. The first argument is the file you want to link to, and the second is the name of the symlink:

Terminal
ln -s source_file symbolic_link

If only one file is given as an argument, ln creates a link to that file in the current working directory with the same name as the file it points to.

Removing files and directories (rm command)

Use the rm command to delete directories and folders.

By default, when executed without any options, rm doesn’t remove directories. It also doesn’t prompt the user to confirm whether to proceed with removing the given files.

To delete a file or symlink, type rm followed by the file name:

Terminal
rm file.txt

rm accepts one or more file or directory names as its arguments.

The -i option tells rm to prompt the user for each given file before removing it:

Terminal
rm -i file.txt
output
rm: remove regular empty file 'file.txt'?

Use the -d option to remove one or more empty directories:

Terminal
rm -d dirname

To remove non-empty directories and all the files within them recursively, use the -r (recursive) option:

Terminal
rm -rf dirname

The -f option tells rm never to prompt the user and to ignore nonexistent files and arguments.

Warning
Be very careful with rm -rf, because it will permanently delete files without asking you first.

Copying files and directories (cp command)

The cp command allows you to copy files and directories.

To copy a file in the current working directory, use the source file as a first argument and the new file as the second:

Terminal
cp file file_backup

To copy a file to another directory, specify the absolute or the relative path to the destination directory. When only the directory name is specified as the destination, the copied file will retain the original file’s name.

Terminal
cp file.txt /backup

By default, if the destination file already exists, it will be overwritten.

To copy a directory, including all its files and subdirectories, use the -R or -r option:

Terminal
cp -R Pictures /opt/backup

Moving and renaming files and directories (mv command)

The mv command (short for move) is used to rename and move files and directories from one location to another.

For example, to move a file to a directory, you would run:

Terminal
mv file.txt /tmp

To rename a file, you need to specify the destination file name:

Terminal
mv file.txt file1.txt

The syntax for moving directories is the same as for moving files.

To move multiple files and directories at once, specify the destination directory as the last argument:

Terminal
mv file.txt file1.txt /tmp

Searching for Files and Text

Finding files (find command)

The find command allows you to search for files and directories based on various criteria such as name, type, size, and modification time.

To find a file by name in the current directory and its subdirectories:

Terminal
find . -name "filename.txt"

To find all directories named config:

Terminal
find /etc -type d -name "config"

To find all files larger than 100MB:

Terminal
find / -type f -size +100M

To find files modified in the last 7 days:

Terminal
find /home -mtime -7

Searching text in files (grep command)

The grep command searches for patterns in files. It’s one of the most useful and popular Linux commands.

To search for a string in a file:

Terminal
grep "search_term" filename.txt

To search recursively in all files within a directory:

Terminal
grep -r "search_term" /path/to/directory

To perform a case-insensitive search, use the -i option:

Terminal
grep -i "search_term" filename.txt

To display line numbers with matching lines, use the -n option:

Terminal
grep -n "error" /var/log/syslog

To show only the file names containing the match, use the -l option:

Terminal
grep -l "TODO" *.py

System Information

Displaying system information (uname command)

The uname command displays information about the system.

To display all system information:

Terminal
uname -a
output
Linux ubuntu 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

To display only the kernel name:

Terminal
uname -s

To display the kernel version:

Terminal
uname -r

Checking disk space (df command)

The df (disk free) command displays the amount of available disk space on file systems.

To display disk space in a human-readable format:

Terminal
df -h
output
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       100G   45G   50G  48% /
/dev/sdb1       500G  200G  275G  43% /home

To display information about a specific file system:

Terminal
df -h /home

Checking directory size (du command)

The du (disk usage) command estimates the space used by files and directories.

To display the size of a directory in human-readable format:

Terminal
du -sh /var/log

To display the size of each subdirectory:

Terminal
du -h --max-depth=1 /home

Checking memory usage (free command)

The free command displays the amount of free and used memory in the system.

To display memory in a human-readable format:

Terminal
free -h
output
              total        used        free      shared  buff/cache   available
Mem:           15Gi       4.2Gi       6.8Gi       512Mi       4.5Gi        10Gi
Swap:         2.0Gi          0B       2.0Gi

Monitoring system processes (top and htop commands)

The top command provides a real-time view of running processes and system resource usage:

Terminal
top

Press q to exit the top command.

The htop command is an interactive process viewer with a more user-friendly interface:

Terminal
htop
Info
htop may not be installed by default. Install it using sudo apt install htop on Debian/Ubuntu or sudo dnf install htop on Fedora/CentOS.

Network Commands

Testing network connectivity (ping command)

The ping command is used to test the reachability of a host on an IP network.

To ping a host:

Terminal
ping google.com

To send a specific number of packets, use the -c option:

Terminal
ping -c 5 google.com

Press Ctrl+C to stop the ping command.

Downloading files (wget and curl commands)

The wget command is used to download files from the Internet.

To download a file:

Terminal
wget https://example.com/file.tar.gz

To save the file with a different name:

Terminal
wget -O newname.tar.gz https://example.com/file.tar.gz

The curl command is a versatile tool for transferring data from or to a server.

To download a file and save it:

Terminal
curl -O https://example.com/file.tar.gz

To display the contents of a URL:

Terminal
curl https://example.com

Displaying network configuration (ip command)

The ip command is used to show and manipulate network interfaces, routing, and tunnels.

To display all network interfaces:

Terminal
ip addr show

To display the routing table:

Terminal
ip route show

Working with Archives

Creating and extracting archives (tar command)

The tar command creates and extracts archive files.

To create a tar archive:

Terminal
tar -cvf archive.tar /path/to/directory

To create a compressed tar.gz archive:

Terminal
tar -czvf archive.tar.gz /path/to/directory

To extract a tar archive:

Terminal
tar -xvf archive.tar

To extract a tar.gz archive:

Terminal
tar -xzvf archive.tar.gz

To extract to a specific directory:

Terminal
tar -xzvf archive.tar.gz -C /path/to/destination

Working with zip archives (zip and unzip commands)

The zip command creates compressed zip archives:

Terminal
zip archive.zip file1.txt file2.txt

To zip a directory recursively:

Terminal
zip -r archive.zip /path/to/directory

The unzip command extracts zip archives:

Terminal
unzip archive.zip

To extract to a specific directory:

Terminal
unzip archive.zip -d /path/to/destination

Installing and Removing Packages

A package manager is a tool that allows you to install, update, remove, and otherwise manage distro-specific software packages.

Different Linux distributions have different package managers and package formats.

Only root or a user with sudo privileges can install and remove packages.

Ubuntu and Debian (apt command)

Advanced Package Tool, or APT, is a package management system used by Debian-based distributions.

There are several command-line package management tools in Debian distributions, with apt and apt-get being the most used ones.

Before installing a new package, first you need to update the APT package index:

Terminal
apt update

The APT index is a database that holds records of available packages from the repositories enabled in your system.

To upgrade all installed packages to their latest versions, run:

Terminal
apt upgrade

Installing packages is as simple as running:

Terminal
apt install package_name

To remove an installed package , enter:

Terminal
apt remove package_name

CentOS and Fedora (dnf command)

RPM is a powerful package management system used by Red Hat Linux and its derivatives, such as CentOS and Fedora. RPM also refers to the rpm command and the .rpm file format.

To install a new package on Red Hat based distributions, you can use either the yum or dnf commands:

Terminal
dnf install package_name

Starting from CentOS 8, dnf replaced yum as the default package manager. dnf is backward compatible with yum.

To upgrade all installed packages to their latest versions, type:

Terminal
dnf update

Removing packages is as simple as:

Terminal
dnf remove package_name

File Ownership and Permissions

In Linux, access to the files is managed through file permissions, attributes, and ownership. This ensures that only authorized users and processes can access files and directories.

In Linux, each file is associated with an owner and a group and assigned permission access rights for three different classes of users:

  • The file owner.
  • The group members.
  • Everybody else.

Three permission types apply to each class:

  • The read permission.
  • The write permission.
  • The execute permission.

This concept allows you to specify which users can read, write, or execute the file.

To view the file owner and permissions, use the ls -l command.

Changing permissions (chmod command)

The chmod command allows you to change the file permissions. It works in two modes, symbolic and numeric.

In numeric mode, you can set permissions for the owner, the group, and all others. Each write, read, and execute permissions have the following number value:

  • r (read) = 4
  • w (write) = 2
  • x (execute) = 1
  • no permissions = 0

The permission number of a specific user class is represented by the sum of the values of the permissions for that group.

For example, to give the file’s owner read and write permissions and only read permissions to group members and all other users, you would run:

Terminal
chmod 644 filename

Only the root user, the file owner, or someone with sudo privileges can change file permissions.

To recursively operate on all files and directories under a given directory, use the chmod command with the -R, (–recursive) option:

Terminal
chmod -R 755 dirname

Be extra careful when recursively changing the files’ permissions.

Changing ownership (chown command)

The chown command lets you change the user and group ownership of a given file, directory, or symbolic link.

To change a file’s owner, type chown, the new owner’s username, and the file name:

Terminal
chown username filename

To change both the owner and the group of a file, invoke the chown command followed by the new owner and group separated by a colon (:) with no intervening spaces and the target file:

Terminal
chown username:groupname filename

Use the -R (--recursive) option, to recursively operate on all files and directories under the given directory:

Terminal
chown -R username:groupname dirname

Elevate privileges (sudo command)

The sudo command allows you to run programs as another user, by default, the root user. If you spend a lot of time on the command line, sudo is one of the commands you will use quite frequently.

Using sudo instead of logging in as root is more secure because you can grant limited administrative privileges to individual users without them knowing the root password.

To use sudo, just put sudo before your command:

Terminal
sudo command

Managing Users and Groups

Linux is a multi-user system, meaning more than one person can interact with the same system at the same time. Groups are used to organize and administer user accounts. The primary purpose of groups is to define a set of privileges, such as read, write, or execute permissions, for a given resource shared among the users in the group.

Creating users (useradd and passwd Commands)

The useradd command allows you to create new users.

To create a new user account, type useradd followed by the username:

Terminal
useradd newuser

Once the user is created, set the user password with the passwd command:

Terminal
passwd newuser

Removing users (userdel Command)

In Linux, you can remove a user account with the userdel command.

To delete a user account, pass the user name to the userdel command:

Terminal
userdel newuser

Use the -r (–remove) option to delete the user’s home directory and mail spool:

Terminal
userdel -r newuser

Managing groups (groupadd and groupdel Command)

To create a new group, invoke the groupadd command followed by the group name:

Terminal
groupadd mygroup

To remove a group, use the groupdel command with the group name as an argument:

Terminal
groupdel mygroup

Adding users to groups (usermod Command)

To add an existing user to a group, use the usermod command followed by the -G option and the name of the group:

Terminal
usermod -a -G sudo linuxize

Quick Reference

Here’s a summary of all the commands we covered in this article:

CommandDescription
manDisplay manual page for a command
pwdPrint current working directory
cdChange directory
lsList directory contents
catDisplay file contents
headDisplay first lines of a file
tailDisplay last lines of a file
touchCreate empty file or update timestamps
mkdirCreate directories
lnCreate symbolic links
rmRemove files and directories
cpCopy files and directories
mvMove or rename files and directories
findSearch for files
grepSearch text in files
unameDisplay system information
dfDisplay disk space usage
duDisplay directory size
freeDisplay memory usage
topDisplay running processes
pingTest network connectivity
wgetDownload files from the web
curlTransfer data from/to servers
ipDisplay network configuration
tarCreate and extract archives
zipCreate zip archives
unzipExtract zip archives
aptPackage manager for Debian/Ubuntu
dnfPackage manager for Fedora/CentOS
chmodChange file permissions
chownChange file ownership
sudoExecute command as root
useraddCreate new user
userdelDelete user
passwdChange user password
groupaddCreate new group
groupdelDelete group
usermodModify user account

Conclusion

We have covered some of the most used GNU/Linux commands for file management, text searching, system information, networking, archiving, package management, and user administration.

Although you can perform most of the development and system-related tasks using a graphical interface, the command line makes you more productive and able to get more done in less time.

Click the links for each command to learn more about their options and how to use them.

If you have any questions or feedback, feel free to leave a comment.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page