linux 基本命令

【文件与目录管理】:
 
      cd、 pwd、 ls、 touch、 rm、 mkdir、 rmdir、 echo、 cp、 mv、
      cat与tac、 more/less、 head/tail、 nl、 od、 stat、 file、 evince
      
【文件、目录权限】:
 
    
      chown、 chgrp、 chmod、 umask、 chattr、 lsattr

【查找文件与目录】:

      which、 whereis、 locate、 find


***************************************************************************
【文件与目录管理】

1、cd:变换工作路径【cd [绝对路径或相对路径]】
 
   绝对路径:路径的写法必定从根目录‘/’写起,eg:/root/Desktop/world。
   相对路径:路径不是从根目录'/'写起,eg:从/root/Desktop调转到/root,写成cd ../root 即可

   [root@localhost Desktop]# cd /usr/share/doc
   [root@localhost doc]# cd ../man
   [root@localhost man]#
   
2、pwd:显示当前工作目录【pwd】

   [root@localhost man]# cd /root/Desktop/
   [root@localhost Desktop]# pwd
   /root/Desktop

3、ls:显示当前目录下的文件与目录名称【 ls -[a/l/]】

   -a:列出所有文件,包括隐藏文件。
   -l:列出文件目录的详细信息(大小、属性等)

   [root@localhost Desktop]# ls
   C-classfile  doc  dynamic_achieve_queue  excercise  picture  world  压缩包
   [root@localhost Desktop]# ls -a
   .   C-classfile  dynamic_achieve_queue  picture  压缩包
   ..  doc          excercise              world
   [root@localhost Desktop]# ls -l
   total 28
   drwxr-xr-x. 20 root root 4096 Jan 29 18:03 C-classfile
   drwxr-xr-x. 19 root root 4096 Feb  1 11:20 doc
   drwx------.  2 root root 4096 Feb  1 22:17 dynamic_achieve_queue
   drwxr-xr-x. 15 root root 4096 Jan 22 20:37 excercise
   drwxr-xr-x.  2 root root 4096 Feb 14 12:11 picture
   -rw-r--r--.  1 root root  170 Feb 14 15:22 world
   drwxr-xr-x.  5 root root 4096 Feb  1 11:22 压缩包
   [root@localhost Desktop]# ls -al
   total 40
   drwxr-xr-x.  8 root root 4096 Feb 14 21:54 .
   dr-xr-x---+ 18 root root 4096 Feb 14 21:58 ..
   drwxr-xr-x. 20 root root 4096 Jan 29 18:03 C-classfile
   drwxr-xr-x. 19 root root 4096 Feb  1 11:20 doc
   drwx------.  2 root root 4096 Feb  1 22:17 dynamic_achieve_queue
   drwxr-xr-x. 15 root root 4096 Jan 22 20:37 excercise
   drwxr-xr-x.  2 root root 4096 Feb 14 12:11 picture
   -rw-r--r--.  1 root root  170 Feb 14 15:22 world
   drwxr-xr-x.  5 root root 4096 Feb  1 11:22 压缩包
   [root@localhost Desktop]#


4、touch:创建文件【touch [文件名]】

   [root@localhost Desktop]# ls
   C-classfile  doc  dynamic_achieve_queue  excercise  picture  world  压缩包
   [root@localhost Desktop]# touch gong
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  gong     world
   doc          excercise              picture  压缩包
   [root@localhost Desktop]# ll gong
   -rw-r--r--. 1 root root 0 Feb 14 22:10 gong
   [root@localhost Desktop]#

5、rm:删除制定文件【rm [要删除的文件名]】

   [root@localhost Desktop]# ls
   C-classfile  doc  dynamic_achieve_queue  excercise  gong  picture  压缩包
   [root@localhost Desktop]# ll gong
   -rw-r--r--. 1 root root 0 Feb 14 22:13 gong
   [root@localhost Desktop]# rm gong
   rm: remove regular empty file ‘gong’? y
   [root@localhost Desktop]# ls
   C-classfile  doc  dynamic_achieve_queue  excercise  picture  压缩包
   [root@localhost Desktop]#
 
