文件分类:

      用命令ls -l 查看文件显示:

-rw-r--r--  1 root mml 1370 Apr 10 18:21 aa
drwxr-xr-x  2 root mml 4096 Apr  3 18:59 bi

      其中以“-”开头的是普通文件,以“d”开头的是目录文件,还有以“c”开头的是字符文件,以“b”开头的块设备文件,以“l”开头的是链接文件,以“p”开头的是管道文件,以“s”开头的是套接字文件(socket)。

      文件具有数据和元数据

 1、数据:文件本身含有的内容

 2、元数据:文件的一些属性、描述信息(权限、属主、属组、大小、最后一次被修改的时间等等)

文件管理命令:

1、cat  选项   文件名
             -n    显示文件行数
             -e    显示文件特殊字符(如:行终结符$)

[root@localhost var]# cat 11
lsssoo99999f
kljlkjl 
hhljljl
khlkjl;l
hlkjlkl

[root@localhost var]# cat -n 11
     1 lsssoo99999f
     2 kljlkjl 
     3 hhljljl
     4 khlkjl;l
     5 hlkjlkl
     
     [root@localhost var]# cat -e 11
lsssoo99999f$
kljlkjl $
hhljljl$
khlkjl;l$
hlkjlkl$

2、more  文件名  分屏显示文件内容(到文件尾部会自动退出)
     其快捷键:    Ctrl+d      往下翻半屏
                           回车键     向下一行
                           空格键     往下翻一屏
                               b         往上翻一屏
3、less  文件名  分屏显示文件内容(到文件尾部不自动退出)
             其快捷键:Ctrl+d     往下翻半屏
                              Ctrl+u     向上翻半屏
                               回车键    向下一行
                                  k         向上一行
                              空格键     往下翻一屏
                                 b          往上翻一屏
                                #G         跳至#行
                              /关键字  ?关键字
                                   n       下一个
                                  N       上一个

4、tail   选项   文件名      查看文件的尾部多少行
               -#          显示文件最后#行,不跟行数默认显示10行
               -f          监视一个文件有没有新增内容,主要用于日志(以.log结尾)查看

5、echo  “ccc”  >> 文件    把ccc添加到文件中

查看最后两行:

[root@localhost var]# tail -2 11
khlkjl;l
hlkjlkl

监听文件11的修改内容:

开启监听

文件管理命令_文件管理

在另一伪终端中添加内容到文件11中

[root@localhost var]# echo "khlkhouoi 123214" >> 11
[root@localhost var]#

监听结果

文件管理命令_快捷键_02

6、head   选项   文件名      查看文件首部的多少行
                -#                显示文件首部#行,不跟行数默认显示10行

[root@localhost var]# head -3 11
uu
hgfgfugyu
lsssoo99999f
[root@localhost var]# cat -n 11
     1 uu
     2 hgfgfugyu
     3 
     4 hhkljhl 
     5 khij lkjl'
     6 khll
     7 
     8 
     9 lsssoo99999f
    10 kljlkjl 
    11 hhljljl
    12 khlkjl;l
    13 hlkjlkl
    14 aadfsdf
    15 khlkhouoi 123214
///默认为十行
[root@localhost var]# head 11
uu
hgfgfugyu
hhkljhl 
khij lkjl'
khll

lsssoo99999f
kljlkjl

7、删除文件:rm   格式:rm   选项    文件名
                                                 -f      强制删除(force:强制)
                                                  -r     递归删除文件,可以删除目录
                                                  -i     显示提示 (默认)

强制删除命令:

[root@localhost var]# ls
11  cc  dd  ee  lleoaoi88789afa
[root@localhost var]# rm 11
rm: remove regular file `11'? y
[root@localhost var]# rm -f cc
[root@localhost var]# ls
dd  ee  lleoaoi88789afa

递归删除命令(目录mml中含有文件ss):

[root@localhost var]# ls
dd  ee  lleoaoi88789afa  mml
[root@localhost var]# rm -r mml
rm: descend into directory `mml'? y
rm: remove regular file `mml/ss'? y
rm: remove directory `mml'? y
[root@localhost var]# ls
dd  ee  lleoaoi88789afa

-i为默认值,可以通过type命令查看:

[root@localhost var]# type rm
rm is aliased to `rm -i'

8、复制文件:cp   格式:cp  选项  源文件(文件、目录)  目标文件(文件、目录)
 (1、源文件是文件,若目标文件不存在则创建文件,其内容与源文件一样;若目标文件存在则覆盖2、源文件是文件,若目标文件是目录(改目录必须存在),则是将源文件的内容放在该目录下  3、源文件是目录,必须使用选项才能将目录复制到目标目录下)
                 cp  选项  源文件(文件、目录)  目标文件(文件、目录)
                      -r           复制目录,会实现递归复制
                      -d           复制目标文件为软连接文件的本身,非其指向文件
                      -a           =-r+-d
                      -i           提示是否进行覆盖
                      -f           强制进行(不建议使用)

递归复制:

[root@localhost ss]# mkdir mml/ee/cc
[root@localhost ss]# cp -r mml/ ee/
cp: overwrite `ee/mml/cc'? y
[root@localhost ss]# cd ee/
[root@localhost ee]# ls
mml

源文件是文件,目标文件不存在:

[root@localhost var]# ls
dd  ee  lleoaoi88789afa  mml  ss
[root@localhost var]# cp dd cc
[root@localhost var]# ls
cc  dd  ee  lleoaoi88789afa  mml  ss
[root@localhost var]# cat dd
  420  cat -n bb
  421  head -10 history >> cc
  422  history |head -10
  423  history |head -10 >> dd
  424  ls
  425  cat dd
  426  cat -n dd
  427  cat -n aa
  428  cat -n bb
  429  history |tail -10 >dd
[root@localhost var]# cat cc
  420  cat -n bb
  421  head -10 history >> cc
  422  history |head -10
  423  history |head -10 >> dd
  424  ls
  425  cat dd
  426  cat -n dd
  427  cat -n aa
  428  cat -n bb
  429  history |tail -10 >dd

源文件为文件,目标文件存在:

[root@localhost var]# cat ee 
ls: cannot access ff: No such file or directory
[root@localhost var]# cp ee cc
cp: overwrite `cc'? y
[root@localhost var]# cat cc
ls: cannot access ff: No such file or directory

源文件为文件,目标文件为目录:

[root@localhost var]# cd ss
[root@localhost ss]# ls
mml
[root@localhost ss]# cd ..
[root@localhost var]# cp cc ss
[root@localhost var]# cd ss
[root@localhost ss]# ls
cc  mml

源文件为目录,目标文件为目录:

[root@localhost var]# mkdir mml ss
[root@localhost var]# ls
dd  ee  lleoaoi88789afa  mml  ss
[root@localhost var]# cd mml
[root@localhost mml]# ls
[root@localhost mml]# vim cc
[root@localhost mml]# ls
cc
[root@localhost mml]# cd ..
[root@localhost var]# cp -r mml ss
[root@localhost var]# cd ss
[root@localhost ss]# ls
mml
[root@localhost ss]# cd mml
[root@localhost mml]# ls
cc

9、移动文件:mv   格式:mv   选项   源文件  目标文件
                                                  -i       显示提示
                                                  -f       强制移动(不建议使用)

[root@localhost var]# mv cc mml/
[root@localhost var]# ls mml/
cc
[root@localhost var]# type mv
mv is aliased to `mv -i'

10、按文件内容查看文件类型:file    格式:file  文件名

[root@localhost tmp]# ls
aa  bi  boot  etc  jiuren  mojlj  mylinux  testdir  tmp  usr  yum.log
[root@localhost tmp]# file yum.log 
yum.log: empty
[root@localhost tmp]# file aa
aa: ASCII text

11、Linux的重定向(输入输出)及管道
     输出:标准输出(正确输出)    通常用1标示
           错误输出               通常用2标示
     输入:标准输入                     0
重定向:改变输入、输出的显示形式
       输出    >>    追加的形式,将输出的内容显示到后面的文件或目录中
               >     覆盖的形式
          以上都是默认标准输出为:1 >>,想将错误的也重定向的话就用:2  >>
       输入    <<    当输入后面的内容时退出输入,显示内容
 管道:|    把一个命令的输出作为下一命令的输入
     形式    命令1|命令2|命令3    命令1是命令2需要的,命令2是命令3需要的,命令3就是我们需要的

Eg:输出历史记录的前十行
[root@localhost var]# history |head -10
    1  vim /etc/selinux/config 
    2  iptables -L
    3  service iptables stop
    4  vim /etc/sysconfig/network-scripts/ifcfg-eth0 
    5  service network restart
    6  ifconfig
    7  init 0
    8  ls
    9  type
   10  ls type
