Home >System Tutorial >LINUX >Want to become a Linux master? Full analysis of mkdir, cat, touch command syntax and examples

Want to become a Linux master? Full analysis of mkdir, cat, touch command syntax and examples

WBOY
WBOYOriginal
2024-06-13 19:58:44525browse

想成为 Linux 高手?mkdir、cat、touch 命令语法及示例全解析

Directory mkdir command sentence pattern command format command function command parameter mkdir command example Example 1: Create an empty directory Example 2: Create multiple directories recursively Example 3: Create a directory with permission 777 Example 4: Display information when creating a new directory Example 5: One command creates the directory structure of a project cat command sentence pattern command format command function command parameters cat command example Example 1: Add the line number to the file content of log2012.log and enter log2013.log Example 2 in this file: Add line numbers to the file contents of log2012.log and log2013.log (blank lines are not added) and then append the contents to log.log. Example 3: Use heredoc to create a file in the future touch command Sentence pattern Command format Command parameter Command function touch command Example Example 1: Create a non-existent file The difference between vi and vim How to use vi/vim Command mode: Input mode Bottom line Command mode vi/vim Usage example Use vi/vim to enter the normal mode. Press the ESC key to return to the normal mode. Press: wq in the normal mode to save and exit vi

mkdir command syntax The linuxmkdir command is used to create a directory with a specified name. The user who creates the directory is required to have write permissions in the current directory, and the specified directory name cannot be an existing directory in the current directory.

Command format mkdir[option] directory...

Command function The mkdir command can be used to create a folder or directory named with DirName (specified file name) at the specified location.

The user who wants to create a folder or directory must have write permissions on the parent file of the created folder.

However, the created folder (directory) cannot have the same name as the file name in its parent directory (that is, the parent folder), that is, there cannot be one with the same name in the same directory (case sensitive).

Command parameters -m, --mode=mode, set permissions (similar to chmod), instead of rwxrwxrwx minus umask-p, --parents can be a path name. At this time, if individual directories in the path do not exist yet, after adding this option, the system will manually build these directories that do not yet exist, that is, multiple directories can be built at one time; -v, --verbose will be created each time a new directory is created. Display information –help Display this help information and exit –version Output version information and exit

mkdir command example

Example 1: Create an empty directory command:

mkdir test1

Output:

[root@localhost soft]# cd test
[root@localhost test]# mkdir test1
[root@localhost test]# ll
总计 4drwxr-xr-x 2 root root 4096 10-25 17:42 test1
[root@localhost test]#

Example 2: Recursively create multiple directories command:

mkdir -p test2/test22

Output:

[root@localhost test]# mkdir -p test2/test22
[root@localhost test]# ll
总计 8drwxr-xr-x 2 root root 4096 10-25 17:42 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
[root@localhost test]# cd test2/
[root@localhost test2]# ll
总计 4drwxr-xr-x 2 root root 4096 10-25 17:44 test22
[root@localhost test2]#

Example 3: Create a directory command with permission 777:

mkdir -m 777 test3

Output:

[root@localhost test]# mkdir -m 777 test3
[root@localhost test]# ll
总计 12drwxr-xr-x 2 root root 4096 10-25 17:42 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
[root@localhost test]#

illustrate:

The permissions of test3 are rwxrwxrwx

Example 4: Display information when creating a new directory:

mkdir -v test4

Output:

[root@localhost test]# mkdir -v test4
mkdir: 已创建目录 “test4”
[root@localhost test]# mkdir -vp test5/test5-1
mkdir: 已创建目录 “test5”
mkdir: 已创建目录 “test5/test5-1”
[root@localhost test]#

实例五:一个命令创建项目的目录结构命令:

mkdir -vp scf/{lib/,bin/,doc/{info,product},logs/{info,product},service/deploy/{info,product}}

输出:

[root@localhost test]# mkdir -vp scf/{lib/,bin/,doc/{info,product},logs/{info,product},service/deploy/{info,product}}
mkdir: 已创建目录 “scf”
mkdir: 已创建目录 “scf/lib”
mkdir: 已创建目录 “scf/bin”
mkdir: 已创建目录 “scf/doc”
mkdir: 已创建目录 “scf/doc/info”
mkdir: 已创建目录 “scf/doc/product”
mkdir: 已创建目录 “scf/logs”
mkdir: 已创建目录 “scf/logs/info”
mkdir: 已创建目录 “scf/logs/product”
mkdir: 已创建目录 “scf/service”
mkdir: 已创建目录 “scf/service/deploy”
mkdir: 已创建目录 “scf/service/deploy/info”
mkdir: 已创建目录 “scf/service/deploy/product”
[root@localhost test]# tree scf/
scf/
|-- bin
|-- doc
||-- info
|`-- product
|-- lib
|-- logs
||-- info
|`-- product
`-- service
 `-- deploy
