Top 15 Linux Commands Every Beginner Should Know
Tags: linux, terminal, ubuntu, shell
Published: 2024-07-01 18:16:48

## 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
        
2. File Operations
    
    * touch
        
    * cp
        
    * mv
        
    * rm
        
    * cat
        
    * nano
        
3. Permissions and Ownership
    
    * chmod
        
    * chown
        
4. Searching and Finding Files
    
    * find
        
    * grep
        

---

## Basic Commands

### `pwd` - Print Working Directory

Syntax:

```bash
pwd [options]
```

This command id used to print the **current working directory**.

Examples:

1. Basic usage:
    

```bash
pwd
```

This will print the current working directory.

2. Using the`-L` option:
    

```bash
pwd -L
```

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

### `ls` - List Directory Contents

Syntax:

```bash
ls [options] [directory]
```

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

Example:

```bash
ls -l /
```

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:

```bash
cd [options] [directory]
```

`cd` command allows you to **change your current working directory**.

Example:

```bash
cd -P /home/user/Documents
```

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:

```bash
mkdir [options] directory
```

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

Examples:

```bash
mkdir dir1
```

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.

```bash
mkdir dir1 dir2 dir3
```

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

### `rmdir` - Remove Directory

Syntax:

```bash
rmdir [options] directory
```

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

Example:

```bash
rmdir -v dir1
```

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:

```bash
touch [options] filename
```

`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:

```bash
touch test.txt
```

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:

```bash
cp [options] [source] [destination]
```

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

Example:

```bash
cp file1.txt file2.txt file3.txt testdir
```

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:

```bash
mv [options] [sourceFile] [destinationFile]
```

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

```bash
mv [options] [sourceDir] [destinationDir]
```

This syntax is used when **renaming/moving a directory**.

```bash
mv [options] [filename] [destinationDir]
```

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

Examples:

```bash
mv file1 file2
```

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

```bash
mv dir1 dir2
```

This example renames the directory `dir1` to be `dir2` .

```bash
mv dir1 /path/to/new/location/
```

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

```bash
mv file.txt /path/to/new/location/
```

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

### `rm` - Remove Files or Directories

Syntax:

```bash
rm [options] filename/directory
```

The `rm` command **deletes files and directories**.

Example:

```bash
rm file.txt
```

This command removes the file `file.txt` .

```bash
rm -r dir1
```

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:

```bash
cat [options] [filename]
```

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:

```bash
cat -n filename
```

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:

```bash
nano [options] filename
```

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

Example:

```bash
nano file1
```

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:

```bash
chmod [options] [mode] [filename]
```

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

Example:

```bash
chmod u+x myfile
```

* `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:

```bash
chown [options] [owner][:group] filename(s)
```

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

Example:

```bash
chown -R user:group mydirectory
```

* `-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:

```bash
find [path] [options] [expression]
```

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

Example:

```bash
find / -name '*test*'
```

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:

```bash
grep [options] pattern [files]
```

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

Example:

```bash
grep "test" file.txt
```

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!