Linux shellcli linux guides

Thanks to Teklan Hosting

A big thank you to Teklan Hosting for providing us with a server. Their support has made it possible for us to create and share this guide with you.

We appreciate Teklan Hosting’s contribution to our project – Give them a look if you need a VPS, Web hosting, Domains or dedicated servers and much more!

Linux is a powerful and flexible operating system that is widely used in various applications, from servers and supercomputers to embedded systems and personal computers. One of the main features of Linux is its command-line interface (CLI), which allows users to interact with the operating system and perform various tasks by entering commands. In this article, we will take a look at some of the most commonly used commands in Linux and how they can be used to manage and maintain a Linux system.

Before we dive into the specific commands, it’s important to understand the structure of a Linux command. Most commands are made up of three parts: the command itself, followed by one or more options and arguments. The options and arguments are used to modify the behavior of the command and provide additional information that the command needs to perform its task.

The command line interface in Linux is often referred to as the “shell.” The shell is a program that interprets the commands that you enter and executes them on the system. There are several different shells available for Linux, with the most common being the Bash shell.

  1. ls – List directory contents

The “ls” command is a commonly used Linux command that allows you to list the files and directories in a directory. By default, it will show you the contents of the current directory, but you can also specify a different directory as an argument.

To use the “ls” command, simply enter “ls” followed by the name of the directory you want to list. For example, to list the files and directories in the current directory, you would enter:

$ ls

To list the contents of a different directory, you can specify the directory as an argument. For example:

$ ls /home/user

This will list the files and directories in the “/home/user” directory.

The “ls” command has a number of options that allow you to customize its behavior. Some of the most commonly used options include:

  • “-l”: This option displays the files and directories in a long format, which includes information such as the file size, owner, group, and permissions.
  • “-a”: This option shows hidden files and directories, which are usually prefixed with a “.” character.
  • “-t”: This option sorts the files and directories by modification time, with the most recently modified ones appearing first.

You can use these options by appending them to the “ls” command. For example:

$ ls -l

This will list the contents of the current directory in a long format.

$ ls -a

This will list the contents of the current directory, including hidden files and directories.

$ ls -t

This will list the contents of the current directory, sorted by modification time.

You can also combine options, like so:

$ ls -l -a -t

This will list the contents of the current directory in a long format, including hidden files and directories, and sorted by modification time.

The “ls” command is an essential tool for navigating and managing files and directories in Linux, and is one of the first commands that most users learn when starting out with the command line.

2. cd – Change directory

The “cd” command is a Linux command that allows you to change the current working directory, or navigate to a different directory on your system. The current working directory is the directory that you are currently in, and it is represented by the “.” character.

To use the “cd” command, simply enter “cd” followed by the name of the directory you want to navigate to. For example:

$ cd /home/user

This will take you to the “/home/user” directory.

There are a few special directories that you can use with the “cd” command:

  • “..”: This represents the parent directory of the current directory. You can use “cd ..” to move up one directory level.
  • “~”: This represents your home directory. You can use “cd ~” to go to your home directory.

You can also use the “cd” command without any arguments to go to your home directory. For example:

$ cd

This will take you to your home directory.

The “cd” command is an essential tool for navigating the file system in Linux, and is one of the first commands that most users learn when starting out with the command line. It is often used in conjunction with other commands, such as “ls” and “pwd”, to explore and manage files and directories on your system.

  1. pwd – Print working directory

The “pwd” command is a Linux command that stands for “print working directory.” It displays the full path of the current working directory, or the directory that you are currently in.

To use the “pwd” command, simply enter “pwd” at the command prompt. For example:

$ pwd

This will display the full path of the current working directory.

The “pwd” command is a simple but useful tool for determining the location of your current working directory. It can be especially helpful when you are working with a large or complex directory structure, and need to know exactly where you are in the file system.

You can use the “pwd” command in conjunction with other commands, such as “cd” and “ls”, to navigate and manage files and directories on your system. For example, you might use it to verify that you are in the correct directory before running a command, or to determine the location of a file or directory relative to the current working directory.

Overall, the “pwd” command is a handy tool to have in your Linux toolkit, and is a useful addition to any user’s command-line repertoire.

  1. mkdir – Create directory

