Linux basic commands (detailed)

This blog contains the use of the following commands:

  • locale (language-dependent)
  • date (date-related)
  • cal (calendar)
  • bc (computer)
  • --help (help command help)
  • man (man manual Help)
  • CD (change directory)
  • pwd (shows the current working directory)
  • mkdir (create folders)
  • rmdir (remove empty folder)
  • ls (see directory contents)
  • cp (copy)
  • rm (delete)
  • mv (move)
  • cat (view file contents)
  • head (see the beginning of the file a few lines)
  • tail (see the end of the file a few lines)
  • touch (create a file or directory)
  • find (find)
  • grep (filter)

command locale (language-dependent):

Inquire:

[root@localhost ~]# locale            #查询系统语言
LANG=zh_CN.UTF-8       #语言语系输出
LC_CTYPE="zh_CN.UTF-8"      #输出使用语系类型
LC_NUMERIC="zh_CN.UTF-8"      #数字方面语系数据
LC_TIME="zh_CN.UTF-8"      #时间方面语系数据
LC_COLLATE="zh_CN.UTF-8"      #校验方面
LC_MONETARY="zh_CN.UTF-8"      #货币方面
LC_MESSAGES="zh_CN.UTF-8"      #信息方面
LC_PAPER="zh_CN.UTF-8"      #文件方面
LC_NAME="zh_CN.UTF-8"      #命名方面
LC_ADDRESS="zh_CN.UTF-8"      #路径方面
LC_TELEPHONE="zh_CN.UTF-8"      #电话方面
LC_MEASUREMENT="zh_CN.UTF-8"      #测量方面
LC_IDENTIFICATION="zh_CN.UTF-8"      #×××明方面
LC_ALL=               #全部的数据同步更新的设置值

Modify the system language, for example:

[root@localhost ~]# LANG=en_US.utf8     #修改为英文
[root@localhost ~]# export LC_ALL=en_US.utf8    #输出到所有语言类型
[root@localhost ~]# locale      #查看是否更改成功
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=en_US.utf8
[root@localhost ~]# date        #查询日期,看到如下,说明更改成功
Tue Aug  6 12:38:35 CST 2019
[root@localhost ~]# LANG=zh_CN.utf8    #再改回来吧
[root@localhost ~]# export LC_ALL=zh_CN.utf8
[root@localhost ~]# date     #再查看日期
2019年 08月 06日 星期二 12:39:43 CST

date command (By Date)

[root@localhost ~]# date            #查看时间
2019年 08月 06日 星期二 12:39:43 CST
[root@localhost ~]# date -s "2018-05-17 09:51:50"   #更改为指定时间
2018年 05月 17日 星期四 09:51:50 CST
[root@localhost ~]# date     #查看是否更改成功
2018年 05月 17日 星期四 09:51:53 CST
[root@localhost ~]# hwclock –w    #将当前系统时间写入BIOS中

cal command (calendar)

1, the command format:
CAL [Options] [month] [year]
2, CAL command options:

-1, --one only displays the current month (default)
-3, --three show last month, month and next month
-s, --sunday Sunday as the first day of the week
-m, --monday Monday with first day of week
-j, --julian output Julian day
-y, --year output throughout the year
-V, --version display version information and exit
-h, --help display this help and exit

3, command functions:
to view calendar information such as time, as only one parameter, the year of (1-9999), if two parameters, then the month and year.

4. Example:

[root@localhost ~]# cal     #显示当前月份日历
      八月 2019     
日 一 二 三 四 五 六
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
[root@localhost ~]# cal 9 2019     #显示指定月份日历
      九月 2019     
日 一 二 三 四 五 六
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
[root@localhost ~]# cal -y 2013       #显示2013年日历
                               2013                               

                             ............................
[root@localhost ~]# cal -j   #显示自1月1日的天数
         八月 2019         
 日  一  二  三  四  五  六
                213 214 215
216 217 218 219 220 221 222
223 224 225 226 227 228 229
230 231 232 233 234 235 236
237 238 239 240 241 242 243
[root@localhost ~]# cal -m               #星期一显示在第一列
      八月 2019     
一 二 三 四 五 六 日
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

bc computer

1, various algorithms symbols:

+: Adder;
-: Subtraction;
*: Multiplication;
/: division;
^: index;
%: remainder;

