Because Linux treats all kinds of information as files Familiarity with working with files is very important for using Linux. Therefore, this time we will introduce the basic commands for file operations and how to use them.
** touch
$ touch hoge
#A new file named hoge is created
Also, by specifying multiple files separated by spaces, Multiple files are created. I'll show you how to specify multiple files at once like this. You can use it with the following command in the same way
--rm: Multiple files are deleted --cat: Multiple files are concatenated and displayed
** touch <File name you want to create 1> <File name you want to create 2> <File name you want to create 3> **
$ touch hoge1 hoge2 hoge3
#Three files are created with one command execution.
** rm
$ rm hoge
#newfile is deleted
If you execute the rm command, the file will be deleted without confirmation. Therefore, add the -i option if you want to confirm before deleting.
$ rm -i hoge
rm:Normal file'hoge'Do you want to delete?
#If you want to delete'y', If not'n'Enter
** cp
$ cp hoge1 hoge2
#hoge2 is overwritten by hoge1.
As with the rm command, add the -i option if you want to check before overwriting.
$ cp -i hoge1 hoge2
rm: 'hoge2'Do you want to overwrite?
#When overwriting'y', If not'n'Enter
** cat
$ cat hoge
This is a test.
#The contents inserted in hoge are displayed.
As introduced in 1, if you specify multiple files with the cat command, The contents of the file are concatenated and displayed. (This is because cat stands for concatenate.)
Also, in the cat command, the display format can be changed by adding an option. Can be changed
option | Description |
---|---|
-n | Display the number of lines |
-E | At the end of the line$Display with |
-b | Display lines other than blank lines as a set with the number of lines |
※Caution The cat command follows the character code convention of the file. The numerical value replaced with the character is displayed. Therefore, when trying to display "a file that contains numbers other than the character code" Garbled characters occur because the numbers are forcibly replaced with characters. This also applies to the less command, which will be introduced later.
$ cat /bin/pwd
#The image is a partial excerpt of the command execution result.
#As you can see, the characters are garbled.
You can also see the contents of the file with the cat command, When displaying the contents of a file that does not fit on one screen We recommend using the less command.
The less command displays long files screen by screen and scrollbars You can move and display files with just the keyboard without having to move them. Also, since it is in file read-only mode until you press q, you can only view files. The reason why we recommend specializing.
** less
Below is a list of keys to use when moving files.
Key | Execution result |
---|---|
f, space key | 1 Move to the bottom of the screen |
b | 1 Move to the screen |
j | Move down one line |
k | Move up one line |
** mkdir
$ mkdir hogedir
#A directory called hogedir is created
If you want to create a deep directory at once, add the -p option.
$ mkdir -p test/sample/hogedir
The command to specify depends on whether the contents of the directory are empty.
If a file or directory exists under the target directory Delete the rm command with the -r option.
** rm -r
$ ls hogedir
dir1 dir2 file1.txt
#Make sure the directory you want to delete is not empty
$ rm -r hogedir
#The directory is deleted, including the files placed in the directory
rmdir is when files and directories are not placed in the directory It is a command that can be used. However, if rmdir contains hidden files that start with. (Dot) Even if it is executed, an error will occur. Therefore, before execution, not only normal files but also hidden files are included. Make sure the directory is empty.
** rmdir
$ ls -a hogedir
#Make sure the directory is empty, including hidden files.
$ rmdir hogedir
#Empty directory is deleted
** mv
If the two arguments are both files and the modified file does not exist The file name is changed. If the modified file already exists, The file is overwritten in the same way as the cp command.
$ mv hoge1 hoge2
#hoge1's name changes to hoge2
You can also rename directories in a similar way.
** mv
Again, if the modified directory does not exist The directory name is changed. If the modified directory already exists The execution result is as shown in 8.
Here, the mv command introduced in 7. is used.
** mv <File path to move (multiple spaces can be specified)>
If the last argument specified is a directory instead of a file like this The move to the directory is done instead of renaming.
$ mv hoge1 hoge2 hoge3 hogedir
#3 files move to hogedir
Also, not only files but also directories can be moved under the directory. You can do it the same way.
** mv <directory path to move (multiple spaces can be specified)>
$ mv hogedir1 hogedir2
#hogedir1 moves under hogedir2.
[New Linux Textbook](https://www.amazon.co.jp/%E6%96%B0%E3%81%97%E3%81%84Linux%E3%81%AE%E6%95%99% E7% A7% 91% E6% 9B% B8-% E4% B8% 89% E5% AE% 85-% E8% 8B% B1% E6% 98% 8E / dp / 4797380942 / ref = sr_1_1? __mk_ja_JP =% E3 % 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & keywords =% E6% 96% B0% E3% 81% 97% E3% 81% 84Linux & qid = 1581934969 & sr = 8-1) [Beginner's Linux](https://www.amazon.co.jp/%E5%85%A5%E9%96%80%E8%80%85%E3%81%AELinux-%E7%B4%A0 % E6% 9C% B4% E3% 81% AA% E7% 96% 91% E5% 95% 8F% E3% 82% 92% E8% A7% A3% E6% B6% 88% E3% 81% 97% E3 % 81% AA% E3% 81% 8C% E3% 82% 89% E5% AD% A6% E3% 81% B6-% E3% 83% 96% E3% 83% AB% E3% 83% BC% E3% 83% 90% E3% 83% 83% E3% 82% AF% E3% 82% B9-% E5% A5% 88% E4% BD% 90% E5% 8E% 9F-% E9% A1% 95% E9% 83% 8E / dp / 4062579898 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & crid = 1Y34EY7W6ONAV & keywords =% E5% 85% A5% E9% 96% 80% E8% 80% 85% E3% 81% AElinux & qid = 15181934995 & sprefix =% E5% 85% A5% E9% 96% 80% 2Caps% 2C171 & sr = 8-1)
Recommended Posts