将历史记录前十行重定向到新建文件dd中(不覆盖)
[root@localhost var]# history |head -10 >> dd
[root@localhost var]# ls
11  aa  bb  cc  dd  lleoaoi88789afa  llowe2345llda
[root@localhost var]# cat dd
    1  vim /etc/selinux/config 
    2  iptables -L
    3  service iptables stop
    4  vim /etc/sysconfig/network-scripts/ifcfg-eth0 
    5  service network restart
    6  ifconfig
    7  init 0
    8  ls
    9  type
   10  ls type
再将历史记录后十行重定向到dd中,(覆盖)
[root@localhost var]# history |tail -10 >dd
[root@localhost var]# cat dd
  420  cat -n bb
  421  head -10 history >> cc
  422  history |head -10
  423  history |head -10 >> dd
  424  ls
  425  cat dd
  426  cat -n dd
  427  cat -n aa
  428  cat -n bb
  429  history |tail -10 >dd
##dd文件中的内容已被改变##
将错误的内容输出
[root@localhost var]# ls ff
ls: cannot access ff: No such file or directory
[root@localhost var]# ls ff 2>> ee
[root@localhost var]# cat ee
ls: cannot access ff: No such file or directory
##ff文件不存在,所以ls ff会报错,想把这个错误的信息显示文件ee中##
 
      tr:格式转换,转换的是一种形式    eg:echo ddd2bb tr  "2"  "s"    显示:dddsbb
Eg:将上面的文件dd中的数字全部转化成a
[root@localhost var]# cat dd | tr "0-9" "a"
  aaa  cat -n bb
  aaa  head -aa history >> cc
  aaa  history |head -aa
  aaa  history |head -aa >> dd
  aaa  ls
  aaa  cat dd
  aaa  cat -n dd
  aaa  cat -n aa
  aaa  cat -n bb
  aaa  history |tail -aa >dd

12、创建目录:mkdir   格式:mkdir   选项   目录名

                                                            -p         级联创建

                                                            -v         提供提示

