Translate

Sunday 19 November 2023

AWS devops study material

 AWS devops study material


pwd in linux explain with example

 


In Linux, the pwd command stands for "print working directory." It is a built-in shell command that displays the full path of the current working directory. The current working directory is the directory that the shell is currently using to locate files and execute commands.

Example Usage:


Bash

user@linux:~$ pwd
/home/user

In this example, the pwd command outputs /home/user, indicating that the current working directory is /home/user.

Benefits of Using pwd:

  • Navigation: The pwd command is helpful for understanding your current location within the file system, especially when navigating through multiple directories.

  • Command Execution: When executing commands that involve file paths, knowing your current working directory can be crucial to ensure you're referencing the correct files or directories.

  • Scripting: In shell scripts, the pwd command can be used to dynamically determine the current working directory and perform operations based on it.

Overall, the pwd command is a simple yet essential tool for navigating and managing your working directory in Linux.


Creation of directories in Linux

 Creation of directories in Linux


Creating directories in Linux is a fundamental task for managing file organization and storage. There are two primary methods for creating directories in Linux: the mkdir command and the mkdtemp command.

Using the mkdir Command

The mkdir command is the standard tool for creating directories in Linux. It allows you to specify the names of the directories you want to create and the locations where you want to place them. The syntax for the mkdir command is as follows:


Bash

mkdir [options] directory_name1 directory_name2...

For example, to create a directory named "mydir" in your current working directory, you would use the following command:


Bash

mkdir mydir

To create a directory named "mydir" within a specific directory, such as "/home/user/Documents", you would use the following command:


Bash

mkdir /home/user/Documents/mydir

Using the mkdtemp Command

The mkdtemp command is specifically designed for creating temporary directories. It creates a directory with a unique, randomly generated name and returns the pathname of the newly created directory. The syntax for the mkdtemp command is as follows:


Bash

mkdtemp [options] [template]

For example, to create a temporary directory in your current working directory, you would use the following command:


Bash

mkdtemp

This will create a directory with a unique name, such as /tmp/d123456, and return the pathname of that directory. The temporary directory will be automatically deleted when the process that created it terminates.

Additional Options for Creating Directories

The mkdir and mkdtemp commands offer various options for customizing the directory creation process. Some commonly used options include:

  • -p: Creates parent directories if they do not exist

  • -m: Sets the permissions of the newly created directory

  • -v: Displays verbose output, including the pathnames of the created directories

For more detailed information on the mkdir and mkdtemp commands, refer to the respective man pages:


Bash

man mkdir
man mkdtemp

Path in linux

 Path in linux


In Linux, a path is a representation of the location of a file or directory within the file system. It is a sequence of directory names separated by slash (/) characters, starting from the root directory (/) and ending with the name of the file or directory. Paths can be either absolute or relative.

Absolute Path:

An absolute path specifies the exact location of a file or directory from the root directory (/). It starts with the slash (/) character and includes all the directories that lead to the desired file or directory. For example, the following path is an absolute path:


/home/user/Documents/myfile.txt

Relative Path:

A relative path specifies the location of a file or directory relative to the current working directory. It does not start with the slash (/) character and only includes the directories that lead to the desired file or directory from the current working directory. For example, if your current working directory is /home/user/Documents, then the following path is a relative path:


myfile.txt

Using Paths:

Paths are used to access files and directories in Linux. You can use paths in various commands, such as:

  • cd: Change directory

  • ls: List directory contents

  • cp: Copy files

  • mv: Move or rename files

  • rm: Remove files

Environment Variable PATH:

The PATH environment variable stores a list of directories that the shell will search for when executing a command. When you run a command without specifying a full path, the shell will search for the command in the directories listed in the PATH variable. You can modify the PATH variable to add or remove directories.

Tips for Using Paths:

  • Use absolute paths when you need to specify the exact location of a file or directory.

  • Use relative paths when you are working within a specific directory structure.

  • Use the PATH environment variable to make it easier to run commands without specifying a full path.

Understanding paths is essential for navigating the Linux file system and using commands effectively. By following these tips, you can use paths efficiently and effectively in your Linux environment.


Using Text Editor vi

 


Vi is a powerful and versatile text editor that is widely used on Linux systems. It is known for its efficiency and flexibility, but it also has a steeper learning curve than some other text editors.

Basic vi commands:

  • Opening a file: To open a file in vi, use the following command:


Bash

vi filename

Replace "filename" with the name of the file you want to open.

  • Entering insert mode: To enter insert mode, press i. In insert mode, you can type text as you would in any other text editor. To exit insert mode, press Esc.

  • Moving around the file: To move around the file, use the arrow keys. You can also use the following commands:

  • h: Move one character left

  • j: Move one line down

  • k: Move one line up

  • l: Move one character right

  • w: Move one word forward

  • b: Move one word backward

  • Deleting text: To delete text, use the following commands:

  • x: Delete the character under the cursor

  • dd: Delete the current line

  • d$: Delete the rest of the line from the cursor to the end of the line

  • d^: Delete the rest of the line from the cursor to the beginning of the line

  • Copying and pasting text: To copy and paste text, use the following commands:

  • y: Copy the line under the cursor

  • p: Paste the last copied or cut text after the cursor

  • P: Paste the last copied or cut text before the cursor

  • Saving a file: To save a file in vi, press Esc to enter command mode, then type the following command:


Bash

:w

This will save the file to its current location. To save the file with a different name, type the following command:


Bash

:w filename

Replace "filename" with the name you want to save the file as.

  • Exiting vi: To exit vi, press Esc to enter command mode, then type the following command:


Bash

:q

This will exit vi without saving any changes. To exit vi and save any changes, type the following command:


Bash

:wq

Additional vi commands:

These are just a few of the many commands available in vi. For a complete list of commands, press Ctrl+K to open the vi help screen.

Conclusion:

Vi is a powerful and versatile text editor that can be a valuable tool for any Linux user. It is easy to learn the basics of vi, but it can take some time to master all of its features. With a little practice, you will be able to use vi to edit text files efficiently and effectively on your Linux system.


how to use Text Editor in linux

 how to use  Text Editor in linux


There are several text editors available for Linux, each with its own strengths and weaknesses. Some of the most popular text editors include:

  • nano: A simple and user-friendly text editor that is easy to learn and use.

  • vi: A powerful and versatile text editor that is known for its efficiency and flexibility. However, it has a steeper learning curve than nano.

  • vim: An improved version of vi with additional features and a more user-friendly interface.

  • emacs: A powerful and customizable text editor that is popular among programmers.

  • gedit: A simple and user-friendly text editor that is included by default in many Linux distributions.

In this guide, we will focus on using nano, as it is a good choice for beginners.

Opening a file in nano:

To open a file in nano, you can use the following command:


nano filename

Replace "filename" with the name of the file you want to open. For example, to open a file named "myfile.txt", you would use the following command:


nano myfile.txt

Saving a file in nano:

To save a file in nano, press Ctrl+O. This will open a prompt where you can enter the filename. If you are saving the file for the first time, you will need to enter a filename. If you are saving an existing file, you can simply press Enter to save the file to its current location.

Exiting nano:

To exit nano, press Ctrl+X. If you have made any changes to the file, you will be asked if you want to save them. Press Y to save the file, N to discard the changes, or C to cancel the exit.

Additional nano commands:

  • Cut: Ctrl+K

  • Copy: Ctrl+C

  • Paste: Ctrl+V

  • Undo: Ctrl+Z

  • Redo: Ctrl+Y

These are just a few of the many commands available in nano. For a complete list of commands, press Ctrl+A to open the nano help screen.

Conclusion:

nano is a powerful and versatile text editor that is a valuable tool for any Linux user. It is easy to learn and use, yet it is also capable of handling complex tasks. With a little practice, you will be able to use nano to effectively edit text files on your Linux system.