[Must-see for beginners] Basics of Linux

Contents of this article and target audience

In this article, I will write the basic knowledge for those who are starting programming on Linux to see other articles on the net and proceed with self-learning. Write on the assumption that you know the basic knowledge of computers. For those who are not familiar with the command line, here is a guide that you can remember this so that you will not have any trouble searching on the net.

environment

Ubuntu 18.04.5 LTS

Precautions for environment construction

It is the environment construction of the first barrier. Many beginners stumble. Basically, let's create an environment that boots with dual boot. "Ubuntu" is recommended as the OS when creating a Linux environment. When creating an environment, it is recommended to install it directly on the SSD without using a virtual environment such as Virtualbox. That's because you'll be bothered by the problems specific to that virtual environment. Please do it after getting used to it to some extent. Lenovo Thinkpad series and ASUS Zenbook series are recommended as the hardware environment. (Comment if there is another PC that can be done smoothly without problems) Single board computers such as Jetson and Raspberry pi will be troubled by unique environmental problems different from Ubuntu and will consume time, so study basic knowledge If your goal is to use an ordinary PC.

Basic knowledge of Linux

What is Linux

Unix is an old operating system, and its descendants are still in use today. Linux is currently the most widely used Unix-like operating system based on the Unix kernel. By the way, MacOS is also a direct line of Unix, so you can use Unix commands as they are.

Linux distribution

-Ubuntu ... A popular OS for students and the general public. It is possible to operate lightly even on a machine with low specifications. This article assumes this distribution environment. ・ Redhat Enterprise ... Linux for enterprises ・ Debian ... For those who want to play with various things independently ・ Mint ... For weak computers

Command line interface (CLI)

merit

・ Accurate and detailed operation is possible ・ Easy to automate operations ・ Less effort

Demerit

・ It is necessary to memorize the statement ・ It takes some time to master

The command line can seem daunting at first, but once you get used to it, you'll be able to work many times faster than with a mouse.

Where to type the command

Type the command into the terminal and press the "enter key" to execute it. Terminal by pressing "ctrl" + "alt" + "T" keys You can start it. Then a screen like this will appear. Type the command after the $. Screenshot from 2020-09-26 11-51-54_LI.jpg

Shortcut key

The shortcut keys for operating the terminal are as follows.

Key Description
「ctrl」+「c」 Exit the current command
「ctrl」+「a」 Move to the beginning of the input character
「ctrl」+「e」 Move to the end of the input character
「ctrl」+「u」 Delete the character before the cursor part
ctrl」+「k」 Delete the characters after the cursor part
「ctrl」+「z」 Suspend current command
!! Execute the last executed command

Linux directory structure

The directory structure of Linux is as follows. At the top is the root directory, and below that there are directories separated by various roles. The first thing you'll see when you boot Linux is / home. The user's work is basically done in / home.

/(Root directory) ** / bin ** Basic commands and general work commands are saved. ** / sbin ** Commands that can be executed only by the system administrator are saved. ** / boot ** Includes files required for booting. ** / etc ** The program setting file is saved. ** / home ** This is the user's home directory. ** / lib ** A shared library for programs. ** / mnt ** This is a directory to temporarily connect when connecting general removable media (USB memory, SD card). ** / opt ** There are additional applications. ** / root ** This is the home directory for root. ** / srv ** I have data for services such as HTTP / FTP. ** / usr ** The program shared by the user is saved. ** / tmp ** Saves files for temporary use. It disappears when you restart. ** / dev ** Contains device files. ** / proc ** Displays the status such as process information.

Special directory name

name function
. Current directory(Current directory)Point to
.. Point to a directory above your current directory
/ The first character points to the root directory
~ Point to your home directory

Special file name

If you use .filename, the file will not be visible normally. For example, this is the configuration file .bashrc that is loaded when you start the terminal.

bash bash is the most basic and well-known CUI on Linux. There are also tcsh and zsh. bash can also be used as a programming language. It can be used when automating operations.

basic bash commands

If you search on the net, a list of commands will appear, but it is difficult to find out which one should be prioritized and remembered. First of all, you should remember the following three things. Let's type in the terminal and try it. By the way, there is a space after the command. cd(change directory) This command moves from the current directory.

