Mastering The Linux Command Line/Terminal

Mastering The Linux Command Line/Terminal

Over the years, Linux has undergone a remarkable transformation. From an initially intimidating platform, it has evolved into a user-friendly environment. What was once considered daunting is now accessible, with improved documentation, intuitive interfaces, and a supportive community. Before we dive in, let's explore why we need to look at lines of text in a terminal when we have the good old graphical interface.

  1. Efficiency: Command-line operations often require fewer system resources, allowing for quicker execution of tasks.

  2. Precision: The command line provides precise control over system configurations and file manipulations, allowing users to execute commands with specific parameters.

  3. Automation: Scripts and commands can be automated, streamlining repetitive tasks and saving time.

  4. Remote Management: Command-line tools are essential for managing Linux servers remotely, where graphical interfaces may not be available or practical.

  5. Learning Curve: While it may seem intimidating initially, becoming familiar with the command line is empowering and deepens your understanding of how the system operates.

Fun Fact: Most of the servers worldwide run on Linux, particularly in web hosting, cloud computing, and enterprise environments. So you might as well get on with it. It's actually really fun once you get the hang of it.

Let's get started, shall we?

Terminal vs Shell

Terminal:

The terminal is a software program that provides a text-based interface for users to interact with the operating system. It allows users to enter commands and receive text-based output. Basically, it provides an interface to interact with the shell.

Shell:

The shell is a command interpreter that translates user inputs (commands) into actions performed by the operating system. It is a software program that provides an interface for users to interact with the operating system's kernel. The shell interprets the commands and executes them.

If you have a local Linux installation or you're on a virtual machine, you'll have a software preinstalled named something close to the "Terminal". Now go ahead and open it. You should have something similar to this.

Commands, Options and Arguments

  • The basic syntax of a Linux command is command [options] [arguments]

  • A command is an instruction given to the computer's operating system to perform a specific task or operation.

  • Options modify the behavior of a command and are usually preceded by a hyphen (-).

  • Arguments are the inputs provided to a command to act upon. They can be files, directories, or any other data required for the command to execute.

  • While command is the mandatory field, options and arguments depend on the command you are using.

Now, let's explore the various types of commands commonly used in Linux.

Directory Commands

These commands are for navigating and managing directories and files.

cd - Change Directory

  • This command is used to change directory. The syntax is cd [path].

      ansuman@fedora:~$ cd Downloads/
      ansuman@fedora:~/Downloads$
    
  • cd takes you to the home directory.

      ansuman@fedora:~$ cd Documents/Programming/
      ansuman@fedora:~/Documents/Programming$ cd
      ansuman@fedora:~$
    
  • cd .. moves up one directory. You can use cd ../.. to move up multiple directories.

      ansuman@fedora:~$ cd ..
      ansuman@fedora:/home$ cd ../..
      ansuman@fedora:/$
    
  • cd - takes you back to the previous working directory.

      ansuman@fedora:~/Downloads$ cd -
      /home/ansuman
      ansuman@fedora:~$
    

ls - List Items

  • This command is frequently used to display information about directories. The syntax is ls [options] [directory_path]

      ansuman@fedora:~$ ls
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
      ansuman@fedora:~$
    
  • ls /path/to/directory shows information in that particular directory.

      ansuman@fedora:~$ ls Downloads/
      file1
      ansuman@fedora:~$
    
  • ls -a lists all files, including hidden ones that start with a dot(.).

  • ls -l lists long format, providing detailed information including file permissions, owner, group, size, and modification time.

  • ls -h lists human-readable sizes, displaying file sizes in a more readable format (e.g., KB, MB, GB).

  • ls -d list directories but not their contents.

  • You can combine multiple options together. For example, ls -lah

      ansuman@fedora:~$ ls -lah
      total 656K
      drwx------. 1 ansuman ansuman  964 Feb  2 19:51 .
      drwxr-xr-x. 1 root    root      14 Dec 30 15:50 ..
      -rw-------. 1 ansuman ansuman 1.3K Feb  2 19:50 .bash_history
      -rw-r--r--. 1 ansuman ansuman   18 Jul 19  2023 .bash_logout
      -rw-r--r--. 1 ansuman ansuman  144 Jul 19  2023 .bash_profile
      -rw-r--r--. 1 ansuman ansuman  522 Jul 19  2023 .bashrc
      drwx------. 1 ansuman ansuman  558 Jan 31 11:26 .cache
      drwxr-xr-x. 1 ansuman ansuman  646 Jan 31 11:15 .config
      drwxr-xr-x. 1 ansuman ansuman    0 Dec 30 15:50 Desktop
      ...
    