The “mkdir” command is a Linux command that stands for “make directory.” It allows you to create new directories, or folders, on your system.

To use the “mkdir” command, simply enter “mkdir” followed by the name of the directory you want to create. For example:

$ mkdir newdirectory

This will create a new directory called “newdirectory” in the current working directory.

The “mkdir” command has a few options that allow you to customize its behavior. Some of the most commonly used options include:

  • “-p”: This option creates any necessary intermediate directories, rather than just the final directory. For example:
$ mkdir -p newdirectory/subdirectory

This will create the “newdirectory” and “subdirectory” directories if they don’t already exist.

  • “-m”: This option allows you to specify the permissions for the new directory. For example:
$ mkdir -m 755 newdirectory

This will create the “newdirectory” directory with read, execute, and write permissions for the owner, and read and execute permissions for everyone else.

The “mkdir” command is a simple but essential tool for creating and organizing files and directories on your system. It is often used in conjunction with other commands, such as “cd” and “ls”, to navigate and manage the file system in Linux.

  1. rmdir – Remove directory

The “rmdir” command is a Linux command that stands for “remove directory.” It allows you to delete empty directories, or folders, on your system.

To use the “rmdir” command, simply enter “rmdir” followed by the name of the directory you want to delete. For example:

$ rmdir emptydirectory

This will delete the empty directory called “emptydirectory” in the current working directory.

The “rmdir” command only works on empty directories, and will not delete directories that contain files or other directories. If you want to delete a non-empty directory, you can use the “rm” command with the “-r” option, which allows you to delete directories and their contents recursively.

For example:

$ rm -r nonemptydirectory

This will delete the “nonemptydirectory” directory and all of its contents, including any files and subdirectories it contains.

The “rmdir” and “rm” commands are essential tools for managing files and directories in Linux. Be careful when using them, as there is no trash bin or undelete function in Linux, and once a file or directory is deleted, it is permanently gone.

Overall, the “rmdir” command is a simple but useful tool for deleting empty directories on your system

  1. cp – Copy files and directories

The “cp” command is a Linux command that stands for “copy.” It allows you to copy files and directories from one location to another on your system.

To use the “cp” command, enter “cp” followed by the source file or directory and the destination. For example:

$ cp file.txt /home/user

This will copy the file “file.txt” from the current working directory to the “/home/user” directory.

The “cp” command has a number of options that allow you to customize its behavior. Some of the most commonly used options include:

  • “-r”: This option allows you to copy directories and their contents recursively. For example:
$ cp -r directory /home/user

This will copy the “directory” and all of its contents, including any subdirectories and files, to the “/home/user” directory.

  • “-p”: This option preserves the original file’s attributes, such as its permissions and modification time, when copying it. For example:
$ cp -p file.txt /home/user

This will copy “file.txt” to the “/home/user” directory, preserving its attributes in the process.

  • “-u”: This option only copies a file if the destination file is older than the source file, or if the destination file does not exist. For example:
$ cp -u file.txt /home/user

This will copy “file.txt” to the “/home/user” directory if the destination file is older than the source file, or if the destination file does not exist.

The “cp” command is an essential tool for copying and organizing files and directories on your system. It is often used in conjunction with other commands, such as “mv” and “rm”, to manage the file system in Linux.

  1. mv – Move or rename files and directories

The “mv” command is a Linux command that stands for “move.” It allows you to move files and directories from one location to another on your system, or to rename them.

To use the “mv” command to move a file or directory, enter “mv” followed by the source file or directory and the destination. For example:

$ mv file.txt /home/user

This will move the file “file.txt” from the current working directory to the “/home/user” directory.

To use the “mv” command to rename a file or directory, enter “mv” followed by the source file or directory and the new name. For example:

$ mv file.txt newfile.txt

This will rename “file.txt” to “newfile.txt” in the current working directory.

The “mv” command has a number of options that allow you to customize its behavior. Some of the most commonly used options include:

  • “-i”: This option prompts you for confirmation before overwriting an existing file. For example:
$ mv -i file.txt /home/user

This will ask you for confirmation before overwriting an existing file with the same name in the “/home/user” directory.

  • “-f”: This option forces the “mv” command to overwrite an existing file without prompting for confirmation. For example:
$ mv -f file.txt /home/user

