5 DERECHA

linux1 본문

Linux

linux1

kay30 2023. 4. 5. 14:02

Linux is a free and open-ource operating system that powers everything from smartphones and home appliances to servers and supercomputers. It was created in 1991 by Linus Torvalds, who wanted to build a Unix-like system that could run on personal computers. Today, Linux is widely used by developers, system administrators, and enthusiasts around the world, thanks to its flexibility, security, and abundance of software tools. In this article, we'll take a closer look at some basic Linux commands that you can use to navigate and explore the filesystem.

Linux commands (directory default commands)

Here's a description paragraph about three commonly used commands in Linux: pwd, cd, and ls.

One of the first things you'll need to do when working with Linux is to navigate the filesystem, which is organized as a hierarchy of directories (also known as folders).

The pwd command (short for "print working directory") shows you the current directory that you're in. This can be useful if you need to know where you are in the filesystem, especially if you're working with relative paths.

The cd command (short for "change directory") allows you to move between directories. You can use it to go up one level (by typing "cd ..") or to go to a specific directory (by typing "cd /path/to/directory").

The ls command (short for "list") shows you the contents of a directory. By default, it lists the names of files and directories in the current directory, but you can also use options to show more information, such as the file size, permissions, and timestamps.

The "ls -lah" command in Linux is used to list all the files and directories in a directory, including hidden files, with detailed information about each file. The "-l" option provides a detailed listing, the "-a" option includes hidden files, and the "-h" option makes the file sizes human-readable.

 

Understanding File Information

In Linux, when you list the contents of a directory using the "ls" command with the "-" option, you'll see a detailed listing that includes various pieces of information about each file or directory. Among this information, you'll see a string of characters that represents the file type and permissions.

Here's what each character in the string represents:

The first character indicates the file type. A "-" represents a regular file, a "d" represents a directory, and an "l" represents a symbolic link. Other characters, such as "c" and "b", represent special files such as character and block devices, respectively.

The next three characters represent the permissions for the file's owner. "r" indicates read permission, "w" indicates write permission, and "x" indicates execute permission. If a permission is not granted, it is represented by a "-".

The next three characters represent the permissions for the file's group.

The last three characters represent the permissions for all other users.

For example, a file with the following string "-rw-r--r--" would be a regular file that the owner has read and write permissions for, but other users can only read the file.

Understanding file permissions is important in Linux because it determines who can access, modify, or execute a file. You can change file permissions using the "chmod" command.

Directory Handling

1. mkdir: The "mkdir" command in Linux is used to create a new directory. You can use it by typing "mkdir directory_name" where "directory_name" is the name of the new directory you want to create.

2. rmdir: The "rmdir" command in Linux is used to remove an empty directory. You can use it by typing "rmdir directory_name" where "directory_name" is the name of the directory you want to remove. If the directory is not empty, you'll need to use the "rm" command with the "-r" option to remove it recursively.

3. tree: The "tree" command in Linux is used to display the directory structure in a tree-like format. You can use it by typing "tree" in a directory to display the directory structure starting from that directory. It can be helpful when you need to visualize the directory structure of a complex file system.

4. find: The "find" command in Linux is used to search for files and directories in a specified location based on various criteria such as name, size, and modification time. You can use it by typing "find path -name filename" where "path" is the location you want to search and "filename" is the name of the file you're looking for. You can also use various options to refine your search.

5. grep -n: The "grep" command in Linux is used to search for text patterns in files. The "-n" option is used to display the line numbers that match the search pattern. You can use it by typing "grep -n pattern filename" where "pattern" is the text pattern you're searching for and "filename" is the name of the file you want to search in.

6. du: The "du" command in Linux is used to display the disk usage of files and directories. You can use it by typing "du" in a directory to display the disk usage of all files and directories in that directory. You can also use various options to refine the output, such as "-h" to display the sizes in a human-readable format or "-s" to display only the total size of a directory.

File Handling

1. cp -r: The "cp" command in Linux is used to copy files or directories. The "-r" option is used to copy directories recursively. You can use it by typing "cp -r source_directory destination_directory" where "source_directory" is the directory you want to copy and "destination_directory" is the location where you want to copy it.

2. mv: The "mv" command in Linux is used to move or rename files or directories. You can use it by typing "mv source_file_or_directory destination_file_or_directory" where "source_file_or_directory" is the file or directory you want to move or rename and "destination_file_or_directory" is the new location or name.

3. rm -rf: The "rm" command in Linux is used to remove files or directories. The "-r" option is used to remove directories recursively, and the "-f" option is used to force the removal without prompting for confirmation. Be very careful when using this command, as it can delete files and directories permanently. You can use it by typing "rm -rf directory_name" where "directory_name" is the name of the directory you want to remove.

4. ln -s: The "ln" command in Linux is used to create links between files or directories. The "-s" option is used to create a symbolic link, also known as a soft link. A symbolic link is a special type of file that points to another file or directory. You can use it by typing "ln -s source_file_or_directory link_name" where "source_file_or_directory" is the file or directory you want to link to and "link_name" is the name of the link you want to create.

5. cat: The "cat" command in Linux is used to display the contents of a file. You can use it by typing "cat filename" where "filename" is the name of the file you want to display.

6. touch: The "touch" command in Linux is used to create a new file or update the modification time of an existing file. You can use it by typing "touch filename" where "filename" is the name of the file you want to create or update.

7. wget: The "wget" command in Linux is used to download files from the Internet. You can use it by typing "wget URL" where "URL" is the address of the file you want to download.

8. tar cvf: The "tar" command in Linux is used to create a compressed archive of one or more files or directories. The "c" option is used to create a new archive, the "v" option is used to display the progress of the archive creation, and the "f" option is used to specify the name of the archive file. You can use it by typing "tar cvf archive_name.tar file_or_directory_name" where "archive_name.tar" is the name of the archive file you want to create and "file_or_directory_name" is the name of the file or directory you want to add to the archive.

9. tar xvf: The "tar" command in Linux is also used to extract the contents of a compressed archive. The "x" option is used to extract the contents, the "v" option is used to display the progress of the extraction, and the "f" option is used to specify the name of the archive file. You can use it by typing "tar xvf archive_name.tar" where "archive_name.tar" is the name of the archive file you want to extract.

File Permissions

1. chown: The "chown" command in Linux is used to change the owner of a file or directory. You can use it by typing "chown new_owner file_or_directory_name" where "new_owner" is the username of the new owner you want to set and "file_or_directory_name" is the name of the file or directory you want to change the owner for.

2. chgrp: The "chgrp" command in Linux is used to change the group ownership of a file or directory. You can use it by typing "chgrp new_group file_or_directory_name" where "new_group" is the name of the new group you want to set and "file_or_directory_name" is the name of the file or directory you want to change the group ownership for.

3. chmod: The "chmod" command in Linux is used to change the permissions of a file or directory. Permissions in Linux are set for three types of users: the owner of the file, the group the file belongs to, and all other users. Permissions are represented by a three-digit number, where each digit corresponds to one of the three types of users. The first digit represents the owner's permissions, the second digit represents the group's permissions, and the third digit represents the permissions for all other users. Each digit is calculated by adding the values of the corresponding permissions. The values are as follows: 4 for read, 2 for write, and 1 for execute. You can use "chmod" by typing "chmod permissions file_or_directory_name" where "permissions" is the three-digit number representing the permissions you want to set and "file_or_directory_name" is the name of the file or directory you want to change the permissions for. For example, to give the owner read, write, and execute permissions, the group read and execute permissions, and all other users read permission, you would type "chmod 750 file_or_directory_name".

Handling Usernames/Groups

1. id: The "id" command in Linux is used to display information about the current user, such as the user ID (UID), group ID (GID), and any additional groups the user belongs to. You can use it by typing "id" in the terminal.