pwd - See Current Directory

  • This command displays the current working directory, indicating the absolute path to the directory where the user is located in the file system.

      ansuman@fedora:~/Documents$ pwd
      /home/ansuman/Documents
      ansuman@fedora:~/Documents$
    

mkdir - Create Directory

  • The mkdir command, short for "make directory," is used to create new directories (folders). The syntax is mkdir [options] directory_name.

      ansuman@fedora:~$ mkdir TestDir
      ansuman@fedora:~$ ls
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  TestDir  Videos
    
  • You can create multiple directories with mkdir dir1 dir2 dir3.

  • mkdir -p creates parent directories as needed. If the parent directory doesn't exist, it will be created.

      ansuman@fedora:~$ mkdir -p Downloads/New/Test
      ansuman@fedora:~$ cd Downloads/
      ansuman@fedora:~/Downloads$ ls
      New
      ansuman@fedora:~/Downloads$ cd New
      ansuman@fedora:~/Downloads/New$ ls
      Test
    

rmdir - Remove Directory

  • This command is used to remove directories (folders) only if they are empty. The syntax is rmdir [options] directory_name.

      ansuman@fedora:~$ ls
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  TestDir  Videos
      ansuman@fedora:~$ rmdir TestDir/
      ansuman@fedora:~$ ls
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
    
  • rmdir -p removes the directory and its parent directories if they become empty after removal.

      ansuman@fedora:~/Downloads$ ls
      New
      ansuman@fedora:~/Downloads$ rmdir -p New/Test/
      ansuman@fedora:~/Downloads$ ls
      ansuman@fedora:~/Downloads$       #The directory NEW got deleted
    

File Operations:

File operation commands in Linux are essential for managing files, directories, and their contents.

touch - Create Empty Files

  • This command is used primarily to create empty files. The syntax is touch [options] filename.

      ansuman@fedora:~/Hashnode$ touch newfile
      ansuman@fedora:~/Hashnode$ ls
      newfile
      ansuman@fedora:~/Hashnode$touch newfile.txt #this is also possible
      ansuman@fedora:~/Hashnode$ ls
      newfile newfile.txt
    
  • It can also update the access and modification timestamps of the file without changing its content.

      ansuman@fedora:~/Hashnode$ touch newfile
      ansuman@fedora:~/Hashnode$ ls -l
      total 0
      -rw-r--r--. 1 ansuman ansuman 0 Feb  4 10:10 newfile
      ansuman@fedora:~/Hashnode$ touch -c newfile
      ansuman@fedora:~/Hashnode$ ls -l
      total 0
      -rw-r--r--. 1 ansuman ansuman 0 Feb  4 10:14 newfile #modification time changed
    

cat - Display Content or Concatenate

  • This command is used to concatenate files and display its content. The syntax is cat [options] [file(s)].

      ansuman@fedora:~/Hashnode$ ls
      file1  newfile2
      ansuman@fedora:~/Hashnode$ cat file1 file2
      This is a file.
      This is the second file.
    
  • cat is also used to just display the content of a file.

      ansuman@fedora:~/Hashnode$ ls
      file1
      ansuman@fedora:~/Hashnode$ cat file1
      This is a file.
    
  • To concatenate multiple files, use this syntax cat file2 file2 file3 > NewFile.

      ansuman@fedora:~/Hashnode$ ls
      file1  file2
      ansuman@fedora:~/Hashnode$ cat file1 file2 > NewFile
      ansuman@fedora:~/Hashnode$ ls
      file1 file2 NewFile  
      ansuman@fedora:~/Hashnode$ cat NewFile
      This is a file.
      This is the second file.
    
  • Use options -n to show content with line numbers.

      ansuman@fedora:~/Hashnode$ cat -n NewFile
           1  This is a file.
           2  This is the second file.
      ansuman@fedora:~/Hashnode$
    

