Linux study notes

What is this article is?

――The time has come when you need to know Linux, which you have avoided for some reason after 13 years of engineering experience. ――I can't catch up with just the site (or rather, I'm too afraid of incidents due to command mistakes without knowing it), so I study independently. ――Make a note of what you did. Output is important. ――Since it is troublesome to separate articles, I will add or modify without worrying about anything.

(Ichiou) Editing history

--2020/01/18: Newly created --2020/01/19: Added directory operation and file operation --2020/01/20: Added about authority change

1. Advance preparation

I decided to use CentOS for no particular reason. Feel free to try the commands. Also, I decided to use Docker so that I could crush it. Two birds with one stone because it will be useful for studying Docker. I did it.

(1) What I did

Build the environment by referring to the following site. https://www.orangeitems.com/entry/2018/06/18/153510

I couldn't use Hyper-V because my PC was windows 10 Home. Furthermore, the environment was constructed with reference to the following sites. https://qiita.com/idani/items/fb7681d79eeb48c05144


Sometimes you may want to go back to the Windows console, so run it in the background.

$ docker run -i -d -t centos bin/bash
5e2e721950e466cfd1583d8db34e72908a848209ff7f0aef71357b3884a257d6

I was able to start it.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e2e721950e4        centos              "bin/bash"          7 seconds ago       Up 6 seconds                            fervent_jones

centOS-chan Konnichiha.

$ docker attach 5e2e721950e4 
[root@5e2e721950e4 /]#

2. Study notes

(1) For the time being, let's see what kind of directories there are.

[root@5e2e721950e4 /]# pwd
/
[root@5e2e721950e4 /]# ls -la
total 56
drwxr-xr-x   1 root root 4096 Jan 18 07:46 .
drwxr-xr-x   1 root root 4096 Jan 18 07:46 ..
-rwxr-xr-x   1 root root    0 Jan 18 07:46 .dockerenv
lrwxrwxrwx   1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x   5 root root  360 Jan 18 07:46 dev
drwxr-xr-x   1 root root 4096 Jan 18 07:46 etc
drwxr-xr-x   2 root root 4096 May 11  2019 home
lrwxrwxrwx   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwx------   2 root root 4096 Jan 13 21:48 lost+found
drwxr-xr-x   2 root root 4096 May 11  2019 media
drwxr-xr-x   2 root root 4096 May 11  2019 mnt
drwxr-xr-x   2 root root 4096 May 11  2019 opt
dr-xr-xr-x 131 root root    0 Jan 18 07:46 proc
dr-xr-x---   2 root root 4096 Jan 13 21:49 root
drwxr-xr-x  11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 May 11  2019 srv
dr-xr-xr-x  13 root root    0 Jan 18 07:46 sys
drwxrwxrwt   7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x  12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x  20 root root 4096 Jan 13 21:49 var

The content seems to be like this. Super thanks to my ancestors. https://qiita.com/twipg/items/d8d7a0797a392bc4b75e

Questions so far

--# is what? --The # at the beginning of the command is If you are the root user now. --For general users, it will be $.

---> is what? --A symbolic link is set up. --There seems to be something called a hard link. ―― ~~ Let's actually try it when studying file operations. ~~ ――I found out when I googled, so I'll omit it. --Reference URL: http://wordpress.ideacompo.com/?p=10717

(2) Let's operate directories and files.

Try creating and deleting directories and files.

Directory manipulation

Command I tried

#Create a new directory
mkdir Directory name you want to create

#Create a new parent-child hierarchy directory
mkdir -p Directory name you want to create/Directory name you want to create

#Copy directory
cp -r Copy source directory Copy destination directory

#Delete an empty directory
rmdir Directory you want to delete

#Delete the directory containing the contents
rm -rf Directory you want to delete

Try creating a new directory in the current directory.

[root@6b65b4917285 tmp]# pwd
/tmp
[root@6b65b4917285 tmp]# mkdir directory1

directory1 was created.

[root@6b65b4917285 tmp]# ls -l
total 12
drwxr-xr-x 2 root root 4096 Jan 19 05:47 directory1

I was angry when I tried to create a parent-child hierarchy directory. It seems that it cannot be created without the parent directory.

[root@b12193fa42d0 tmp]# mkdir dir1/subdir1
mkdir: cannot create directory 'dir1/subdir1': No such file or directory

If you specify the -p option, you can create parent and child directories at the same time.

[root@b12193fa42d0 tmp]# mkdir -p dir1/subdir1
[root@b12193fa42d0 tmp]# ls -l dir1/
total 4
drwxr-xr-x 2 root root 4096 Jan 19 07:25 subdir1

When I tried to copy the directory, I was angry with the -r option.

[root@b12193fa42d0 tmp]# cp directory1 directory_copy
cp: -r not specified; omitting directory 'directory1'

I was able to copy it with the -r option.

[root@b12193fa42d0 tmp]# cp -r directory1 directory_copy
[root@b12193fa42d0 tmp]# ls -l
total 24
drwxr-xr-x 3 root root 4096 Jan 19 07:25  dir1
drwxr-xr-x 2 root root 4096 Jan 19 07:35  directory1
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy    #The directory has been copied.

Try deleting the directory.

[root@b12193fa42d0 tmp]# ls -l
total 24
drwxr-xr-x 3 root root 4096 Jan 19 07:25  dir1
drwxr-xr-x 2 root root 4096 Jan 19 07:35  directory1    #I'll erase this one.
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy
[root@b12193fa42d0 tmp]# rmdir directory1
#Had disappeared.
[root@b12193fa42d0 tmp]# ls -l
total 20
drwxr-xr-x 3 root root 4096 Jan 19 07:25  dir1
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy

I was angry when I tried to erase the directory containing the contents.

[root@b12193fa42d0 tmp]# rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty

It was erased with rm -r. (However, various things are confirmed.)

[root@b12193fa42d0 tmp]# rm -r dir1
rm: descend into directory 'dir1'? y
rm: remove directory 'dir1/subdir1'? y
rm: remove directory 'dir1'? y
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy

If it is rm -rf, it disappears without confirmation.

[root@b12193fa42d0 tmp]# rm -rf dir1
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy

File operations

Command I tried

#Create new file
touch File name you want to create

#Create a file and edit it as it is (open with vi)
vi File name you want to create

#Copy file
cp copy source file copy destination file

#Delete file
rm -rf Directory you want to delete

Try creating a new file in the current directory.

[root@b12193fa42d0 tmp]# touch file.txt
[root@b12193fa42d0 tmp]# ls -l file.txt
-rw-r--r-- 1 root root 0 Jan 19 08:04 file.txt

You can also make it with vi.

[root@b12193fa42d0 tmp]# vi file2.txt
#Since vi opens, edit it appropriately and save it.
#The file cannot be created unless it is saved.
[root@b12193fa42d0 tmp]# ls -l file2.txt
-rw-r--r-- 1 root root 5 Jan 19 08:09 file2.txt

Try copying files in the same directory

[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt    #Copy this guy.
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rwx------ 1 root root  671 Jan 13 21:49 ks-script-_srt3u3c
-rwx------ 1 root root 1379 Jan 13 21:49 ks-script-gpqu_kuo
[root@b12193fa42d0 tmp]# cp file.txt file_copy.txt
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt    #I can copy it.

Try copying to another directory.

[root@b12193fa42d0 tmp]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt    #This guy dir/Copy to subordinate.
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt
[root@b12193fa42d0 tmp]# cp file.txt  dir/copy.txt
[root@b12193fa42d0 tmp]# ls -l dir/copy.txt
-rw-r--r-- 1 root root 0 Jan 19 08:17 dir/copy.txt    #I can copy it.

Try deleting the file

[root@b12193fa42d0 tmp]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt
[root@b12193fa42d0 tmp]# rm file2.txt
rm: remove regular file 'file2.txt'? y    #You will be asked if you want to erase it.
#Had disappeared.
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt

You can delete it without confirmation by adding the -rf option.

[root@b12193fa42d0 tmp]# rm -rf file_copy.txt
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt

(3) Let's play with the file permissions.

What is the mysterious list of English characters when ls -l is done?

What is -rw-r--r--?

-rw-r--r-- 1 root root    5 Jan 20 05:59 file.txt

This is the meaning.

word count meaning value
1st character file category - :File
d: Directory
l: Symbolic link
2nd-4th characters Owner authority r: read
w: write
x: Run
5th-7th characters Owned group permissions r: read
w: write
x: Run
8th-10th characters Authority of others r: read
w: write
x: Run

So if it's -rw-r--r--,

Try removing the authority.

There are two ways to do it.

  1. Specify with numbers.
  2. Specify in alphabet.
chmod Privilege file path you want to add

#The numerical values specified by the authority you want to attach are as follows.
#   4:r
#   2:w
#   1:x
#If you want to add r and w, "4+Specify 6 with "2".

#In the case of the alphabet
#1st character: Change target (u: User g: Group o: Other a: All)
#2nd character: How to change (=: Replace with the specified authority+: Grant the specified authority-: Erase the specified authority)
#3rd character: Changes (r: Read w: Write x: Execute)

Try to give the user all privileges.

#Specify by number.
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod 744 file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt

#Specified by alphabet.
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod u+x file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt

Try removing execute permission from the user.

#Specify by number.
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod 644 file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt

#Specified by alphabet.
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod u-x file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt

Try to give multiple permissions at the same time. Give other people the strongest authority (read / write execution).

#Specify by number.
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod 647 file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--rwx 1 root root 5 Jan 20 05:59 file.txt

#Specified by alphabet.
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod o+wx file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--rwx 1 root root 5 Jan 20 05:59 file.txt

#You can do this. (=Specify and replace)
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod o=rwx file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--rwx 1 root root 5 Jan 20 05:59 file.txt

`Personally, it's easier to understand if you specify the alphabet.

Recommended Posts

Linux study notes
New Linux textbook (study notes)
Python study notes _000
Jupyter study notes_006
Python study notes_006
Python study notes _005
Jupyter study notes_008
Jupyter study notes_004
Jupyter study notes_001
Python study notes_001
Arch Linux installation notes
Network (mainly Linux) notes
Linux
Linux study session 2nd: File operation
Minecraft boot options [Linux] Personal notes
Linux, conda, ssh, git related personal notes
linux memorandum
Linux command # 4
Linux commands
Linux commands
Linux command # 3
Linux overview
Linux basics
direnv (linux)
SQLAlchemy notes
[Linux] [C / C ++] Notes about Waf build system
pyenv notes
Organize Linux
[Bash / linux] Notes in case of trouble
linux commands
Linux practice
Ubuntu Linux 20.04
[linux] Command notes that you often forget
Linux Summary
SQL notes
Pandas notes
Sphinx notes
Linux process
django notes
Linux permissions
Notes on using OpenCL on Linux on the RX6800
Linux command # 5
Jupyter_Learning Notes_000
About Linux
Linux basics
Forgot linux
About Linux
Linux commands
Linux commands
About Linux
About Linux
About Linux ①
Django notes
Linux redirect