command motion
cd Go to your home directory
cd .. Move to the next higher folder
cd hoge Move to a folder called hoge under the current directory
cd /hoge Move to a folder called hoge under the root directory
cd ~/hoge Go to hoge under your home folder

ls(list) Display the contents of the directory.

command motion
ls Show all files and folders in the current directory
ls -l Display various information together
ls -F For folders, "/, If it is an executable file, "*Is added to the end and displayed
ls -a Display including hidden files

echo This command displays characters on the screen. If you type ʻecho hoge`, ** hoge ** will be displayed in the terminal.

Note that commands are case sensitive, so you can't use Ls instead of ls.

Folder / file operation commands

Now that you have the basic commands, let's learn how to work with files and folders. mkdir ** Command to create a folder. ** There are two methods, one is to specify the path and the other is to create in the current directory.

Create a huga folder inside a folder called hoge.

mkdir hoge/huga

The following is how to create in the current directory.

mkdir hoge

touch ** Creates a file with the specified name if the file does not exist. ** If it already exists, the creation date and time will be updated. Create a file named "file" with touch file. rm This command deletes a file. If you specify a directory name with the -r option, the folder will be deleted including the files inside.

rm -r hoge

cp This is the command to copy.

Copy the file A to the directory B.

cp A B

mv It has two functions: renaming files and moving files.

Rename the file A to B.

mv A B

Move the file A to a folder called hoge. You can specify multiple files to move.

mv A hoge

cat  If you specify a file name after this command, the contents of the file will be displayed.

Filter command

Use this when searching for files or directories or when you want to narrow down. grep ** Extract the line containing the specified character ** command. Used when searching for a file name. I think it's the most frequently used command. It is often used in combination with a pipe. file This command checks the file type. wc This command counts the number of characters and lines in a sentence.

Count the number of lines in a file called hoge.

wc -l hoge

Counts the number of characters in a file called hoge.

wc -m hoge

tr Replaces the specified characters and outputs.

Compressed file decompression command

Unzip the zip file

unzip xxx.zip

Unpack the tar.gz file

tar -zxvf xxxx.tar.gz

Unpack the tar file

tar -xvf xxxx.tar

Granting administrator privileges

Prefix the command with sudo to grant administrator privileges. It is used when dealing with files that cannot be edited without administrator privileges.

Package installation and update

On Linux you can install the package using the ʻapt install` command. apt is a command for package management. The packages provided by apt are very easy to install with a single command. For software not provided by apt, download the source and build it yourself.

Below are the commands to install the vim editor. There are other editors such as "nano", "emacs", and "code", but detailed explanations are not given here.

sudo apt install vim

Update the package list.

sudo apt update

Update the package.

sudo apt upgrade

Convenient functions you want to know

bash tab completion

・ ** Command completion ** If you press the Tab key after typing a part of the command, the command will be completed. This will prevent typos. If there are multiple command candidates, a list of candidates will appear. ・ ** File name completion ** If you press the Tab key after typing a part of the file name, the file name will appear. If there are multiple candidates, a list will be displayed.

bash command history

-You can display and edit the history of previously issued commands with the arrow keys ↑ ↓. -The history list of commands can be displayed with the history command. You can execute the history with ! Number using the number on the left side of the command. -You can execute a part of the previously typed command by typing ! Part of the command. For example, if you typed cat hoge before, you can get the same function with! Cat.

Wildcard

It is a special character that can replace a character string in a file name or the like. You can use it for file search etc. by replacing it. You can write any character string as "?". You can write any character string of 0 or more characters as "*". You can write a or b or c as [abc]. You can write a ~ z and 0 ~ 9 as [a-z] and [0-9].

Let's search for arbitrary files

** 1. Show what matches any string ** If a directory is specified, the contents of all matching directories will be displayed. ls ab * Shows only files with arbitrary strings starting with "ab". ls * e Shows only files with arbitrary strings ending in" e ". ls q * ta Display only files with" q "+ arbitrary character string +" ta ".

** 2. Display the match for any single character ** No characters are included. ls a? Shows only those that match "a + any string". ls ??? Shows only 3-character filenames.

** 3. Display matching characters in square brackets ** ls [cd] * Display only files with "c or d + arbitrary string". ls * [0-9] Display only files of arbitrary character strings + numbers.