cp - Copy Files

  • The cp command in Linux is used for copying files and directories from one location to another. The syntax is cp [options] source destination

  • We can copy a single file, multiple files or a directory to another directory/location.

      ansuman@fedora:~/Hashnode$ ls
      newfile  NewFile  newfile2
      ansuman@fedora:~/Hashnode$ cp newfile ~/Downloads
      ansuman@fedora:~/Hashnode$ cd ~/Downloads/
      ansuman@fedora:~/Downloads$ ls
      LINUX.png  newfile
    
      ansuman@fedora:~/Hashnode$ ls
      newfile  NewFile  newfile2
      ansuman@fedora:~/Hashnode$ cp newfile newfile2 ~/Downloads/ #multiple files
      ansuman@fedora:~/Hashnode$ cd ~/Downloads/
      ansuman@fedora:~/Downloads$ ls
      LINUX.png  newfile  newfile2
    
      ansuman@fedora:~$ cp -r ~/Hashnode/ ~/Downloads/
      ansuman@fedora:~$ cd Downloads/
      ansuman@fedora:~/Downloads$ ls
      Hashnode newfile  newfile2
    
  • The options -r, -R are used to Copy Directories Recursively.

  • -i is used for interactive mode. It gives a prompt before overwriting existing files.

      ansuman@fedora:~/Hashnode$ cp -i newfile2 ~/Downloads/
      cp: overwrite '/home/ansuman/Downloads/newfile2'? Y
      ansuman@fedora:~/Hashnode$
    

mv - Move Files

  • The mv command is used to move files or directories from one location to another or to rename them within the same directory. The syntax is mv [options] source destination.

  • Similar to cp, we can move a single file, multiple files or directories. We can use option -i to enter interactive prompt before moving files.

      ansuman@fedora:~/Hashnode$ ls
      newfile  NewFile  newfile2
      ansuman@fedora:~/Hashnode$ mv NewFile ~/Downloads/
      ansuman@fedora:~/Hashnode$ ls
      newfile  newfile2
      ansuman@fedora:~/Hashnode$ ls ~/Downloads/
      Hashnode  LINUX.png  newfile  NewFile  newfile2
    
  • We can rename existing files with command mv old_filename new_filename

      ansuman@fedora:~/Hashnode$ ls
      newfile  newfile2
      ansuman@fedora:~/Hashnode$ mv newfile renamedfile
      ansuman@fedora:~/Hashnode$ ls
      newfile2  renamedfile
      ansuman@fedora:~/Hashnode$
    

rm - Remove/Delete Files:

  • The rm command is used to remove or delete files and directories. This command permanently deletes files, and should be approached with caution. The syntax is rm [options] file(s) or directory(s).

      ansuman@fedora:~/Hashnode$ ls
      newfile2  renamedfile
      ansuman@fedora:~/Hashnode$ rm renamedfile
      ansuman@fedora:~/Hashnode$ ls
      newfile2
    
  • Delete Multiple files with rm file1 file2.

      ansuman@fedora:~/Downloads$ ls
      Hashnode newfile NewFile newfile2
      ansuman@fedora:~/Downloads$ rm newfile newfile2 NewFile
      ansuman@fedora:~/Downloads$ ls
      Hashnode
    
  • Delete Directory with rm -r directory/. The -r option is used for recursive removal. Basically, it deletes the file structure recursively.

      ansuman@fedora:~/Downloads$ ls
      Hashnode
      ansuman@fedora:~/Downloads$ rm -r Hashnode/
      ansuman@fedora:~/Downloads$ ls
      ansuman@fedora:~/Downloads$
    
  • More options for this command are, -i which is used for interactive mode and the -f that forcefully deletes a file/directory. Be careful while using the -r & -f options to avoid accidental removal.

      ansuman@fedora:~$ ls
      Desktop  Documents  Downloads  Hashnode  Music  Pictures  Public  Templates  Videos
      ansuman@fedora:~$ rm -rf Hashnode/
      ansuman@fedora:~$ ls
      Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
      ansuman@fedora:~$
    

