学习笔记-The Linux Command Line-WILLIAM(0)

301 阅读4分钟

1 说明

此系列作为学习The Linux Command Line此书的一个记录,类似于课堂笔记。因此会把书上比较重要的内容以及个人的理解写入。命令前是[me@linuxbox ~]为书中代码,而命令前带有[root@izuf67yuy6secatlp4sztez]类似字符串是实践代码;实践代码均是在centos 7上实现。

2 常用的Linux命令

[me@linuxbox ~]$ date     #显示当前时间
[me@linuxbox ~]$ cal      #显示日历
[me@linuxbox ~]$ df       #查看磁盘空间
[me@linuxbox ~]$ free     #查看内存空间
[me@linuxbox ~]$ exit     #结束终端session
[me@linuxbox ~]$ pwd      #Print name of current working directory.
[me@linuxbox ~]$ cd       #Change directory.
[me@linuxbox ~]$ ls       #List directory contents.
[me@linuxbox ~]$ cp       #Copy files and directories.
[me@linuxbox ~]$ mv       #Move/rename files and directories.
[me@linuxbox ~]$ mkdir    #Create directories.
[me@linuxbox ~]$ rm       #Remove files and directories.
[me@linuxbox ~]$ ln       #Create hard and symbolic links.
[me@linuxbox ~]$ type     #Indicate how a command name is interpreted.
[me@linuxbox ~]$ which    #Display which executable program will be executed.
[me@linuxbox ~]$ man      #Display a command’s manual page.
[me@linuxbox ~]$ apropos  #Display a list of appropriate commands.  与man -k 等价
[me@linuxbox ~]$ info     #Display a command’s info entry.GUN 提供了man 的另一个选择info
[me@linuxbox ~]$ alias    #Create an alias for a command.
[me@linuxbox ~]$ file     #Determine file type.
[me@linuxbox ~]$ less     #View file contents.

3 关于Linux的一些知识

3.1 关于文件系统

  • ls命令默认不显示隐藏文件,后面加上-a会显示隐藏文件,ls -a显示全部文件

  • 在Linux操作系统中,没有文件扩展名,但是程序中可能会带有文件扩展名来使得文件用途更加明确。

  • 在Linux操作系统中,涉及到目录表示时,.代表着当前目录,..代表着父目录

  • linux文件系统树(hierarchical directory structure),root文件夹在文件系统中是第一个文件夹。不像是windows系统上每个硬盘有独立的文件系统,类Unix操作系统总是只有一个文件系统。存储设备可以按照系统管理员的意愿挂载到树上的不同位置。

  • 在Linux操作系统中,每次登录到系统,初始目录为自己的home文件夹

  • 在Linux操作系统中,尽可能使文件名不要有空格

  • 在Linux操作系统中,文件名的大小写是敏感的,既在同一文件夹内testTEST两个文件为不同文件

3.2 关于ls 命令

  • 当执行了ls -l命令,结果的每列意义如下:
[root@izuf67yuy6secatlp4sztez bin]# ls -l
total 420364
-rwxr-xr-x. 1 root root      29096 Nov  5  2016 addr2line
...
-rwxr-xr-x. 1 root root 29096 Nov 5 2016 addr2line
Access rights to the file. The first character indicates the type of file. Among the different types, a leading dash means a regular file, while a d indicates a directory. The next three characters are the access rights for the file’s owner, the next three are for mem-bers of the file’s group, and the final three are for everyone else. File’s number of hard links. The user name of the file’s owner. The name of the group that owns the file. Size of the file in bytes. Date and time of the file’s last modification. Name of the file
  • 执行ls 命令后面可以指定多个文件夹,结果格式类似于下面所示:
[root@izuf67yuy6secatlp4sztez run]# ls ~ /run/
/root:

/run/:
atd.pid     crond.pid    dhclient-eth0.pid  lock   ...

3.3 关于链接

Linux系统中可以创建两种类型链接:硬链接(Hard Links)以及符号链接(Symbolic Links)

3.3.1 硬链接(Hard Links)

创建硬链接命令格式 :[me@linuxbox ~]$ln file link

默认情况下,每个文件都有一个单独的硬链接~文件名。当我们创建一个硬链接时,我们为一个文件创建一个额外的目录条目。目录中包含硬链接,当它被删除时,文件内容依然存在,直到文件所有的硬链接被删除。

以上特点使得硬链接有两个限制:

  • 1.这意味着链接不能引用与链接本身不在同一磁盘分区上的文件

  • 2.不能引用文件夹

如果想要看两个硬链接是否指向同一文件内容,可以使用命令:[me@linuxbox ~]$ls -li;结果的第一列如果编号相同则指向同一份文件。

上述命令依据为,可以尝试将文件分为两个部分~文件内容存储数据部分以及文件名部分。当创建硬链接,实际上创建了一个额外的文件名指向文件内容的索引节点(inode),因此只要索引节点相同一定指向同一文件。

3.3.2 符号链接文件(Symbolic Links)

创建符号链接文件命令格式 :[me@linuxbox ~]$ln -s item link

类似于windows中的快捷方式,除了rm命令外,对其进行操作是对目标文件进行操作。

3.4 关于通配符

命令行相比于图形化界面的优势主要是功能强大,灵活。可以看一个例子:只从源文件中拷贝目标文件夹中不存在的html文件以及新的html文件,只需要键入下面命令就可以了

[me@linuxbox ~]cp -u *.html destination

  • 通配符表格:
Wildcard Matches
* Any characters
? Any single character
[characters] Any character that is a member of the set characters
[!characters] Any character that is not a member of the set characters
[[:class:]] Any character that is a member of the specified class
  • 经常用到的字符类:
Character Class Matches
[:alnum:] Any alphanumeric character
[:alpha:] Any alphabetic character
[:digit:] Any numeral
[:lower:] Any lowercase letter
[:upper:] Any uppercase letter
  • 一些例子以及说明:
Examples PatternMatches
* All files
g* Any file beginning with g
b*.txt Any file beginning with b followed by any characters and ending with .txt
Data??? Any file beginning with Data followed by exactly three characters
[abc]* Any file beginning with either a, b, or c
BACKUP.[0-9][0-9][0-9] Any file beginning with BACKUP. followed by exactly three numerals
[[:upper:]]* Any file beginning with an uppercase letter
[![:digit:]]* Any file not beginning with a numeral
*[[:lower:]123] Any file ending with a lowercase letter or the numerals 1, 2, or 3
  • 通配符可以用在任何以文件名称作为参数的地方

  • 有些图形化文件管理器支持通配符,比如 Nautilus,Dolphib以及Konqueror

  • 避免使用诸如[a-z][A-Z]的字符区间,执行结果可能不是预期的结果

3.5 关于rm命令

执行rm命令的时候一定要小心,因为一旦执行了rm命令,就不能撤回这个命令了。尤其是在使用rm命令和通配符的时候,可能多键入一个空格结果相差十万八千里。比如,你想要删除一个文件夹中所有html文件,你键入[me@linuxbox ~]rm *.html,执行结果是正确的,但是可能手一抖在*号后面加上了一个空格,那么这个文件夹中所有文件都会被删除,这显然不是预期的结果。 当在rm命令中使用通配符的时候最好使用ls来测试一下通配符,查看是否是像要删除的文件,确认无误后使用向上箭头按键调出上个命令并且把ls换成rm命令删除。

3.6 关于alias命令

命令(commands)可以是以下其中一个:

  • 一个可执行的程序

  • 内置于shell本身的命令

  • 一个shell函数

  • 别名

可以通过 alias 命令来创建 命令,另外使用alias命令前使用type命令可以来确认命令是否正在使用:

创建命令格式:alias name='string' 删除命令格式:unalias name

示例:

[root@izuf67yuy6secatlp4sztez playground]# type foo
-bash: type: foo: not found
[root@izuf67yuy6secatlp4sztez playground]# alias foo='ls -l'
[root@izuf67yuy6secatlp4sztez playground]# type foo
foo is aliased to `ls -l`
[root@izuf67yuy6secatlp4sztez playground]# foo
total 12
-rw-r--r-- 1 root root   13 Mar 18 17:41 ls.txt
...
[root@izuf67yuy6secatlp4sztez playground]# unalias foo
[root@izuf67yuy6secatlp4sztez playground]# type foo
-bash: type: foo: not found