DEV Community

Cover image for Top 15 Linux Commands Every Beginner Should Know
Mennatallah Ashraf
Mennatallah Ashraf

Posted on

Top 15 Linux Commands Every Beginner Should Know

Why is it important to learn Linux commands?

Learning Linux commands is essential for anyone who wants to use the terminal effectively. If you are a beginner interested in fields where Linux is important, this article is perfect for you. Mastering these commands will make you more efficient and give you a deeper understanding of how your system works. So, let's dive in and explore the fundamental Linux commands that every aspiring professional should know!

In this article, we will learn about:

  1. Basic Commands
* pwd

* ls

* cd

* mkdir

* rmdir
Enter fullscreen mode Exit fullscreen mode
  1. File Operations
* touch

* cp

* mv

* rm

* cat

* nano
Enter fullscreen mode Exit fullscreen mode
  1. Permissions and Ownership
* chmod

* chown
Enter fullscreen mode Exit fullscreen mode
  1. Searching and Finding Files
* find

* grep
Enter fullscreen mode Exit fullscreen mode

Basic Commands

pwd - Print Working Directory

Syntax:

pwd [options]
Enter fullscreen mode Exit fullscreen mode

This command id used to print the current working directory.

Examples:

  1. Basic usage:
pwd
Enter fullscreen mode Exit fullscreen mode

This will print the current working directory.

  1. Using the-L option:
pwd -L
Enter fullscreen mode Exit fullscreen mode

This will print the logical current working directory, showing the path as it appears logically, including any symbolic links.

ls - List Directory Contents

Syntax:

ls [options] [directory]
Enter fullscreen mode Exit fullscreen mode

ls command is used to display the contents of the current directory.

Example:

ls -l /
Enter fullscreen mode Exit fullscreen mode

The command ls -l / in Linux lists the contents of the root directory / in long format.

  • -l: An option to list the contents in long format, providing detailed information about each file and directory.

  • /: The root directory of the filesystem.

cd - Change Directory

Syntax:

cd [options] [directory]
Enter fullscreen mode Exit fullscreen mode

cd command allows you to change your current working directory.

Example:

cd -P /home/user/Documents
Enter fullscreen mode Exit fullscreen mode

This command will take you to /mnt/storage/Documents, the physical location to which /home/user/Documents points. The output of pwd (print working directory) will show /mnt/storage/Documents instead of /home/user/Documents.

  1. -P: will change the directory to the actual location that /home/user/Documents points to.

  2. /home/user/Documents: This is the target directory to which you want to change.

mkdir - Make Directory

Syntax:

mkdir [options] directory
Enter fullscreen mode Exit fullscreen mode

mkdir is used to create a directory if not already exist.

Examples:

mkdir dir1
Enter fullscreen mode Exit fullscreen mode

The previous example creates a new directory named dir1 in the current working directory. If a directory named dir1 already exists in the current location, you will get an error message indicating that the directory already exists.

mkdir dir1 dir2 dir3
Enter fullscreen mode Exit fullscreen mode

This creates multiple directories named dir1 , dir2 and dir3 in the current working directory.

rmdir - Remove Directory

Syntax:

rmdir [options] directory
Enter fullscreen mode Exit fullscreen mode

The rmdir command in Unix/Linux is used to remove empty directories.

Example:

rmdir -v dir1
Enter fullscreen mode Exit fullscreen mode
  1. -v: This is an option that stands for "verbose". In this case, it will print a message for each directory that is successfully removed.

  2. dir1: This is the name of the directory you want to remove. The directory must be empty for rmdir to succeed.

But what if I want to remove a non-empty directory?🤔

In the following section, we will learn how to do that using the rm command.


File Operations

touch - Create an Empty File

Syntax:

touch [options] filename
Enter fullscreen mode Exit fullscreen mode

touch command is used to create empty files. You can update the modification and access time of each file with the help of the touch command.

Example:

touch test.txt
Enter fullscreen mode Exit fullscreen mode

This example is used to create a new file named test.txt or update the timestamp of an existing file.

cp - Copy Files or Directories

Syntax:

cp [options] [source] [destination]
Enter fullscreen mode Exit fullscreen mode

You use the cp command in Linux to copy files and directories from one location to another.

Example:

cp file1.txt file2.txt file3.txt testdir
Enter fullscreen mode Exit fullscreen mode

This command copies multiple files (file1.txt, file2.txt, file3.txt) into the directory testdir.

mv - Move or Rename Files and Directories

The mv command can be used for various purposes, such as:

mv [options] [sourceFile] [destinationFile]
Enter fullscreen mode Exit fullscreen mode

We use this syntax if we want to rename a file.

mv [options] [sourceDir] [destinationDir]
Enter fullscreen mode Exit fullscreen mode

