每天2分钟学习unix/linux系统shell编程(四)mkdir,echo,cp,mv,cat

内容提要,今天将要学习的有chmod,touch,mkdir,echo,cp,mv,rm,history,cat等shell命令,下面我们开始。

一、shell命令:chmod
命令:chmod
作用:给文件或者文件夹增加或者减少权限
选项:数字格式的权限表达方式或者字母方式权限表达方式。
参数:带路径的文件或者文件夹。
1.1字母方式
使用格式:chmod [u/g/o/a][+/-][r/w/x] filename

选项解释:第一对中括号中的u表示用户自己,g表示用户所在的组,o表示其它人,a表示所有,第二对中括号中的+或者-表示增加或者减少权限,第三对中括号中的r/w/x对应的是读写执行。最后跟上filename

使用举例:

root@debian:~/test# ls -l
总用量 0
-rwxr-xr-x 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# chmod u-x a.sh 
root@debian:~/test# ls -l
总用量 0
-rw-r-xr-x 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# chmod a+w a.sh 
root@debian:~/test# ls -l
总用量 0
-rw-rwxrwx 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# chmod a-w a.sh
root@debian:~/test# ls -l
总用量 0
-r--r-xr-x 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# 

1.2数字方式
使用格式:chmod 741 filename

选项解释:第一个7表示读(4)+写(2)+执行(1)=7表示用户有所有权限,第二个4表示用户只读(4),第三个1表示其它人有执行权限(1),数字的含义上节讲过。

使用举例:

root@debian:~/test# ls -l
总用量 0
-r--r-xr-x 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# chmod 741 a.sh 
root@debian:~/test# ls -l
总用量 0
-rwxr----x 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# chmod 644 a.sh 
root@debian:~/test# ls -l
总用量 0
-rw-r--r-- 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
root@debian:~/test# 

二、shell命令:touch
命令:touch
作用:创建新文件
选项:无
参数:一个或者多个,带路径或者不带路径的文件名。
使用举例:

root@debian:~/test# ls
a.sh  b.txt
root@debian:~/test# touch c.txt d.log e.abc
root@debian:~/test# ls -l
总用量 0
-rw-r--r-- 1 root root 0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root 0 11月 14 23:37 b.txt
-rw-r--r-- 1 root root 0 11月 15 23:05 c.txt
-rw-r--r-- 1 root root 0 11月 15 23:05 d.log
-rw-r--r-- 1 root root 0 11月 15 23:05 e.abc
root@debian:~/test# 

三、shell命令:mkdir
命令:mkdir
作用:创建新文件夹
选项:-p表示创建多层级文件夹不存在就创建,可选,没有-p时只能创建一级文件夹。
参数:新文件夹路径
使用举例:

root@debian:~/test# mkdir -p a/b/c
root@debian:~/test# ls -l
总用量 4
drwxr-xr-x 3 root root 4096 11月 15 23:13 a
-rw-r--r-- 1 root root    0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root    0 11月 14 23:37 b.txt
-rw-r--r-- 1 root root    0 11月 15 23:05 c.txt
-rw-r--r-- 1 root root    0 11月 15 23:05 d.log
-rw-r--r-- 1 root root    0 11月 15 23:05 e.abc
root@debian:~/test# mkdir b
root@debian:~/test# ls -l
总用量 8
drwxr-xr-x 3 root root 4096 11月 15 23:13 a
-rw-r--r-- 1 root root    0 11月 14 23:37 a.sh
drwxr-xr-x 2 root root 4096 11月 15 23:13 b
-rw-r--r-- 1 root root    0 11月 14 23:37 b.txt
-rw-r--r-- 1 root root    0 11月 15 23:05 c.txt
-rw-r--r-- 1 root root    0 11月 15 23:05 d.log
-rw-r--r-- 1 root root    0 11月 15 23:05 e.abc
root@debian:~/test# ls a
a/    a.sh  
root@debian:~/test# ls a/b/c/
root@debian:~/test# ls -a a/b/c/
.  ..
root@debian:~/test# 

四、shell命令:echo
命令:echo
作用:回显,回馈显示
选项:无
参数:要回显的内容
使用举例:

root@debian:~/test# echo "hello, everyone"
hello, everyone
root@debian:~/test# echo "hello, everyone" > c.txt
root@debian:~/test# cat ./c.txt 
hello, everyone
root@debian:~/test# 

说明:第三行的 > c.txt是指把前面命令输出的结果重定向到文件中

五、shell命令:cp
命令:cp
作用:复制文件或者文件夹
选项:-r 表示复制的是文件夹
参数:源文件或者源文件夹 目的文件或者目的文件夹
使用举例:

oot@debian:~/test# ls
a  a.sh  b  b.txt  c.txt  d.log  e.abc
root@debian:~/test# cp c.txt f.txt
root@debian:~/test# ls
a  a.sh  b  b.txt  c.txt  d.log  e.abc  f.txt
root@debian:~/test# cp -r a c
root@debian:~/test# ls
a  a.sh  b  b.txt  c  c.txt  d.log  e.abc  f.txt
root@debian:~/test# 

六、shell命令:mv
命令:mv
作用:移动文件或者文件夹,或者改名
选项:无
参数:源文件或者源文件夹 新的文件或者新的文件夹
使用举例:

root@debian:~/test# ls
a  a.sh  b  b.txt  c  c.txt  d.log  e.abc  f.txt
root@debian:~/test# mv a d
root@debian:~/test# ls
a.sh  b  b.txt  c  c.txt  d  d.log  e.abc  f.txt
root@debian:~/test# mv c.txt g.txt
root@debian:~/test# ls
a.sh  b  b.txt  c  d  d.log  e.abc  f.txt  g.txt
root@debian:~/test#

七、shell命令:rm
命令:rm
作用:删除文件或者文件夹
选项:-r 表示复制的是文件夹
参数:要删除的文件或者文件夹
使用举例:

root@debian:~/test# ls
a.sh  b  b.txt  c  d  d.log  e.abc  f.txt  g.txt
root@debian:~/test# rm -r b
root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  f.txt  g.txt
root@debian:~/test# rm f.txt 
root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  g.txt
root@debian:~/test# 

八、shell命令:history
命令:history
作用:显示命令
选项:无
参数:无
使用举例:

root@debian:~/test# history 
   78  ls
root@debian:~/test# !78
ls
a.sh  b.txt  c  d  d.log  e.abc  g.txt
root@debian:~/test# !!
ls
a.sh  b.txt  c  d  d.log  e.abc  g.txt
root@debian:~/test# 

说明:

!带上数字表示再次执行这个编号的命令记录。

!!表示重新执行上次命令,也就是最近那一次的命令。

九、shell命令:cat
命令:cat
作用:查看文件内容
选项:无
参数:文件路径
使用举例:

root@debian:~/test# ls -l
总用量 12
-rw-r--r-- 1 root root    0 11月 14 23:37 a.sh
-rw-r--r-- 1 root root    0 11月 14 23:37 b.txt
drwxr-xr-x 3 root root 4096 11月 15 23:20 c
drwxr-xr-x 3 root root 4096 11月 15 23:13 d
-rw-r--r-- 1 root root    0 11月 15 23:05 d.log
-rw-r--r-- 1 root root    0 11月 15 23:05 e.abc
-rw-r--r-- 1 root root   16 11月 15 23:16 g.txt
root@debian:~/test# cat g.txt 
hello, everyone
root@debian:~/test# 

好了今天就分享到这里,主要介绍一些常用命令,未来预计还有两次分享shell编程就完结了。最后一次分享会写一个简单的shell脚本。感谢大家的支持,点赞关注不迷路哦。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容