This will force “file.txt” to be overwritten in the “/home/user” directory without prompting for confirmation.

The “mv” command is an essential tool for moving and organizing files and directories on your system. It is often used in conjunction with other commands, such as “cp” and “rm”, to manage the file system in Linux.

  1. rm – Remove files and directories

The “rm” command is a Linux command that stands for “remove.” It allows you to delete files and directories from your system.

To use the “rm” command, enter “rm” followed by the name of the file or directory you want to delete. For example:

$ rm file.txt

This will delete the file “file.txt” from the current working directory.

The “rm” command has a number of options that allow you to customize its behavior. Some of the most commonly used options include:

  • “-r”: This option allows you to delete directories and their contents recursively. For example:
$ rm -r directory

This will delete the “directory” and all of its contents, including any subdirectories and files.

  • “-f”: This option forces the “rm” command to delete a file without prompting for confirmation. For example:
$ rm -f file.txt

This will delete “file.txt” without prompting for confirmation.

  • “-i”: This option prompts you for confirmation before deleting a file. For example:
$ rm -i file.txt

This will ask you for confirmation before deleting “file.txt”.

It’s important to note that the “rm” command is a powerful tool, and once a file or directory is deleted with it, it is permanently gone. There is no trash bin or undelete function in Linux, so be careful when using the “rm” command.

Overall, the “rm” command is an essential tool for deleting files and directories on your system, and is a valuable addition to any user’s command-line repertoire.

9. chmod - Change permissions of a file or directory

The “chmod” command is a Linux command that stands for “change mode.” It allows you to change the permissions of files and directories on your system.

In Linux, every file and directory has a set of permissions that control who can read, write, and execute it. The “chmod” command allows you to modify these permissions.

To use the “chmod” command, enter “chmod” followed by the permissions you want to set and the file or directory you want to modify. The permissions are specified using a combination of letters and numbers.

For example:

$ chmod 755 file.txt

This will set the permissions of “file.txt” to 755, which gives the owner read, write, and execute permissions, and gives everyone else read and execute permissions.

The “chmod” command has a number of options that allow you to customize its behavior. Some of the most commonly used options include:

  • “-R”: This option allows you to modify the permissions of a directory and its contents recursively. For example:
$ chmod -R 755 directory

This will set the permissions of the “directory” and all of its contents, including any subdirectories and files, to 755.

  • “-v”: This option displays the names of the files and directories whose permissions are being modified. For example:
$ chmod -v 755 file.txt

This will set the permissions of “file.txt” to 755, and display the name of the file as it is being modified.

The “chmod” command is an essential tool for managing the permissions of files and directories on your system. It is often used in conjunction with other commands, such as “ls” and “chown”, to manage the file system in Linux.

10. man – used to display the manual pages

The manual pages, or “man pages,” contain detailed information about the command, including a description of its syntax and options, as well as examples of how to use it.

To use the “man” command, simply type “man” followed by the name of the command you want to learn about. For example, to view the manual page for the “ls” command, you would enter:

$ man ls

This will display the manual page for the “ls” command in your terminal.

The manual page is divided into sections, each of which covers a different aspect of the command. The sections are identified by capital letters, and the most commonly used sections are:

  • NAME: This section provides a brief description of the command.
  • SYNOPSIS: This section provides a summary of the command’s syntax and options.
  • DESCRIPTION: This section provides a detailed description of the command, including its purpose and how it works.
  • OPTIONS: This section lists and describes the command’s options and their purpose.
  • EXAMPLES: This section provides examples of how to use the command.

To navigate the manual page, you can use the arrow keys or the space bar to scroll up and down.

It’s also worth noting that the “man” command is not only limited to displaying manual pages for command-line utilities. It can also be used to view manual pages for system calls, library functions, and other types of documentation. To view these types of manual pages, you can use a command like “man 2 open” to view the manual page for the “open” system call, or “man 3 strcmp” to view the manual page for the “strcmp” library function.

Overall, the “man” command is a valuable resource for learning about and using the various tools and utilities available in Linux, and is an essential part of any Linux user’s toolkit. So, it’s always a good idea to use the “man” command whenever you are trying to learn about a new command or utility in Linux.

11. grep – Search for a pattern in a file or set of files