[root@localhost tmp]# mkdir -p bb/cc
[root@localhost tmp]# ls
aa  bb  bi  boot  etc  jiuren  mojlj  mylinux  testdir  tmp  usr  yum.log
[root@localhost tmp]# ls ./bb
cc
[root@localhost tmp]# mkdir -v ee
mkdir: created directory `ee'

13、删除目录:rm     格式 :rm       选项     目录名 (只能删除空目录)

                                                          -p     级联删除

                                                          -v      提供提示

[root@localhost tmp]# rmdir -p bb/cc
[root@localhost tmp]# ls
aa  bi  boot  etc  jiuren  mojlj  mylinux  testdir  tmp  usr  yum.log

选项-v,同上,

14、查看以前的命令历史:history

       格式:history    选项

                                  -d   条目数      清空指定命令历史

                                   -c                   清空所有命令历史

                                   3                    显示最后3行命令历史

                                   -a                   将当前缓存命令写到历史文件中

 命令:  !234               执行第234行历史命令

              !!                执行上次命令  

           !cle                  执行清屏命令,只记住部分命令

[root@localhost tmp]# history | tail
 1072  history
 1073  head history
 1074  head -5 history
 1075  history | head
 1076  HISTCONTROL
 1077  export HISTCONTROL
 1078  history |tail
 1079  aaa
 1080  export HISTCONTROL
 1081  history | tail
[root@localhost tmp]# history -d   1079
[root@localhost tmp]# history |tail
 1075  history | head
 1076  HISTCONTROL
 1077  export HISTCONTROL
 1078  history |tail
 1079  export HISTCONTROL
 1080  history | tail
 1081  history -d
 1082  history -d   head
 1083  history -d   1079
 1084  history |tail
[root@localhost tmp]# history 3
 1083  history -d   1079
 1084  history |tail
 1085  history 3
[root@localhost tmp]# !1091
history | tail -5
 1091  history | tail -5
 1092  history history | tail -5
 1093  history | tail -5
 1094  history history | tail -5
 1095  history | tail -5
[root@localhost tmp]# history | head
   76  ls
   77  cd /etc
   78  ls
   79  cd 
   80  cd /etc
   81  ls
   82  cd ../tmp
   83  ls
   84  cd /tmp
   85  ls

其环境变量:HISTFILE           指定命令历史的存储文件,修改该环境变量的命令:# export HISTFILE

                    HISTSIZE           指定可存储命令的条目数,格式:# export HISTSIZE=500

                    HSITFILESIZE     指定命令历史文件可存储条目数

                    HISTCONTROL  忽略两次相同的命令

                    HISTCONTROL   ignoredups     忽略两次相同的命令

                                               ignoredspace  忽略空白开始的字符

                                               ignoredboth   忽略前两者的忽略

[root@localhost tmp]# aaa
-bash: aaa: command not found
[root@localhost tmp]# aaa
-bash: aaa: command not found
[root@localhost tmp]# aaa
-bash: aaa: command not found
[root@localhost tmp]# export HISTCONTROL
[root@localhost tmp]# history | tail
 1072  history
 1073  head history
 1074  head -5 history
 1075  history | head
 1076  HISTCONTROL
 1077  export HISTCONTROL
 1078  history |tail
 1079  aaa
 1080  export HISTCONTROL
 1081  history | tail

15、命令:

          命令补全:tab键     路径补全键:两次tab键

          命令执行状态结果    #echo $?    显示上次命令执行之后的状态码,成功为0,失败为1-255

          命令执行自身结果    就是每次命令之后的显示

16、命令执系统管理相关的命令
    关机命令:halt ; init 0 ;shutdown -h
    重启命令:init 6 ; reboot;shutdown -r
    日期命令:date [选项]
                    +%F   显示日期    2015-04-04
                    +%D   段分号日期:  04/04/15
                    +%Y   长格式      2015
                    +%y    短格式     15
                    +%T             00:58:22
                    +%H   只显示小时    00
                    +%M  只显示分钟  
                    +%S   只显示秒
                    +%s   时间戳   Linux元年(1970年1月1日0点0秒)到现在经过了多少秒
                      cal    日历软件(显示日历的命令)

[root@localhost tmp]# date +%F
2015-04-12
[root@localhost tmp]# date +%D
04/12/15
[root@localhost tmp]# date +%T
04:19:56

17、文件查看及处理工具

    统计文件:wc      格式: wc  选项   文件名

                                                   -l     统计文件行数

                                                  -c     统计文件字符数

                                                  -w    统计文件单词数

[root@localhost tmp]# wc -l /etc/passwd
33 /etc/passwd
[root@localhost tmp]# wc -c /etc/passwd
1555 /etc/passwd
[root@localhost tmp]# wc -w /etc/passwd
54 /etc/passwd

 截取文件内容:cut    

格式:cut     选项     文件名

                    -d   分隔符   指定按什么格式的分隔符来截取文件内容(不指定,默认是以空格为分隔符)

                     -f    字段    指定要显示的字段   (#|#-#|#-#,#-#。。。) 

[root@localhost tmp]# cut -d : -f 1,7  /etc/passwd | tail -3 
testbash:/sbin/nologin
basher:/sbin/nologin
nologin:/sbin/nologin
[root@localhost tmp]# tail -3 /etc/passwd
testbash:x:501:501::/home/testbash:/sbin/nologin
basher:x:502:502::/home/basher:/sbin/nologin
nologin:x:503:503::/home/nologin:/sbin/nologin

    提取文件内容排序:sort

格式: sort    选项    文件名      (默认按首字母显示)

                     -n     按数值大小排序(顺序排序)

                     -r                        逆序排序

                     -f     忽略字符大小写

                     -t  分隔符   指定分割符

                     -k     指定第#个字段进行排序

                    -u     去重,只保留一个  

[root@localhost tmp]# sort -t : -k 4 -n  /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mml:x:65530:0:klkjljk:/home/mml:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost tmp]# sort -t : -k 3 -r /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost tmp]# cat cc
asd
sdfs
asd
sdf
asd
asd
[root@localhost tmp]# sort -u cc
asd
sdf
sdfs

统计每一行出现的次数: uniq  (相邻重复的行数才为重复,当做一行)

格式:uniq    选项   文件名

                      -c     统计每行出现的次数

                      -d     仅显示重复的行

                      -u     仅显示不重复的行

[root@localhost tmp]# cat cc
asd
sdfs
asd
sdf
asd
asd
[root@localhost tmp]# uniq -c cc
      1 asd
      1 sdfs
      1 asd
      1 sdf
      2 asd
      1 
[root@localhost tmp]# uniq -d cc
asd
[root@localhost tmp]# uniq -u cc
asd
sdfs
asd
sdf