Basic Linux commands

Day-2

·

11 min read

What is Linux?

Linux is a free and open-source operating system that was originally developed by Linus Torvalds in 1991. It is based on the Unix operating system and is known for its stability, security, and flexibility. Linux is used by millions of people worldwide, from individual users to large corporations and organizations.

One of the key features of Linux is that it is open-source, which means that the source code is freely available for anyone to use, modify, and distribute. This has led to a large and active community of developers who contribute to the ongoing development and improvement of the operating system.

Linux is used in a wide range of applications, from servers and mainframes to desktop computers and mobile devices. It is particularly popular in the server market, where it is widely used for web hosting, cloud computing, and other applications that require high levels of reliability and security.

Basic commands of Linux

ls -l :

The "ls -l" command in Linux is used to display a detailed list of files and directories in the current working directory. When you enter the command "ls -l" in the terminal, it displays the following information for each file or directory:

  • File permissions

  • Number of hard links

  • Owner Name

  • Group name

  • File size in bytes

  • Date and time the file was last modified

  • File or directory name

The "ls -l" command is commonly used by Linux users to get more detailed information about the files and directories in a given directory. The output of the command can help users identify the ownership and permissions of files, as well as their size and modification time.

ls -a

The "ls -a" command in Linux is used to display a list of all files and directories, including hidden files, in the current working directory. When you enter the command "ls -a" in the terminal, it displays a list of all files and directories in the current directory, including the hidden files and directories that start with a dot (.) symbol.

In Linux, hidden files are usually configuration files or files that are not intended to be modified by users directly. By default, these files are not displayed when using the "ls" command. However, with the "ls -a" command, you can view these hidden files and directories.

The "ls -a" command is commonly used by Linux users to view all files and directories in a given directory, including those that are hidden. The output of the command can help users identify any hidden files or directories that may be important for system configuration or other purposes.

ls *.sh

The "ls *.sh" command in Linux is used to display a list of all files in the current working directory that have a file name extension of ".sh".

In Linux, file name extensions are used to identify the type or format of a file. For example, ".sh" file extension is commonly used for shell scripts in Linux. When you enter the command "ls *.sh" in the terminal, it displays a list of all files in the current directory that have a ".sh" extension.

The "ls *.sh" command is commonly used by Linux users to find and list all shell scripts in a given directory. The output of the command can help users identify all the files that have a ".sh" extension, which can be useful for running or editing shell scripts.

ls -i

The "ls -i" command in Linux is used to display a list of all files and directories in the current working directory, along with their inode numbers. An inode is a data structure used by the file system to store information about a file or directory, including its location, ownership, permissions, and other attributes.

When you enter the command "ls -i" in the terminal, it displays a list of all files and directories in the current directory, along with their corresponding inode numbers. The inode number is displayed as the first column in the output, followed by the file or directory name.

The "ls -i" command is commonly used by Linux users to get detailed information about files and directories, including their inode numbers. The inode number can be useful for identifying a file or directory, particularly when working with large or complex file systems. For example, you can use the inode number with the "find" command to search for a specific file or directory by its inode number.

ls -d */

The "ls -d */" command in Linux is used to display a list of all directories (folders) in the current working directory, without showing the files contained within them.

When you enter the command "ls -d */" in the terminal, it displays a list of all directories in the current directory, followed by a forward slash (/) to indicate that it is a directory. This command does not display any files within those directories.

The "ls -d */" command is commonly used by Linux users to get a list of all directories in a given directory, without cluttering the output with the files contained within them. This can be useful for quickly identifying the subdirectories within a directory or for piping the output to another command for further processing.

Pwd

The "pwd" command in Linux is used to print the current working directory. When you enter the command "pwd" in the terminal, it displays the full path of the current working directory, which is the directory in which you are currently located and where any commands or actions you perform will be applied.

The "pwd" command is a useful command in Linux, especially when you are working with the command line interface (CLI) and need to know your current location in the file system. It can be helpful for navigating to a specific directory, copying or moving files, and for performing other tasks that require knowledge of the current working directory.

For example, if you are working in the terminal and want to navigate to a specific directory, you may first use the "pwd" command to check your current location in the file system. This can help you determine the correct path to use when navigating to the desired directory.

cd change_path_to_directory

The "cd" command in Linux is used to change the current working directory. When you enter the command "cd" followed by the path of the directory you want to navigate to, the shell changes the current working directory to the specified directory.

For example, to change the current working directory to a directory named "Documents" in your home directory, you would enter the command "cd /Documents". The "" symbol represents the home directory, so this command changes the current working directory to the "Documents" directory within your home directory.

The "cd" command is an essential command in Linux, especially when working with the command line interface (CLI). It allows you to navigate to different directories within the file system, which is necessary for performing tasks such as editing files, running programs, and executing commands. The "cd" command can also be used with relative or absolute paths to navigate to directories outside of the current working directory.

cd ~