6、mkdir:创建新目录【mkdir [目录名称]】
 
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  linux基础命令  压缩包
   doc          excercise              picture
   [root@localhost Desktop]# mkdir gong
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  gong           picture
   doc          excercise              linux基础命令  压缩包
   [root@localhost Desktop]# ll
   total 28
   drwxr-xr-x. 20 root root 4096 Jan 29 18:03 C-classfile
   drwxr-xr-x. 19 root root 4096 Feb  1 11:20 doc
      drwx------.  2 root root 4096 Feb  1 22:17 dynamic_achieve_queue
   drwxr-xr-x. 15 root root 4096 Jan 22 20:37 excercise
   drwxr-xr-x.  2 root root    6 Feb 15 14:11 gong
   -rw-r--r--.  1 root root 3460 Feb 14 22:15 linux基础命令
   drwxr-xr-x.  2 root root 4096 Feb 14 12:11 picture
   drwxr-xr-x.  5 root root 4096 Feb  1 11:22 压缩包

7、rmdir:删除目录【rmdir [要删除的目录名称]】

   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  gong           picture
   doc          excercise              linux基础命令  压缩包
   [root@localhost Desktop]# rmdir gong/
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  linux基础命令  压缩包
   doc          excercise              picture
   [root@localhost Desktop]#

8、echo:显示【echo [要显示的内容]】

   [root@localhost Desktop]# echo 5
   5
   [root@localhost Desktop]# echo hello,world
   hello,world
   [root@localhost Desktop]# echo hello world
   hello world
   [root@localhost Desktop]#

9、cp:复制文件【cp [文件名] [复制后的路径]】

   cp .*:copy所有.文件
   cp /etc/skel/.*:copycte/skel下所有文件

   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  file           linux基础命令~  world
   doc          excercise              linux基础命令  picture         压缩包
   [root@localhost Desktop]# ll
   total 36
   drwxr-xr-x. 20 root root 4096 Jan 29 18:03 C-classfile
   drwxr-xr-x. 19 root root 4096 Feb  1 11:20 doc
   drwx------.  2 root root 4096 Feb  1 22:17 dynamic_achieve_queue
   drwxr-xr-x. 15 root root 4096 Jan 22 20:37 excercise
   drwxr-xr-x.  2 root root    6 Feb 15 16:12 file
   -rw-r--r--.  1 root root 4797 Feb 15 16:07 linux基础命令
   -rw-r--r--.  1 root root 3460 Feb 14 22:15 linux基础命令~
   drwxr-xr-x.  2 root root 4096 Feb 14 12:11 picture
   -rw-r--r--.  1 root root    0 Feb 15 16:12 world
   drwxr-xr-x.  5 root root 4096 Feb  1 11:22 压缩包
   [root@localhost Desktop]# cp world file/
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  file           linux基础命令~  world
   doc          excercise              linux基础命令  picture         压缩包
   [root@localhost Desktop]# cd file
   [root@localhost file]# ls
   world
   [root@localhost file]#

9、mv:
  (1)移动文件【mv [移动后的路径]】
 
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  file           linux基础命令~  压缩包
   doc          excercise              linux基础命令  picture
   [root@localhost Desktop]# ll file/
   total 0
   -rw-r--r--. 1 root root 0 Feb 15 16:27 world
   [root@localhost Desktop]# mv file/world /root/Desktop/
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  file           linux基础命令~  world
   doc          excercise              linux基础命令  picture         压缩包
   [root@localhost Desktop]# cd file/
   [root@localhost file]# ls
   [root@localhost file]#

 (2)更改文件名称【mv [文件名] [新文件名]】
    
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  file           linux基础命令~  world
   doc          excercise              linux基础命令  picture         压缩包
   [root@localhost Desktop]# mv world hello
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  file   linux基础命令   picture
   doc          excercise              hello  linux基础命令~  压缩包
   [root@localhost Desktop]#

