Linux command for self-collection

Article content

I want children who are inexperienced and newly participating in the project to remember I will describe the Linux command.

Target person

・ Inexperienced new engineer

command

pipeline

Pipes can pass command I / O to the (next) command. It is used when using a combination of commands. I use it very often. command | command

①
$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
...
root         1     0  0 Mar09 ?        00:00:02 /usr/lib/systemd/systemd --switc
root         2     0  0 Mar09 ?        00:00:00 [kthreadd]
root         4     2  0 Mar09 ?        00:00:00 [kworker/0:0H]
root         6     2  0 Mar09 ?        00:00:00 [mm_percpu_wq]
root         7     2  0 Mar09 ?        00:00:00 [ksoftirqd/0]
root         8     2  0 Mar09 ?        00:00:00 [rcu_sched]
Inon     11129 10953  0 04:51 pts/0    00:00:00 ps -ef
...

②
$ ps -ef | grep apache
UID        PID  PPID  C STIME TTY          TIME CMD
apache   11256 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11257 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11258 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11259 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11260 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     11304 11133  0 04:53 pts/0    00:00:00 grep --color=auto apache

③
$ ps -ef | grep -v grep | grep apache
UID        PID  PPID  C STIME TTY          TIME CMD
apache   11256 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11257 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11258 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11259 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11260 11255  0 04:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

When you want to check the Apache process, use the ps command to check the process. It is troublesome to search for Apache one by one from the many displayed processes, so at that time pipe (and You can save time by using the grep command). ps -ef | grep apache Output the process being executed by the ps -ef command, and from the output result The part corresponding to apache is output by the grep command. パイプ.jpg

Pipes are used really often and need to be remembered.

cd [options] [directory]

** Command to move directories **.

cd ..

cd..


$ pwd
/tmp/test/app/logs

$ cd ..

$ pwd
/tmp/test/app

This command moves the directory up one level. It's convenient to remember.

cd -

$ pwd
/var/log

$ cd /tmp/test/app/logs/

$ pwd
/tmp/test/app/logs

$ cd -
/var/log

cd-can go back to the previous directory. In the above case, I was originally in / var / log and moved to / tmp / test / app / logs / It looks like a log again in / var / log! It can be used when it becomes. I use it once in a while.

ls A command that displays files, directories, and detailed information about them.

$ ls
test  test1.tar  test3.tar

If there is no option, only files / directories will be displayed.

Display details.

$  ls -l
total 24
-rw-r--r-- 1 root     root         0 Mar 11 10:47 nemui111
drwxrwxr-x 3 ec2-user ec2-user    17 Mar 10 05:40 test
-rw-r--r-- 1 root     root         0 Mar 11 10:47 test111
-rw-r--r-- 1 root     root     10240 Mar 11 09:54 test1.tar
-rw-r--r-- 1 root     root     10240 Mar 11 10:06 test3.tar

Display files in reverse order of modification date.

$ ls -ltr
total 24
drwxrwxr-x 3 ec2-user ec2-user    17 Mar 10 05:40 test
-rw-r--r-- 1 root     root     10240 Mar 11 09:54 test1.tar
-rw-r--r-- 1 root     root     10240 Mar 11 10:06 test3.tar
-rw-r--r-- 1 root     root         0 Mar 11 10:47 test111
-rw-r--r-- 1 root     root         0 Mar 11 10:47 nemui111

If you do not add the option -r, the files and directories will be displayed in order from the top. In order to reverse the order, -r is added so that it can be seen immediately from the bottom.

cat [option] [file]

This command displays the contents of the file.

$ cat test1
Hello World!

If you cat a file called test1 that says Hello World! The output result will be as shown above.

cat -n [file]

$ cat -n test1
     1  root cd
     2  Inon exit
     3  root view
     4  root tar
     5
     6  Inon ps
     7  Inon grep
     8  Inon cat
     9  Inon vi

The -n option can be displayed with a line number at the beginning of the line. Even if there is a blank line, it will be displayed with a line number.

cat -b [file]

$ cat -b test1
     1  root cd
     2  Inon exit
     3  root view
     4  root tar

     5  Inon ps
     6  Inon grep
     7  Inon cat
     8  Inon vi

The -b option allows blank lines to be displayed without line numbers.

All you have to do is remember the above.

vi

A command to start a text editor. If the file exists, open it for editing, otherwise create a new one. Other than vi, there are also nano commands. vi may be hard to remember at the beginning, but commands you absolutely must remember So let's do our best to remember it.

vi has the following two modes

mode Description
Edit mode This mode is for entering characters in a file.
Command mode This mode is used to execute commands.

Enter characters in edit mode, save in command mode, or search for characters in a file. You can switch from edit mode to command mode with the ** ESC key **. ss_002.JPG In command mode When you enter, the input result is displayed in the lower left.

The commands in command mode are listed below.

Use command meaning
End :q Exit without saving
End :q! Edited but exited without saving
End :w Save and continue editing
End :wq! / ZZ Save and exit
Move to edit mode i Insert characters from the current cursor position
Move to edit mode A Insert character from the end of the current cursor line
Move to edit mode a Insert characters from behind the current cursor position
Move to edit mode O Insert line before the current cursor
Move to edit mode o Insert line next to the current cursor
Move 1G Move to the beginning of the sentence
Move G Move to the end of the sentence
Move 0 Move to the beginning of the line
Move & Move to the end of line
Move ctrl + f Move to the next screen
Move ctrl + b Move to the previous screen
Search /<String(Regular expressions)> Stringをカーソルより下部から検索
Search ?<String(Regular expressions)> Stringをカーソルより上部から検索
Search n /Or?After searching with, search for the next candidate in succession
Search N /Or?After searching with, search for the previous candidate in succession

