Shell Script

2.Bash Shell Basic Command

트리스탄1234 2022. 8. 10. 11:02
728x90
반응형

1. Starting Shell By default,

shell settings for each user are set in the etc/passwd file. In the example below, the root user is set to use the /bin/bash shell by default, and if you want to use a different shell, change the last value in the figure below and the shell will be executed when the terminal is executed.

In column order, Username:Password:UserID:GroupID:Full Name:HomeDirectory:Shell to use is displayed.

2. Shell Prompt

When you open a terminal on a Linux system, by default, the prompt is marked with $ as in the picture as below.

The configuration of the default prompt is in the form of username@hostname:current directory. Linux provides two prompt formats by default as shown below. By default, the shell uses PS1, and if the user sets additional information, the second prompt, PS2, is used.

Prompt settings can be inquired with the echo $PS1, echo $PS2 commands as shown in the figure below.

And if the user wants to change the prompt format, input the format to be replaced in the PS1 or PS2 variable as shown in the figure below. For example, let's change the prompt format to current time:username format.

반응형

As above, you can see that the prompt has been changed. However, this prompt is only valid until the terminal is closed. If you close the terminal and open a new terminal, the same prompt format is displayed. How to permanently change it will be explained in detail in a later post.

3. Shell Manual:

It's too heap to memorize all the shell commands. If you want to know the format of the command you want to use, you can use the man command to find out how to use the command. ​ You can use the format by entering man as the command to search. In the figure below, I tried to inquire how to use the date command. If you want to see more command options, press the Space key, and if you want to end the man command, press the q key.

4. Filesystem Navigation

In Linux, the cd command is used to change the current prompt location. To use, you can change the current location by entering the cd destination path. And the names and uses of the basic directories in the Linux system are as follows.

There are two ways to specify a destination path when changing the current directory: an absolute path and a relative path. When specifying a relative path, . and .. symbols can be used. . can be specified from the current directory location, and .. can be specified from the location of the parent directory.

- Absolute path: path starting from ROOT cd /etc

- Relative path: Specifies the location from the current directory.

./DC (Move to DC under current directory),

../test: (Move to test under parent directory)

The ls command is used as a command to display the files in the directory on the monitor. The ls command has multiple parameters, and like ls -abc, multiple parameters can be used by adding a single - after. The figure below lists the files in the current directory using the ls -al command and displays them on the monitor.

The ls command allows you to filter the output as you see fit. For example, if you want to see only the myprog file, Enter ls -l myprog to display only the file. and ? The mark can filter the output for a single character, and the * filters more than one character. The figure below shows an example where * is used when the ? mark is used.

5. File Handling Commands

Below are the commands used to process files. -Create file: touch the name of the file to be created - Copy file: cp source file name File name to be copied

The example below creates a file called linux and copies the linux file to create a linux2 file

The options of the cp command are as follows.

In Linux, there is a link file. Link files include a symbolic link file and a hard link file. ​ To create a

hard link file, you can use the cp -l command in the form of a link file to the original file. Then, first, using the linux file, we will make a hard link file called linux2 and check the contents of each file.

As you can see above, the i node number of the original file and the hard link file is the same as 67, the file contents are the same, and the file size is the same. In this state, if you look at the contents of the linux2 file after inputting the text "Original file to be modified" in the linux file, you can see that the original file has been changed to be the same as the original.

On the other hand, the symbolic link file is created using the -s option. You can enter the command in the form of "cp -s source file link file".

It is different from the hard link file, but it points to the original file in the form of linux3 -> linux, and you can see that the inode number of the file is also different. And if you look at the first letter, it is displayed as l (symbolic link). Also, the symbolic link file only stores the location of the original file. So when to use hard links and when to use symbolic links? A hard link is used in the same physical device, and a symbolic link is used when a file exists in a space other than the same physical space. ​

When moving or renaming a file, you can process the file using the mv command as shown below.

Let's try to rename the linux file to linux4 using the mv linux linux4 command.

Renaming a directory can be changed in the same way as a file. ​ When deleting a file, the rm command is used. Let's use rm to delete the linux2 file.

When creating a directory, you can use the mkdir command to create it, and when you want to delete it, you can use the rmdir command to delete it. ​ The stat command can inquire information about a file.

You can inquire information about the file type by using the file command.

When you want to see the contents of a file, you can use the cat command to view the contents of the file. Let's look up the contents of the linux4 file using cat.

As you can see from the above, cat has several options, such as n, b, and T. The description of the parameters used in the example is as follows. For details, check the additional options using the man command.

-n : Retrieve file contents and number of lines (including blank lines)

-b : Retrieve file contents with number of lines (excluding blank lines)

-T : Delete TAB when querying

The more command is the ls command or a command that prints pages in units of long output. How to use: ls -al | It can be used by using the pipe symbol after the command in the form more

The options of the more command are shown in the table below.

to view the entire contents of a file, use the cat command, but if you want to view only a part, there are tail and HEAD commands.

The tail and HEAD commands show 10 lines by default. The example below is an example of outputting the first 10 and the last 10 by using the head and tail of the progress output through the ls -al command.

The table below shows the options of the tail command.

728x90
반응형

'Shell Script' 카테고리의 다른 글

6. Basic Shell Script writing  (3) 2022.08.11
5 Linux File Permission  (2) 2022.08.11
4 Environment variables  (1) 2022.08.11
3 Bash Shell Command  (8) 2022.08.10
1 Linux command Line  (3) 2022.08.10