Linux self-study journey-basic commands (3)

Linux self-study journey-basic commands (3)


Preface

1. In the previous section, we talked about some basic commands for operating directories. Since there are directory operations, should there be operating commands for files?

2. Then we will introduce some basic file operation commands in this section


Tip: The following is the content of this article

1. Review of the previous section

In the previous section, we mainly introduced the following basic commands for operating directories and shutdown and restart commands {

1. "ls": Display the contents under the directory

2. "cd": switch current working path

3. "pwd": Display the current working path

4. "mkdir": Create an empty directory

5. "rmdir": delete empty directories

6.shutdown -h now: Shut down immediately

7.shutdown -r now: restart immediately

(If you want to know more about how to use these commands, please click the link below to visit:
                              Linux basic commands (1) )
}


2. (File/Directory) Time

1. Before starting to introduce file operation commands, let's talk about the time (file/directory) under Linux (that is, files and directories under Linux have their own time), three parameters {

1. Access Time (Access Time, referred to as atime): Every time we read a file or view a file, the time will be modified [For example, your file is a text, if I use the command to view the contents of the file to view your text file If there is something in it, I will change the interview time once I see it]

2. Data modification time (Modify Time, mtime for short): When the data content of the file is modified, the time will change (I checked what is in your text file before, so if I am modifying it, change If the content inside changes, the data modification time will change)

3. Status modification time (Change Time, referred to as ctime): When the file permissions or attributes are modified, the time will also change (it can be understood as the "access time" or "data modification time" in front of me as long as there is Change, then my time will also change along with it. Because for the file, even if your access time has changed and other things have not changed, it can be regarded as the attribute change of the file itself)
}


3. File operation commands (1)

1.stat command

stat is a command to view the detailed information of a file or directory .

  • Command name: stat
  • The full name of the command: display file or file system status
  • Location: /usr/bin/stat
  • Execution authority: all users
  • Function description: View the detailed information of a file or directory (it can be understood as something that pops up after right-clicking on an application under windows)
命令格式
stat 文件/目录

for example:

[root@localhost ~]# ls -lh
总用量 48K
-rw-r--r--. 1 root root    0 1月  14 07:53 2020-01-01 18:01
-rw-r--r--. 1 root root    0 1月  14 07:52 2020-01-02 09:10:33
drwxr-xr-x. 2 root root 4.0K 1月  13 07:59 aa
-rw-------. 1 root root 1.2K 1月  11 11:22 anaconda-ks.cfg
-rw-r--r--. 1 root root  27K 1月  11 11:22 install.log
-rw-r--r--. 1 root root 7.4K 1月  11 11:21 install.log.syslog
[root@localhost ~]# stat aa/	【(stat xx)stat后接目录或文件名】
  File: "aa/"
  Size: 4096      	Blocks: 8          IO Block: 4096   目录
Device: 803h/2051d	Inode: 919029      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-01-14 07:56:51.227165819 +0800
Modify: 2021-01-13 07:59:57.512804253 +0800
Change: 2021-01-14 07:55:07.394163343 +0800
[root@localhost ~]# 

从上至下的解释:
(File:名称		Size:大小		Blocks:块		IO Block:IO块
Device:设备		Inode:节点		Links:硬链接	Access:权限
Uid:用户id		Gid:组id		Access:访问时间
Modify:数据修改时间		Change:状态修改时间)
【仔细注意下面三条的时间,就是我们前面讲的Linux下文件或目录的三条时间】

~~

~~

2.touch command

So now that we have a command to view the detailed information of a file or directory, we already know how to create a directory, so we also want to view the detailed content of our file, then where do we create the file? touch can help you: The
main function of the touch command is to create an empty file or modify the file time.

  • Command name: touch
  • The full name of the command: change file timestamps
  • Location: /bin/touch
  • Execution authority: all users
  • Function description: Create empty file or modify file time