10、cat与tac:
    cat:从第一行开始显示文件内容【cat [文件名]】
    
   [root@localhost Desktop]# cat hello
   aaa
   bbb
   ccc
   ddd
   eee
   fff
   ggg
   hhh
   iii
   jjj
   kkk
   lll
   mmm
   nnn
   ooo
   ppp
   qqq
   rrr
   sss
   ttt
   uuu
   vvv
   www
   xxx
   yyy
   zzz
   000
   111
   222
   333
   444
   555
   666
   777
   888
   999
   [root@localhost Desktop]#

  tac:从最后以后以行开始显示文件内容【tac [文件名]】

   [root@localhost Desktop]# tac hello
   999
   888
   777
   666
   555
   444
   333
   222
   111
   000
   zzz
   yyy
   xxx
   www
   vvv
   uuu
   ttt
   sss
   rrr
   qqq
   ppp
   ooo  
   nnn
   mmm
   lll
   kkk
   jjj
   iii
   hhh
   ggg
   fff
   eee
   ddd
   ccc
   bbb
   aaa
   [root@localhost Desktop]#

11、more与less:
  more:按页显示,空格键向下翻,‘b’键向上查看。【more [文件名]】

   [root@localhost Desktop]# more hello
   aaa
   bbb
   ccc
   ddd
   eee
   fff
   ggg
   hhh
   iii
   jjj
   kkk
   lll
   mmm
   nnn
   ooo  
   ppp  
   qqq
   rrr
   sss
   ttt
   uuu
   vvv
   www
   xxx  
   yyy   
   zzz
   000
   111
   222
   333
   444
   555
   666
   --More--(91%)
   
  less:用法与more相似,可用pgup、pgdn向上、向下翻页,
  查看完用esc+:+q退出【less [文件名]】
   
   aaa
   bbb
   ccc
   ddd
   eee
   fff
   ggg
   hhh
   iii
   jjj
   kkk
   lll
   mmm
   nnn
   ooo
   ppp
   qqq
   rrr
   sss
   ttt
   uuu
   vvv
   www
   xxx
   yyy
   zzz
   000
   111
   222
   333
   444
   555
   666
   777
   hello

12、head/tail:
  head:只看头几行【head -n number [文件名]】

   [root@localhost Desktop]# head -n 3 hello
   aaa
   bbb
   ccc

  tail:只看倒数几行【tail -n number [文件名]】

   [root@localhost Desktop]# tail -n 3 hello
   777
   888
   999
 
13、nl:查看文件的同时可以显示行数【nl [文件名]】

[root@localhost Desktop]# nl hello
     1    aaa
     2    bbb
     3    ccc
     4    ddd
     5    eee
     6    fff
     7    ggg
     8    hhh
     9    iii
    10    jjj
    11    kkk
    12    lll
    13    mmm
    14    nnn
    15    ooo
    16    ppp
    17    qqq
    18    rrr
    19    sss
    20    ttt
    21    uuu
    22    vvv
    23    www
    24    xxx
    25    yyy
    26    zzz
    27    000
    28    111
    29    222
    30    333
    31    444
    32    555
    33    666
    34    777
    35    888

14、od:用于读取非ASCII码文件,将文件用数据方式读取出来【od [文件名]】

   [root@localhost Desktop]# od hello
   0000000 060541 005141 061142 005142 061543 005143 062144 005144
   0000020 062545 005145 063146 005146 063547 005147 064150 005150
   0000040 064551 005151 065152 005152 065553 005153 066154 005154
   0000060 066555 005155 067156 005156 067557 005157 070160 005160
   0000100 070561 005161 071162 005162 071563 005163 072164 005164
   0000120 072565 005165 073166 005166 073567 005167 074170 005170
   0000140 074571 005171 075172 005172 030060 005060 030461 005061
   0000160 031062 005062 031463 005063 032064 005064 032465 005065
   0000200 033066 005066 033467 005067 034070 005070 034471 005071
   0000220
   [root@localhost Desktop]#

15、stat:显示文件的重要信息及状态、大小、创建日期【stat [文件名]】

[root@localhost Desktop]# stat hello
  File: ‘hello’
  Size: 144           Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 138103687   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2016-02-15 17:41:49.522791849 +0800
Modify: 2016-02-15 17:41:38.940700373 +0800
Change: 2016-02-15 17:41:39.030701164 +0800
 Birth: -
[root@localhost Desktop]#

16、file:查看文件类型【file [文件名或目录名]】
 
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  hello          linux基础命令~  picture
   doc          excercise              linux基础命令  note            压缩包
   [root@localhost Desktop]# file hello
   hello: ASCII text
   [root@localhost Desktop]# file note/
   note/: directory
   [root@localhost Desktop]#

