Hi, I'm @ryusei__46.
This time, as a reminder to myself, I would like to summarize the Linux commands that I use frequently and forget easily.
I don't forget the commands that I use frequently when I manage a server, but other than that, my memory becomes dim when I don't use them for a while.
It is also inefficient to Google every time I use them.
Chat GPT is good, but phind is quite wonderful. It's a programming-specific generative AI, and it's free for GPT4. I recommend it and I hope you will use it.
This is a bit off topic, but here is a summary of the commands. The commands introduced below are those used in my server environment "Ubuntu 22.04".
I will add to this article as I add new, easily forgotten commands that I will be using.
I will omit commands that every server administrator knows and uses every day, as I don't think it is necessary to include them.
“ls” command: Lists the contents of a directory
I think this is one of the most standard Linux commands, but it can be executed with a variety of options to make it much easier to see. Here is an example of the ls
command with options that I use all the time.
$ ls -laF --group-directories-first --color -h
When this is done, the individual items are displayed in the following order: "permissions", "owning user", "owning group", "size (with units)", "modified date/time", and "name of file or directory (for directories, / is added at the end and text is colored)", and the sort order is The directories are grouped at the top and the files are grouped at the bottom. This display method is the same as that of Windows Explorer, so it is very easy to understand at a glance.
However, since the options are very long and it is a hassle to type them in every time, you can register the command as an alias.
$ alias myls="ls -laF --group-directories-first --color -h"
Now, if you type myls
in the terminal and run it, it will display the above format. myls
part can be changed to any string you like. However, if you register a string for which there is already an existing command, the command registered as an alias will be executed first, so you must be careful not to cover it.
If you want to delete an alias, execute unalias [command]
.
However, even if you set the alias once, the setting will be lost if you log out, so you need to add a note to the ~/.bashrc
file to run the above command at login.
“htop” command: monitor system resources
htop
is an interactive system monitoring tool for Linux and Unix-like systems and is an alternative to the top
command. It is much more sophisticated than the top
command that comes standard with Linux-based operating systems.
If there is no particular reason, it is better to use the htop
command. htop
command can be installed with the following command.
$ sudo apt install htop
The screen is also graphical, and various operations can be performed with the function keys displayed at the bottom of the screen. Operations such as filters, search functions, and process stops are very convenient.
Especially for operations such as forcibly stopping processes, I used to search for processes with the ps
command and stop them with the kill
command, but recently I have started using htop to do everything.
“lsof” command: check if the specified port number is used
The lsof
command is used to list information about processes that have a particular file or port number open; if you want to see processes that are also open by the root user or other users, you must run it with the sudo command or as root user.
To see which processes are using a particular directory or file, run lsof [directory path or file path]
, but to see which processes have a particular port number open, run lsof -i:[port]
.
The output will be as follows
// Check if port 22, the standard SSH port, is open
$ sudo lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1004 root 3u IPv4 23201 0t0 TCP *:ssh (LISTEN)
sshd 1004 root 4u IPv6 23203 0t0 TCP *:ssh (LISTEN)
sshd 32619 root 4u IPv4 151342 0t0 TCP my-host:ssh->192.168.1.19:53733 (ESTABLISHED)
sshd 32728 root 4u IPv4 151342 0t0 TCP my-host:ssh->192.168.1.19:53733 (ESTABLISHED)
“ifconfig” command: check network interface
The ifconfig
command is a Linux network interface configuration utility that sets, manages, and queries network interface parameters. This command is used to display current network configuration information, set IP addresses, netmasks, or broadcast addresses to network interfaces, create aliases for network interfaces, set hardware addresses, and enable or disable network interfaces. or disable a network interface.
I use it exclusively to check IP addresses; you can also use the ip a
command, but the ifconfig
command has a better layout and is easier to read.
$ ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.6 netmask 255.255.255.0 broadcast 192.168.4.255
inet6 240b:12:8241:9205:c836:8dff:faf8:13d4 prefixlen 64 scopeid 0x0<global>
inet6 fe80::b826:8efe:fed5:13d0 prefixlen 64 scopeid 0x20<link>
ether ca:46:8e:f5:13:d0 txqueuelen 1000 (イーサネット)
RX packets 383025 bytes 81380768 (81.3 MB)
RX errors 0 dropped 24 overruns 0 frame 0
TX packets 139316 bytes 12230672 (12.2 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
inet
represents IPv4, inet6
represents IPv6, and ether
represents a Mac address.
“screen” command: Terminal session management
The screen
command is a tool for managing multiple screen sessions. It allows you to initiate a screen session that contains multiple windows and isolates the processes running on the terminal. The screen
command also allows you to switch between sessions and select which session to connect.
I currently use this tool to manage my web server processes and the systems running on the back end. However, I plan to switch to an open source process management tool for Node.js called PM2 in the near future.
To start a new terminal session, execute the screen
command, and to leave the session (touch in), press Ctrl + A/D
on the keyboard.
To see a list of running terminal sessions, you can run the screen -ls
command, and to connect (attach) to a specified screen, you can use screen -r [screen ID]
.
If you want to scroll the screen within the connected screen, press Ctrl + A and [
on the keyboard to enter copy mode. Then you can use the up and down arrow keys to scroll the screen. To exit copy mode, press the keyboard esc
key.
“bat” command: output rich file contents
The bat
command is developed as an alternative to the cat
and less
commands and automatically applies syntax highlighting and line numbering depending on the file extension. It also has the pager functionality of the less command and automatically enters pager mode if the file does not fit on the screen. In addition, a string search function is available within the file.
To use this tool, the package must be installed.
$ sudo apt install bat -y
To view the file contents, run batcat [file path]
in a terminal.
“fd” command: file search
The fd
command was developed as an alternative to the find
command that comes standard with Linux. fd does not support all the features of the find
command, but because it is developed in the Rust language, it is faster than the find
command.
The package must be installed with the following command.
$ sudo apt install fd-find -y
To run the fd
command on Ubuntu, use fdfind
instead of fd
. The reason is unpacked in the GitHub issue below.
A simple recursive search for files in the current directory can be done with fdfind [search string]
. Also, the default function treats the search string as a regular expression pattern. So, the regular expression used in .
and ?
which are used in regular expressions, must be escaped if you want to include them in the search string. If you want to disable the regular expression pattern and treat the search string as normal text, use the -g
option.
Also, fdfind
does not include hidden files and directories in its search if not specifically configured to do so. To change fdfind
to include them, use the -H
option. Also, when searching for files in GitHub repositories, .gitignore
files are taken into account. To change to ignore .gitignore
files, give the -I
option.
If you want to search by file extension, you can use fdfind -e [extension] [search search pattern]
.
If you wish to specify a directory to search, you can do so with fdfind [search pattern] [path]
.
By default, fdfind
searches only file names, but you can also make the full path of a file the target of the search. To do so, the -p
option is given.
All of these options can be combined to provide flexibility for individual situations and applications.
“rg” command: file content search
The rg
command was developed as an alternative to the grep command that comes standard with Linux. While the fd
command can search for files by filename, the rg command searches for strings within files. In fact, it is used more often. It is also often piped into a command like [command] | rg "search pattern"
.
The official name of the command is "ripgrep" and below is the GitHub repository.
If you simply want to search for files in the current directory that contain the specified string, you can use rg [search string]
. rg command is also treated as a regular expression by default, so you need to escape any search string that contains meta characters.
To specify a target directory, use rg [search string] [path]
.
If you want to include files with specific file extensions in the search criteria, give the --type
option and describe the file type (e.g., rg [search string] [path] --type html
). Conversely, if you want to exclude certain file extensions, use the --type-not
option (e.g., rg [search string] [path] --type-not html
). To get a list of supported file types, run rg --type-list
.
Like the fd command, the rg command has a default to ignore hidden files and directories, which can be changed to include them by using the --hidden
option. Also like the fd
command, .gitignore
files are considered when searching for files in GitHub repositories. To change to ignore .gitignore files, give the --no-ignore
option. From this point on, the contents of binary files can also be searched, as is specific to the rg
command, and can be enabled with the --text
or -a
option. In addition, following symbolic links can be enabled with the --follow
or -L
option.