There are many others, but I wonder if I should remember the above for the time being.

view Browsing command. The basic operation of command mode is the same as vi and cannot be edited.

view testA

grep

This command searches for and outputs a character string in a file.

$ grep root test1
root cd
root view
root tar
root rm
root df
root cd

In this case, we are looking for the string root in file test1. Another option I often use is the option **-e **, which is used when using regular expressions for searching. grep is often used in the form ** command1 | grep XX **.

ps It is used when checking the process.

$ ps
  PID TTY          TIME CMD
 3377 pts/0    00:00:00 bash
 3560 pts/0    00:00:00 ps

It looks like this with only ps. The following ** options ** are often used by me.

option meaning
-e(-A) Show all processes
f Hierarchical display

ps -ef

$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 08:08 ?        00:00:01 /usr/lib/systemd/systemd --switc
root         2     0  0 08:08 ?        00:00:00 [kthreadd]
apache    3567  3566  0 09:08 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      3640     2  0 09:14 ?        00:00:00 [kworker/0:1]
ec2-user  3641  3377  0 09:15 pts/0    00:00:00 ps -ef

Then, display and use only the process you want to check with pipe and grep. I often go there.

df This command checks the free disk space.

$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          485480       0    485480   0% /dev
tmpfs             503480       0    503480   0% /dev/shm
tmpfs             503480     408    503072   1% /run
tmpfs             503480       0    503480   0% /sys/fs/cgroup
/dev/xvda1       8376300 1437988   6938312  18% /
tmpfs             100700       0    100700   0% /run/user/1000

option I often use **-h ** as an option.

option meaning
-h Display in optimal units
-k Display in KB
-m Display in MB

df -h

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        475M     0  475M   0% /dev
tmpfs           492M     0  492M   0% /dev/shm
tmpfs           492M  408K  492M   1% /run
tmpfs           492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1      8.0G  1.4G  6.7G  18% /
tmpfs            99M     0   99M   0% /run/user/1000

du This command checks the disk usage. option

option meaning
-h Display in optimal units
-k Display in KB
-m Display in MB
-s Show only directory totals

du -sh

$ du -sh /var/
214M    /var/

tar A command to create / extract / decompress an archive. option

option meaning
-c Archive
-f File name specification
-v View processed files
-x Extract from archive
-z Compress archive in gzip format/Defrost

Archive / Deployment

tar -cvf .tar <file / directory>
$ tar -cvf test1.tar test
test/
test/app/
test/app/conf/
test/app/logs/
test/app/bin/
tar -xvf .tar
$ tar -xvf test1.tar
test/
test/app/
test/app/conf/
test/app/logs/
test/app/bin/

You can see that it can be compressed / decompressed by using the ls command respectively.

Compress / decompress with archive gzip

tar -cvfz .tar.gz <file / directory>
$ tar -zcvf TEST.tar.gz test01
test01/
test01/app/
test01/app/conf/
test01/app/logs/
test01/app/bin/
tar -xvfz .tar.gz
$ tar -zxvf DD.tar.gz
test01/
test01/app/
test01/app/conf/
test01/app/logs/
test01/app/bin/

You can see that it can be compressed / decompressed by using the ls command respectively. For tar, you should remember to create .tar and .tar.gz, decompress, compress and decompress.

whoami This command displays the current user name.

$ whoami
Inon

If you execute the command as an Inon user, Inon will be returned. The user is correct before work, isn't it? It is used when confirming.

the end

It's important for inexperienced / novice engineers (as well as me) to remember the commands Above all, I think it is most important to make sure to report, contact, and consult.

Recommended Posts

Linux command for self-collection
Linux command # 4
Linux command # 3
Linux command # 5
Linux Command Dictionary (for myself)
Linux command memorandum [for beginners]
Linux command list
linux at command
[Linux] Search command
Linux command <Basic 2>
Linux [directory command]
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
Linux [shell command]
[Linux] Command / Knowledge
pyenv for linux
My linux command
Linux command <Basic 1>
Linux command collection
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
[Linux command summary] Command list [Must-see for beginners]
Linux operation for beginners Basic command summary
[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] Volume configuration command
Linux command cheat sheet
[Linux command] cat command option list [Must-see for beginners]
[For memo] Linux Part 2
Linux basic command memorandum
[Linux command] pwd command option list [Must-see for beginners]
[Linux command] rm command option list [Must-see for beginners]
Linux command [File operation]
[Linux command] mv command option list [Must-see for beginners]
[Linux] Basic command summary
What is Linux for?
How to create a shortcut command for LINUX
linux command error collection 1
[Linux command] ssh command option list [Must-see for beginners]
Linux command line shortcut
[Linux command] mkdir command option list [Must-see for beginners]
linux sar command CPU usage
[Linux] tar.gz compression / decompression command
What is Linux? [Command list]
Linux Kernel Build for DE10nano
Flutter platform channels for Linux
Easy df command on Linux
Linux tar xz command memo
Made a command for FizzBuzz
linux: create original Terminal command
[Note] Useful linux command collection
Linux PC spec check command
[Linux] User / group command summary
Convenient Linux shortcuts (for beginners)
[Note] [For myself] Django command
Search for large files on Linux from the command line