From this year, I became an engineer and started learning Linux because I wanted to learn the basic concepts of command line operation and networks. I will write an article as a memorandum. I would appreciate it if you could point out any mistakes.
-Command to display a list of files and directories -Options, file names, and directory names can be specified after ls. -You can use wildcards (*) to narrow down the results and display a list.
ls [option] [file name]
You can enter in the format of.
$ ls
Download desktop image music
Template document release
In this way, you can display a list of files and directories in the current directory.
If you specify a directory or file after ls, you can display a list of files and directories in that directory.
$ ls /usr/
bin etc games include lib libexec local sbin share src tmp
Use the option to change the behavior of the command, such as changing the order of the files and displaying them.
-a
.Show all files, including hidden files that start with
-l
Display in Long format
-t
Sort and display according to the last update time
-r
Sort and display in reverse order
Narrow down and display files
$ls hosts*
hosts hosts.allow hosts.deny
In this case, all files with hosts as the acronym are displayed.
Narrow down when you know the number of characters
$ cd /etc
$ ls hosts.????
hosts.deny
Narrow down the characters by the number of? 4 characters in the above case
You can also narrow down by adding a character string after the?
cd /etc
$ ls hosts.????w
hosts.allow
-Create a duplicate of the file -You can also specify the name of the file that made the copy. · You can also copy directories using the -r option
cp [option] [Original] [Copy to]
Example of use
cp a b
In the above example, the movement changes depending on the state of b, which is the copy destination. -If b is not the existing file name, b's file is created with a as a copy. -If b is a directory name, a copy of a is created under b. -If the file name b exists, b is overwritten by a copy of a.
-i
When making an overwrite copy of a file"overwrite file name? (y/n [n])"For confirmation like
I will make an inquiry. It is used to apologize and not make an overwrite copy.
-r
Copy the directory. The cp command is basically a command to copy a file, but with this command
You can copy all the files and directories in the directory.
-p
Copy while retaining the original information. The file contains the owner, administrator privileges, modification date, etc.
There is various information. In a normal copy, the information becomes new and is copied. I want to keep old information
If so, add this option.
-Files can be moved. -You can also change the file name when moving.
mv move source file move destination file
Example of use
mv a b
Even with the mv command, the behavior changes depending on the state of the file / directory. -If a is the file name and b is in the directory, the file of a is moved under the directory of b. -If a is a directory and b also exists, the directory of a moves under the directory of b. -If a is the file name and b does not exist, the file name of a is changed to b. -If a is the directory name and b does not exist, the directory name of a is changed to b.
-i
When making an overwrite copy of a file"overwrite file name? (y/n [n])"For confirmation like
I will make an inquiry. It is used to apologize and not make an overwrite copy.
-f
Forcibly execute the process. In some cases, there is an inquiry to confirm the process, but it is ignored and the process is forced to be executed.
-Files can be deleted.
rm file name
-i
When deleting a file, ask for confirmation whether it is really okay to delete it. .. Used to prevent accidental deletion of files.
-f
Forcibly execute the process. In some cases, there is an inquiry to confirm the process, but it is ignored and the process is forced to be executed.
-r
Copy the directory. The cp command is basically a command to copy files, but with it you can copy all the files and directories in the directory.
On UNIX-like OS such as Linux, once deleted files cannot be restored, so be careful when using them. Also, since rm is a command to delete files, directories cannot be deleted, so use the rmdir command described later to delete directories.
In Linux, there are special directories and symbols that represent them. The following are typical ones.
The current directory. File and directory operations are represented using dots (.).
One level above the current directory. If the current directory is / user / host, the parent directory will be / user.
The directory where the user starts working. The user immediately after logging in is always in the home directory. File and directory operations are represented using a tilde (~).
Represents the top level of the directory. File / directory operations are represented using the root (/).
There are two ways to specify / user / bin / xxx when the current current directory is / user / local.
1./user/bin/xxx Absolute path specification is the method to specify the directory file from the highest root directory. Absolute path specification can be specified in any directory, but the description may be long.
-It is possible to display where you are currently in a hierarchical directory.
$ pwd
/home/hosts
/ Is a symbol that represents a directory delimiter.
-You can change the current directory.
cd [Directory name]
You can specify the directory with either an absolute path or a relative path. If you do not specify anything, move to your home directory.
-A directory can be created.
mkdir directory name
If you want to create dir2 under dir1 and dir3 under it
mkdir dir1
mkdir dir1/dir2
mkdir dir1/dir2/dir3
-p
It also creates a directory above the specified directory.
With the -p option
mkdir -p dir1/dir2/dir3
Can be written as
-You can delete the directory.
rmdir directory name
-p
Deletes directories up to the specified hierarchy at once.
rmdir deletes only the bottom directory if you don't use the option. If the contents of the directory are included, it cannot be deleted. If there is a file directory in the directory, use the rm -r command to delete it.
-You can check the contents of the file.
cat file name
View file contents
-n
Display with line numbers added.
-If there are too many lines on the page displayed by cat, it will flow without being displayed. (Usually about 25 lines) The paging function is to control the screen so that you can check even if there are many lines, and stop scrolling in the middle. You can do that with this command.
item | Contents |
---|---|
space | Go to the next page |
b | Return to the previous screen |
f | Go to the next screen |
/word | wordの検索。nキーで検索結果をジャンプ |
q | Exit pager command(quit) |
item | Contents |
---|---|
space | Go to the next page |
b | Return to the previous screen |
f | Go to the next screen |
↑ | Go to the previous line |
↓ | Go to the next line |
/word | wordの検索。nキーで検索結果をジャンプ |
q | Exit pager command(quit) |
-You can search where the file is located.
find path-name file name
The commands I have used so far are programs, and programs are also a type of file. It is located in directories for programs such as / bin and / sbin.
Basically, when you execute a command, you don't care where the program is located. This is because the directory where the program is located is set in the environment variable called PATH. You can use which command to display the path of the command located under the directory included in the PATH environment variable.
which command name
If the directory in which the command resides is not included in the PATH environment variable, the result of the which command will result in an error.
By executing the execution command with the --help option etc., you can check the options that can be used to execute the command.
command--help
man command name
-k word
'word'Output a list of entries that include
Recommended Posts