命令格式
touch [选项] 文件名
选项:
-a:只修改文件的访问时间
-c:仅修改文件的时间参数(3个时间参数都改变),如果文件不存在,则不建立新文件
-d:后面可以跟欲修订的日期,而不用当前的日期,即把文件的atime和mtime时间改为指定的时间
-m:只修改文件的数据修改时间
-t:命令后面可以跟欲修订的时间,而不用目前的时间,时间书写格式为【YYMMDDhhmm】

So let me first give two examples of using touch to modify file time (commonly used modification time examples):

(修改文件访问时间)
[root@localhost ~]# ls -lh
总用量 48K
-rw-r--r--. 1 root root    0 1月  14 07:53 2020-01-01 18:01
-rw-r--r--. 1 root root    0 1月  14 07:52 2020-01-02 09:10:33
drwxr-xr-x. 2 root root 4.0K 1月  13 07:59 aa
-rw-------. 1 root root 1.2K 1月  11 11:22 anaconda-ks.cfg
-rw-r--r--. 1 root root  27K 1月  11 11:22 install.log
-rw-r--r--. 1 root root 7.4K 1月  11 11:21 install.log.syslog
[root@localhost ~]# 
[root@localhost ~]# stat aa/
  File: "aa/"
  Size: 4096      	Blocks: 8          IO Block: 4096   目录
Device: 803h/2051d	Inode: 919029      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-01-14 07:56:51.227165819 +0800
Modify: 2021-01-13 07:59:57.512804253 +0800
Change: 2021-01-14 07:55:07.394163343 +0800
[root@localhost ~]# 
[root@localhost ~]# touch -a -d "2020-01-01 13:11" aa/
[root@localhost ~]# 
[root@localhost ~]# stat aa/
  File: "aa/"
  Size: 4096      	Blocks: 8          IO Block: 4096   目录
Device: 803h/2051d	Inode: 919029      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-01-01 13:11:00.000000000 +0800
Modify: 2021-01-13 07:59:57.512804253 +0800
Change: 2021-01-14 08:19:13.022154519 +0800
[root@localhost ~]# 

(如上,我目录下有aa这个目录,我用stat查看了aa目录的访问时间,
发现它的访问时间是“2021-01-14 07:56:51”--2021年1月14日7点56分51秒访问过

那么我们知道它的原始时间后,我们利用“touch -a -d “2020-01-01 13:11 aa/””
的方式对它的访问时间进行了一个修改;-a是只修改访问时间,-d是指后面可跟上一段
要修改的时间,修改完之后再次stat查看发现时间就变成了我们修改的时间)
(修改数据修改时间)
[root@localhost ~]# ls -lh
总用量 48K
-rw-r--r--. 1 root root    0 1月  14 07:53 2020-01-01 18:01
-rw-r--r--. 1 root root    0 1月  14 07:52 2020-01-02 09:10:33
drwxr-xr-x. 2 root root 4.0K 1月  13 07:59 aa
-rw-------. 1 root root 1.2K 1月  11 11:22 anaconda-ks.cfg
-rw-r--r--. 1 root root  27K 1月  11 11:22 install.log
-rw-r--r--. 1 root root 7.4K 1月  11 11:21 install.log.syslog
[root@localhost ~]# stat aa/
  File: "aa/"
  Size: 4096      	Blocks: 8          IO Block: 4096   目录
Device: 803h/2051d	Inode: 919029      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-01-01 13:11:00.000000000 +0800
Modify: 2021-01-13 07:59:57.512804253 +0800
Change: 2021-01-14 08:19:13.022154519 +0800
[root@localhost ~]# touch -m -d "1999-01-01 12:01" aa/
[root@localhost ~]# stat aa/
  File: "aa/"
  Size: 4096      	Blocks: 8          IO Block: 4096   目录
Device: 803h/2051d	Inode: 919029      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-01-01 13:11:00.000000000 +0800
Modify: 1999-01-01 12:01:00.000000000 +0800
Change: 2021-01-14 08:27:35.851153422 +0800
[root@localhost ~]# 

(这个例子我们将-a只修改访问时间换成了 -m只修改数据修改时间,
后面依旧-d并接上要修改的时间,最后stat再查看一下就发现已经更改)

【记住,最后一个状态修改时间是不能修改的,它是随着你前面的“访问时间”和
“数据修改时间”变化而变化的;但是它不是你前面修改了什么时间他就会跟着变成
什么时间,而是你修改了前面的时间之后,状态修改时间会自动变成系统的最新的时间
----举个例子,比如你访问时间修改成了“1999-11-11 12:11”,状态修改时间
的话他是不会也变成“1999-11-11 12:11”的,而是你修改成功的时候系统当前什么
时间,他就是什么时间】

Finally, I also said that touch can create files. Let's give an example of creating files:
(创建文件)
[root@localhost aa]# ls -lha
总用量 8.0K
drwxr-xr-x. 2 root root 4.0K 1月   1 1999 .
dr-xr-x---. 3 root root 4.0K 1月  14 08:07 ..
[root@localhost aa]# 
[root@localhost aa]# touch snljh
[root@localhost aa]# ls -lha
总用量 8.0K
drwxr-xr-x. 2 root root 4.0K 1月  14 08:37 .
dr-xr-x---. 3 root root 4.0K 1月  14 08:07 ..
-rw-r--r--. 1 root root    0 1月  14 08:37 snljh
[root@localhost aa]# 

(直接touch后面接一个文件名就可以直接创建出一个文件了)

~~

~~

3.cat command

So we have created the file, but I always feel something is wrong. We remembered the previous section, we can create and enter the directory, then we create a file in this section, can we enter the file? Can duck, but it is not called to enter the file, we are checking what is in the file.
The cat command is used to view the data content of the file itself

  • Command name: cat
  • The full name of the command: concatenate files and print on the standard output
  • Location: /bin/cat
  • Execution authority: all users
  • Function description: View file content and standard output (view the content in the file and display it directly on the screen)
命令格式
cat [选项] 文件
选项:
-A:相当于-vET选项的集合,用于列出所有隐藏符号
-E:列出每行结尾的回车符$
-n:显示行号
-T:把Tab键用^显示出来
-v:列出特殊字符

Let us cite two common examples at random:

(直接查看)
[root@localhost aa]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m

[root@localhost aa]# 

(直接cat后面接你要查看的文件的路径,
如上使用的是绝对路径查看,
我查看的是“根目录下的etc目录下的名叫issue的文件的内容”,
要你的文件里面有东西他才会显示,要不然没东西只显示空白)
-n进行查看)
[root@localhost aa]# cat -n /etc/issue
     1	CentOS release 6.6 (Final)
     2	Kernel \r on an \m
     3	
[root@localhost aa]# 

(还是查看issue这个文件,不过我加了-n选项,文本就会在前面自动显示行号,
如上,我有三行数据【最后一行有数据的,大家可以自行-A显示特殊符号查看一下】)

~~

~~

4.more command

There are many kinds of commands for viewing files, cat is just one of them, now we will introduce the second one;
more is to view files in split screen

  • Command name: more
  • The full name of the command: file perusal filter for crt viewin
  • Location: /bin/more
  • Execution authority: all users
  • Function description: View files in split screen (turn up and down page by page)
命令格式
more 文件名

Give 2 examples:

(查看内容少的文件)
[root@localhost aa]# 
[root@localhost aa]# more /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m

[root@localhost aa]# 

