[Linux command] A memorandum of frequently used commands

Overview

If you don't use Linux commands, you'll soon forget them, so I've summarized the frequently used commands. In fact, I'm writing at a reference level about the basic commands I've used at work.

table of contents

--cd command --Basic command --Move to the root directory --Move to your home directory --Move with a relative path --Move with an absolute path --Move to the upper level

--cp command --Basic command --Copy the file name as it is --Copy multiple files --Copy with wildcard --Copy the entire directory --Forced copy --Check and copy --Back up and copy --Copy the entire directory structure --Protect and copy file attributes --Display copy information and copy --Create a symbolic link

--pwd command --Basic command

--ls command --Basic command --Show all --Display file details as well --Display in reverse order --Display side by side in order of update time --Display side by side in file size order --Display files by extension --Display directory contents recursively --Display file names separated by commas --Display units in an easy-to-read format

--mv command --Basic command --Move files --Rename the file --Move directory --Rename the directory --Move and rename files --Move multiple files at once --Move files with specific names together --Forced move --Check and move --Back up and move

--touch command --Basic command

--mkdir command --Basic command --Create a directory with permissions specified --Create a directory if it doesn't exist --Display the message of the created result

--cat command --Basic command --Output to another file --Add to another file --Output with line numbers --Output with line numbers without blank lines --Output with line feed position --Replace tabs and output

--rm command --Basic command --Delete all files in the current directory --Delete while checking the target file --Delete hidden files --Delete the directory --Recursively delete directories --Forced deletion

--find command --Basic command --○ Search for files updated more than a minute ago --○ Search for files updated within minutes --- Search for files updated within ○ days --- Search for files updated after the day --Search case-sensitive --Search case-insensitive --Search by OR condition --Search by AND condition --Search for an empty directory --Search directory name --Full-text search with grep --Search by specifying the size of the file --Recursively change permissions --Output the file name with the full path

--grep command --Basic command --Search case-insensitive --Search by extended regular expression --Search using the regular expression specified for match processing --Search for inconsistencies --Display line numbers in search results --Display only the file name in the search results --Display characters that match the search results --Search recursively in directories

--tar command --Create an archive

--chmod command --Specify numerically

--chown command --Basic command --Display a message --Change the owner of the symbolic link itself --Change the owner in the directory

--tail command --Basic command --Specify the number of characters to output --Specify the number of lines to output --Monitor file appends

--df command --Basic command --Display only the information of the stored disk --Show all discs --Display with units

--du command --Basic command --Display file size --Display total capacity --Display capacity in bytes --Display in descending order of size

cd command

This command is for moving the working directory. I think the cd command is the first command you will encounter when using Linux.

Basic commands

cd destination path

Move to the root directory

#Move to the root directory, the top level of the disk
cd /

Go to your home directory

#Go to the user's home directory
cd

Move by relative path

#It becomes a relative path and moves to the specified directory in the current working directory.
$cd directory name

Move with an absolute path

#Move in a way that indicates where you are from the top level of the entire disk, regardless of the directory you are currently working in.
#In this example/usr/Move to the bin directory
$ cd /usr/bin

Move up

#Two periods represent the next higher hierarchy...Expressed by
cd ..
#In the case of two levels above
cd ../..

cp command

A command to copy files and directories.

Basic commands

cp copy source file name copy destination file name
#(Command example)
cp file_ori.txt file_copy.txt

Copy the file name as it is

cp Copy source file name Copy destination directory name
#(Command example)
cp file_ori.txt dirA

Copy multiple files

cp source file part 1 copy source file part 2 copy destination directory name
#(Command example)
cp fileA.txt fileB.txt dir

Copy with wildcard

#(Command example)
cp file*Copy destination directory name

Copy the entire directory

#The most frequently used option is this "-Isn't it the "r" option?
cp -r Copy source directory name Copy destination directory name

Forcibly copy

#You may be asked if there is a file with the same name in the copy destination,
#It can be used when it is troublesome to answer it one by one.
cp -f Copy source file name Copy destination file name

Check and copy

#Sometimes you want to see if it will be overwritten.
#When dealing with important files, overwriting can be fatal, so check "yes or no".
cp -i Copy source file name Copy file name

Back up and copy

#When dealing with important files, overwriting is still scary, so at that time, "-Use the "b" option.
# -If you use the b option, a backup file will be automatically generated at the same time you execute the cp command.
#As in the example below--It's helpful to use the suffix option to add today's date to the end of the filename.
cp -b --suffix=_$(date +%Y%m%d)Copy source file name Copy file name

Copy the entire directory structure

#It is not possible to copy with the normal cp command including the child directories.
# 「-P(--Use the "parents)" option.
cp -P /Directory name/コピー元ファイル名 コピー後Directory name

Protect and copy file attributes

#It may be a problem if the attributes of the copy file change without permission at the copy destination.
#For example, when building a production environment and a development environment in exactly the same way. At this time"-The "p" option comes in very handy.
cp -p Copy source file name Copy file name

Display copy information and copy

# -The v option will display information about what was copied.
#It is convenient to attach it when using a wild card.
cp -v Copy source Copy destination
#(Command example)
cp -v file* dir

Create a symbolic link

#You can also create a symbolic link with the cp command.
cp -s Original file symbolic link

pwd command

This command displays the directory you are currently operating. I use it a lot at work.

When you move frequently with the cd command or symbolic link It happens that you make a typo or work in a directory that you are not aware of. At this time, deletion commands and forced overwriting are irreversible. Failures that result in operations often occur.

Before operating the command on a daily basis, use the pwd command to change the current working directory. I have a habit of checking.

It appears in the procedure manual and checklist at the time of software release It may be included.

Basic commands

pwd

ls command

This command displays file and directory information. It may be no exaggeration to say that you can't use Linux without the ls command. I think some people will hit this command for the time being.

Basic commands

#It will list the files and folders in your current directory.
ls

Show all

# -You can also use the a option to display hidden files that start with a period in the filename.
#Hidden files with a period at the beginning are often like configuration files that you don't usually want to edit easily.
#I often use it among the options so that I don't miss it.
ls -a

Also show file details

# 「-I also often use the "l" option.
#the above"-In combination with the "a" option-It is often used in "la" etc.
ls -l

Display in reverse order

#You may also want to view the list in the reverse order of normal. At that time, this "-Use the "r" option.
#the above"-In combination with the "l" option "-It is often used in "lr" etc.
ls -r

Display in order of update time

#You can also sort the files by update time. At that time, this "-Use the "t" option.
#the above"-In combination with the "l" option "-It is often used in "tl" etc.
ls -tl

Display in order of file size

#There are times when you want to know which files are using how much space in your research. In such a case, "-The "S" command.
#the above"-In combination with the "l" option "-It is often used in "lS".
#When arranging in lighter order, "-Add the "r" option.
ls -lS

Display files by extension

#You can also sort by extension.
#Use it to find files and organize the contents of directories.
ls -Xl

Recursively display directory contents

#When you want to see the child directories side by side at once "-Display using the "R" option.
ls -R

Display file names separated by commas

#"File names can be displayed separated by commas"-m "option.
#You can use the list of files as CSV by dumping it to another file.
ls -m

Display units in an easy-to-read format

#It is used to display the file size unit in an easy-to-read format.
#Display the capacity using K, M, etc.
ls -lh

mv command

A command that allows you to move files and directories and rename files.

Basic commands

mv move source move destination

Move files

mv move file name move destination directory name
#(Command example)
mv fileA.txt dir

Rename the file

mv File name before change File name after change
#(Command example)
mv fileA.txt fileB.txt

Move directory

mv move directory name move destination directory name
#(Command example)
mv dir1 ./hoge/dir2

Rename the directory

mv File name before change File name after change
#(Command example)
mv dirA dirB

Move and rename files

mv File name before change File name after change
#(Command example)
mv fileC.txt ./dir3/fileD.txt

Move multiple files together

mv move file name 1 move file name 2 move destination directory name
#(Command example)
mv fileA.txt fileB.txt dir

Move files with specific names together

#Use wildcards to move files with specific names together.
#(Command example)
mv *.txt ./dir

Force to move

# 「-With the "f" option, even if there is a file with the same name in the destination, it will be overwritten without warning.
#It's the same as the cp command.
mv -f source file name move destination directory name

Check and move

#Sometimes you want to see if it will be overwritten.
#When dealing with important files, overwriting can be fatal, so check "yes or no".
#It's the same as the cp command.
mv -i move source file name move destination directory name

Back up and move

#When dealing with important files, overwriting is still scary, so at that time, "-Use the "b" option.
# 「-If you use the "b" option, a backup file will be automatically generated at the same time you execute the mv command.
#As in the example below,--It's helpful to use the "suffix" option to add today's date to the end of the filename.
#It's the same as the cp command.
mv -b --suffix=_$(date +%Y%m%d)Source file name Move destination file name

touch command

touch is a command to change the time stamp of a file. However, if you specify a file name that does not exist, it also has a function to create a new empty file, so you may use it for this purpose.

Basic commands

#The file specified below is changed to the current date and time, but the same command is used to create a new file.
touch file name

mkdir command

This command creates a new directory. This command is also frequently used. Basic operations cannot be performed without this command.

Basic commands

mkdir directory name

Create a directory by specifying permissions

#Permissions are in 3-digit octadecimal format from 000 to 777,
# u=w+User who gives authority such as=It can be specified in the form of authority.
mkdir -m Permission specification directory name
#(Command example: Allow all users to read and write)
mkdir -m 777 dirA

Create a directory if it doesn't exist

#If the directory you are trying to create exists, an error will occur and the directory cannot be created.
#Also, instead of creating a directory with only one level, the deeper the directory structure, the more troublesome it becomes.
#To handle these two cases,-p option (--Specifies the parents option).
mkdir -p dirA

Display the message of the created result

#When creating multiple directories from parent directory to child directory
#It is used to check which directories are newly created at once while creating them.
mkdir -v dirA

#the above"-It is even more convenient when combined with the "p" option.
mkdir -pv dirA

cat command

The command is to concatenate files and output to standard output, I think the most commonly used use is to browse files.

I think it is often compared to the less and more commands, In this article, both the less command and the more command are omitted.

Basic commands

cat fileA

Output to another file

#Since it is standard output, redirect ">You can write the output contents in another file by using.
#(Command example)
cat fileA > fileB

Add to another file

#If you want to add instead of overwriting, redirect ">>"Use the.
#(Command example)
cat fileA >> fileB

Output with line numbers

# 「-n "option. This is often used. I think there is no loss to remember.
# "number"I remember it with n.
#(Command example)
cat -n file name

Output with line numbers without blank lines

# 「-b "option."blank"I remember it with b.
#(Command example)
cat -b File name

Output with line feed position

#At the end of each line"$"Option to display "-It is "E".
#When it is difficult to understand the line feed position of consecutive character strings$Make it easier to understand by inserting.
#(Command example)
cat -b File name

Replace tabs and output

#Tab"^I"It is also displayed by replacing it with.
#(Command example)
cat -T file name

rm command

A command to delete a file or directory. Most of the big mistakes and accidents happened with this command, mv command, and cp command. It is one of the commands that requires special attention when handling.

Basic commands

rm file name

Delete all files in the current directory

#All files in the current directory will be deleted. I can't get it back.
rm *

Delete while checking the target file

#Deletion without confirmation is scary, so if you want to delete while checking the target file,-i "Option" is added.
#The basis is this "-It is relatively safe to execute while checking each one with the "i" option.
# 「-If you also add the "v" option, the execution contents are displayed and deleted.
rm -i file name

Delete hidden files

# 「rm *Even if you specify as "dot file (hidden file)", it will not be deleted.
#I often forget to delete it, but if you want to delete the dot file, follow the command example below.
#Specify the file name or delete the entire directory.
#(Command example)
rm .dotfile
rm .dot*

Delete directory

#If you want to delete the directory, click-Specify the "d" option.
#If the directory is not empty, an error will occur and it cannot be deleted.
rm -d directory name

Recursively delete directories

#To delete everything, including the contents of the directory, click-Specify the "r" option.
rm -r directory name

Forcibly delete

#Normally, if you try to delete a file that does not exist, you will get an error message.
#this"-With the "f" option, this message disappears.
#In other words, it can be erased without asking questions.
rm -f directory name

I often talk about running "rm -rf /" as a superuser (root), The meaning is "delete all files on the system", so In principle, I think it is important to check with the "-i" option.

find command

A command to search for files and directories.

It is a command that is very helpful in investigation and analysis even in the field. In an environment where Linux commands are necessary for work, productivity will be completely different between those who can use this command and those who can not, so I think it is worth remembering.

Basic commands

find file name

○ Search for files updated more than a minute ago

# 「-Specify the "mmin" option and the specified time as a plus.
#(Command example: 5 minutes or more ago)
find . -mmin +5

○ Search for files updated within minutes

# 「-Specify the "mmin" option and the specified time as a minus.
#(Command example: within 5 minutes)
find . -mmin -5

○ Search for files updated within a day

# 「-Specify the "mtime" option and the specified number of days in minus.
#(Command example: within 1 day)
find . -mtime -1

○ Search for files updated after the day

# 「-Specify the "mtime" option and the specified number of days as a plus.
#(Command example: 1 day or later)
find . -mtime +1

Search case-sensitive

# 「-Specify the name option.
#(Command example)
find . -name "*.txt"

Search case-insensitive

# 「-Specify the iname option.
#(Command example)
find . -iname "*.txt"

Search by OR condition

#The OR condition is "-o "option. OR'o'I remember it.
#(Command example: OR search by extension)
find . -name "*.txt" -o -name "*.log"

Search by AND condition

#The AND condition is "-a "option. AND'a'I remember it.
#(Command example: extension and time specification)
find . -name "*.log" -a -mtime -1

Search for an empty directory

#There are quite a few empty directories that are useless, so search in such cases.
# 「-"type d" option and "-A combination of "empty" options.
#(Command example)
find . -type d -empty

Search for a directory name

#If you want to simply search for the directory name, click "-I will specify it with the "type d" option.
# 「-It's like specifying the directory name to search with the "name" option.
#Using regular expressions,-name '*-css'You can also search the directory name like this.
#(Command example)
find ./ -name 'css' -type d

Full-text search with grep

#This is the case when searching for the specified keyword in all files under the current directory.
#I use it by connecting it with the grep command with a pipe, but I always use it for investigation and confirmation of the range of influence.
#Without this, research work is difficult.
#(Command example)
find ./ -name '*' | xargs grep 'Search word'

#If you search all files with an asterisk, image files such as jpg will also be searched, so
#This is when specifying the file extension.
#(Command example)
find ./ -name '*.html' | xargs grep 'Search word'

#If you want to specify a directory and limit it to files, click "-Specify the "type f" option.
#(Command example)
find ./ -name '*' -type f | xargs grep 'Search word'

Specify the size of the file and search

# 「-Specify the size option to search for the specified file size.
#(Command example)
find search source-size file size

#If you want to search for more than the specified file size, before the file size+Is specified.
#(Command example)
find search source-size +File size

#When searching for files smaller than the specified file size, before the file size-Is specified.
#(Command example)
find search source-size -File size

#If c is added after the file size, the unit becomes bytes.
#(Command example)
find search source-size File size c

#If you add k after the file size, the unit will be kilobytes.
#(Command example)
find search source-size file size k

#When applied by connecting with the sort command with a pipe
#The command to search for files larger than the specified size looks like this.
#(Command example)
find search source-size +File size| xargs ls -l | sort -rn

Recursively change permissions

#You can also change permissions recursively by connecting with chmod.
#For directories
#(Command example)
find ./ -type d | xargs chmod 777

#For files
#(Command example)
find ./ -type f | xargs chmod 666

Output the file name with the full path

#It is used to recursively create a list of all files.
#(Command example)
find ./dir -type f

grep command

This command searches for a character string in a file. It is used together with the find command. This command also helps to increase work productivity.

Basic commands

grep search regular expression filename

Search case-insensitive

# 「-The i "option is specified for a case-insensitive search.
grep -i search regular expression filename

Search by extended regular expression

#When performing an OR search|I use the OR operator of, but for that I need to use an extended regular expression.
#The expression of the extended seat is "-"E" option.
grep -E Search regular expression file name
#(Command example)
grep -E 'a|b' hoge/*

Search using the regular expression specified for match processing

# 「-Specify the "e" option to perform the regular expression specified for match processing.
grep -e Search regular expression 1-e Search regular expression 2 File name
#(Command example)
grep -e a -e b hoge/*

Search for inconsistencies

#When searching for a mismatch, "-Specify the "v" option.
grep -v Search regular expression file name
#(Command example)
grep -v a hoge/*

Show line numbers in search results

#It is used to display the line number and specify the location. "-Specify the "n" option.
grep -n Search regular expression file name
#(Command example)
grep -n a hoge/*

Show only file name in search results

#When you want to display only the file name in the search result, you often see "-Specify the "l" option.
grep -l Search regular expression file name
#(Command example)
grep -l a hoge/*

Display characters that match the search results

#Display the characters that match the search results. I use it for confirmation.
grep -o Search regular expression filename
#(Command example)
grep -o a.*b hoge/*

Recursively search in directories

#It is also used when searching in the directory.
# 「-Specify the "r" option, but it is often used because it is often searched at once.
grep -r Search regular expression filename
#(Command example)
grep -r a hoge/*

tar command

This command creates and expands an archive. Basically, it is used by specifying options, but this command is also used for file compression and decompression. When securing capacity or transferring large files, etc., compress them in advance and use them.

Create an archive

#This is the simplest command format, but it is often used.
# 「-"cvf file name" is fine, but "cvf file name" is fine-Please note that "cfv file name" is invalid.
#(Command example)
tar -cvf archive file name Files included in the archive

#(Command example: All PHP files are included)
tar -cvf hoge.tar *.php

Compress

#Use this command because you can also compress at the same time as archiving.
#Originally, there is a command dedicated to compression, which may be the standard.
#(Command example: Compressed in gzip format)
tar -zcvf archive file name.tar.Files included in the gz archive

#(Command example: Compressed in bzip2 format)
tar -jcvf archive file name.tar.Files included in the bz2 archive

#(Command example: Compressed in xz format)
tar -Jcvf archive file name.tar.Files included in the xz archive

Unzip

#Decompression is usually done with this command, which automatically determines the compression format.
#(Command example :)
tar -xvf archive name

Check the contents of the archive

#It is used when confirming that the archive has been properly archived as expected.
#(Command example :)
tar -tf archive name

chmod command

This command changes the permissions of files and directories. There are two ways to specify numerical values and alphabets, but this manual only describes numerical values.

Specify with a numerical value

chmod mode Target file name

#(Command example)
chmod 754 hoge.txt

#mode(Numbers) |modeアルファベット)   |Authority
# ------------|-----------------------|------
#     4       |  r                    |reading
#     2       |  w                    |writing
#     1       |  X                    |Run

#Enter the above total value in the order of "Owner", "Owned group", "Other",
#You can change the permissions.
#The above "754" is
#"Read", "Write", "Execute" for "Owner",
#"Read" and "Execute" for "Owned group",
#"Read" is added to "Other".

chown command

A command to change the owner or group of a file. If you do not use the chown command with administrator user privileges, you may not be able to operate it without privileges. It is better to operate the chown command with an account with administrator privileges.

Basic commands

chown owner name file name or directory name

Display a message

#Used when a message is displayed when there is a change.
chown -c Owner name File name or directory name

Change the owner of the symbolic link itself

#If you change the symbolic link using the chown command
#You will change the symbolic link destination, not the symbolic link itself.
# 「-By specifying the "h" option, you can change the symbolic itself.
chown -h Owner name File name or directory name

Also change the owner in the directory

#This is because you may want to change multiple files or directories at once.
chown -R Owner name Directory name

tail command

This command displays a few lines from the last line of the file. However, I am often taken care of by file monitoring using the options in the tail command.

Basic commands

tail file name

Specify the number of characters to output

#If not specified, 10 lines are displayed from the last line.
# 「-Change the number of characters specified in the "c" option to perform output operations.
#The number of characters is in bytes, but you can also specify kilobytes by adding K to the number of characters.
tail -c Number of characters File name

Specify the number of lines to output

#If not specified, 10 lines are displayed from the last line.
# 「-Change the number of lines specified by the "n" option to operate the output.
tail -n number of lines file name

Monitor file appends

#It will wait to monitor the appending of the file.
#It is useful for monitoring log files.
#When the operation ends, you can end it with the shortcut key of Ctrl + C.
tail -f file name

df command

This command checks the disk capacity.

Since the capacity is finite, I think it is natural that if you continue to use the data, the capacity will eventually fill up. I think the file server administrator is a required command.

Basic commands

df

Display only the information of the stored disk

#This is the case if you want to see only the information on the disk that contains the specified directory.
df directory name

Show all discs

# 「-With the a "option, you can also view disks with zero capacity, such as dummy file systems.
df -a

Display with a unit

#The display of capacity without options is poorly visible because it is not digit separated.
# 「-The "H" option is displayed using the units of "k", "M", and "G", so it is easy to understand.
df -H

du command

This command checks the directory and file size. It is often used to find out which directory is using data.

Basic commands

#In case of du only, the capacity of all directories under the current directory is displayed.
du directory name

Display file size

# 「-You can also view the file size with the "a" option.
du -a directory name

Display total capacity

#Since it is troublesome to count by myself, "-Display the total capacity with the "s" option.
du -s directory name

Display capacity in bytes

# 「-You can display the capacity in bytes with the "b" option.
df -b Directory name

Display in descending order of size

#(Command example: When displaying directories in child directories as well)
du -k /Directory name/* | sort -rn | head -Number of lines you want to display

#(Command example: When not displaying the directories in the directory)
du -sk /Directory name/* | sort -rn | head -Number of lines you want to display

#If you want to find out which user is storing data in your home directory, click "-Add "s" or
#In which directory in the user's home directory
#If you want to check if a lot of data is stored,
# 「-Use it properly, such as not adding the "s" option.

Summary

Operation commands for editing the contents of the file, Since monitoring commands such as memory and CPU are omitted, If there are more useful commands or opportunities, I will write them again.

This time, the main commands that I use and take care of on a daily basis I think it's just basic commands, but I hope it helps.

Recommended Posts

[Linux command] A memorandum of frequently used commands
List of frequently used Linux commands
[Linux] Review of frequently used basic commands 2
Frequently used linux commands
[Linux] Review of frequently used basic commands
A collection of commands frequently used in server management
Display a list of frequently used commands on Zsh
Linux Frequently Used Commands [Personal Memo]
[Linux] Frequently used Linux commands (file operation)
Frequently used Linux commands (for beginners)
[Anaconda3] Summary of frequently used commands
[Linux] Frequently used Linux commands (folder operation)
Summary of frequently used commands of django (beginner)
Summary of frequently used commands in matplotlib
[Linux] List of Linux commands used in practice
pyenv Frequently used commands
[Python/Django] Summary of frequently used commands (3) <Operation of PostgreSQL>
Frequently used tmux commands
[Python/Django] Summary of frequently used commands (4) -Part 2- <Production operation: Amazon EC2 (Amazon Linux 2)>
Linux basic command memorandum
[Linux] Command to get a list of commands executed in the past
[Python/Django] Summary of frequently used commands (4) -Part 1- <Production operation: Amazon EC2 (Amazon Linux 2)>
[Python/Django] Summary of frequently used commands (2) <Installing packages>
Summary of frequently used commands (with petit commentary)
Frequently used pip commands
[Linux] A list of Linux commands that beginners should know
A small memorandum of openpyxl
Frequently used subpackages of SciPy
Frequently used commands in virtualenv
A brief summary of Linux
Linux command memorandum [for beginners]
A memorandum of using eigen3
Basic knowledge of Linux and basic commands
8 Frequently Used Commands in Python Django
A memorandum to change to Manjaro Linux
[Python] A memorandum of beautiful soup4
Completion of docker command on Linux
A memorandum of files under conf.d
A memorandum of closure survey contents
[Python] A memo of frequently used phrases (by myself) in Python scripts
[Linux Nginx] Command collection used for initial setting of Web server
How to output the output result of the Linux man command to a file
linux memorandum
Linux command # 4
Linux commands
Linux command # 3
Command memorandum
[Linux] Summary of middleware version confirmation commands
A memorandum of using Python's input function
A memorandum of speed of arbitrary degree diagonalization
linux commands
Status check command used (sometimes) on linux
A quick overview of the Linux kernel
A memorandum of understanding about django's QueryDict
Linux command # 5
Command memorandum
[Linux] Learn the basics of shell commands
Frequently used syntax memorandum for each language
[Machine learning] List of frequently used packages
Linux commands
Linux commands