grep is a command-line utility for searching plain-text data for lines that match a regular expression. It is a powerful tool for searching through large bodies of text, such as log files, code, and documentation.

The basic syntax for using grep is:

grep [options] pattern [files]

pattern is a regular expression that specifies the text to search for. files is a list of one or more files to search. If no files are specified, grep will search through standard input.

Here are a few common options you can use with grep:

  • -i: Ignore case. This option makes grep perform case-insensitive searches.
  • -v: Invert match. This option makes grep print lines that do not match the search pattern.
  • -w: Match whole words. This option makes grep match only whole words, rather than matching substrings.
  • -n: Print line numbers. This option makes grep include the line number of each matching line in the output.

For example, to search for the word “error” in a file named “log.txt”, you could use the following command:

grep error log.txt

To search for the word “error” in all files in the current directory and its subdirectories, you could use the following command:

grep error *

To search for the word “error” in all files in the current directory and its subdirectories, ignoring case, you could use the following command:

grep -i error *

You can also use grep to search through the output of other commands. For example, to search for the word “error” in the output of the ls command, you could use the following command:

ls | grep error

grep is a very powerful tool, and there are many more options and features available. You can learn more about grep by reading its manual page (man grep) or by consulting online resources.

12. find – Search for files and directories based on various criteria, such as name, size, and modification time.

find is a command-line utility for searching for files and directories in a file system. It is often used to locate files based on their names, types, or other attributes.

The basic syntax for using find is:

find [options] path [expression]

path is the directory where the search should start. expression is an optional argument that specifies the criteria for selecting files. If no expression is given, find will select all files in the specified path.

Here are a few common options you can use with find:

  • -name pattern: Search for files with names that match the specified pattern. The pattern can include wildcards such as * and ?.
  • -type c: Search for files of a specific type. c can be f for regular files, d for directories, l for symbolic links, and so on.
  • -size n[c]: Search for files with a specific size. n is the size in units of c, which can be b for blocks (512 bytes), c for bytes, k for kilobytes, and so on.
  • -mtime n: Search for files that were last modified within a certain number of days. n can be a positive or negative number.

For example, to find all files in the current directory with names that end in “.txt”, you could use the following command:

find . -name "*.txt"

To find all directories in the /usr/local directory and its subdirectories, you could use the following command:

find /usr/local -type d

To find all files in the current directory that are larger than 100MB, you could use the following command:

find . -size +100M

To find all files in the current directory that were modified within the last 7 days, you could use the following command:

find . -mtime -7

Like grep, find is a very powerful tool, and there are many more options and features available. You can learn more about find by reading its manual page (man find) or by consulting online resources.

13. pwd – This command displays the current working directory..

The pwd command is a command line utility that displays the current working directory on a Linux system. It stands for “print working directory” and is commonly used to determine the location of the current directory in the file system.

To use the pwd command, you simply need to open a terminal and type pwd, then press Enter. The command will then display the full path of the current working directory.

For example, if you are currently in the /home/john directory on a Linux system, typing pwd will display the following output:

/home/john

The pwd command is often used in scripts and other automated processes to determine the location of the current directory. It can also be useful for verifying that you are in the correct directory before performing certain actions.

Note that the pwd command may behave differently on different Linux distributions or in different contexts. For example, it may not work if you are logged in remotely using SSH or if you are running the command through a non-interactive shell.

14. whoami – This command displays the current user’s username.

On a Linux system, the whoami command is a built-in utility that displays the username of the current user. It is usually found in the /usr/bin directory and is part of the GNU Core Utilities package, which is installed by default on most Linux distributions.

To use the whoami command on a Linux system, you simply need to open a terminal and type whoami, then press Enter. The command will then display the username of the user currently logged in to the system.

For example, if you are logged in as the user “john” on a Linux system, typing whoami will display the following output:

john

The whoami command is often used in scripts and other automated processes to determine the current user’s identity. It can also be useful for verifying that you are logged in as the correct user before performing certain actions.

Note that the whoami command may behave differently on different Linux distributions or in different contexts. For example, it may not work if you are logged in remotely using SSH or if you are running the command through a non-interactive shell.

By Tech Tutorial

Hey, I'm Chris! Nerd, Business owner, Serial Procrastinator! Will add more info soon :)

Leave a Reply