This syntax is used when renaming/moving a directory.

mv [options] [filename] [destinationDir]
Enter fullscreen mode Exit fullscreen mode

If we want to move a file we use this syntax.

Examples:

mv file1 file2
Enter fullscreen mode Exit fullscreen mode

The previous example changes the name of the file file1 to be file2 .

mv dir1 dir2
Enter fullscreen mode Exit fullscreen mode

This example renames the directory dir1 to be dir2 .

mv dir1 /path/to/new/location/
Enter fullscreen mode Exit fullscreen mode

Here we are changing the location of dir1 to /path/to/new/location/ .

mv file.txt /path/to/new/location/
Enter fullscreen mode Exit fullscreen mode

This example moves the file file.txt to /path/to/new/location/ .

rm - Remove Files or Directories

Syntax:

rm [options] filename/directory
Enter fullscreen mode Exit fullscreen mode

The rm command deletes files and directories.

Example:

rm file.txt
Enter fullscreen mode Exit fullscreen mode

This command removes the file file.txt .

rm -r dir1
Enter fullscreen mode Exit fullscreen mode

Another useful use of the rm command is that it can remove non-empty directories. As shown in the example, it removes the directory dir1 when we use the -r option with it.

cat - Concatenate and Display Files

Syntax:

cat [options] [filename]
Enter fullscreen mode Exit fullscreen mode

The cat (concatenate) command in Linux displays file contents. It reads one or multiple files and prints their content to the terminal. cat is used to view file contents, combine files, and create new files.

Example:

cat -n filename
Enter fullscreen mode Exit fullscreen mode

This command displays the contents of filename with line numbers.

  • -n: Option to number all output lines, starting from 1.

nano - Edit File with Nano Text Editor

Syntax:

nano [options] filename
Enter fullscreen mode Exit fullscreen mode

Nano is a simple, easy-to-use text editor available in many Unix-based operating systems, including Linux.

Example:

nano file1
Enter fullscreen mode Exit fullscreen mode

This example opens the Nano text editor with file1 loaded for editing. After modifying file1, if you want to save the changes, press Ctrl + O. To exit Nano, press Ctrl + X.


Permissions and Ownership

chmod - Change File Mode

Syntax:

chmod [options] [mode] [filename]
Enter fullscreen mode Exit fullscreen mode

The chmod command is used to change the permissions of files and directories.

Example:

chmod u+x myfile
Enter fullscreen mode Exit fullscreen mode
  • u+x: This specifies the permission change:

    • u: Refers to the user (owner) of the file.
    • +: Indicates that you are adding a permission.
    • x: Represents the execute permission.

After executing this command, the owner of myfile will be able to execute it as a program or script.

chown - Change File Owner and Group

Syntax:

chown [options] [owner][:group] filename(s)
Enter fullscreen mode Exit fullscreen mode

We use chown command to change the owner and group of a file or directory.

Example:

chown -R user:group mydirectory
Enter fullscreen mode Exit fullscreen mode
  • -R: This option stands for "recursive." It means that the command will apply the ownership change not only to the specified directory (mydirectory) but also to all files and subdirectories within it.

  • user:group: This specifies the new owner and group for the directory and its contents.

    • user: This is the new owner of the files and directories.
    • group: Refers to the new group of the files and directories.

This example changes the owner of mydirectory and all files and subdirectories within mydirectory to user and the group to group.


Searching and Finding Files

find - Search for Files in a Directory

Syntax:

find [path] [options] [expression]
Enter fullscreen mode Exit fullscreen mode

The find command helps you locate files, and not just by their names.

Example:

find / -name '*test*'
Enter fullscreen mode Exit fullscreen mode

In this example find searches for files and directories containing the word "test" anywhere in their names, starting from the root directory (/).

  • /: The directory to start the search from (root directory).

  • -name: Option to specify the name pattern to search for.

  • '*test*': The pattern to match. The asterisks (*) are wildcards that match any number of characters before and after "test".

grep - Search Inside Files

Syntax:

grep [options] pattern [files]
Enter fullscreen mode Exit fullscreen mode

The grep command in Linux is used to search for patterns within files.

Example:

grep "test" file.txt
Enter fullscreen mode Exit fullscreen mode

Here we search for the string "test" within the file file.txt.


Conclusion

Learning these top 15 Linux commands is a great start for any beginner. They help you move around the system, manage files, and check what's going on with your computer. Getting comfortable with these basics will make using Linux a lot easier and more fun.

Remember, this is just the beginning. There are many more commands and tools to discover. Keep exploring, and don't be afraid to try new things. The Linux community is always there to help with plenty of tutorials and guides.

Happy learning, and enjoy your Linux journey!

Top comments (0)