Linux operation for beginners Basic command summary

Introduction

From this year, I became an engineer and started learning Linux because I wanted to learn the basic concepts of command line operation and networks. I will write an article as a memorandum. I would appreciate it if you could point out any mistakes.

Basic commands

1. File operation command

1-1. Display a list of files and directories (ls)

[What you can do]

-Command to display a list of files and directories -Options, file names, and directory names can be specified after ls. -You can use wildcards (*) to narrow down the results and display a list.

How to use

ls [option] [file name]

You can enter in the format of.

$ ls
Download desktop image music
Template document release

In this way, you can display a list of files and directories in the current directory.

If you specify a directory or file after ls, you can display a list of files and directories in that directory.

$ ls /usr/
bin etc games include lib libexec local sbin share src tmp

Use the option to change the behavior of the command, such as changing the order of the files and displaying them.

-a
.Show all files, including hidden files that start with
-l
Display in Long format
-t
Sort and display according to the last update time
-r
Sort and display in reverse order

Narrow down and display files

 $ls hosts*
hosts  hosts.allow  hosts.deny

In this case, all files with hosts as the acronym are displayed.

Narrow down when you know the number of characters

$ cd /etc
$ ls hosts.????
hosts.deny

Narrow down the characters by the number of? 4 characters in the above case

You can also narrow down by adding a character string after the?

 cd /etc
$ ls hosts.????w
hosts.allow

1-2. File copy (cp)

[What you can do]

-Create a duplicate of the file -You can also specify the name of the file that made the copy. · You can also copy directories using the -r option

How to use

cp [option] [Original] [Copy to] 

Example of use

cp a b

In the above example, the movement changes depending on the state of b, which is the copy destination. -If b is not the existing file name, b's file is created with a as a copy. -If b is a directory name, a copy of a is created under b. -If the file name b exists, b is overwritten by a copy of a.

option

-i
When making an overwrite copy of a file"overwrite file name? (y/n [n])"For confirmation like
I will make an inquiry. It is used to apologize and not make an overwrite copy.

-r
Copy the directory. The cp command is basically a command to copy a file, but with this command
You can copy all the files and directories in the directory.

-p
Copy while retaining the original information. The file contains the owner, administrator privileges, modification date, etc.
There is various information. In a normal copy, the information becomes new and is copied. I want to keep old information
If so, add this option.

1-3. Move files (mv)

What you can do

-Files can be moved. -You can also change the file name when moving.

How to use

mv move source file move destination file

Example of use

mv a b

Even with the mv command, the behavior changes depending on the state of the file / directory. -If a is the file name and b is in the directory, the file of a is moved under the directory of b. -If a is a directory and b also exists, the directory of a moves under the directory of b. -If a is the file name and b does not exist, the file name of a is changed to b. -If a is the directory name and b does not exist, the directory name of a is changed to b.

option

-i
When making an overwrite copy of a file"overwrite file name? (y/n [n])"For confirmation like
I will make an inquiry. It is used to apologize and not make an overwrite copy.

-f
Forcibly execute the process. In some cases, there is an inquiry to confirm the process, but it is ignored and the process is forced to be executed.

1-4. Delete file (rm)

What you can do

-Files can be deleted.

How to use

rm file name

option

-i
When deleting a file, ask for confirmation whether it is really okay to delete it. .. Used to prevent accidental deletion of files.

-f
Forcibly execute the process. In some cases, there is an inquiry to confirm the process, but it is ignored and the process is forced to be executed.
-r
Copy the directory. The cp command is basically a command to copy files, but with it you can copy all the files and directories in the directory.

On UNIX-like OS such as Linux, once deleted files cannot be restored, so be careful when using them. Also, since rm is a command to delete files, directories cannot be deleted, so use the rmdir command described later to delete directories.

2. Directory manipulation

Prerequisite knowledge

Special directory

In Linux, there are special directories and symbols that represent them. The following are typical ones.

Current directory (.)

The current directory. File and directory operations are represented using dots (.).

Parent directory (..)

One level above the current directory. If the current directory is / user / host, the parent directory will be / user.

Home directory

The directory where the user starts working. The user immediately after logging in is always in the home directory. File and directory operations are represented using a tilde (~).

Root directory

Represents the top level of the directory. File / directory operations are represented using the root (/).

Absolute path specification and relative path specification

There are two ways to specify / user / bin / xxx when the current current directory is / user / local.

1./user/bin/xxx Absolute path specification is the method to specify the directory file from the highest root directory. Absolute path specification can be specified in any directory, but the description may be long.

  1. ../bin/xxx Relative path specification is the method of specifying the path from the location of the current directory. Relative path specification cannot be specified if the current directory changes, but if the directory is specified closer to the current location, less description is required.

2-1. Show current directory (pwd)

What you can do

-It is possible to display where you are currently in a hierarchical directory.

How to use

$ pwd
/home/hosts

