September 28, 2023

Mastering the Command Line: A Guide to Basic and Intermediate Linux Commands

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

In the world of operating systems, Linux stands as a venerable giant. Renowned for its versatility, security, and the unparalleled power it offers to its users, Linux has become the go-to choice for many developers, sysadmins, and tech enthusiasts. Yet, for newcomers, the Linux command line interface (CLI) can appear as an impenetrable fortress, guarded by cryptic commands and arcane syntax. Here, I will introduce you to a selection of fundamental and intermediate CLI commands that will help you become proficient with the Linux operating system.

Exploring the Command Line Interface

The Command Line Interface (CLI) is a powerful tool that allows users to interact with a computer or server using text-based commands. Unlike graphical user interfaces (GUIs), which rely on visual elements like icons and buttons, the CLI lets you perform tasks by typing commands directly into a terminal window.

One of the key advantages of the CLI is its efficiency and flexibility. With just a few keystrokes, you can navigate the file system, manipulate files and directories, and perform system administration tasks.

In this article, we’ll embark on a journey to explore the CLI, starting with the fundamentals and gradually moving on to more advanced commands.

Basic Linux Commands

Let me introduce some fundamental Linux commands such as ls, cd, pwd, mkdir, touch, rm and cp.

  1. ls (List):
  • ls is used to list files and directories in the current directory.
  • Example: ls will display a list of files and directories in the current folder.

2. cd (Change Directory):

  • cd is used to change the current working directory.
  • Example: cd /home/user will change the current directory to "/home/user."

3.pwd (Print Working Directory):

  • pwd prints the current working directory to the terminal.
  • Example: pwd will display the path to the current directory.

4. mkdir (Make Directory):

  • mkdir is used to create a new directory (folder).
  • Example: mkdir myfolder will create a directory named "myfolder" in the current directory.

5. touch:

  • touch is used to create an empty file.
  • Example: touch myfile.txt will create an empty file named "myfile.txt."

6. rm (Remove):

  • rm is used to remove files and directories.
  • Be cautious when using rm as it can permanently delete files.
  • Example to remove a file: rm myfile.txt
  • Example to remove a directory and its contents: rm -r myfolder

7. cp (Copy):

  • cp is used to copy files or directories from one location to another.
  • Example to copy a file: cp file1.txt file2.txt (copies "file1.txt" to "file2.txt").
  • Example: To copy a directory and its contents: cp -r sourcedir destinationdir (recursively copies "sourcedir" to "destinationdir").

8. passwd (change password):

To change your own password, simply type passwd followed by your username.

For example:

passwd your_username

You’ll be prompted to enter your current password, and then to enter the new password twice for confirmation.

Changing Another User’s Password (Requires Superuser or Root Privileges):

To change the password of another user, you need superuser or root privileges. Use sudo before passwd to change another user's password.

For example:

sudo passwd your_username

Intermediate Linux Commands

Let us see some Intermediate Linux Commands such as grep, find, chmod, chown, ps, kill, and tar.

  1. grep:
  • Purpose: grep stands for "Global Regular Expression Print." It is used for searching and processing text within files or the output of other commands. grep allows you to find specific patterns or keywords in text data.
  • Common Usage: Searching for specific words in log files, filtering command output, and pattern matching.

Grep Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores case for matching.
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern.
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE).
-w : Match whole word.
-o : Print only the matched parts of a matching line, with each such part on a separate output line.

-A n : Prints searched line and n 0lines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

2. find:

  • Purpose: The find command is used for searching files and directories within a directory hierarchy based on various criteria, such as file name, size, modification time, and more.
  • Common Usage: Locating files or directories, performing batch operations on files, and managing disk space.

Options:

  • -exec CMD: The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.
  • -ok CMD : It works same as -exec except the user is prompted first.
  • -inum N : Search for files with inode number ‘N’.
  • -links N : Search for files with ’N’ links.
  • -name demo : Search for files that are specified by ‘demo’.
  • -newer file : Search for files that were modified/created after ‘file’.
  • -perm octal : Search for the file if permission is ‘octal’.
  • -print : Display the path name of the files found by using the rest of the criteria.
  • -empty : Search for empty files and directories.
  • -size +N/-N : Search for files of ’N’ blocks; ’N’ followed by ‘c’can be used to measure the size in characters; ‘+N’ means size > ’N’ blocks and ‘-N’ means size < ’N’ blocks.
  • -user name : Search for files owned by username or ID ‘name’.
  • \(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
  • ! expr : True if ‘expr’ is false.
Listing all the directories, creating new directories. Finding and deleting the empty directories.

3. chmod:

  • Purpose: chmod (change mode) is used to modify file or directory permissions. It allows you to control who can read, write, and execute files and directories.
  • Common Usage: Managing file and directory permissions to enhance security and access control.

User Level permissions

These operations control permissions on the user level. Here’s the commands you can use:

  • u – Grant permission to a user.
  • g – Grant permission to a group (A Group of users).
  • o – Grant permission to others (who do not come under either of the above).

File Level permissions

These control permissions on the file level.

  • r – Grants read permission.
  • w – Grant write permission.
  • x – Grant execute permission.
bash file content
chmod execution command line

4. chown:

  • Purpose: chown (change owner) is used to change the ownership of files and directories. It allows you to change the user and group ownership of files and directories.
  • Common Usage: Changing ownership of files after copying or moving, especially when changing users or administrators

Linux Chown Command Syntax

The basic chown command syntax consists of a few segments. The help file shows the following format:

chown [OPTIONS] USER[:GROUP] FILE(s)

  • [OPTIONS] — the command can be used with or without additional options.
  • [USER] — the username or the numeric user ID of the new owner of a file.
  • [:] — use the colon when changing a group of a file.
  • [GROUP] — changing the group ownership of a file is optional.
  • FILE — the target file.

Check the ownership of Linux files and directories

ls -l

Change the ownership of Linux files and directories

5. ps:

  • Purpose: The ps command is used for listing running processes on a Linux system. It provides information about active processes, their statuses, and resource usage.
  • Common Usage: Monitoring system processes, troubleshooting performance issues, and identifying resource-hungry processes.
`ps` to list the process
`ps aux` command to show all the proccess

6. kill:

  • Purpose: The kill command is used to send signals to processes. It allows you to terminate or control running processes gracefully.
  • Common Usage: Stopping unresponsive or misbehaving processes, managing services, and performing system maintenance.

7. tar:

  • Purpose: The tar command is used for creating and manipulating archive files. It can compress and bundle files and directories into a single archive file.
  • Common Usage: Creating backups, transferring files, and distributing software packages.

The tar command provides the following options −

  • -c − This creates an archive file.
  • -x − The option extracts the archive file.
  • -f − Specifies the filename of the archive file.
  • -v − This prints verbose information for any tar operation on the terminal.
  • -t − This lists all the files inside an archive file.
  • -u − This archives a file and then adds it to an existing archive file.
  • -r − This updates a file or directory located inside a .tar file.
  • -z − Creates a tar file using gzip compression.
  • -j − Create an archive file using the bzip2 compression.
  • -W − The -w option verifies an archive file.
Create a compressed tar file
Extract a tar file

Conclusion

In conclusion, mastering the command line and becoming proficient in basic and intermediate Linux commands is a crucial skill for anyone working with Linux-based systems. These commands provide the foundation for efficiently managing files, directories, processes, and system configurations. They empower users to automate tasks, troubleshoot issues, and maintain the security and stability of their Linux environments.


CodeStax.Ai
Profile
September 28, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles