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.
--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
This command is for moving the working directory. I think the cd command is the first command you will encounter when using Linux.
cd destination path
#Move to the root directory, the top level of the disk
cd /
#Go to the user's home directory
cd
#It becomes a relative path and moves to the specified directory in the current working directory.
$cd directory name
#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
#Two periods represent the next higher hierarchy...Expressed by
cd ..
#In the case of two levels above
cd ../..
A command to copy files and directories.
cp copy source file name copy destination file name
#(Command example)
cp file_ori.txt file_copy.txt
cp Copy source file name Copy destination directory name
#(Command example)
cp file_ori.txt dirA
cp source file part 1 copy source file part 2 copy destination directory name
#(Command example)
cp fileA.txt fileB.txt dir
#(Command example)
cp file*Copy destination directory name
#The most frequently used option is this "-Isn't it the "r" option?
cp -r Copy source directory name Copy destination directory name
#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
#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
#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
#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
#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
# -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
#You can also create a symbolic link with the cp command.
cp -s Original file symbolic link
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.
pwd
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.
#It will list the files and folders in your current directory.
ls
# -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
# 「-I also often use the "l" option.
#the above"-In combination with the "a" option-It is often used in "la" etc.
ls -l
#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
#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
#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
#You can also sort by extension.
#Use it to find files and organize the contents of directories.
ls -Xl
#When you want to see the child directories side by side at once "-Display using the "R" option.
ls -R
#"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
#It is used to display the file size unit in an easy-to-read format.
#Display the capacity using K, M, etc.
ls -lh
A command that allows you to move files and directories and rename files.
mv move source move destination
mv move file name move destination directory name
#(Command example)
mv fileA.txt dir
mv File name before change File name after change
#(Command example)
mv fileA.txt fileB.txt
mv move directory name move destination directory name
#(Command example)
mv dir1 ./hoge/dir2
mv File name before change File name after change
#(Command example)
mv dirA dirB
mv File name before change File name after change
#(Command example)
mv fileC.txt ./dir3/fileD.txt
mv move file name 1 move file name 2 move destination directory name
#(Command example)
mv fileA.txt fileB.txt dir
#Use wildcards to move files with specific names together.
#(Command example)
mv *.txt ./dir
# 「-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
#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
#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 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.
#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
This command creates a new directory. This command is also frequently used. Basic operations cannot be performed without this command.
mkdir directory name
#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
#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
#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
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.
cat fileA
#Since it is standard output, redirect ">You can write the output contents in another file by using.
#(Command example)
cat fileA > fileB
#If you want to add instead of overwriting, redirect ">>"Use the.
#(Command example)
cat fileA >> fileB
# 「-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
# 「-b "option."blank"I remember it with b.
#(Command example)
cat -b File name
#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
#Tab"^I"It is also displayed by replacing it with.
#(Command example)
cat -T file name
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.
rm file name
#All files in the current directory will be deleted. I can't get it back.
rm *
#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
# 「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*
#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
#To delete everything, including the contents of the directory, click-Specify the "r" option.
rm -r directory name
#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.
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.
find file name
# 「-Specify the "mmin" option and the specified time as a plus.
#(Command example: 5 minutes or more ago)
find . -mmin +5
# 「-Specify the "mmin" option and the specified time as a minus.
#(Command example: within 5 minutes)
find . -mmin -5
# 「-Specify the "mtime" option and the specified number of days in minus.
#(Command example: within 1 day)
find . -mtime -1
# 「-Specify the "mtime" option and the specified number of days as a plus.
#(Command example: 1 day or later)
find . -mtime +1
# 「-Specify the name option.
#(Command example)
find . -name "*.txt"
# 「-Specify the iname option.
#(Command example)
find . -iname "*.txt"
#The OR condition is "-o "option. OR'o'I remember it.
#(Command example: OR search by extension)
find . -name "*.txt" -o -name "*.log"
#The AND condition is "-a "option. AND'a'I remember it.
#(Command example: extension and time specification)
find . -name "*.log" -a -mtime -1
#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
#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
#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 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
#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
#It is used to recursively create a list of all files.
#(Command example)
find ./dir -type f
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.
grep search regular expression filename
# 「-The i "option is specified for a case-insensitive search.
grep -i search regular expression filename
#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/*
# 「-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/*
#When searching for a mismatch, "-Specify the "v" option.
grep -v Search regular expression file name
#(Command example)
grep -v a hoge/*
#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/*
#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 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/*
#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/*
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.
#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
#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
#Decompression is usually done with this command, which automatically determines the compression format.
#(Command example :)
tar -xvf archive name
#It is used when confirming that the archive has been properly archived as expected.
#(Command example :)
tar -tf archive name
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.
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".
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.
chown owner name file name or directory name
#Used when a message is displayed when there is a change.
chown -c Owner name File name or directory name
#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
#This is because you may want to change multiple files or directories at once.
chown -R Owner name Directory name
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.
tail file name
#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
#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
#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
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.
df
#This is the case if you want to see only the information on the disk that contains the specified directory.
df directory name
# 「-With the a "option, you can also view disks with zero capacity, such as dummy file systems.
df -a
#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
This command checks the directory and file size. It is often used to find out which directory is using data.
#In case of du only, the capacity of all directories under the current directory is displayed.
du directory name
# 「-You can also view the file size with the "a" option.
du -a directory name
#Since it is troublesome to count by myself, "-Display the total capacity with the "s" option.
du -s directory name
# 「-You can display the capacity in bytes with the "b" option.
df -b Directory name
#(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.
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