/ Is a symbol that represents a directory delimiter.

2-2. Change directory (cd)

What you can do

-You can change the current directory.

How to use

cd [Directory name]

You can specify the directory with either an absolute path or a relative path. If you do not specify anything, move to your home directory.

2-2. Creating a directory (mkdir)

What you can do

-A directory can be created.

How to use

mkdir directory name

If you want to create dir2 under dir1 and dir3 under it

mkdir dir1
mkdir dir1/dir2
mkdir dir1/dir2/dir3

option

-p
It also creates a directory above the specified directory.

With the -p option

mkdir -p dir1/dir2/dir3 

Can be written as

2-3. Delete directory (rmdir)

What you can do

-You can delete the directory.

How to use

rmdir directory name

option

-p
Deletes directories up to the specified hierarchy at once.

rmdir deletes only the bottom directory if you don't use the option. If the contents of the directory are included, it cannot be deleted. If there is a file directory in the directory, use the rm -r command to delete it.

3. View the contents of the file

3-1. Display the contents of the file (cat)

What you can do

-You can check the contents of the file.

How to use

cat file name
View file contents

option

-n
Display with line numbers added.

3-2. Display using pager (more, less)

What you can do

-If there are too many lines on the page displayed by cat, it will flow without being displayed. (Usually about 25 lines) The paging function is to control the screen so that you can check even if there are many lines, and stop scrolling in the middle. You can do that with this command.

How to use

for the more command
item Contents
space Go to the next page
b Return to the previous screen
f Go to the next screen
/word wordの検索。nキーで検索結果をジャンプ
q Exit pager command(quit)
For less command
item Contents
space Go to the next page
b Return to the previous screen
f Go to the next screen
Go to the previous line
Go to the next line
/word wordの検索。nキーで検索結果をジャンプ
q Exit pager command(quit)

4. File search

4-1. File search (find)

What you can do

-You can search where the file is located.

How to use

find path-name file name

5. Command path

The commands I have used so far are programs, and programs are also a type of file. It is located in directories for programs such as / bin and / sbin.

5-1. Show command path (which)

Basically, when you execute a command, you don't care where the program is located. This is because the directory where the program is located is set in the environment variable called PATH. You can use which command to display the path of the command located under the directory included in the PATH environment variable.

How to use

which command name

If the directory in which the command resides is not included in the PATH environment variable, the result of the which command will result in an error.

6. How to use help

By executing the execution command with the --help option etc., you can check the options that can be used to execute the command.

How to use

command--help

7. How to use the manual

man command name

option

-k word
'word'Output a list of entries that include

Recommended Posts

Linux operation for beginners Basic command summary
[Linux] Basic command summary
[Linux command summary] Command list [Must-see for beginners]
Linux command memorandum [for beginners]
Linux command <Basic 2>
Linux Command Summary
[Basic] linux command
Linux command <Basic 1>
Linux basic command memorandum
Linux command [File operation]
Linux command for self-collection
[Linux command] cp command option list [Must-see for beginners]
[Linux command] ls command option list [Must-see for beginners]
[Linux command] touch command option list [Must-see for beginners]
[Linux command] less command option list [Must-see for beginners]
[Linux command] cat command option list [Must-see for beginners]
[Linux command] pwd command option list [Must-see for beginners]
[Linux command] rm command option list [Must-see for beginners]
[Linux command] cd command option list [Must-see for beginners]
[Linux command] mv command option list [Must-see for beginners]
[Linux command] man command option list [Must-see for beginners]
[Linux command] ssh command option list [Must-see for beginners]
[Linux command] mkdir command option list [Must-see for beginners]
Linux distribution recommended for beginners
Basic Python grammar for beginners
Linux Command Dictionary (for myself)
Reference resource summary (for beginners)
[Linux] User / group command summary
Convenient Linux shortcuts (for beginners)
What is scraping? [Summary for beginners]
Frequently used Linux commands (for beginners)
[Must-see for beginners] Basics of Linux
Linux Basic Education for Front-end Engineer
Pandas basics summary link for beginners
Linux command (basic in basic) personal memo
Linux command # 4
Linux command # 3
Linux Summary
Linux command # 5
Elasticsearch installation and basic operation for ubuntu
Django tutorial summary for beginners by beginners ③ (View)
Summary of petit techniques for Linux commands
Django tutorial summary for beginners by beginners ⑤ (test)
Linux command list
[Explanation for beginners] TensorFlow basic syntax and concept
Django tutorial summary for beginners by beginners ⑦ (Customize Admin)
Django tutorial summary for beginners by beginners ⑥ (static file)
linux at command
[Linux] Search command
Roadmap for beginners
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
Django tutorial summary for beginners by beginners ① (project creation ~)
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux [shell command]
Basic LINUX commands
[Linux] Command / Knowledge
pyenv for linux
How to create a shortcut command for LINUX
Django tutorial summary for beginners by beginners ④ (Generic View)