Stackify is now BMC. Read theBlog

Top Linux Commands: 11 You Need to Know

By: Stackify Team
  |  August 13, 2024
Top Linux Commands: 11 You Need to Know

If you’re a software developer, you can’t avoid Linux. Created by Linus Torvalds, Linux is the preferred OS for most servers and the environment in which many popular development tools run best. While you don’t need to know everything about Linux, learning the top Linux commands is a valuable investment of your time.

With this post, you’ll have a handy guide to the Linux commands most often used. Starting with a quick introduction to Linux commands, we’ll dive right into the list of top commands, in a very hands-on way. Before wrapping up, we give you a cheat sheet of the top commands, so you can bookmark it and use it as reference.

Prerequisites

To be able to follow this guide, you’ll need access to a Linux system. If you are not familiar with Linux, I suggest you download Ubuntu and run it in live mode on a USB stick. Many people consider Ubuntu to be the most user-friendly Linux distro, and running it without installing it is a nice, no-strings-attached way to get to know the system.

If you’re on Windows, there’s an even easier alternative: use WSL (Windows Subsystem for Linux).

Let’s get started.

Introduction to Linux Commands and Syntax

If you have some familiarity working with the command line—even if it’s the Windows Command Prompt or Powershell—you might want to skip this section and go directly to “What Are the Basic Commands in Linux.”

So, what are Linux commands, and how do they work in practice? First, you need to know where to type them. If you’re running Ubuntu, search for an application called “Terminal.” In case you’re using WSL, just open Windows Terminal and start a new WSL tab.

Linux commands are usually small words or sequences of letters. You type them into your terminal and press the Enter key. That’s it. You might get some visual output from the command, but not always. If something goes wrong, the command will display an error message.

Also, many commands allow you to pass options to them, which can alter their behavior. For instance, after running the ls command inside a folder that contains files with different extensions, you’ll see a list of those files.

But let’s say I only wanted to see the .pdf files. I could do this:

ls *.pdf

And as a result, I’d only see the .pdf files.

It’s also possible to chain commands to one another, sending—or “piping,” as people usually call it—the output of one command to the input of the next, creating flexible and powerful pipelines for text manipulation and other tasks.

What Are the Basic Commands in Linux?

Linux commands perform a wide variety of tasks, including text manipulation, file management, and networking. We’ll now cover some of the most commonly used commands, in no particular order. You’ll see each command’s explanation, followed by one or more examples.

Interestingly, many Linux commands have additional “side effects” beyond their primary function. For example, the touch command’s primary purpose is to update a file’s access and modification timestamps. However, if the specified file doesn’t exist, touch will create a new, empty file. As a result, many people associate touch with creating blank files.

In this guide, we’ll highlight these popular side effect usages in the headings, while the detailed explanations will cover the original purposes of the commands.

ls – List contents

As you’ve seen, the ls command lists the content of the current directory. Some examples:

List the contents of the current directory

ls

Or list the contents of the current directory with the extension .java

ls *.java

List all files, including hidden ones

ls -a

List the contents of a child directory ls subfoldername

clear – Clear the screen

The clear command has a self-explanatory name: it clears the screen of all the outputs from previous commands.

cd – Change directory

This is the command you use to change directories. In other words, to move to or from a given path.

Examples:

Move to the user home directory

cd ~

Move to the specified path

cd /home/johndoe/somefolder

Or move one level up from the current directory

cd ..

Go back to the previous directory

cd -

mkdir – Create a directory

You use mkdir to create one or more new directories.

Examples:

Create a new directory

mkdir new-folder

Try to create the same one again and get the error

mkdir new-folder
mkdir: cannot create directory ‘new-folder’: File exists

Create three new directories

mkdir folder1 folder2 folder3

Create a whole path of directories

mkdir -p root/child/grandchild

rm – Remove files or directories

rm is the command you use to delete files and directories.

Examples:

Remove a single file

rm file.txt

Remove an empty directory

rm -d folder

Or, remove a non-empty directory and all its contents, recursively.

rm -r non-empty-folder

touch – Create a blank file

The touch command is meant to change the access and modification times of the specified file to the time of execution. If the specified file doesn’t exist, touch creates it. That’s why this command is often used to create empty files.

Example:

Create an empty file

touch yet-another-file.txt

cp – Copy files or directories

You use the cp command when you want to copy files or directories.

Examples:

Copy a file

# Create an empty file named "file1.txt"
touch file1.txt


# List the contents of the current directory
ls
# Output: file1.txt


# Copy "file1.txt" to a new file named "file2.txt"
cp file1.txt file2.txt


# List the contents of the current directory again
ls
# Output: file1.txt  file2.txt

Copy a directory and its contents

cp -r cpdemo cpdemo2

mv – Move or rename files

You use the mv command to move or rename files. The command is the same because, from the OS’s point of view, the operation is identical.

Example:

Rename a single file

mv original-name.txt new-name.txt

pwd – Displays the current directory

pwd stands for print working directory, and that’s what it does: it tells you the path where you’re currently at.

Example:

pwd
/home/username/demo/cpdemo2

echo – Write something to the standard output

The echo command displays something to the standard output and is very useful to display messages when creating scripts to automate administrative tasks. You can also pipe the output of echo to a file, which results in a file being created with that content.

Examples:

Display a message

echo Hello, world!

Create a file with the given content

echo content > new-file.md

cat – Display the contents of a file

The cat command is meant to concatenate files and display the result to the standard output. If only one file is specified, the command then displays its contents to the standard output. That’s why people often refer to cat as the command to display a file, forgetting its concatenation capability.

Examples:

Display the content of a file

echo Hello, world! > new-file.md
cat new-file.md
Hello, world!

Concatenate two files and displays the resulting content

echo Hello > hello.txt
 echo ", World!" > world.txt
 cat hello.txt world.txt
Hello
, World!

Concatenate two files and sends the result to a new file

cat hello.txt world.txt  > helloworld.txt
cat helloworld.txt
Hello
, World!

Top Linux Commands Cheat Sheet

CommandDescriptionExample
lsList directory contentsls -l
clearClear the terminal screenclear
cdChange directorycd /path/to/directory
mkdirCreate a new directorymkdir newdir
rmRemove files or directoriesrm -r directory_name
touchCreate an empty file or update the timestamp of an existing filetouch newfile.txt
cpCopy files or directoriescp file.txt /path/to/destination
mvMove or rename files or directoriesmv oldname.txt newname.txt
pwdPrint working directorypwd
echoDisplay a line of textecho “Hello, World!”
catConcatenate and display filescat file.txt
Keeping a helpful list of commands nearby will improve your efficiency, while you learn more commands.

Conclusion

There are many Linux commands, and most can be used with options that elevate the number of variations to staggering numbers. In this post, we’ve shown you some of the most important commands, but we’ve barely scratched the surface of all Linux has to offer.

For instance, an important command that didn’t make it to our list is the tail command: it allows you to display the last 10 lines of a file to standard output. The tail command also has an option that, when used, keeps appending data to the output as the file grows. This command is very useful when handling log files. And speaking of logs, we also suggest this article on Linux logs.

Finally, to keep all your applications running on Linux performing at their best, check out Stackify Retrace. Better still, start your free trial of Stackify Retrace today and see how full lifecycle APM helps you maintain code quality and performance.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]