DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published at yuscode.hashnode.dev

Chapter 2 - Basic Linux Commands

Image description

Introduction

Linux commands form the backbone of working with a Linux system. Mastering these commands allows you to efficiently navigate and manipulate files, directories, and system resources. In this chapter, we will cover the most essential commands for file and directory operations.

Table of Contents

Commands for File and Directory Operations

ls

The ls command is used to list files and directories within the file system.

ls
Enter fullscreen mode Exit fullscreen mode
  • ls -/: Lists all files and directories at the root of the file system.

      ls /
    
  • ls -l: Provides a detailed listing including file permissions, number of links, owner, group, size, and timestamp.

    ls -l
    
  • ls -a: Shows all files, including hidden files.

    ls -a
    

cd

The cd command is used to change the current directory.

cd [directory]
Enter fullscreen mode Exit fullscreen mode
  • cd ~: Changes to the current user's home directory.

    cd ~
    
  • cd ..: Moves up one directory level.

    cd ..
    

pwd

pwd stands for "print working directory". This command displays the current directory.

pwd
Enter fullscreen mode Exit fullscreen mode

touch

The touch command is used to create an empty file or update the timestamp of an existing file.

touch file1.txt
Enter fullscreen mode Exit fullscreen mode

cat

The cat command is used to view the contents of a file.

cat [file_name]
Enter fullscreen mode Exit fullscreen mode
  • cat file1.txt : Displays the content of file1.txt.

    cat file1.txt
    

mkdir

The mkdir command is used to create new directories.

mkdir [directory_name]
Enter fullscreen mode Exit fullscreen mode
  • To create a nested folder structure in Linux, you can use the mkdir command with the -p option like this:

     mkdir -p directory-1/directory-2/directory-3/directory-4
    

    The -p option tells mkdir to create the parent directories as needed.

rmdir

The rmdir command is used to remove empty directories.

rmdir [directory_name]
Enter fullscreen mode Exit fullscreen mode

cp

The cp command is used to copy files or directories.

cp [source] [destination]
Enter fullscreen mode Exit fullscreen mode
  • cp file1.txt /home/user: Copies file1.txt to the /home/user directory.

     cp file1.txt /home/user
    
  • cp -r dir1 /home/user : Recursively copies the directory dir1 and its contents to /home/user. Recursively as used in this case means to copy everything in the specified directory (dir1), including all its contents, subdirectories, and sub-subdirectories, and so on, all the way down.

    cp -r dir1 /home/user
    

mv

The mv command is used to move or rename files or directories.

mv [source] [destination]
Enter fullscreen mode Exit fullscreen mode
  • mv file1.txt /home/user : Moves file1.txt to the /home/user directory.

    mv file1.txt /home/user
    
  • mv old_name.txt new_name.txt : Renames old_name.txt to new_name.txt.

    mv old_name.txt new_name.txt
    

rm

The rm command is used to remove files or directories.

rm [file_name]
Enter fullscreen mode Exit fullscreen mode
  • rm file1.txt : Deletes file1.txt.

    rm file1.txt
    
  • rm -r dir1 : Recursively deletes the directory dir1 and its contents.

    rm -r dir1
    

Globbing

Globbing is a way to find files on your computer that match a certain pattern. Think of it like a search function, but instead of searching for a specific word or phrase, you can use special characters called "wildcards" to match many files at once. Globbing is useful because it saves you time and effort when working with files. You can use globbing to:

  • Find and delete many files at once

  • Copy or move many files to a new location

  • Run scripts or programs on many files at once

Wildcards

In globbing, "wildcards" are special characters that represent one or more characters in a file name. Here are the most common wildcards:

  • * (asterisk): matches any characters (including none).

  • [abc] (square brackets): matches any character inside the brackets (e.g., a, b, or c).

  • [a-z] (square brackets with a range): matches any character in the range (e.g., a to z).

Below are example codes for each of the wildcards listed above:

  • * (asterisk):

    ls *.txt
    

    The command above lists all files ending with .txt

  • [abc] (square brackets):

    ls [ab]*.txt
    

    The command above lists files starting with a or b and ending with .txt (e.g., a_file.txt, b_file.txt)

  • [a-z] (square brackets with a range):

    ls [a-z]*.txt
    

    The command above lists files starting with any lowercase letter and ending with .txt (e.g., a_file.txt, z_file.txt)

    NB: These examples use the ls command to list files, but you can replace ls with other commands like rm, cp, or mv to perform different actions on the matched files.

Conclusion

Mastering basic Linux commands is essential for navigating and managing a Linux system effectively. These commands form the foundation of working with files, directories, and system resources, empowering you to perform various tasks efficiently.

Feel free to leave comments and share this article. Follow my blog for more insights on Linux!

Top comments (0)