Linux 基本指令的操作和示例说明(五节)

指令规则:命令 【参数选项】【文件或路径】
ls  -ld   (参数  l   和  d)  多参数可以直接连起来操作    前面 加  “ - ”
操作查看目录指令:
mkdir   创建目录
全名称:make directorys   ----    :mkdir   (/data)是创建的目录;或者 cd  /; mkdir  data
第一个指令 是 直接创建 目录 在根  " / "下 :  mkdir  /data  ;
第二个是 进入根" / "下直接创建   :cd /; mkdir data
例子: mkdir  /myfile    或者     cd  /; mkdir  myfile    
ls   列表  
ll  :查看该目录下的文件总个数    
例子:   ll 
参数  l   (long)      长格式       
参数 d (directorys) 查看目录
例子: ls  -l    \    ls  -d   \    ls -ld()   ls  /data (进入目录下查看)
cd   改变目录路径 
全名称 :change directory 
相当路径:没有从根 " / " 开始    cd   data
绝对路径:从根 " / "  开始      cd  /etc
例子:  cd  /etc ;  cd  data  
pwd  打印工作目录(显示当前所在路径)
全名称  :print work  directory 
例子:  pwd     输出结果:/directory   / (那个文件)
操作文件指令:
touch    创建文件及更新时间戳
若文件不存在   touch  file.txt  
若文件存在  更新时间戳(文件的时间属性)  ;
例子: touch   /myfile/file.txt 
vi   编辑文件      
vi  file.txt     
vi  后跟 操作的文件  ; 回车进入待编辑状态
按 " a "或 " i  " 字母 进入编辑状态  ; 编辑好之后
按 键盘   Esc  回到  待编辑状态;
输入  " :wq "  保存和退出 编辑  回到  命令行模式;
不想保存: 输入 :q     
强制退出 输入   :q!   
解释   wq  :  w(write) 、q (Quit) ;
vim  复杂编辑器   
功能复杂,高亮,自动缩进,写脚本(shell  / python 脚本用)
vim  file.txt
按 " a "或 " i  " 字母 进入编辑状态  ; 编辑好之后
按 键盘   Esc  回到  待编辑状态;
输入  " :w "  保存后再按":q"退出 编辑  回到  命令行模式;
echo  覆盖或追加内容  配合  " 重定向 >     "  ,  " 追加   >>"
echo    ' I am study Linux now; ' >myfile.txt
解释:echo 后面跟  文件内容    " > "  代表重定向到    那个文件  或者其他
" > " 后的文件若存在 会 先清空 该文件的内容   后  写入 " > " 前面的 内容 ;
此 "  >  " 指令  操作  危险   ,在操作前  请先备份 ,在操作;
若想追加  则用   "  >>   "
例子: echo 'I drop in love river with you; '>>myfile.txt
cat   单行和多行文本追加
cat  >myfile.txt 回车 后编辑输入  I love you  in my life;在按  Ctrl  + C   或   D 结束编辑;
[root@root myfile]# cat >>myfile.txt<<my     : (多行追加实例:)
> i love you in mylife;
> do you want to mirry with me?
> my(my  可以换成任意,但必须顶格写)
[root@root myfile]# cat myfile.txt   (实例显示的结果:)
I studying Linux now;
i love you in mylife;
do you want to mirry with me?
特殊符号:   
> 或 (0\1\2)> 输出重定向:把前面的内容输入到后面,会清除原有的文件内容;(有 1 没有都一样)
例子 :>myfile.txt      (清空原有内容)    echo ' i love you ' >myfile.txt
>> 或  (0\1\2)>>     追加输出重定向:把前面的内容追加到后面,不会清除原有文件内容;
例子:echo 'I drop in love river with you; '>>myfile.txt
0< 或 <     输入重定向  : 把文件或文本内容   输入给  前面的命令;
例子:
[root@root myfile]# echo ' 1 2 3 4 '>myfile.txt
[root@root myfile]# cat myfile.txt
 1 2 3 4 
[root@root myfile]# xargs -n 2 <myfile.txt  (xargs -n 2  理解为:2个一个单位分组)
1 2
3 4
0<< 或 <<   输入追加重定向
[root@root myfile]# cat >>myfile.txt<<my     : (多行追加实例:)
> i love you in mylife;
> do you want to mirry with me?
> my
特殊符号 < 、< <、>、>> 的说明:
箭头的指向代表数据的流向
数字的说明:
标准输入(stdin):        代码为0 ,使用 0< 或 0<< ,数据(输入的数据)流向从右到左
例子: cat  < myfile.txt
标准正常输出(stdout):代码为1 ,使用 1> 或 1>> ,数据(正确的数据)流向从左到右
例子: echo  oldbook >myfile.txt     echo  oldbooks >> myfile.txt
标准错误输出( stderr ):   代码为2 ,使用 2> 或 2>> ,数据(错误的数据)流向从左到右  
echo oldbook > myfile.text  2>b.txt     意思是:正确的数据输出到 myfile.txt,错误的输出到b.txt
cho odkbb > a.txt 2>'&'     意思是: 正确或者错误都输出到 a.txt
cho  oooddd  &>a.txt        意思是: 正确或者错误都输出到 a.txt
echo  shuikm  &>>a.txt      意思是:正确或者错误输出追加到 a.txt
xargs -n  number <wenjian   分组
echo 1 2 3 4 5 6 7 8 >a.txt
例子:xargs  -n   3 <a.txt
cp  file  /temp  把某个文件或目录拷贝到 某个地方
例子:  cp  a.txt  /temp/      相对路径拷贝
[root@root myfile]# cp /myfile/a.txt  /my
cp: overwrite `/my/a.txt'? y
绝对路劲拷贝      若有文件输入 " y " 覆盖;输入" n " 不覆盖
若拷贝  目录  需要 加参数  " -r   递归拷贝,用于复制目录 "
" -a  相对于 -p: 保持属性 ;-d:链接文件   -r  "
例子:   cp  -r /myfile  /temp
rm   remove    
rm    -f(force)强制   -r(recuresive) 递归删除目录   /  mydirectory
mv  移动文件和目录
mv  file.txt  /temp    原位置文件就没有了
find  查找   -type f (什么类型)  -name (叫什么名字)  " file.txt"   -exec(做什么动作)
find  /myfile   -type f  -name  ' myfile.txt '    -exec  rm {} \;
上面例子是:找到/myfile 目录下file类型的myfile.txt 文件 ' {} 内容 '删除操作
find  /myfile   -type f  -name  "*.txt "  | xargs rm -f 
把查找的内容  放到一行上  进行删除

按照一个系统的流程练习总结:

[root@root ~]# cd / 进入根目录
[root@root /]# ls 跟列表清单
---a.txt boot dev home lost+found mnt proc sbin srv tmp var
---bin data etc lib media opt root selinux sys usr
[root@root /]# mkdir /my 创建my目录
[root@root /]# ls 查看目录里有没有my
---a.txt data home media opt sbin sys var
---bin dev lib mnt proc selinux tmp
---boot etc lost+found my root srv
[root@root /]# touch /my/a.txt 在目录里创建文件a.txt
[root@root /]# ls /my 查看目录里有没有a.txt
---a.txt
[root@root /]# echo '111 222 333 444 向文件里输入内容编辑
555 666 444 555 666
111 222 55 66 44 88
552'>/a.txt 根目录下有该文件就写入,没有就创建
[root@root /]# cat a.txt 查看根目录下a.txt文件的编辑的内容
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
---552
[root@root /]# pwd 打印当前目录位置
[root@root /]# vi a.txt vi编辑器编辑内容
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
---552
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
"a.txt" 6L, 115C written
[root@root /]# cat a.txt 查看编辑的内容
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
---552
---if you love that
---please do something for it
[root@root /]# vim a.txt vim 编辑器编辑
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
---552
---if you love that
---please do something for it
please do something for it
---vim to do this
---55556666
[root@root /]# cat a.txt 查看编辑的内容
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
---552
---if you love that
-- please do something for it
---vim to do this
---55556666
---8888 99999
[root@root /]# cho 123456 >>a.txt 2>&1 错误数据是否和正确数据输出写入到了 a.txt 文件里
[root@root /]# cat a.txt 查看错误数据有没有写入
---111 222 333 444
---555 666 444 555 666
---111 222 55 66 44 88
---552
---if you love that
please do something for it
---vim to do this
---55556666
---8888 99999
-bash: cho: command not found 这里是写入的 错误数据输出值
[root@root /]# xargs -n 4 <a.txt 对a.txt 文件进行 分组整理 按每行 4 个数据分组
---111 222 333 444
---555 666 444 555
---666 111 222 55
---66 44 88 552
---if you love that
---please do something for
---it ----------- vim to
---do this 55556666 8888
---99999 -bash: cho: command
---not found
[root@root /]# cp /my/a.txt / cp 复制文件到 根目录
---cp: overwrite /a.txt'? y 根目录有 是否要覆盖 'y' 覆盖 'n' 不覆盖<br/>[root@root /]# mv /a.txt /my mv 移动根目录的a.txt文件到/my目录下<br/>---mv: overwrite/my/a.txt'? y
[root@root /]# cat /my/a.txt
[root@root /]# cd /my
[root@root my]# cat a.txt
[root@root my]# echo 1 2 3 4 5 6 7 8 >a.txt 对文件进行编辑
[root@root my]# cat a.txt
---1 2 3 4 5 6 7 8
[root@root my]# xargs -n 3 <a.txt 按 3 分组整理
---1 2 3
---4 5 6
---7 8
[root@root my]# cp a.txt /
[root@root my]# cd /
[root@root /]# ls
& boot etc lost+found my root srv usr
---a.txt data home media opt sbin sys var
---bin dev lib mnt proc selinux tmp
[root@root /]# rm /my/a.txt 删除文件
---rm: remove regular file /my/a.txt'? y<br/>[root@root /]# ls /my<br/>[root@root /]# find / -type f -name 'a.txt' -exec rm{} \; 按条件找到文件删除<br/>---find:rm/a.txt': No such file or directory
[root@root /]# touch /my/b.txt
[root@root /]# ls /my
---b.txt
[root@root /]# find /my -type f -name 'b.txt' |xargs rm 按条件进行管道删除
[root@root /]# ls /my**

猜你喜欢

转载自blog.51cto.com/12838569/2388071