|-- info
 `-- product
12 directories, 0 files
[root@localhost test]#

cat命令句型cat命令的用途是联接文件或标准输入并复印。这个命令常拿来显示文件内容,或则将几个文件联接上去显示,或则从标准输入读取内容并显示,它常与重定向符号配合使用。

命令格式cat[选项][文件]…

命令功能cat主要有三大功能:

一次显示整个文件:catfilename从按键创建一个文件:cat>filename只能创建新文件,不能编辑已有文件.将几个文件合并为一个文件:catfile1file2>file命令参数

-A, --show-all等价于 -vET
-b, --number-nonblank对非空输出行编号
-e等价于 -vE
-E, --show-ends 在每行结束处显示 $
-n, --number 对输出的所有行编号,由1开始对所有输出的行数编号
-s, --squeeze-blank 有连续两行以上的空白行,就代换为一行的空白行 
-t与 -vT 等价
-T, --show-tabs 将跳格字符显示为 ^I
-u(被忽略)
-v, --show-nonprinting使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

cat命令示例

实例一:把log2012.log的文件内容加上行号后输入log2013.log这个文件里命令:

cat -n log2012.log log2013.log 

输出:

[root@localhost test]# cat log2012.log 
2012-01
2012-02
======[root@localhost test]# cat log2013.log 
2013-01
2013-02
2013-03
======[root@localhost test]# cat -n log2012.log log2013.log 
 1 2012-01
 2 2012-02
 3
 4
 5 ======
 6 2013-01
 7 2013-02
 8
 9
 10 2013-03
 11 ======[root@localhost test]#

说明:

实例二:把log2012.log和log2013.log的文件内容加上行号(空白行不加)以后将内容附加到log.log里。命令:

选项越多人的选择越困难_linux mkdir选项_选项卡

cat -b log2012.log log2013.log log.log

输出:

[root@localhost test]# cat -b log2012.log log2013.log log.log
 1 2012-01
 2 2012-02
 3 ======
 4 2013-01
 5 2013-02
 6 2013-03
 7 ======[root@localhost test]#

实例三:使用heredoc来世成文件输出:

[root@localhost test]# cat >log.txt < Hello
> World
> Linux
> PWD=$(pwd)
> EOF
[root@localhost test]# ls -l log.txt
-rw-r–r-- 1 root root 37 10-28 17:07 log.txt
[root@localhost test]# cat log.txt
Hello
World
Linux
PWD=/opt/soft/test
[root@localhost test]#

说明:

注意斜体部份,heredoc可以进行字符串替换。

备注:

tac(反向列示)

命令:

tac log.txt

输出:

[root@localhost test]# tac log.txt 
PWD=/opt/soft/test
Linux
World
Hello

说明:

tac是将cat反写过来,所以他的功能就跟cat相反,cat是由第一行到最后一行连续显示在荧幕上,而tac则是由最后一行到第一行反向在荧幕上显示下来!

touch命令句型linux的touch命令不常用,通常在使用make的时侯可能会用到,拿来更改文件时间戳,或则新建一个不存在的文件。

命令格式touch[选项]…文件…

命令参数-a或–time=atime或–time=access或–time=use只修改存取时间。-c或–no-create不完善任何文档。-d使用指定的日期时间,而非现今的时间。-f此参数将忽视不予处理,仅负责解决BSD版本touch指令的兼容性问题。-m或–time=mtime或–time=modify只更改变动时间。-r把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。-t使用指定的日期时间,而非现今的时间。命令功能touch命令参数可修改文档或目录的日期时间,包括存取时间和修改时间。

touch命令示例

实例一:创建不存在的文件命令:

touch log2012.log log2013.log

linux mkdir选项_选项卡_选项越多人的选择越困难

输出:

[root@localhost test]# touch log2012.log log2013.log
[root@localhost test]# ll
-rw-r--r-- 1 root root0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root0 10-28 16:01 log2013.log

假如log2014.log不存在,则不创建文件

[root@localhost test]# touch -c log2014.log
[root@localhost test]# ll
-rw-r--r-- 1 root root0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root0 10-28 16:01 log2013.log

实例二:更新log.log的时间和log2012.log时间戳相同

命令:

touch -r log.log log2012.log

输出:

[root@localhost test]# ll
-rw-r--r-- 1 root root0 10-28 16:01 log2012.log
-rw-r--r-- 1 root root0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root0 10-28 14:48 log.log
[root@localhost test]# touch -r log.log log2012.log 
[root@localhost test]# ll
-rw-r--r-- 1 root root0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root0 10-28 14:48 log.log

实例三:设定文件的时间戳

命令:

touch -t 201211142234.50 log.log

输出:

[root@localhost test]# ll
-rw-r--r-- 1 root root0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root0 10-28 14:48 log.log
[root@localhost test]# touch -t 201211142234.50 log.log
[root@localhost test]# ll
-rw-r--r-- 1 root root0 10-28 14:48 log2012.log
-rw-r--r-- 1 root root0 10-28 16:01 log2013.log
-rw-r--r-- 1 root root0 2012-11-14 log.log

说明:

-ttime使用指定的时间值time作为指定文件相应时间戳记的新值.此处的time规定为如下方式的十补码数:

[[CC]YY]MMDDhhmm[.SS]

这儿,CC为年数中的前两位,即”世纪数”;

YY为年数的后两位,即某世纪中的年数.假若不给出CC的值,则touch将把年数CCYY限定在1969–2068之内.

MM为月数,DD为天将把年数CCYY限定在1969–2068之内.

MM为月数,DD为天数,hh为小时数(几点),mm为分钟数,SS为秒数.

选项卡_linux mkdir选项_选项越多人的选择越困难

此处秒的设定范围是0–61,这样可以处理闰秒.

这种数字组成的时间是环境变量TZ指定的时区中的一个时间.

因为系统的限制,早于1970年1月1日的时间是错误的。

The difference between vi and vim The vi editor is the standard editor under all Unix and Linux systems. It is equivalent to Notepad in Windows systems. Its power is not inferior to any latest text editor.

It is an indispensable tool for us to use Linux systems.

Because the vi editor is exactly the same for any version of Unix and Linux systems, after learning it, you will be able to move smoothly in the Linux world.

vim has the ability to edit programs, and can distinguish the correctness of sentence patterns by font color, which facilitates program design; because the program is simple, the editing speed is quite fast.

vim can be regarded as an upgraded version of vi. It can display some special information in a variety of colors.

vim will determine the content of the file based on the file extension or the beginning information in the file, manually execute the sentence pattern judgment of the program, and then display the program code and general information in color.

Vim has added many additional features, such as support for regular expression search, multi-file editing, block copy, etc. This is a great feature when we are working on some configuration file changes on Linux.

The use of vi/vim is basically divided into three modes: command mode (Commandmode), input mode (Insertmode) and bottom line command mode (Lastlinemode).

The functions of these three modes are:

Command mode: The user just started vi/vim and entered the command mode.

In this state, keystrokes will be recognized by Vim as commands instead of entering characters.

For example, if we press i at this time, we will not enter a character. i is regarded as a command.

The following are some commonly used commands:

iSwitch to input mode to enter characters. x deletes the character at the current cursor location. :Switch to bottom line command mode to enter commands on the bottom line. If you want to edit text: Start Vim, enter command mode, press i to switch to input mode.

The command mode only has some basic commands, so you still need to use the bottom line command mode to enter more commands.

Input mode Press i in command mode to enter input mode.

In input mode, the following keyboards can be used:

Character keyboard and Shift combination, enter the character ENTER, enter key, line feed BACKSPACE, backspace key linux simulation, delete the character before the cursor DEL, delete key, delete the character after the cursor direction key, in the text Unicom cursor HOME/END, Unicom cursor to the beginning/end of the line PageUp/PageDownlinux mkdir option, page up/down Insert, switch the cursor to input/replacement mode, the cursor will become a vertical line/pause No. ESC, exit input mode and switch to command mode

Underline command mode Press: (English quotation marks) in command mode to enter the underline command mode.

The bottom line command mode allows you to enter single or multiple character commands, and there are many available commands.

In the bottom line command mode, the basic commands are (the comma has been omitted):

qExit the program wSave the file Press the ESC key to exit the bottom line command mode at any time.

简单的说,我们可以将这三个模式想成下边的图标来表示:

img

vi/vim使用实例

使用vi/vim步入通常模式假如你想要使用vi来构建一个名为runoob.txt的文件时,你可以这样做:

$ vim runoob.txt

直接输入vi文件名就能否步入vi的通常模式了。请注意,记得vi前面一定要加文件名,不管该文件存在与否!

img

按下i步入输入模式(俗称为编辑模式),开始编辑文字

在通常模式之中,只要按下i,o,a等字符就可以步入输入模式了!

在编辑模式当中,你可以发觉在左下角状态栏中会出现–INSERT-的字样,那就是可以输入任意字符的提示。

这个时侯,鼠标上不仅Esc这个按钮之外,其他的键盘都可以视作为通常的输入按键了,所以你可以进行任何的编辑。

img

按下ESC按键回到通常模式好了,假定我早已根据前面的款式给他编辑完毕了,这么应当要怎么退出呢?是的!没错!就是给他按下Esc这个按键即可!马上你才会发觉画面左下角的–INSERT–不见了!

在通常模式中按下:wq存储后离开viOK,我们要存档了linux mkdir选项,镜象并离开的指令很简单linux 虚拟主机,输入:wq即可保存离开!

OK!这样我们就成功创建了一个runoob.txt的文件。

到此这篇关于Linux命令之mkdir,cat,touch,vi/vim的解读的文章就介绍到这了,更多相关Linux之mkdir,cat,touch,vi/vim内容请搜索曾经的文章或继续浏览下边的相关文章希望你们之后多多支持!

The above is the detailed content of Want to become a Linux master? Full analysis of mkdir, cat, touch command syntax and examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn