Oct 27, 2023 11 min read

Basic Linux Commands

Learn Basic Linux Commands with our step-by-step tutorial. Common Linux commands used daily by Linux system administrators.

Basic Linux Commands
Table of Contents

Introduction

Before we begin talking about Basic Linux Commands, let's briefly understand – What is Linux Command?

Working with the command line might be intimidating for new Linux users who are coming from the Windows environment. However, using it is not that challenging. You only need to learn a few basic commands to start using the command line.

Linux commands are instructions given to the Linux operating system through the command-line interface (CLI) to perform various tasks and operations. These commands can be used to manage files, directories, processes, users, networks, and more. Linux commands provide powerful functionality and flexibility for administering and interacting with the operating system.

Despite the fact that most Linux distributions are user-friendly and have a simple graphical interface, being able to use the command line can be quite helpful. The command line gives you more control over your system and access to features that a graphical interface does not provide.

In this tutorial, you will understand basic command in Linux. We will also address a few FAQs on Basic Linux Commands.

Getting Information About the Command

In most cases, it is unnecessary and even time-consuming to memorize command options. Usually, if you do not use a command often, you will probably forget its options.

Most commands include a --help option that prints a short message explaining how to use the command and then exits:

command_name --help

The man command

Almost all Linux commands come with man pages. A man or manual page is a type of documentation that describes the function of a command, provides examples of how to use it, and specifies the arguments it will accept.

The manual page for a specific command can be viewed using the man command.

man command_name

For instance, you would enter the following to see the man page for the cd command:

man cd

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

In Linux, the root directory, which is the first or top-most directory in the directory tree, contains every file and directory. A single leading slash / indicates the root directory.

You can use either an absolute or a relative path to the resource while navigating the file system or operating on files.

The relative path begins in your current directory, while the absolute or full path begins at the system root /.

Current Working Directory (pwd command)

The directory that the user is presently working in is known as the current working directory. You are operating within a directory each time you interact with your command prompt.

To find the directory you are currently in, use the pwd command:

pwd

The command shows your current working directory's path:

Output

/home/vegastack

Changing directory (cd command)

In Linux and other Unix-like operating systems, the current working directory is changed using the cd (“change directory”) command.

cd will bring you to your home directory when used without any argument:

cd

You can change to a directory by using its absolute or relative pathname.

You can access the directory Downloads by using the relative path to the directory, presuming it is present in the directory from which you execute the command:

cd Downloads

You can also use the absolute path of a directory to navigate to it:

cd /home/vegastack/Downloads

The parent directory, or more specifically, the directory directly above the current one, is denoted by two dots (..), one after the other.

Let us say you are currently in the /usr/local/share directory.  You would enter the following command to switch to the /usr/local directory (one level up from the current directory) :

cd ../

To go up two levels:

cd ../../

Use the dash (-) character as an argument to switch back to the previous working directory:

cd -

If the directory you want to go to contains spaces, you need to either enclose the path in quotes or escape the space using the backslash () character:

cd Dir\ name\ with\ space

Working with Files and Directories

Listing directory contents (ls command)

The ls command displays details about the files and directories that are present in a directory.

ls lists all file names in the current working directory in alphabetical order when used without any arguments or options:

ls

Pass the directory's path as an argument to list files in that particular directory:

ls /usr

The ls command's default output just displays the names of the files and directories. To print files in long listing format, use the -l option:

ls -l /etc/hosts

Filename, size, date, owner, group, permissions, number of hard links, file type, and other information are included in the output:

Output

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

By default, the ls command does not display the hidden files. Any file that starts with a period is considered a hidden file (.).

Use the -a option to see all files, including hidden ones:

ls -a ~/

Displaying file contents (cat command)

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

Pass the file name to cat as an argument to have the contents of the file displayed on the screen:

cat /etc/hosts

Creating files (touch command)

The touch command is used to create new, empty files as well as to update the timestamps on already-existing files and directories.

Specify the file name as an argument to create a file:

touch file.txt

touch will update the file's last access and modification times to the current time if the file already exists.

Creating directories (mkdir command)

The mkdir command in Linux can be used to create new directories, which are also known as folders.

To create a directory, provide the directory name as the command's argument:

mkdir /tmp/newdirectory

mkdir accepts one or more directory names as arguments.

The new directory is created in the current working directory if the argument is a directory name without the full path.

Use the -p option to create parent directories:

mkdir -p Projects/vegastack.com/src/assets/images

The entire directory structure is created by the command mentioned above.

When mkdir is executed with the -p option, it only creates the directory if it does not already exist.

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

To make a symbolic link to a specific file, use the ln command with the -s option, the file name as the first argument, and the symbolic link name as the second argument:

ln -s source_file symbolic_link

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

Removing files and directories (rm command)

The rm command can be used to delete files and directories.

rm does not remove directories by default when executed without any options. It also does not prompt the user on whether to proceed with the removal of the given files.

The rm command must be used with the file name as an argument to delete a file or symlink:

rm file.txt

rm takes one or more file or directory names as arguments.

The -i option directs rm to prompt the user before removing each specified file:

rm -i file.txt
Output

rm: remove regular empty file 'file.txt'?

To remove one or more empty directories, use the -d option:

Use the -r (recursive) option to remove non-empty directories and all the files contained within them:

rm -rf dirname

The -f option tells rm to never ask the user and to disregard nonexistent files and arguments.

Copying files and directories (cp command)

You can copy directories and files using the cp command.

Use the source file as the first argument and the new file as the second argument to copy a file into the current working directory:

cp file file_backup

Specify the absolute or relative path to the destination directory to copy a file to another directory.

cp file.txt /backup

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

Use the -R or -r option to copy a directory, together with all of its files and subdirectories:

cp -R Pictures /opt/backup

Moving and renaming files and directories (mv command)

To rename and move files and directories from one location to another, use the mv command (short for move).

Run the following command, for instance, to move a file to a directory:

mv file.txt /tmp

You must specify the destination file name when renaming a file:

mv file.txt file1.txt

Moving directories and moving files both use the same syntax.

Specify the destination directory as the last argument to move many files and directories at once:

mv file.txt file1.txt /tmp

Installing and Removing Packages

A package manager is a utility that lets you install, update, uninstall, and otherwise manage distro-specific software packages.

Different package managers and package formats are used by different Linux distributions.

Packages can only be installed and removed by root or a user with sudo privileges.

Ubuntu and Debian (apt command)

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

Debian distributions have a number of command-line package management tools, with apt and apt-get being the most popular ones.

You must first update the APT package index before installing a new package:

sudo apt update

The APT index is a database that keeps track of available packages from the repositories that have been enabled on your system.

Run the following command to update the installed packages to the most recent versions:

sudo apt upgrade

Running the following commands will install packages:

apt install package_name

To remove an installed package, type:

apt remove package_name

CentOS and Fedora (dnf command)

RPM is a comprehensive package management system used by Red Hat Linux and its derivatives including CentOS and Fedora. RPM also pertains to the rpm command and .rpm file format.

On Red Hat-based distributions, you can use the yum or dnf commands to install a new package:

dnf install package_name

Since CentOS 8, dnf has been the default package manager, replacing yum. dnf and yum are backward compatible.

Type the following to update the installed packages to the newest versions:

dnf update

It is easy to remove packages by doing the following:

dnf remove package_name

File Ownership and Permissions

Access to files on Linux is managed through file permissions, attributes, and ownership. In this way, files and directories are protected so that only authorized users and processes have access.

Each file in Linux has an owner, a group, and permission access rights assigned to it for three separate classes of users:

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

Each class has three types of permissions:

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

You can specify which users can read the file, write to the file, or execute the file using this concept.

Use the ls -l command to view the file owner and permissions.

Changing permissions (chmod command)