17、evince:用来打开PDF文件【evince [PDF名]】

   [root@localhost C-class相关文档]# evince 内存对齐.pdf

***************************************************************************
【文件、目录权限】:
    
1、chown:更改文件的拥有者【chown [新拥有者] [文件名]】
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  hello          linux基础命令~  picture
   doc          excercise              linux基础命令  note            压缩包
   [root@localhost Desktop]# ll hello
   -rw-r--r--. 1 root root 144 Feb 15 17:41 hello
   [root@localhost Desktop]# chown westos hello
   [root@localhost Desktop]# ll hello
   -rw-r--r--. 1 westos root 144 Feb 15 17:41 hello
   [root@localhost Desktop]#

2、chgrp:改变文件的所属组群,用法与chown相似【chgrp [新属组群] 【文件名】】

   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  hello          linux基础命令~  picture
   doc          excercise              linux基础命令  note            压缩包
   [root@localhost Desktop]# ll hello
   -rw-r--r--. 1 westos root 144 Feb 15 17:41 hello
   [root@localhost Desktop]# chgrp westos hello
   [root@localhost Desktop]# ll hello
   -rw-r--r--. 1 westos westos 144 Feb 15 17:41 hello
   [root@localhost Desktop]#
   
3、chmod:更改文件权限【chmod [权限数] [文件名]】
   
   一个文件或目录属性至少有三组:每组由r、w、x组成,最前面是表示文件类型:
   c :字符设备
   s:套接字设备
   -:普通文件
   d:目录设备
   l:连接文件
   r(4):可读;w(2):可写;x(1):可执行
   chmod(修改文件权限) chown(修改所处用户和组)chgrp(修改组)
   对目录可读:可以看到目录里边的东西
   对目录可写:可以对目录里边的东西进行更改
   对目录可执行:可以进入目录里边

 更改文件权限:
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  hello          linux基础命令~  picture
   doc          excercise              linux基础命令  note            压缩包
   [1]+  Done                    firefox
   [root@localhost Desktop]# ll hello
   -rw-r--r--. 1 westos westos 144 Feb 15 17:41 hello
   [root@localhost Desktop]# chmod 777 hello
   [root@localhost Desktop]# ll hello
   -rwxrwxrwx. 1 westos westos 144 Feb 15 17:41 hello
   [root@localhost Desktop]#

  更改目录的文件权限:
   [root@localhost Desktop]# ll note/
   total 4
   -rwxr-xr-x. 1 root root 144 Feb 15 22:16 hello
   [root@localhost Desktop]# chmod -R 471 note/
   [root@localhost Desktop]# ll note/
   total 4
   -r--rwx--x. 1 root root 144 Feb 15 22:16 hello
   [root@localhost Desktop]#
   
  -R:循环的将目录的文件权限改变。

4、umask:
  查看umask:
   [root@localhost test]# umask
   0022
  默认目录权限:  777 - umask
  默认文件权限:  777 - 111 - umask  (去除可执行权限)

  相应权限可以根据自己的需要设定

5、chattr:设置文件隐藏属性,是文件无法更改

  chattr +i [文件名]: 无法更动该文件
   [root@localhost Desktop]# ll hello
   -rwxrwxrwx. 1 westos westos 144 Feb 15 17:41 hello
   [root@localhost Desktop]# chattr +i hello
   [root@localhost Desktop]# rm hello
   rm: remove regular file ‘hello’? y
   rm: cannot remove ‘hello’: Operation not permitted
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  hello          linux基础命令~  picture
   doc          excercise              linux基础命令  note            压缩包
   [root@localhost Desktop]#
   
  chattr -i [文件名]:解除该属性:
   [root@localhost Desktop]# chattr -i hello
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  hello          linux基础命令~  picture
   doc          excercise              linux基础命令  note            压缩包
   [root@localhost Desktop]# rm hello
   rm: remove regular file ‘hello’? y
   [root@localhost Desktop]# ls
   C-classfile  dynamic_achieve_queue  linux基础命令   note     压缩包
   doc          excercise              linux基础命令~  picture
   [root@localhost Desktop]#

6、lsattr:查看隐藏属性
   lsattr [-aR]:
   
   -a:将隐藏属性也显示出来
   -R:连通子目录的数据一起显示出来

[root@localhost Desktop]# lsattr
---------------- ./C-classfile
---------------- ./dynamic_achieve_queue
---------------- ./linux基础命令
---------------- ./linux基础命令~
----i----------- ./note
---------------- ./压缩包
---------------- ./excercise
---------------- ./doc
---------------- ./picture
   

[root@localhost Desktop]# lsattr -a
---------------- ./.
---------------- ./..
---------------- ./C-classfile
---------------- ./dynamic_achieve_queue
---------------- ./linux基础命令
---------------- ./linux基础命令~
----i----------- ./note
---------------- ./压缩包
---------------- ./excercise
---------------- ./doc
---------------- ./picture


[root@localhost C-class相关文档]# lsattr -aR
---------------- ./.
---------------- ./..
---------------- ./1.linux系统应用:介绍上课环境,安装linux操作系统.pdf
---------------- ./2.linux系统应用:shell.pdf
---------------- ./3.linux系统应用:在linux下获取帮助.pdf
---------------- ./4.linux系统应用:标准输入输出、管道、vim和gedit使用.pdf
---------------- ./5.linux系统应用:linux下的用户和组管理.pdf
---------------- ./6.linux系统应用:linux下的文件权限管理.pdf
---------------- ./9.linux系统应用:使用openssh进行远程访问.pdf
---------------- ./11.linux系统应用:打包压缩命令.pdf
---------------- ./12.linux系统应用:linux下的软件管理(yum仓库).pdf
---------------- ./操作符和表达式.pdf
---------------- ./让你不再害怕指针.pdf
---------------- ./动态内存分配和函数.pdf
---------------- ./内存对齐.pdf
---------------- ./结构体和共用体.pdf
---------------- ./Malloc_tutorial.pdf
---------------- ./C语言深度剖析.pdf
---------------- ./基于Lnmp架构的论坛系统.pdf
---------------- ./C语言趣味程序百例精解.pdf
---------------- ./Linux C函数库参考手册.pdf
---------------- ./Linux_C函数库参考手册(清晰版完整版).pdf
---------------- ./华为技术有限公司c语言编程规范.pdf
[root@localhost C-class相关文档]#

****************************************************************************
【查找文件与目录】:

1、which:查看可执行文件的位置[它是通过PATH环境变量到该路径内寻找可执行文件]【which [文件名]】

   [root@localhost Desktop]# which nop
   /usr/bin/nop
   [root@localhost Desktop]# cd /usr/bin/
   [root@localhost bin]# ls
   [                                    nm
   411toppm                             nmcli
   a2p                                  nm-connection-editor
   abrt-action-analyze-backtrace        nm-online
   abrt-action-analyze-c                nmtui
   abrt-action-analyze-ccpp-local       nmtui-connect
   abrt-action-analyze-core             nmtui-edit
   abrt-action-analyze-oops             nmtui-hostname
   abrt-action-analyze-python           nohup
   abrt-action-analyze-vmcore           nop

2、whereis:查看文件的位置【whereis [参数] [目录名称]】
  可用参数及说明:
   -b:只找二进制文件
   -m:只找在说明文件manual路径下的文件
   -s:只找source源文件
   -u:没有说明的文档文件!

   [root@localhost sbin]# whereis passwd
   passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
   [root@localhost sbin]# whereis -b passwd
   passwd: /usr/bin/passwd /etc/passwd
   [root@localhost sbin]# whereis -m passwd
   passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
   [root@localhost sbin]# whereis -s passwd
   passwd:[root@localhost sbin]# whereis -u passwd
   passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
   [root@localhost sbin]#

3、find实际搜寻硬盘查询文件名称【find [路径] [参数]】

   常见参数的说明:
    -name  [文件名称]   :寻找该名称的文件
    -user  [文件所属用户]:寻找名称为该用户名的文件
    -group [文件所属组] :寻找组群为该组群名的文件
   
   [root@localhost Desktop]# find / -name note
   /root/Desktop/note
   [root@localhost Desktop]#

   [root@localhost Desktop]# find note/ -user westos
   note/
   note/world
   note/file
   note/data

   [root@localhost Desktop]# find note/ -group westos
   note/
   note/world
   note/file
   note/data

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值