2. addgroup: The "addgroup" command in Linux is used to create a new group. You can use it by typing "addgroup groupname" where "groupname" is the name of the new group you want to create.

3. adduser: The "adduser" command in Linux is used to create a new user account. You can use it by typing "adduser username" where "username" is the name of the new user you want to create.

4. delgroup: The "delgroup" command in Linux is used to delete an existing group. You can use it by typing "delgroup groupname" where "groupname" is the name of the group you want to delete.

5. deluser: The "deluser" command in Linux is used to delete an existing user account. You can use it by typing "deluser username" where "username" is the name of the user account you want to delete. By default, the user's home directory and mail spool will also be deleted. If you want to keep the user's home directory and mail spool, you can add the "--remove-home" and "--remove-mail" options to the command, like this: "deluser --remove-home --remove-mail username".

6. su: The "su" command in Linux is used to switch to another user account. You can use it by typing "su username" where "username" is the name of the user account you want to switch to. If you don't specify a username, it will switch to the root user account.

7. sudo: The "sudo" command in Linux is used to run a command with administrative privileges. You can use it by typing "sudo command" where "command" is the command you want to run with administrative privileges. You will need to enter your password to use "sudo".

8. apt-get install: The "apt-get install" command in Linux is used to install a package from the software repository. You can use it by typing "sudo apt-get install package_name" where "package_name" is the name of the package you want to install. You will need administrative privileges to use "apt-get".

Handling Outputs

1. echo: The "echo" command in Linux is used to print a message or value to the terminal. You can use it by typing "echo message" where "message" is the message or value you want to print.

2. clear: The "clear" command in Linux is used to clear the terminal screen. You can use it by typing "clear" in the terminal.

3. cat: The "cat" command in Linux is used to display the contents of a file. You can use it by typing "cat filename" where "filename" is the name of the file you want to display.

4. head: The "head" command in Linux is used to display the first few lines of a file. You can use it by typing "head filename" where "filename" is the name of the file you want to display.

5. tail: The "tail" command in Linux is used to display the last few lines of a file. You can use it by typing "tail filename" where "filename" is the name of the file you want to display.

6. more: The "more" command in Linux is used to display the contents of a file one page at a time. You can use it by typing "more filename" where "filename" is the name of the file you want to display.

7. less: The "less" command in Linux is similar to "more", but it allows you to scroll through the file with the arrow keys. You can use it by typing "less filename" where "filename" is the name of the file you want to display.

8. nl: The "nl" command in Linux is used to add line numbers to a file. You can use it by typing "nl filename" where "filename" is the name of the file you want to add line numbers to.

9. tee: The "tee" command in Linux is used to redirect output to both the terminal and a file. You can use it by typing "command | tee filename" where "command" is the command whose output you want to redirect and "filename" is the name of the file you want to redirect the output to.

** sudo passwd** : when you forget you password and wanna change your password

Redirection

Redirection in Linux is the process of redirecting the input or output of a command or program. There are three types of redirection:

1. Input Redirection: Input redirection allows you to redirect the input of a command or program. This is done using the "<" symbol. For example, if you have a file called "input.txt" and you want to use its contents as input for a program, you can use the following command:

program < input.txt


This will run the "program" command with the contents of "input.txt" as its input.



2. Output Redirection: Output redirection allows you to redirect the output of a command or program to a file instead of the terminal. This is done using the ">" symbol. For example, if you have a program that outputs its results to the terminal and you want to save those results to a file called "output.txt", you can use the following command:

program > output.txt


This will run the "program" command and redirect its output to the "output.txt" file instead of the terminal.



3. Appending Output: Appending output allows you to append the output of a command or program to an existing file instead of overwriting it. This is done using the ">>" symbol. For example, if you have a program that outputs its results to the terminal and you want to append those results to a file called "output.txt", you can use the following command:

program >> output.txt