You can modify a file's permissions by using the chmod command. It operates in both symbolic and numeric modes.

You can specify permissions for the owner, group, and all others when in numeric mode. Each write, read, and execute permission has the following numeric value:

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

The sum of the values of the permissions for that group serves as a representation of the permissions number for a given user class.

For instance, you would execute the following command to provide read and write permissions to the file's owner and read-only permissions to group members and all other users.

chmod 644 filename

The only people who can alter a file's permissions are root, the file's owner, or users who have sudo privileges.

Use the chmod command with the -R, (-recursive), option to recursively operate on all files and directories under a specific directory:

chmod -R 755 dirname

Be extra cautious when recursively modifying the files’ permissions.

Changing ownership (chown command)

You can change the user and group ownership of a specific file, directory, or symbolic link by using the chown command.

Use the chown command, the target file, followed by the user name of the new owner to change a file's owner:

chown username filename

When changing a file's owner and group, use the chown command followed by the target file, the new owner and group separated by a colon (:), and no spaces in between:

chown username:groupname filename

To recursively act on all files and directories under the specified directory, use the -R (--recursive) option:

chown -R username:groupname dirname

Elevate privileges (sudo command)

Using the sudo command, you can execute programs as another user, by default the root user. sudo is one of the commands that you will use quite often if you spend a lot of time on the command line.

It is more secure to use sudo as compared to logging in as root since you can give specific users limited administrative privileges without disclosing the root password.

Prefix the command with sudo to use sudo:

sudo command

Managing Users and Groups

Linux is a multi-user operating system, which allows multiple users to interact with the system at once. User accounts are organized and managed using groups. Groups are primarily used to specify a set of privileges, such as reading, writing, or executing permission for a specific resource shared among the users within the group.

Creating users (useradd and passwd Commands)

By using the useradd command, you can add new users.

Use the useradd command and the username to create a new user account:

useradd newuser

After the user has been created, run the passwd command to set the user password:

passwd newuser

Removing users (userdel Command)

The userdel command in Linux allows you to remove a user account.

Passing the user name to the userdel command will delete the specified user account:

userdel newuser

To remove the user's home directory and mail spool, use the -r (-remove) option:

userdel -r newuser

Managing groups (groupadd and groupdel Command)

Invoke the groupadd command followed by the group name to create a new group:

groupadd mygroup

Use the groupdel command with the group name as an argument to remove a group:

groupdel mygroup

Adding users to groups (usermod Command)

Use the usermod command, the -G option, and the group name to add an existing user to the group:

usermod -a -G sudo vegastack

FAQs on Basic Linux Commands

How do I list files and directories in Linux? 

You can use the ls command to list files and directories in the current working directory. Adding options like -l provides more detailed information.

How do I change directories in Linux? 

The cd command is used to change directories. Simply specify the desired directory, either with an absolute path or a relative path from the current directory.

What command do I use to create a new directory in Linux? 

To create a new directory, use the mkdir command followed by the desired directory name. For example, mkdir my_directory will create a directory named "my_directory".

How can I copy files in Linux? 

The cp command is used to copy files in Linux. Specify the source file and the destination directory or filename. For example, cp file.txt /path/to/destination will copy "file.txt" to the specified location.

How do I delete files and directories in Linux? 

To delete files, you can use the rm command followed by the file name. For directories, use rm -r followed by the directory name. Exercise caution as files/directories are permanently deleted.

What Linux command is used to search for specific text within a file? 

The grep command is commonly used for searching text within files. Specify the pattern you are looking for and the file(s) to search in. For example, grep "search_text" file.txt.

How can I see a list of running processes in Linux? 

You can use the ps command to display a list of currently running processes. Adding options like -ef provides detailed information about each process.

Conclusion

We have gone over some of the most commonly used GNU/Linux commands.

The majority of development and system-related tasks may be performed using a graphical interface, but the command line increases productivity and enables you to complete more work in less time.

If you have any queries, feel free to post a comment below and we'll be happy to answer them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.