The "cd " command in Linux is used to change the current working directory to the user's home directory. The tilde () symbol is a shorthand way of referring to the home directory of the current user.

When you enter the command "cd ~" in the terminal, the shell changes the current working directory to the home directory of the current user. For example, if the username is "johndoe", the "cd ~" command will change the current working directory to the home directory of the "johndoe" user, typically "/home/johndoe" on most Linux systems.

The "cd ~" command is a useful shortcut for quickly navigating to the user's home directory, especially when working with the command line interface (CLI) in Linux. The home directory is a central location for storing user-specific files, such as configuration files, documents, and scripts. Many Linux commands and tools assume the user is working in their home directory, so it is often convenient to start from there when working with files and directories.

cd -

The "cd -" command in Linux is used to change the current working directory to the previous working directory. This command is essentially a shortcut for "cd $OLDPWD", where $OLDPWD is an environment variable that holds the path of the previous working directory.

When you enter the "cd -" command in the terminal, the shell changes the current working directory to the path stored in the $OLDPWD environment variable. This can be useful when you need to quickly switch back and forth between two directories, especially when working in the command line interface (CLI).

For example, let's say you are currently working in the directory "/home/user/Documents" and you want to switch to the directory "/home/user/Pictures". You can use the "cd /home/user/Pictures" command to navigate to that directory. Then, if you want to switch back to the Documents directory, you can simply enter the "cd -" command to change the current working directory back to the previous directory ("/home/user/Documents" in this case).

The "cd -" command is a simple and convenient way to switch between two directories without having to remember or type out their full paths each time.

cd ..

The "cd .." command in Linux is used to change the current working directory to the parent directory of the current working directory. The ".." symbol represents the parent directory in the file system.

When you enter the command "cd .." in the terminal, the shell changes the current working directory to the directory that contains the current working directory. For example, if the current working directory is "/home/user/Documents", the "cd .." command will change the current working directory to "/home/user".

The "cd .." command is useful when you need to navigate to a directory that is one level up from the current directory, especially when working with the command line interface (CLI) in Linux. It can be used repeatedly to navigate up multiple levels in the file system. For example, if you are in the directory "/home/user/Documents/Folder1/Folder2" and you want to navigate to the "/home/user" directory, you can enter the command "cd ../../" to navigate up two levels to the parent directory of the parent directory.

Using the "cd .." command in combination with other commands and options can allow you to efficiently navigate the file system and perform tasks such as copying or moving files.

cd ../..

The "cd ../.." command in Linux is used to change the current working directory to two levels up from the current working directory. The ".." symbol represents the parent directory in the file system, and repeating it twice represents the parent directory of the parent directory, or two levels up.

When you enter the command "cd ../.." in the terminal, the shell changes the current working directory to the directory that is two levels up from the current working directory. For example, if the current working directory is "/home/user/Documents/Folder1/Folder2", the "cd ../.." command will change the current working directory to "/home/user".

The "cd ../.." command can be useful when you need to quickly navigate to a directory that is two levels up from the current directory, especially when working with the command line interface (CLI) in Linux. It can be used with other commands and options to perform tasks such as copying or moving files that are located in a different directory.

Note that you can use multiple "../" to navigate to directories that are further up in the file system hierarchy. For example, the "cd ../../../" command will change the current working directory to three levels up from the current directory.

mkdir directory name

The "mkdir directory_name" command in Linux is used to create a new directory with the specified name in the current working directory. The "mkdir" command stands for "make directory".

When you enter the "mkdir directory_name" command in the terminal, the shell creates a new directory with the specified name in the current working directory. For example, if the current working directory is "/home/user/Documents" and you enter the command "mkdir MyFolder", the shell will create a new directory called "MyFolder" in the "/home/user/Documents" directory.

The "mkdir" command is useful when you need to create a new directory to store files or organize your file system. You can specify the name of the new directory after the "mkdir" command, and the shell will create a new directory with that name in the current working directory.

You can also specify the full path of the new directory if you want to create it in a different location in the file system. For example, the command "mkdir /home/user/Documents/MyFolder" will create a new directory called "MyFolder" in the "/home/user/Documents" directory.

Additionally, you can use various options and flags with the "mkdir" command to specify permissions, create nested directories, or display verbose output, among other things. You can learn more about these options by using the "man mkdir" command in the terminal.

What is the Linux command to check your present working directory?

To determine the exact location of the current directory at a shell prompt and type the command pwd . This example shows that you are in the user Mukesh's directory, which is in the /home/ directory. The command pwd stands for print working directory.

What is the Linux command to List all the files or directories including hidden files?

To show hidden files you must use the -a option. This tells the ls command to list "all" files and folders including hidden ones, i.e. those starting with a dot (.). As you can see, there are several files that start with a dot. If you just run the ls command without the -a option, the output will ignore hidden files.

What is the Linux command to Create a nested directory A/B/C/D/E?

We can use the -p option with the mkdir i.e mkdir -p A/B/C/D/E command to create a directory inside another directory.