File permissions

** ・ User type ** u ... file owner g ... users in the same group o ... other users ** ・ Permission to allow ** r ... read permission w ... write permission x ... execute permission chmod A command to change permissions. The created file cannot be executed as it is, so it is often used to make it executable.

The following is the command to give execute permission to the file.

chmod u+x hoge.bash

Unix-like OS file abstraction

On Unix-like operating systems including Linux, all operations are treated as file operations. For example, keyboard input, screen output, and printer are all files. This abstraction allows you to do a lot of useful things that you couldn't do when using device drivers. Typical examples are filter functions and redirects.

redirect

ファイル抽象化.png

<Input redirect

Read from a file instead of standard input

A < input.txt

> Output redirect

Output to file instead of standard output

echo abc > list.txt

If you want to add it, set <to two.

echo def >> list.txt 

If you do cat list.txt, you can see that" abc "and" def "are written.

| Pipe

** A command that connects the standard output of the program on the left to the standard input on the right. ** ** By typing the following command, you can output only those containing "vim" from the list of packages. The results displayed by ʻapt listare connected by|and narrowed down bygrep`. Searching is difficult if it is left as it is, but you can search with just one line by connecting with a pipe.

sudo apt list |grep vim

Filter function

You can sort, format, and filter the contents of the file. For example, prepare a file in which words are described as follows.

word_list.txt


Qiita
LINUX
Post
shell
bash
Library

The following command should convert uppercase letters to lowercase letters, sort strings containing i or l in abc order, and output.

tr [A-Z] [a-z] < word_list.txt |sort|grep [il]

Summary

This is basically what you need. If you have any other knowledge that you should know about this, please comment.

Reference material

TECCAMP blog, "[Must-see for beginners] Let's know the basic knowledge of Unix / Linux!" Qiita, "Linux directory structure" Qiita, "[Linux] File compression and decompression method" ITatmarkit, "[apt] command (basic) -installing software" Qiita, "apt command cheat sheet"

Recommended Posts

[Must-see for beginners] Basics of Linux
[Linux] Basics of authority setting by chmod for beginners
[Linux command summary] Command list [Must-see for beginners]
[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] 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]
Basics of pandas for beginners ② Understanding data overview
Linux distribution recommended for beginners
Overview of Docker (for beginners)
Seaborn basics for beginners ④ pairplot
Linux command memorandum [for beginners]
Convenient Linux shortcuts (for beginners)
Pandas basics for beginners ① Reading & processing
[For beginners] Basics of Python explained by Java Gold Part 2
Pandas basics for beginners ⑧ Digit processing
Seaborn basics for beginners ② Histogram (distplot)
Frequently used Linux commands (for beginners)
Linux basics
Pandas basics for beginners ④ Handling of date and time items
[For beginners] Basics of Python explained by Java Gold Part 1
Pandas basics summary link for beginners
Learn the basics of Python ① Beginners
Seaborn basics for beginners ① Aggregate graph of the number of data (Countplot)
[Python] Minutes of study meeting for beginners (7/15)
Linux operation for beginners Basic command summary
Summary of petit techniques for Linux commands
[Linux] Learn the basics of shell commands
For beginners of SageMaker --Collection of material links -
Roadmap for beginners
Basics of Python ①
Basics of python ①
pyenv for linux
Linux command basics
[Linux] Review of commands for deploying on AWS
Pandas basics for beginners ③ Histogram creation with matplotlib
2-2. Input for becoming a WEB engineer (Linux basics)
Easy understanding of Python for & arrays (for super beginners)
Convenient tool for beginners (under consideration of ideas)
Basic principles of image recognition technology (for beginners)
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
Basic story of inheritance in Python (for beginners)
2-1. Input for becoming a WEB engineer (Linux basics)
[Linux] A list of Linux commands that beginners should know
[Translation] NumPy Official Tutorial "NumPy: the absolute basics for beginners"
Try compiling "Linux driver for Dual Sense of PS5"
Spacemacs settings (for beginners)
Python basics ② for statement
Summary of useful tips for Linux terminals ☆ Updated daily
Basics of Python scraping basics
linux basics (users / groups)
[For memo] Linux Part 2
Meaning of Linux permissions
[Linux] Installation of nvm