2, use:

[root@localhost ~]# bc              #进入计算机环境
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2 Inc.
This is free software with ABSOLUTELY NO WANTY.
For details type `warranty'. 
1*5                          #直接进行运算即可,按回车键得到运算结果
5

Ctrl+C退出计算机环境

--help command (Command Help)

And man command functions as provided use of a command for us.

1, use:

[root@localhost ~]# date --help
Usage: date [OPTION]... [+FORMAT]               #date命令的基本语法
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
#下面是主要的选项说明
Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
                       ...............................#省略部分内容
#下面是可以定义内容输出的格式
FORMAT controls the output.  Interpreted sequences are:      

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
                           ...............................#省略部分内容
#下面是几个常用的范例
Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
  $ date --date='@2147483647'
                           ...............................#省略部分内容

The man command (man manual Help)

And --help same role.

1, use:

[root@localhost ~]# man date        #执行后将进入man环境
DATE(1)                            User Commands                            DATE(1)
#请注意上面的数字,(1)是命令代号,稍后解释命令代号。
NAME       #这个指令的完整全名,如下所示
       date - print or set the system date and time

SYNOPSIS    #这个指令的基本语法如下:
       date [OPTION]... [+FORMAT]  #第一种只是显示的用法
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
#上面这种是可以设置系统时间的用法
DESCRIPTION      #详细说明选项和参数的用法
       Display the current time in the given FORMAT, or set the system date.

       Mandatory arguments to long options are mandatory for short options too.

       -d, --date=STRING  #-d是短选项名称,-date为完整选项名称
              display time described by STRING, not 'now'

       -f, --file=DATEFILE
                           ...............................#省略部分内容            
#下面是格式化输出的详细数据                                               
FORMAT controls the output.  Interpreted sequences are:
                           ...............................#省略部分内容            

2, the command man manual code meanings are as follows (1,5,8 which is to keep in mind, you can understand the other):
Linux basic commands (detailed)

3, in the man page for some of the code's meaning:
Linux basic commands (detailed)

4, man manual environment, some of the operating buttons:

Linux basic commands (detailed)

cd command (change directory)

1, the special representative of the meaning of the symbol:

: This layer represents the directory.
..: represents the parent directory.

  • : Indicates the previous working directory.
    ~: The "current users' home directory.
    ~ account: This account represents the user's home directory.

2, usage example:

[root@localhost ~]# cd ~lv     #切换至用户“lv”的宿主目录
[root@localhost lv]# pwd             #查看是否在指定位置下
/home/lv
[root@localhost lv]# cd ..              #切换至上一层工作目录
[root@localhost home]# pwd      #查看
/home
[root@localhost home]# cd ./lv      
#切换至当前目录下的“lv”目录,一般直接“cd lv”即可。
[root@localhost lv]# pwd     #查看
/home/lv

pwd (shows the current working directory)

In the above command cd presentation, pwd command has been used, I believe that to understand the role of the pwd command, this command is also a main option, which is "-P".

[root@localhost lv]# cd /var/mail/      #切换到一个链接目录下
[root@localhost mail]# pwd                 #查询当前工作路径
/var/mail  
[root@localhost mail]# pwd -P     #加“-P”再查看下,发现路径不一样
/var/spool/mail

In the above example, because the / var / mail is a link file, link to / at / var / spool mail, plus the "-P" option displays the correct full path, rather than the linked file attributes display.

mkdir (create directory)

1, command options:

-p: Use multiple layers to create a recursive directory.
-m: permission settings file, otherwise the default permissions (umask).
-v: Details when creating output

2, -m option usage example:

[root@localhost ~]# mkdir -m 777 /test1/       #创建目录,并指定权限为777
[root@localhost ~]# ls -ld /test1/           #查看目录属性
drwxrwxrwx 2 root root 6 Aug  6 14:08 /test1/
#可以看到目录创建后的权限就是创建时指定的权限。

3, -p, -v option usage example:

[root@localhost ~]# mkdir -pv /t1/t2/t3     #使用“-p”创建多层目录
mkdir: created directory ‘/t1’
mkdir: created directory ‘/t1/t2’
mkdir: created directory ‘/t1/t2/t3’
#会输出创建成功的信息,这就是“-v”选项的作用,一般不用“-v”选项

rmdir (remove empty directories)

1, command options:

-p: together with the upper empty directories are deleted.

2, usage examples:

[root@localhost ~]# rmdir /test1/     #删除刚才创建的空目录test1
[root@localhost ~]# tree /t1      #查看目录结构
/t1
`-- t2
    `-- t3

2 directories, 0 files
#可以确认都是空目录,tree命令需要手动安装,这里不过多介绍。
[root@localhost /]# rmdir -p t1/t2/t3/            #加上“-p”选项
[root@localhost /]# tree /t1     #查看目录结构,确认已经被删除了
t1 [error opening dir]

0 directories, 0 files

Need to be assured that, rmdir only removes empty directories, "-p" is the option, if the parent directory is not empty, will be skipped.

ls (see directory contents)

1, command options:

-l:以长格式(long)显示文件和目录的列表,包括权限、大小、最后更新的时间等详细信息;
-a:显示所有(All)子目录和文件的信息,包括以“.”号开头的隐藏目录和隐藏文件。
-A:与“-a”效果差不多,只是不包括“.”和“..”这两个目录。
-f:直接列出结果、而不进行排序(ls默认以文件名排序),同时显示以“.”开头的隐藏文件。
-F:根据文件、目录等信息,给予附加数据结构,如:*:代表可执行文件;/:代表目录;=:代表socket文件;|:代表FIFO文件。
-i:将列出inode号码,关于inode会在以后的博文介绍。
-n:列出UID与GID而非使用者与群组的名称。
-d:显示目录(directory)本身的属性,而不是显示目录中的内容。

-h:结合-l选项一起使用,可以以人性化的方式显示文件或目录的大小,默认的大小单位为字节(b),使用-h选项后,将显示为KB、MB等单位。

-R:以递归(Recursive)的方式显示指定的目录和它子目录中所有的内容。
-r:将排序结果反向输出,例如:原本文件名由小到大,反向则会由大到小。
-S:以文件大小排序,而不是用文件名排序。
-t:依时间排序,而不是用文件名。
--color=never:不要依据文件特性给予颜色显示;
--color=always:显示颜色
--color=auto:让系统自行依据设置来判断是否给予颜色(该选项为默认选项)。
--full-time:以完整时间模式(包含年、月、日、时、分)输出
--time={atime,ctime}:输出access时间或改变权限属性时间(ctime)而不是内容变更时间(mtime)。

关于上面提到的文件属性:ctime、mtime、atime的解释如下:

  • mtime:当该文件的“内容数据”变更时,就会更新这个时间!内容数据是指文件的内容,而不是文件的属性或权限。
  • ctime:当该文件的“状态”改变时,就会更新这个时间,举例来说,就像是该文件的属性和权限被更改了,都会更改这个时间。
  • atime:当我们使用查看命令(如cat命令)去读取这个文件,就会更新该文件的atime。

2、用法示例:

[root@localhost ~]# ls -alF --color=never     
#不指定路径,则默认查看当前目录下

输出结果如下:
Linux basic commands (detailed)

可以看到输出结果只有一种颜色。

[root@localhost ~]# ls -alF     #去掉命令末尾的“--color”再查看一下

输出结果如下:
Linux basic commands (detailed)

在Linux中关于各种颜色的代表含义(使用Xshell等连接可能会不一样):

  • 深蓝色:表示目录。
  • 白色:表示一般文件。
  • 绿色:表示可执行文件。
  • ×××:表示设备文件。
  • 红色:表示压缩文件。
[root@localhost ~]# ls -al --full-time

输出结果如下:
Linux basic commands (detailed)

“-al”选项只能列出简单的时间,借助“--full-time”可以列出完整的时间格式。

cp(复制)

1、命令选项:

-a:相当于同时“-dr”,关于“-dr”可以参考下面说明。
-d:若来源文件是链接文件的属性,则复制链接文件属性而非文件本身。
-r:复制目录时必须使用此选项,表示递归复制所有文件及子目录。
-i:覆盖目标同名文件或目录时提醒用户确认(interactive,交互式)
-f:覆盖目标同名文件或目录时不进行提醒,而直接强制(force)复制。
-p: 复制时保持(preserve)源文件的权限、属主及时间标记等属性不变。
-u:目标比源旧才更新目标,或目标不存在的情况下才复制。

2、用法示例:

[root@localhost ~]# cp -r /etc/ /tmp    #将“/etc/”复制到“/tmp”目录下。

rm(删除)

1、命令选项:

-f:删除文件或目录时不进行提醒,而直接强制删除。
-r:删除目录时必须使用此选项,表示递归删除整个目录树。
-i:删除文件或目录时提醒用户确认。

2、用法示例:
默认rm命令已经加了“-i”选项,所以可以省略。

[root@localhost tmp]# rm -r vmware-root/     #删除该目录
rm: remove directory ‘vmware-root/’? y     #输入“y”确定

mv(移动文件或目录、重命名)

mv命令用于将指定的文件或目录转移位置,如果目标位置与原位置相同,则效果相当于为文件或目录改名。
1、命令选项:

-f:强制的意思,如果目标文件已经存在,不会询问而直接覆盖,慎用。
-i:若目标文件已存在,则会询问是否覆盖,默认使用了该选项。
-u:若目标文件已经存在,而且源比较新,才会覆盖。

2、用法示例:

[root@localhost tmp]# mv test1 test2    #将test1改名为test2
[root@localhost tmp]# mv test2 /home   #将test2移动到home目录下

cat、head、tail等命令

要把我写哭了,真的没耐心写下去了,关于cat、head、tail命令可以查看我最初写的博客:Linux中如何查看文件内容,不写了这个了。

touch命令(创建文件或目录)

touch这个命令最常用的的情况是:

  • Create a file or directory;
  • Will file a revised date for the current (mtime and atime);

Can see, touch command can not create a file or directory.

1, commonly used options:

-a: Change atime only;
-c: only file modification time, if the file does not exist a new file is created.
-d: behind can pick the date you want to modify rather than the current date, you can also use --date = "date or time";
-m: only modify mtime;
-t: the back can take time want to modify without the current time in the format [YYYYMMDDhhmm].

2, usage examples:

[root@localhost tmp]# touch test     #先新建一个空文件
[root@localhost tmp]# ls -l test     #观察时间
-rw-r--r-- 1 root root 0 Aug  6 16:01 test
#可以看到这个文件大小是0,在默认的状态下,如果touch后面有接文件,
#则该文件的三个时间(atime/ctime/mtime)都会更新为目前的时间。
#若该文件不存在,则会主动创建一个新的空文件。
[root@localhost tmp]# cp -a ~/.bashrc bashrc
#将 ~/.bashrc复制到当前目录下,并命名为bashrc。
[root@localhost tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc  
#查看该文件的各种时间及日期的属性
Tue Aug  6 16:05:36 CST 2019      #这是当前时间
-rw-r--r--. 1 root root 176 Dec 29  2013 bashrc   #这是mtime
-rw-r--r--. 1 root root 176 Aug  6 12:14 bashrc   #这是atime
-rw-r--r--. 1 root root 176 Aug  6 16:04 bashrc    #这是ctime
[root@localhost tmp]# touch -d "4 days ago" bashrc    
#将文件日期调整到四天前
[root@localhost tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc Tue Aug  6 16:11:22 CST 2019
#再查看各种时间
-rw-r--r--. 1 root root 176 Aug  2 16:11 bashrc
-rw-r--r--. 1 root root 176 Aug  2 16:11 bashrc
-rw-r--r--. 1 root root 176 Aug  6 16:11 bashrc
#会发现除了ctime之外,都改变了,
#ctime只有在文件属性发生改变后,才可以改变。
[root@localhost tmp]# touch -t 201406150202 bashrc 
#将该文件的日期改为2014/06/15 2:02
[root@localhost tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc Tue Aug  6 16:15:46 CST 2019
-rw-r--r--. 1 root root 176 Jun 15  2014 bashrc
-rw-r--r--. 1 root root 176 Jun 15  2014 bashrc
-rw-r--r--. 1 root root 176 Aug  6 16:15 bashrc
#还是除了ctime之外,都改变了。

find command (find)

Linux basic commands (detailed)

Linux basic commands (detailed)
Linux basic commands (detailed)
Linux basic commands (detailed)

Linux basic commands (detailed)

Linux basic commands (detailed)

grep command (filtered)

Linux basic commands (detailed)

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2427122