命令行与shell编程系列文章目录
第一章 什么是命令行
第二章 文件权限
第三章 文件的处理
第四章 磁盘和文件系统
第五章 理解shell解析器和shell进程
前言
命令行字面意思就是一行命令,也就是让计算机执行一行命令,这里说明命令不是让CPU执行一次指令,不涉及GUI和输入方式等,执行一行命令和打开一个app没有太大区别。
命令行的本质是程序,是可以自己实现和替换的程序(黑客技术中的一种就是替换系统原有命令行程序)
一些基本的Linux系统命令
- 查看文件系统内的文件(需要懂基本文件系统和文件路径)
#切换到系统根目录cd 是 change directory
zhang:~$ cd /
#列出当前目录文件内容 ls 是 list
zhang:/$ ls
bin core etc initrd.img.old lib64 mnt root smbShare sys usr vmlinuz.old
boot data home lib lost+found opt run snap tftp var
cdrom dev initrd.img lib32 media proc sbin srv tmp vmlinuz
#切换到bin文件夹内
zhang:/$ cd /bin/
zhang:bin$ ls -alh l*
-rwxr-xr-x 1 root root 166K 2月 29 2016 less
-rwxr-xr-x 1 root root 11K 2月 29 2016 lessecho
lrwxrwxrwx 1 root root 8 4月 21 2018 lessfile -> lesspipe
-rwxr-xr-x 1 root root 20K 2月 29 2016 lesskey
-rwxr-xr-x 1 root root 7.6K 2月 29 2016 lesspipe
-rwxr-xr-x 1 root root 55K 2月 18 2016 ln
-rwxr-xr-x 1 root root 109K 9月 17 2015 loadkeys
-rwxr-xr-x 1 root root 47K 5月 17 2017 login
-rwxr-xr-x 1 root root 444K 2月 5 2020 loginctl
-rwxr-xr-x 1 root root 103K 3月 22 2019 lowntfs-3g
-rwxr-xr-x 1 root root 124K 2月 18 2016 ls
-rwxr-xr-x 1 root root 76K 4月 14 2016 lsblk
lrwxrwxrwx 1 root root 4 7月 25 2017 lsmod -> kmod
zhang:/$ which ls
/bin/ls
上述都是基本系统自带的命名,这里提一下命令tree(需要单独安装),可以把目录按照树状结构显示,有兴趣的可尝试
2. 文件和目录的基本处理
zhang:~$ pwd
/home/zhang
zhang:~$ mkdir shellTest
zhang:~$ cd shellTest/
zhang:shellTest$ pwd
/home/zhang/shellTest
zhang:shellTest$ touch file1
zhang:shellTest$ ls -alh
total 8.0K
drwxrwxr-x 2 zhang zhang 4.0K 4月 12 17:09 .
drwxr-xr-x 37 zhang zhang 4.0K 4月 12 17:08 ..
-rw-rw-r-- 1 zhang zhang 0 4月 12 17:09 file1
zhang:shellTest$ cp file1 file1_copy
zhang:shellTest$ ls
file1 file1_copy
zhang:shellTest$ mv file1 file
zhang:shellTest$ ls
file file1_copy
zhang:shellTest$ rm file
zhang:shellTest$ ls
file1_copy
zhang:shellTest$ cd ..
zhang:~$ rmdir shellTest/
rmdir: failed to remove 'shellTest/': Directory not empty
zhang:~$ cd shellTest/
zhang:shellTest$ ls
file1_copy
zhang:shellTest$ rm file1_copy
zhang:shellTest$ cd ..
zhang:~$ rmdir shellTest
- 查看文件
zhang:shellTest$ ls -alh file
-rw-rw-r-- 1 zhang zhang 31 4月 12 17:20 file
zhang:shellTest$ cat file
H1
H2
H3
H4
H5
H6
H7
H8
H9
H10
zhang:shellTest$ tail -n 2 file
H9
H10
zhang:shellTest$ head -n 2 file
H1
H2
细心的读者可能看到在/bin/的目录下有一个ls文件,是的,这个就是ls命令的实际可执行程序,当输入“ls”回车后,实际运行的就是这个可执行程序文件,一般系统命令程序文件存放在/bin等目录下
大部分命令都可以带参数等,可以通过帮助信息查看学习或者直接通过man手册查看更加具体的描述
zhang:shellTest$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,