more and less - View File Contents Page by Page:

  • The more and less commands are used for viewing the content of text files.

  • Difference between these two commands are: more allows only forward navigation using the spacebar. less allows both forward and backward navigation and provides additional search capabilities.

  • less is faster that more and has a search option which makes it easier to find things you are looking for.

  • Basic syntax is less filename and more filename.

Text Manipulation:

Text manipulation commands are powerful tools for processing and transforming text data.

grep - Search for Patterns

  • grep is useful for extracting information from text files based on specified patterns. The syntax is grep [options] pattern file(s).

  • This command searches for the specified pattern in the given file and prints lines containing the pattern.

      ansuman@fedora:~$ grep "linux" file.txt
      This will be used for using linux commands.
      ansuman@fedora:~$
    
  • We can also search for the pattern in multiple files and print matching lines along with the filename.

      ansuman@fedora:~$ grep linux file.txt file2.txt
      file.txt:This will be used for using linux commands.
      file2.txt:This contains a linux file.
      ansuman@fedora:~$
    
  • -i performs a case-insensitive search.

  • -n displays line numbers along with matching lines.

  • -r, -R searches for the pattern recursively in all files within the specified directory.

  • -v displays lines that do not contain the specified pattern.

  • -w matches whole words, not sub-strings.

      ansuman@fedora:~$ grep "bash" -in .vimrc
      79:" Make wildmenu behave like similar to Bash completion.
      ansuman@fedora:~$
    

sed - Stream Editor

  • The sed command, short for "stream editor," is a powerful tool for text stream processing and manipulation. It allows you to perform text transformations on an input stream (a file or input from a pipeline) and produce an output stream. The syntax is sed [options] 'script' filename.

      ertainly! ansuman@fedora:~$ cat file.txt
      It is a file.
      It contains some content.
      ansuman@fedora:~$ sed -i 's/It/This/g' file.txt #-i is to replace it in place
      ansuman@fedora:~$ cat file.txt
      This is a file.
      This contains some content.
      ansuman@fedora:~$
    
  • We can delete lines matching a certain pattern with sed 'pattern/d' file.txt.

      ansuman@fedora:~$ cat file.txt
      This is a file.
      This contains some content.
      This is divided into some lines.
      This will be used for using linux commands.
      ansuman@fedora:~$ sed '/linux/d' file.txt
      This is a file.
      This contains some content.
      This is divided into some lines.
      ansuman@fedora:~$
    

awk - Pattern Scanning and Processing Language

The basic usage of awk involves specifying patterns and actions to be performed on each line of a file. The general syntax is:

awk 'pattern { action }' filename

Here are some basic examples:

  1. Print Entire Lines:

     awk '{ print }' filename
    

    Prints the entire content of each line in the file.

  2. Print Specific Columns:

     awk '{ print $1, $3 }' data.txt
    

    Prints the first and third columns of each line in the file.

  3. Filter Lines Based on a Pattern:

     awk '/pattern/ { print }' filename
    

    Prints lines containing the specified pattern.

  4. Print Lines Longer Than a Certain Length:

     awk 'length($0) > 10' textfile.txt
    

    Prints lines with a length greater than 10 characters.

  5. Calculate and Print Sum of a Column:

     awk '{ sum += $2 } END { print "Sum:", sum }' numbers.txt
    

    Calculates and prints the sum of the values in the second column.

User and Permissions:

In Linux and Unix-like operating systems, user management and permissions play a crucial role in maintaining security and access control. Here are some key concepts related to users and permissions:

  • ls - View Permission

      ansuman@fedora:~$ ls -l file.txt
      -rw-r--r--. 1 ansuman ansuman 121 Feb  5 12:35 file.txt
      ansuman@fedora:~$
    

    Displays detailed information about the file, including its owner, group, and permissions.

  • chmod - Changing Permissions:

    Changes the permissions of a file. Permissions can be represented numerically (e.g., 755) or symbolically (e.g., u=rwx,g=rx,o=r). The basic syntax of the chmod command is as follows:

      chmod permissions file(s) or directory(s)
    

    Here, permissions represent the new permissions you want to set, and file(s) or directory(s) specify the target file or directory.

    Symbolic Representation:

    • u (user): Represents the owner of the file or directory.

    • g (group): Represents the group associated with the file or directory.

    • o (others): Represents users who are neither the owner nor part of the group.

    • a (all): Represents all users (u + g + o).

Numeric Representation:

  • Read (4): Assigned the value 4.

  • Write (2): Assigned the value 2.

  • Execute (1): Assigned the value 1.

The sum of these values represents the permissions. For example, read and write (4 + 2 = 6), read and execute (4 + 1 = 5), etc.

  1. Symbolic Representation:

     ansuman@fedora:~$ ls -l file.txt
     -rw-r--r--. 1 ansuman ansuman 121 Feb  5 12:35 file.txt
     ansuman@fedora:~$ chmod u=r,go=rwx file.txt
     ansuman@fedora:~$ ls -l file.txt
     -r--rwxrwx. 1 ansuman ansuman 121 Feb  5 12:35 file.txt
     ansuman@fedora:~$
    

    Sets read permissions for the owner/user and read, write & execute permissions for the group and others on "file.txt."

  2. Numeric Representation:

     ansuman@fedora:~$ ls -l file.txt
     -rw-r--r--. 1 ansuman ansuman 121 Feb  5 12:35 file.txt
     ansuman@fedora:~$ chmod 777 file.txt
     ansuman@fedora:~$ ls -l file.txt
     -rwxrwxrwx. 1 ansuman ansuman 121 Feb  5 12:35 file.txt
     ansuman@fedora:~$
    

chown - changing ownership

  • The chown command in Linux is used to change the owner and/or group of a file or directory. It allows system administrators to transfer ownership or change group assignments, which is important for managing permissions and access control. The Basic syntax is chown [options] new_owner[:new_group] file(s) or directory(s). Here, new_owner is the new owner name or UID, and new_group is the new group name or GID. If new_group is omitted, the file's group remains unchanged.

  • The chown command in Linux is used to change the owner and/or group of a file or directory. It allows system administrators to transfer ownership or change group assignments, which is important for managing permissions and access control. The basic syntax of the chown command is as follows:

      chown [options] new_owner[:new_group] file(s) or directory(s)
    

    Here, new_owner is the new owner name or UID, and new_group is the new group name or GID. If new_group is omitted, the file's group remains unchanged. Here are some examples.

    1. Change Owner:

       chown new_owner myfile.txt
      

      Changes the owner of "myfile.txt" to the user with the username or UID specified by new_owner.

    2. Change Owner and Group:

       chown new_owner:new_group myfile.txt
      

      Changes both the owner and group of "myfile.txt" to the specified values.

    3. Change Group Only:

       chown :new_group myfile.txt
      

      Changes only the group of "myfile.txt" to the specified group.

Options:

  • -c or --changes:

    • Only report changes. Displays a message for each changed file.
  • -v or --verbose:

    • Be verbose. Shows the files that are changed.

These are the commands mostly used in Linux. Mastering these will improve your expertise in Linux. Although, these are not all the commands you will get in Linux, but these are more than enough to get you started with it. Hopefully you liked this one. See you in the next one.

Did you find this article valuable?

Support Ansuman Satapathy by becoming a sponsor. Any amount is appreciated!