[Linux] A list of unique command selections that are convenient but unexpectedly unknown

Introduction

I have more opportunities to use Linux commands, but I found that some commands I didn't know seemed to be useful, so I searched and wrote an article. I also introduce commands that I use occasionally but don't remember properly. It is a memorial something.

Frequently used commands (cd, mkdir, cp, etc.) are not introduced with arbitrary judgment and prejudice. Please note that some commands may be used frequently. This is a command list that I haven't used much personally but want to use.

Command list

clear In the terminal, the contents that have been written up to that point will be raised and cleaned up. Since it is not deleted, you can see the past contents by scrolling.

$ clear

find A command to search for files and directories.

If you want to find out if index.html is somewhere under ~ / html /, use:

$ find ~/html -name index.html
/home/myuser/html/section2/index.html
/home/myuser/html/section1/index.html
/home/myuser/html/index.html

If you want to find out the name of a file you were working on somewhere below the desktop within 10 minutes You can check it with the following command.

$ find ~/MyDesktop/ -amin -10

ln A command to create a file link.

ln -s file name link name

The -s option creates a symbolic link instead of a hard link. A hard link is created unless otherwise specified.

Practical example

$ ln -s available/File name enable/Link name
=> enable/Create a symbolic link with a link name

By doing this, only the symbolic links in the enabled enable folder will be used, and when they are no longer used, they will be deleted.

id This command displays registered user information. You can check the user number (UID), main registration group name and its number (GID), and sub-registration group name and its number (GID). If there is no user name, the information of the current executing user will be returned.

$id username

Who am I? It answers the philosophical question of "Smoothly".

whoami It will return the name of the currently registered user. Unlike id, it's just the name.

$ whoami

Use it when you forget even your name.

top The currently running processes are displayed in descending order of CPU utilization.

$ top

It may be a heavy process, but looking at the active monitor can be useful when it's a hassle.

du This command aggregates and displays the disk usage for each directory. If you specify a file, only the size of the specified file, and if you specify a directory, the usage of that directory and all subdirectories is totaled. If you do not specify the target, the usage of the current directory is displayed.

$du file name or directory name

which This command displays the first executable file found by checking the specified command in order for the directories set in the environment variable PATH.

$ which ssh
/usr/bin/ssh

ping The ping command is a command to check whether the network is communicating with the specified host.

$ ping www.google.co.jp
PING www.google.co.jp (172.217.31.163): 56 data bytes
64 bytes from 172.217.31.163: icmp_seq=0 ttl=115 time=2.523 ms
64 bytes from 172.217.31.163: icmp_seq=1 ttl=115 time=3.583 ms
64 bytes from 172.217.31.163: icmp_seq=2 ttl=115 time=3.560 ms
64 bytes from 172.217.31.163: icmp_seq=3 ttl=115 time=5.939 ms
...

By default, it sends a packet to the other party at 1-second intervals and displays the response.

nslookup If you write the domain name after nslookup, information such as the corresponding IP address will be returned. If you write the IP address after nslookup, the domain name will be returned depending on the server settings.

$ nslookup www.google.co.jp
Server:		8.8.8.8
Address:	8.8.8.8#53

Non-authoritative answer:
Name:	www.google.co.jp
Address: 216.58.197.195

tail -f It only displays the end of the file. It is often used to display logs. It is common to add the -f option, and it can be displayed sequentially.

$ tail -f access.log

less You can browse the contents of the file. If you only want to view scripts and configuration files, the less command is convenient because there is no risk of accidentally overwriting it.

$less file name

ls -lt By adding the "-t" option, the files can be displayed in order of modification time.

$ ls -lt

This is useful when you want to go back in the order of updates to investigate the cause when something goes wrong.

ls -1 By adding -1 after ls, the result is displayed vertically and it is easy to see.

$ ls -1

Convenient commands using the pipe "|"

You can use a combination of commands by using "|" called a pipe. You can type commands smartly in one line. When $ command A | command B is executed, two commands A and B are executed at the same time. Instead of being executed separately, Command B further processes the standard output of Command A and returns the result as a single standard output.

I have referred to the following regarding the concept of pipes. https://qiita.com/angel_p_57/items/03582181e9f7a69f8168#%E3%83%AA%E3%83%80%E3%82%A4%E3%83%AC%E3%82%AF%E3%83%88%E3%83%91%E3%82%A4%E3%83%97%E3%83%A9%E3%82%A4%E3%83%B3

ls | grep By combining ls and grep, only the files narrowed down by grep from those that can be displayed by ls are displayed line by line. Even if you don't write ls -1, grep will display it line by line.

$ ls | grep apple
apple.txt

Only files with the name apple will be displayed line by line.

ls | grep -e You can narrow down by multiple keywords with the -e option.

$ ls | grep -e apple -e orange
apple.txt
orange.txt

ls | grep -v It will display the result that does not match the character string specified by the -v option.

$ ls | grep -v apple
banana.txt
lemon.txt
melon.txt
orange.txt

cat | grep Use cat to search for a character string and display a file. It's similar to less, but cat fills the terminal with the contents of the file as it is, which makes it difficult to work with long files. Therefore, cat is often used with grep.

$cat file name|grep keyword

It only shows the lines that match the keywords in the file.

Continuous use of pipes

Pipes can be concatenated and executed with three or more commands. Command A|Command B|Command CI will write it like this. The result of processing in the order of command A → command B → command C is returned as standard output.

cat | grep | less

Although it is narrowed down by cat | grep and displayed, it is a little difficult to see because it is displayed in the terminal. Therefore, if you connect the less command with a pipe, you can see only the narrowed down contents, which is very easy to see.

$cat file name|grep keyword| less

in conclusion

It's been about two months since I started working as an engineer, and I wrote this article because I wanted to get used to Linux commands and basics.

The commands I use too often aren't listed here, but if you know of any useful commands, I'd appreciate it if you could let me know in the comments. Since I wrote it at my own discretion, I think it is impossible to understand the intention, but any command will do.

Also, I would appreciate it if you could let me know if you have any strange explanations.

Recommended Posts

[Linux] A list of unique command selections that are convenient but unexpectedly unknown
[Linux] A list of Linux commands that beginners should know
[Django] A collection of scripts that are convenient for development
Twitter API: Get a list of accounts that you follow but are not followed back
Introduction of ls command lsix that can display a list of images
[Linux] Command to get a list of commands executed in the past
Summary of linux command techniques that I knew when I was a fledgling engineer
List of links that machine learning beginners are learning
[Linux command] A memorandum of frequently used commands
Linux command list
A general-purpose program that formats Linux command strings in python
CLI tool that just outputs a list of prefectures quickly
A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"