关于Linux的命令其实有个下面简单的概念,等到实际用到时再去深入即可。就像学习语文,遇到不认识的字去查字典,但是不用一上来就背字典。
Linux基础命令
文章目录
- Linux基础命令
- 命令简介
- 命令的构成
- 命令使用的原因
- 命令提示符
- 常用的命令
- ls
- ls实验
- cd
- cd实验
- pwd
- pwd实验
- 符号
- 通配符 *
- 通配符 ?
- *?实验
- | 管道
- | 管道实验
- 针对文件的的基本操作
- touch
- touch实验
- touch拓展实验
- rm
- rm实验
- mkdir
- mkdir实验
- rmdir
- rmdir 实验
- cp
- cp实验
- mv
- mv实验
- 针对文件内容的基本操作
- 文件的查看
- 文件查看实验
- 文件的修改
- echo实验
- 文件的过滤
- 文件过滤实验
- 帮助命令
- type
- --help 参数
- help
- man
- info(作为拓展内容)
- 关于时间的命令
- date
- date 实验
- date 拓展实验
- hwclock
- timedatectl
- cal
- Linux 基础命令作业
-
在教室物理机包括rhel7和rhel6两台实验虚拟机,上打开虚拟机命令: rht-vmctl start rhel7
- 在物理机上远程登陆到虚拟机rhel7 ssh root@rhel7-fN ,N 为你的机器编号, root密码都是uplooking,系统中有一个普通用户student,其密码为student。
- 在虚拟机中关闭虚拟机的命令为shutdown -h now,也可以在物理机上执行rht-vmctl poweroff rhel7。
-
在物理机上设置录屏视频,由于Gnome3的bug会有30秒时间限制,修正时间限制的命令如下:
[kiosk@fundation0 ~]$ gsettings set org.gnome.settings-daemon.plugins.media-keys max-screencast-length "uint32 0"
命令简介
命令的构成
命令字 选项 参数
- 命令分:内部命令、外部命令;
- 选项: - 单个字符 – 多个字符
- 参数:对谁执行这个命令,可以有多个,选项和参数可以互换位置
命令使用的原因
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。从这里开始学习Linux命令,本课程让你更清楚地了解和掌握它,在Linux中命令是讲究大小写的。使用命令即快速又能减少机器的性能消耗。
命令提示符
- # root 用户
- $ 一般用户
- [ 用户的身份 @ 主机名 当前位置 ]
当前位置显示的是目录名
[root@rhel7 ~]# hostname
www.dabao.com
[root@rhel7 ~]# groupadd tom
[root@rhel7 ~]# useradd -g tom tom
[root@rhel7 ~]# id tom
uid=501(tom) gid=501(tom) groups=501(tom)
[root@rhel7 ~]# su - tom
[tom@rhel7 ~]$ who
root tty1
2016-03-20 23:56 (:0)
root pts/0
2016-03-21 00:39 (:0.0)
常用的命令
ls
常用参数 | 含义及用法 |
---|---|
-l | long 的缩写 详细列出当前目录下的所有文件属性 七列 文件名 <=255 个字符 |
-d | directory 的缩写 查看当前目录本身的信息 |
-h | 以人性化的方式显示文件大小,录的大小并不代表目录内所有文件的大小 du -sh /etc<== 查看 etc 目录真正的大小 |
-a | 查看隐藏文件 以 . 开头的文件 |
-R | 查看多层目录 |
-b | 特殊字符将以 分割 ls 查看有特殊字符的文件 |
ls实验
[tom@rhel7 tmp]$ ls -hl
total 68K
srwxr-xr-x. 1 root root 0 Mar 20 23:59 gedit.root.3177893063
drwx------. 2 root root 4.0K Feb 5 18:24 keyring-JL7MKY
[tom@rhel7 tmp]$ ls -ld /tmp
drwxrwxrwt. 22 root root 4096 Mar 21 00:43 /tmp
[tom@rhel7 tmp]$ ls -la /tmp
total 96
drwxrwxrwt. 22 root root 4096 Mar 21 00:43 .
dr-xr-xr-x. 29 root root 4096 Mar 20 23:55 ..
drwx------. 2 root root 4096 Mar 20 23:56 .esd-0
drwx------. 2 cong cong 4096 Jan 1 18:33 .esd-500
[tom@rhel7 tmp]$ ls -bl
-rw-rw-r--. 1 tom tom 0 Mar 21 00:48 a\ b.txt
[tom@rhel7 tmp]$ ls -l
-rw-rw-r--. 1 tom tom 0 Mar 21 00:48 a b.txt
[tom@rhel7 tmp]$ mkdir -p a/b/c
[tom@rhel7 tmp]$ ls -lR a/
a/:
total 4
drwxrwxr-x. 3 tom tom 4096 Mar 21 00:50 b
a/b:
total 4
drwxrwxr-x. 2 tom tom 4096 Mar 21 00:50 c
a/b/c:
total 0
cd
cd | change directory 切换工作目录 |
---|---|
绝对路径 | 以根为起始的路径 |
相对路径 | ~当前用户的家目录 ;. 当前目录 ;… 上一层用户 ;- 回到上一次所在位置 |
cd实验
[tom@rhel7 tmp]$ cd /etc/
[tom@rhel7 etc]$ cd ~
[tom@rhel7 ~]$ cd .
[tom@rhel7 ~]$ cd ..
[tom@rhel7 home]$ cd -
/home/tom
pwd
pwd:print working directory 显示当前所在位置的绝对路径
pwd实验
[tom@rhel7 tmp]$ cd /etc/nginx
[tom@rhel7 nginx]$ cd conf.d
[tom@rhel7 conf.d]$ pwd
/etc/nginx/conf.d
符号
通配符 *
匹配任意多个字符
例如:a* 包括aa*、ab*、ac* 等等
通配符 ?
匹配任意单个字符
例如: a? 可以是ab、ac、ad、a1、a9、a#等等
*?实验
rm -f *1
rm -f 1*
rm -f 1*1
rm -f test?
想删除 test 后面有一个字符的文件
| 管道
output | input
对某些命令执行的结果去作操作,会用到管道
| 管道实验
详细列出 /tmp 目录下的文件,并截取以空格为分割的第三列
[tom@rhel7 ~]$ ls -l /tmp|cut -d" " -f3
tom
tom
root
root
root
详细列出 /tmp 目录下的文件,截取含有关键字 tom 的行,再截取以空格为分割的第一列内容
[tom@rhel7 ~]$ ls -l /tmp|grep tom|cut -d" " -f1
drwxrwxr-x.
-rw-rw-r--.
针对文件的的基本操作
touch
touch [filename]
创建文件,参数可以跟多个
如果要创建 50 个有规律的文件,例如 text1-text50
利用参数扩展
touch test{1..50}
touch test{a..e}
touch test{a..e}_{1..3}---> 会创建 a_1 a_2 a_3...
上帝之手,本来是用来修改文件时间戳的。文件的三个时间 ctime\mtime\atime
拓展内容:可以通过“stat”命令查看文件的三个时间
touch " " 可以放一些特殊字符
touch实验
[tom@rhel7 ~]$ touch test{a..c}_{1..4}
[tom@rhel7 ~]$ ls
testa_1 testa_4 testb_3 testc_2
testa_2 testb_1 testb_4 testc_3
testa_3 testb_2 testc_1 testc_4--full-time可以查看mtime的完整时间[tom@rhel7 ~]$ ls -l --full-time
total 0
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_1
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_2
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_3
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testa_4
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_1
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_2
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_3
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.853039590 +0800 testb_4
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_1
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_2
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_3
-rw-rw-r--. 1 tom tom 0 2016-03-21 01:31:22.854039544 +0800 testc_4[tom@rhel7 ~]$ touch "ab cd"[tom@rhel7 ~]$ ls -b
ab\ \ \ cd testa_3 testb_2 testc_1 testc_4
testa_1 testa_4 testb_3 testc_2
testa_2 testb_1 testb_4 testc_3
touch拓展实验
[booboo@rhel7 ~]$ touch booboo
[booboo@rhel7 ~]$ ll
total 0
-rw-rw-r--. 1 booboo booboo 0 Jun 15 23:28 booboo
[booboo@rhel7 ~]$ stat boobooFile: ‘booboo’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 143 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ booboo) Gid: ( 1001/ booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:28:55.041578819 -0400 #atime 文件最近一次被访问的时间
Modify: 2016-06-15 23:28:55.041578819 -0400 #mtime 文件内容最近一次修改的时间
Change: 2016-06-15 23:28:55.041578819 -0400 #ctime 文件属性最近一次修改的时间Birth: -
使用cat去访问booboo文件,可以发现atime被修改了
[booboo@rhel7 ~]$ cat booboo
[booboo@rhel7 ~]$ stat boobooFile: ‘booboo’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 143 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ booboo) Gid: ( 1001/ booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:32:35.898724748 -0400
Modify: 2016-06-15 23:28:55.041578819 -0400
Change: 2016-06-15 23:28:55.041578819 -0400Birth: -
通过chmod修改文件权限后,会看到ctime时间改变,通过ll命令看到的时间为mtime
[booboo@rhel7 ~]$ chmod 777 booboo
[booboo@rhel7 ~]$ ll
total 0
-rwxrwxrwx. 1 booboo booboo 0 Jun 15 23:28 booboo
[booboo@rhel7 ~]$ stat boobooFile: ‘booboo’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 143 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1001/ booboo) Gid: ( 1001/ booboo)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2016-06-15 23:32:35.898724748 -0400
Modify: 2016-06-15 23:28:55.041578819 -0400
Change: 2016-06-15 23:33:49.195445761 -0400Birth: -
通过echo命令向booboo文件追加一些内容,会看到mtime时间变了,并且ctime也变了,思考为什么?
[booboo@rhel7 ~]$ echo hi >&