(还是刚才那个文件,和cat一样的操作,
这时候有人问了,这不是和cat一样么?那直接cat不香嘛,cat功能还多;
那么请看下面的例子)
cat查看内容多的文件)
[root@localhost aa]# cat /var/log/anaconda.log 
......
11:22:06,271 INFO    : leaving (1) step writeksconfig
11:22:06,271 INFO    : moving (1) to step setfilecon
11:22:06,271 DEBUG   : setfilecon is a direct step
11:22:06,271 INFO    : setting SELinux contexts for anaconda created files
11:22:11,196 INFO    : leaving (1) step setfilecon
11:22:11,196 INFO    : moving (1) to step copylogs
11:22:11,196 DEBUG   : copylogs is a direct step
11:22:11,196 INFO    : Copying anaconda logs
[root@localhost aa]# cat有一个缺陷,就是如果你的文件内容过多的话,
cat直接查看会直接跳到文件的最后几行,前面的你想看的话还得自己慢慢翻上去;
如上,log目录下的anaconda.log文件内容很多的,大家可以自己试试cat看看,
就可以知道缺陷了,你想看第一条的内容还得自己慢慢往上面翻,还不一定翻得到)
(more查看长文件)
[root@localhost aa]# more /var/log/anaconda.log 
08:56:06,299 INFO    : stage2 url is cdrom:///dev/sr0:/mnt/stage2
08:56:06,341 INFO    : Loading SELinux policy
08:56:06,892 INFO    : getting ready to spawn shell now
08:56:07,129 INFO    : Running anaconda script /usr/bin/anaconda
08:56:08,452 INFO    : CentOS Linux is the highest priority installclass, using it
08:56:08,457 INFO    : created new libuser.conf at /tmp/libuser.4kQJyR with instPath="/mnt/sysimage"
08:56:08,458 INFO    : anaconda called with cmdline = ['/usr/bin/anaconda', '--stage2', 'cdrom:///dev/sr0:/mnt/stage2', '--grap
hical', '--selinux']
--More--(8%)
......
[root@localhost aa]#

(可以知道more是翻页式从第一页开始游览文件的,就是一页一页看的。操作指令如下:

 - 空格键:向下翻页(空格一次就是看下一页一次)
 - b:向上翻页(上一页的意思)
 - 回车键:下一行(一行一行的看的意思)
 - /字符串(直接/后面加上你要搜索的字符串,他会自动搜索你想找的东西)
 - q:退出(退出more查看文件内容)
以上选项都是直接按键盘就会生效了的,大家自己试试吧
)

~~

~~

5.less command

less is also a command to view files, similar to more, but less displays the contents of the file in separate lines

  • Command name: less
  • The full name of the command: opposite of more
  • Location: /usr/bin/less
  • Execution authority: all users
  • Function description: branch display content (support small keyboard to scroll down and up line by line)

The command operation of the less command is basically the same as that of more, but less can support the up, down, left, and right keys of the small keyboard to view the content of the file, and you can more carefully review the data of each line .
(For the basic command operation of less, please refer to more , just press the corresponding key of the keyboard. In addition, less has the up, down, left and right keys on the small keyboard that more does not have to view the file)-it means that the lower right corner of your computer keyboard does not have the following picture Press the key, you can directly operate the file content on the small keyboard;-I will not demonstrate
it here [then someone here will say that less is the same as more? So is it necessary for these two commands to exist together? I will summarize the specific command comparison and talk to you later]
Insert picture description here


to sum up

In this section, we talked about some simple file operation commands, but this file operation command chapter is not over, and there are some commands that are not covered, but because the space is really a bit too much, I want to leave it to the next section and continue to talk about the rest Command, then we review the commands learned in this section {

1.stat: View detailed information of files or directories

2.touch: file creation or file modification time (if the time is modified, the access time and data modification time can only be changed)

3.cat: View the content of the file and standard output (display directly to the screen)

4.more: View files in pages (turn one page by one)

5.less: View the file by branch (turn it line by line)
}
Today we learned a lot of commands to view the contents of a file, let’s make a comparison:

If you want to view files with little content: cat command

If you want to view a file with a lot of content: the less command (basically it can replace more, you can scroll line by line on the keypad, or you can perform all operations on more)

This is Jiehua, see you next time.

Guess you like

Origin blog.csdn.net/qq313088385/article/details/112604613