- A+
最近都在学习Linux命令,渐渐觉得命令行模式是那么的简洁明了,短小精悍,并且功能强大。本文用来记录学习的过程,仅供学习分享使用,如有不足之处,还请指正。
Linux的目录结构
在Linux命令模式下,输入ls / 或者 先切换到根目录下,在使用ls命令。 即可查询Linux操作系统的目录结构,如下所示:
1 [root@bogon ~]# ls / 2 bin dev home lib64 mnt proc run srv tmp var 3 boot etc lib media opt root sbin sys usr 4 [root@bogon ~]# cd / 5 [root@bogon /]# ls 6 bin dev home lib64 mnt proc run srv tmp var 7 boot etc lib media opt root sbin sys usr
树状目录结构如下所示:
关于各个目录的说明,可参考菜鸟笔记。
Linux账号相关命令
通过useradd新增账号 和通过passwd设置密码,如下所示:
1 [root@bogon ~]# useradd heabc 2 [root@bogon ~]# passwd heabc 3 Changing password for user heabc. 4 New password: 5 BAD PASSWORD: The password is shorter than 7 characters 6 Retype new password: 7 passwd: all authentication tokens updated successfully.
通过userdel删除密码,如下所示:
1 [root@bogon ~]# userdel heabc 2 userdel: user heabc is currently used by process 2477
以上提示表示用户被进程占用,不可以删除,需要关闭进程,才可以删除成功。
通过su命令切换账号,切换账号时,如果不加短横线,表示不切换环境变量;加短横线,则切换变量。如下所示:
1 [root@bogon ~]# su - heabc 2 [heabc@bogon ~]$ ls 3 [heabc@bogon ~]$ who 4 root pts/0 2020-11-29 22:17 (192.168.127.1) 5 [heabc@bogon ~]$ su - root 6 Password: 7 Last login: Sun Nov 29 22:54:26 CST 2020 on pts/0
通过who命令,查询当前登录的账号,如下所示:
1 [root@bogon /]# who 2 root pts/0 2020-11-30 22:42 (192.168.127.1) 3 admin :0 2020-11-30 22:58 (:0)
通过uptime查询系统运行的时间,如下所示:
1 [root@bogon /]# uptime 2 23:01:02 up 19 min, 2 users, load average: 0.14, 0.24, 0.13
同过shutdown -h now 进行关机命令,通过shutdown -r now进行重启命令操作,如下所示:
Linux进程查询命令
通过ps命令查询系统后台运行的进程。如下所示:
1 [root@bogon ~]# ps -aux 2 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 3 root 1 0.0 0.7 128268 6972 ? Ss 22:15 0:01 /usr/lib/syste 4 root 2 0.0 0.0 0 0 ? S 22:15 0:00 [kthreadd] 5 root 4 0.0 0.0 0 0 ? S< 22:15 0:00 [kworker/0:0H] 6 root 5 0.0 0.0 0 0 ? S 22:15 0:00 [kworker/u256: 7 root 6 0.0 0.0 0 0 ? S 22:15 0:00 [ksoftirqd/0]
Linux目录相关命令
通过cd命令,切换目录,可以就相对路径,也可以就绝对路径,如下所示:
1 [root@bogon admin]# cd ../hexx 2 [root@bogon hexx]# ls 3 dir1 dir2 dir2_2 dir3 file2.txt file4 file5.txt file6.txt
通过pwd命令,显示当前所在的目录,如下所示:
1 [root@bogon ~]# pwd 2 /root
通过ls命令显示当前目录的内容,如下所示:
1 [root@bogon ~]# cd /home 2 [root@bogon home]# ls 3 admin hexx 4 [root@bogon home]# cd /root 5 [root@bogon ~]# ls 6 anaconda-ks.cfg initial-setup-ks.cfg
通过ls -a 显示目录下所有的内容,包括隐藏内容,如下所示:
1 [root@bogon ~]# ls -a 2 . .bash_history .bashrc .cshrc .local 3 .. .bash_logout .cache .dbus .tcshrc 4 anaconda-ks.cfg .bash_profile .config initial-setup-ks.cfg
通过touch命令创建文件,默认在当前目录下创建,如下所示:
1 [root@bogon hexx]# touch file1 file2 file3 2 [root@bogon hexx]# ls 3 file1 file2 file3
也可以指定路径下创建文件,如下所示:
1 [root@bogon home]# touch hexx/file4 2 [root@bogon home]# cd hexx 3 [root@bogon hexx]# ls 4 file1 file2 file3 file4
通过cat命令,查看文件内容,如下所示:
1 [root@bogon etc]# cat host.conf 2 multi on 3 [root@bogon etc]# cat passwd 4 root:x:0:0:root:/root:/bin/bash 5 bin:x:1:1:bin:/bin:/sbin/nologin 6 daemon:x:2:2:daemon:/sbin:/sbin/nologin 7 adm:x:3:4:adm:/var/adm:/sbin/nologin 8 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9 ......
通过head命令,查看文件内容前几行,如下所示:
1 [root@bogon etc]# head passwd 2 root:x:0:0:root:/root:/bin/bash 3 bin:x:1:1:bin:/bin:/sbin/nologin 4 daemon:x:2:2:daemon:/sbin:/sbin/nologin 5 adm:x:3:4:adm:/var/adm:/sbin/nologin 6 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 7 sync:x:5:0:sync:/sbin:/bin/sync 8 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 9 halt:x:7:0:halt:/sbin:/sbin/halt 10 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 11 operator:x:11:0:operator:/root:/sbin/nologin
通过cp命令复制文件
复制文件,既可以采用源文件名,也可以重新命名,如下所示:
1 [root@bogon hexx]# cp file1 file6.txt 2 [root@bogon hexx]# ls 3 file1 file2 file3 file4 file5.txt file6.txt
cp命令既可以相同目录内容复制,也可以跨目录复制,如下所示:
1 [root@bogon hexx]# cp file2 ../admin/file2.txt 2 [root@bogon hexx]# ls 3 file1 file2 file3 file4 file5.txt file6.txt 4 [root@bogon hexx]# cd ../admin 5 [root@bogon admin]# ls 6 Desktop Downloads Music Public Videos 7 Documents file2.txt Pictures Templates
通过cp -r 命令复制文件夹及其内容,如下所示:
1 [root@bogon hexx]# cp -r dir2 dir2_2 2 [root@bogon hexx]# ls 3 dir1 dir2 dir2_2 dir3 file2.txt file4 file5.txt file6.txt 4 [root@bogon hexx]# cd dir2_2 5 [root@bogon dir2_2]# ls 6 [root@bogon dir2_2]# cd ../ 7 [root@bogon hexx]# ls 8 dir1 dir2 dir2_2 dir3 file2.txt file4 file5.txt file6.txt 9 [root@bogon hexx]# cp -r dir2 ../admin/dir2_3 10 [root@bogon hexx]# cd ../admin 11 [root@bogon admin]# ls 12 Desktop dir3 Downloads file2.txt Pictures Templates 13 dir2_3 Documents file1 Music Public Videos
通过mv命令移动文件,如下所示:
1 [root@bogon hexx]# mv file1 ../admin/file1 2 [root@bogon hexx]# ls 3 file2 file3 file4 file5.txt file6.txt 4 [root@bogon hexx]# cd ../admin 5 [root@bogon admin]# ls 6 Desktop Downloads file2.txt Pictures Templates 7 Documents file1 Music Public Videos
如果mv移动的文件在同一目录内容,则是重命名,如下所示:
1 [root@bogon hexx]# mv file2 file2.txt 2 [root@bogon hexx]# ls 3 file2.txt file3 file4 file5.txt file6.txt
如果mv在同一目录操作,在当前文件目录中已存在,则移动到目标目录内;如不存在,则是重命名。如下所示:
1 [root@bogon hexx]# ls 2 dir1 dir2 dir2_2 dir3 file2.txt file4 file5.txt file6.txt 3 [root@bogon hexx]# mv dir3 dir3_3 4 [root@bogon hexx]# ls 5 dir1 dir2 dir2_2 dir3_3 file2.txt file4 file5.txt file6.txt 6 [root@bogon hexx]# mv dir2 dir2_2 7 [root@bogon hexx]# ls 8 dir1 dir2_2 dir3_3 file2.txt file4 file5.txt file6.txt 9 [root@bogon hexx]# cd dir2_2 10 [root@bogon dir2_2]# ls 11 dir2
如果mv在不同目录内操作,则相当于剪切功能,如下所示:
1 [root@bogon hexx]# ls 2 dir1 dir2_2 dir3_3 file2.txt file4 file5.txt file6.txt 3 [root@bogon hexx]# mv dir1 ../admin/dir1_1 4 [root@bogon hexx]# ls 5 dir2_2 dir3_3 file2.txt file4 file5.txt file6.txt 6 [root@bogon hexx]# cd ../admin 7 [root@bogon admin]# ls 8 Desktop dir2_3 Documents file1 Music Public Videos 9 dir1_1 dir3 Downloads file2.txt Pictures Templates
从别的目录移动到当前目录,如下所示:
1 [root@bogon hexx]# mv ../admin/dir1_1 dir5 2 [root@bogon hexx]# ls 3 dir2_2 dir3_3 dir5 file2.txt file4 file5.txt file6.txt
通过rm命令删除文件,如下所示:
1 [root@bogon hexx]# rm file3 2 rm: remove regular empty file ‘file3’? Y 3 [root@bogon hexx]# ls 4 file2.txt file4 file5.txt file6.txt
通过占位符?删除文件,如下所示:
1 [root@bogon hexx]# ls 2 dir dir2_2 dir5 file2.txt file4 file5.txt file6.txt 3 [root@bogon hexx]# rm file? 4 rm: remove regular empty file ‘file4’? Y 5 [root@bogon hexx]# ls 6 dir dir2_2 dir5 file2.txt file5.txt file6.txt
通过通配符*删除文件及目录,如下所示:
1 [root@bogon hexx]# rm -r dir* 2 rm: remove directory ‘dir’? Y 3 rm: descend into directory ‘dir2_2’? Y 4 rm: remove directory ‘dir2_2/dir2’? Y 5 rm: remove directory ‘dir2_2’? Y 6 rm: remove directory ‘dir5’? Y 7 rm: remove regular empty file ‘dir_file1’? Y 8 [root@bogon hexx]# ls 9 file2.txt file5.txt file6.txt
通过find命令查询文件,
查询指定文件名的文件,如下所示:
1 [root@bogon hexx]# find ./ -name file4 2 ./file4
通过?占位符查询,如下所示:
1 [root@bogon hexx]# find ./ -name file? 2 ./file4 3 [root@bogon hexx]# find ./ -name file? 4 ./file4
通过*号模糊查询,如下所示:
1 [root@bogon hexx]# find ./ -name file* 2 ./file4 3 ./file5.txt 4 ./file6.txt 5 ./file2.txt 6 [root@bogon hexx]# find ./ -name *le* 7 ./.bash_profile 8 ./file4 9 ./file5.txt 10 ./file6.txt 11 ./file2.txt
同file命令,查询文件类型,如下所示:
1 [root@bogon admin]# file file2.txt 2 file2.txt: empty 3 [root@bogon admin]# file Music 4 Music: directory
通过ll命令,查询文件的详细内容,如下所示:
1 [root@bogon ~]# ll 2 total 8 3 -rw-------. 1 root root 1987 May 26 2020 anaconda-ks.cfg 4 -rw-r--r--. 1 root root 2018 May 26 2020 initial-setup-ks.cfg
其中,第一个,短横线表示普通文件,D表示文件目录。
通过ls - l查询当前目录所有文件的详细信息,如下所示:
1 [root@bogon hexx]# ls -l 2 total 0 3 drwxr-xr-x. 2 root root 6 Nov 26 22:47 dir1 4 drwxr-xr-x. 2 root root 6 Nov 26 22:47 dir2 5 drwxr-xr-x. 3 root root 19 Nov 26 22:51 dir3 6 -rw-r--r--. 1 root root 0 Nov 25 22:07 file2.txt 7 -rw-r--r--. 1 root root 0 Nov 25 22:09 file4 8 -rw-r--r--. 1 root root 0 Nov 25 22:13 file5.txt 9 -rw-r--r--. 1 root root 0 Nov 25 22:17 file6.txt
也可以查询,其他目录的文件信息,如下所示:
1 [root@bogon hexx]# ls -l ../admin 2 total 0 3 drwxr-xr-x. 2 admin admin 6 May 26 2020 Desktop 4 drwxr-xr-x. 2 root root 6 Nov 26 22:48 dir3 5 drwxr-xr-x. 2 admin admin 6 May 26 2020 Documents 6 drwxr-xr-x. 2 admin admin 6 May 26 2020 Downloads 7 -rw-r--r--. 1 root root 0 Nov 25 22:07 file1 8 -rw-r--r--. 1 root root 0 Nov 25 22:36 file2.txt 9 ......
通过ls -al查询当前目录和其他目录下的全部文件的详细信息,如下所示:
1 [root@bogon hexx]# ls -al 2 total 16 3 drwx------. 8 1001 1001 228 Nov 26 22:51 . 4 drwxr-xr-x. 4 root root 31 Nov 24 22:53 .. 5 -rw-------. 1 1001 1001 23 Nov 24 22:59 .bash_history 6 -rw-r--r--. 1 1001 1001 18 Apr 1 2020 .bash_logout 7 ........ 8 [root@bogon hexx]# ls -al ../admin 9 total 32 10 drwx------. 16 admin admin 4096 Nov 26 22:48 . 11 drwxr-xr-x. 4 root root 31 Nov 24 22:53 .. 12 -rw-------. 1 admin admin 279 Nov 25 23:06 .bash_history 13 -rw-r--r--. 1 admin admin 18 Apr 1 2020 .bash_logout 14 -rw-r--r--. 1 admin admin 193 Apr 1 2020 .bash_profile 15 -rw-r--r--. 1 admin admin 231 Apr 1 2020 .bashrc 16 drwx------. 14 admin admin 4096 May 26 2020 .cache 17 drwxr-xr-x. 14 admin admin 261 May 26 2020 .config 18 ......
通过mkdir命令,创建文件目录,如下所示:
1 [root@bogon hexx]# mkdir dir dir1 2 [root@bogon hexx]# ls 3 dir dir1 dir2_2 dir3_3 dir5 file2.txt file4 file5.txt file6.txt
通过rmdir命令删除目录,如下所示:
1 [root@bogon hexx]# rmdir dir1 2 [root@bogon hexx]# ls 3 dir dir2_2 dir3_3 dir5 file2.txt file4 file5.txt file6.txt
通过rmdir -r命令删除目录及子目录,如下所示:
1 [root@bogon hexx]# rm -r dir3_3 2 rm: descend into directory ‘dir3_3’? Y 3 rm: descend into directory ‘dir3_3/dir33’? Y 4 rm: remove directory ‘dir3_3/dir33/dir333’? Y 5 rm: remove directory ‘dir3_3/dir33’? Y 6 rm: remove directory ‘dir3_3’? Y 7 [root@bogon hexx]# ls 8 dir dir2_2 dir5 file2.txt file4 file5.txt file6.txt
网络相关命令
ifconfig命令,查看网络配置,如下所示:
1 [root@bogon ~]# ifconfig 2 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 3 inet 192.168.127.129 netmask 255.255.255.0 broadcast 192.168.127.255 4 inet6 fe80::24e8:9baf:7982:428b prefixlen 64 scopeid 0x20<link> 5 ether 00:0c:29:6d:3e:16 txqueuelen 1000 (Ethernet) 6 RX packets 2976 bytes 214136 (209.1 KiB) 7 RX errors 0 dropped 0 overruns 0 frame 0 8 TX packets 283 bytes 41368 (40.3 KiB) 9 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 10 11 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 12 inet 127.0.0.1 netmask 255.0.0.0 13 inet6 ::1 prefixlen 128 scopeid 0x10<host> 14 loop txqueuelen 1000 (Local Loopback) 15 RX packets 48 bytes 4080 (3.9 KiB) 16 RX errors 0 dropped 0 overruns 0 frame 0 17 TX packets 48 bytes 4080 (3.9 KiB) 18 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 19 20 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 21 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 22 ether 52:54:00:56:b3:7f txqueuelen 1000 (Ethernet) 23 RX packets 0 bytes 0 (0.0 B) 24 RX errors 0 dropped 0 overruns 0 frame 0 25 TX packets 0 bytes 0 (0.0 B) 26 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ping命令,测试网络连通,通过Ctrl+C停止命令,如下所示:
1 [root@bogon ~]# ping 192.168.1.102 2 PING 192.168.1.102 (192.168.1.102) 56(84) bytes of data. 3 64 bytes from 192.168.1.102: icmp_seq=1 ttl=128 time=48.6 ms 4 64 bytes from 192.168.1.102: icmp_seq=2 ttl=128 time=60.9 ms 5 64 bytes from 192.168.1.102: icmp_seq=3 ttl=128 time=83.2 ms 6 64 bytes from 192.168.1.102: icmp_seq=4 ttl=128 time=105 ms 7 64 bytes from 192.168.1.102: icmp_seq=5 ttl=128 time=35.8 ms 8 64 bytes from 192.168.1.102: icmp_seq=6 ttl=128 time=46.6 ms 9 64 bytes from 192.168.1.102: icmp_seq=7 ttl=128 time=67.5 ms 10 64 bytes from 192.168.1.102: icmp_seq=8 ttl=128 time=14.0 ms 11 64 bytes from 192.168.1.102: icmp_seq=9 ttl=128 time=110 ms 12 64 bytes from 192.168.1.102: icmp_seq=10 ttl=128 time=33.6 ms 13 64 bytes from 192.168.1.102: icmp_seq=11 ttl=128 time=52.2 ms 14 ^C 15 --- 192.168.1.102 ping statistics --- 16 11 packets transmitted, 11 received, 0% packet loss, time 10024ms 17 rtt min/avg/max/mdev = 14.050/59.924/110.534/28.619 ms
通过fdisk -l 查询磁盘挂载情况,如下所示:
1 [root@bogon /]# fdisk -l 2 3 Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors 4 Units = sectors of 1 * 512 = 512 bytes 5 Sector size (logical/physical): 512 bytes / 512 bytes 6 I/O size (minimum/optimal): 512 bytes / 512 bytes 7 Disk label type: dos 8 Disk identifier: 0x000bfc23 9 10 Device Boot Start End Blocks Id System 11 /dev/sda1 * 2048 411647 204800 83 Linux 12 /dev/sda2 411648 25593855 12591104 8e Linux LVM 13 14 Disk /dev/mapper/centos-root: 10.7 GB, 10737418240 bytes, 20971520 sectors 15 Units = sectors of 1 * 512 = 512 bytes 16 Sector size (logical/physical): 512 bytes / 512 bytes 17 I/O size (minimum/optimal): 512 bytes / 512 bytes 18 19 20 Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors 21 Units = sectors of 1 * 512 = 512 bytes 22 Sector size (logical/physical): 512 bytes / 512 bytes 23 I/O size (minimum/optimal): 512 bytes / 512 bytes
归档命令
归档命令tar,也可以进行解压缩,如下所示:
通过tar -czvf 进行压缩成gz格式
1 [root@bogon hexx]# tar -czvf xxx.gz file2.txt file5.txt file6.txt 2 file2.txt 3 file5.txt 4 file6.txt 5 [root@bogon hexx]# ls 6 file2.txt file5.txt file6.txt xxx.gz
解压命令,通过tar -zxvf解压gz格式的文件,如下所示:
1 [root@bogon hexx]# rm -r file* 2 rm: remove regular empty file ‘file2.txt’? Y 3 rm: remove regular empty file ‘file5.txt’? Y 4 rm: remove regular empty file ‘file6.txt’? Y 5 [root@bogon hexx]# ls 6 xxx.gz 7 [root@bogon hexx]# tar -zxvf xxx.gz 8 file2.txt 9 file5.txt 10 file6.txt 11 [root@bogon hexx]# ls 12 file2.txt file5.txt file6.txt xxx.gz
将文件解压到指定目录,如下所示:
1 [root@bogon hexx]# mkdir xxxx 2 [root@bogon hexx]# ls 3 file2.txt file5.txt file6.txt xxx.gz xxxx 4 [root@bogon hexx]# tar -zxvf xxx.gz -C xxxx 5 file2.txt 6 file5.txt 7 file6.txt 8 [root@bogon hexx]# ls 9 file2.txt file5.txt file6.txt xxx.gz xxxx 10 [root@bogon hexx]# cd xxxx 11 [root@bogon xxxx]# ls 12 file2.txt file5.txt file6.txt
tar命令详解,如下所示:
tar -c 归档,-x 解档,-v显示操作的所有文件,-f 归档普通文件的名称 -z 调用gzip来进行解压和压缩,-j 调用bzip2的方式进行解压和压缩,-C 指定解压到的目录。
备注
从军行
作者:杨炯
牙璋辞凤阙,铁骑绕龙城。
雪暗凋旗画,风多杂鼓声。
宁为百夫长,胜作一书生。