This will run the "program" command and append its output to the end of the "output.txt" file instead of overwriting it.



In addition to these basic redirection operations, you can also use pipes to connect multiple commands and redirect the output of one command to the input of another command. Pipes are represented by the "|" symbol. For example, if you have a program that outputs a large amount of data to the terminal and you want to display only the first few lines of that output, you can use the following command:

program | head



This will run the "program" command and pipe its output to the "head" command, which will display only the first few lines of the output.

Work commands

1. sleep: The sleep command is used to pause a shell script or command for a specified amount of time. For example, sleep 5 will pause the script or command for 5 seconds before continuing.

2. &: The & symbol is used to run a command in the background. For example, command & will run the command in the background and allow you to continue using the terminal while the command is running.

3. jobs: The jobs command is used to list all the jobs that are running in the background.

4. fg %: The fg % command is used to bring a background job to the foreground. For example, if you have a job running in the background with ID 1, you can bring it to the foreground using the command fg %1.

5. ctrl+z: The ctrl+z command is used to suspend a job. For example, if you have a job running in the foreground, you can suspend it by pressing ctrl+z. This will pause the job and return you to the terminal.

6. bg %: The bg % command is used to resume a suspended job in the background. For example, if you have a job suspended with ID 1, you can resume it in the background using the command bg %1.


Vim (Text editing)

Vim stands for "Vi IMproved", and is a text editor that is based on the original Unix editor, Vi. It is commonly used on Linux and other Unix-like operating systems, and is popular among programmers, system administrators, and other users who work with text files on the command line.

One of the key features of Vim is its use of modal editing. Modal editing means that Vim has different modes for different tasks. The most commonly used modes are:

* Normal mode: This is the default mode in which Vim starts. In this mode, you can navigate through the file, search for text, and perform other tasks without making any changes to the text itself. For example, you can use the arrow keys or the "h", "j", "k", and "l" keys to move the cursor around the file.

* Insert mode: This mode allows you to add or modify text in the file. To enter insert mode, you can press the "i" key (for "insert"), or one of several other keys, depending on the type of insertion you want to perform. For example, pressing "a" (for "append") will move the cursor one position to the right, and put you in insert mode at that point.

* Visual mode: This mode allows you to select blocks of text for editing or copying. To enter visual mode, you can press the "v" key. Once in visual mode, you can use the arrow keys or other navigation keys to select the text you want to work with.

* Command-line mode: This mode allows you to execute commands and perform other tasks that are not related to editing text. To enter command-line mode, you can press the ":" key. Once in command-line mode, you can enter commands to perform tasks such as saving the file, quitting Vim, or searching for text.

In addition to these modes, Vim also has many built-in commands and shortcuts that can be used to perform common editing tasks.

For example:

- Deleting text: To delete a line of text, you can use the "dd" command. To delete a word, you can use the "dw" command. To delete from the current position to the end of the line, you can use the "d$" command.

- Copying and pasting text : To copy a line of text, you can use the "yy" command. To paste the copied text, you can use the "p" command.

- Searching for text : To search for a word or phrase, you can use the "/" command followed by the text you want to search for. For example, to search for the word "example", you would enter "/example" in command-line mode.

- Undoing changes : To undo a change you have made, you can use the "u" command.

These are just a few examples of the many commands and shortcuts available in Vim. Vim also supports regular expressions for searching and replacing text, as well as macros for automating repetitive tasks.

Although Vim can be challenging to learn, many users find it to be a powerful and efficient tool for editing text files. If you are interested in learning more, there are many online resources available, such as the official Vim documentation, online tutorials, and user forums.

https://bit.ly/3D9XCOz

#코멘토 #코멘토실무PT #실무PT후기

'Linux' 카테고리의 다른 글

linux 4  (0) 2023.04.28
linux 3  (0) 2023.04.28
linux 2  (0) 2023.04.10
Linux2(Shell script)  (1) 2023.04.05
리눅스  (0) 2023.02.28