Linux运维篇05--Linux引导入门阶段(Linux基本命令)

文件管理

 file            查看文件类型(windows是用扩展名识别文件类型)

       语法:file [options] [argvs]  

        -b: 输出结果时不显示文件名

        -c:显示执行file命令的执行过程,便于排错

        -i :输出MIME类型的字符串 ,text、html等等

        -z:显示压缩文件的内容

        -L:  查看软链接对应文件的类型

        -f:查看文件中文件名的类型           

[root@www ~]# cat test.txt 
/root/install.log
/etc/passwd
[root@www ~]# file -f test.txt 
/root/install.log: ASCII text
/etc/passwd:       ASCII text

cat      一次性查看整个文件    

    语法:cat [options] [argvs]

      -n        显示行号

      -b        和-n功能相同,但不包含空行

      -S        当文件中有多个空行时,合并为一个空行

      -E        在显示内容时,结尾添加$符号

   1、从键盘输入创建一个新文件:cat >new_filename   <<EOF    //EOF:end of file 代表文件输入的结束                                                                  

 [root@www ~]# cat >test.txt << EOF
                    > Welcome to china
                    > EOF
                    [root@www ~]# cat test.txt 
                    Welcome to china

    2、向现有文件中追加数据内容:cat >>etst.txt <<EOF               

[root@www ~]# cat >> test.txt << EOF
                    > CISSP
                    > EOF
                    [root@www ~]# cat test.txt 
                    Welcome to china
                    CISSP

     3、把多个文件内容合并到一个文件中

                    cat file1 file2 > file3                                

[root@www ~]# cat /etc/passwd /etc/shadow > user.txt 

 cp    复制文件或目录

        语法:cp [src file] [tar file]

  [root@www ~]# cp /etc/passwd /opt/                //按原文件名拷贝
  [root@www ~]# cp /etc/passwd /opt/123          //重命名
  [root@www ~]# ls /opt/
  123  passwd

         -f        强制赋值文件或目录不进行提示

         -r        递归复制目录,把自己目录及子文件全部拷贝        

         -s        为某个文件创建符号链接(软链接),而不是复制文件

[root@www ~]# cp -s /etc/passwd ./             
[root@www ~]# ll
lrwxrwxrwx. 1 root root    11 Jun 10 12:24 passwd -> /etc/passwd

         -b        覆盖已有的文件前,对目标文件进行备份                       

[root@www ~]# cp -b /etc/passwd ./
overwrite `./passwd'? y
[root@www ~]# ls
passwd    passwd~          //备份文件

          -l         为文件创建硬链接,而不是复制文件

          -p        复制文件时,保留文件的原有属性

[root@www ~]# cp -p /etc/passwd ./
[root@www ~]# cp /etc/shadow ./
[root@www ~]# ll passwd shadow 
-rw-r--r--. 1 root root 1320 May 29 01:18 passwd
----------. 1 root root  780 Jun 10 12:33 shadow

          -d        当复制软链接文件时,把目标文件或目录也会创建为软链接,并指向最原始的文件

          -i         覆盖目标文件前询问(cp = cp -i)                                       

mv    移动或重命名文件或目录 (当原位置和目标位置是同一目录时,是重命名;反之是移动)

        -b    移动前先备份

        -f     强制覆盖

        -i     覆盖目标文件前询问

        -t    将多个文件移动到同一个目录(目标目录在前面,后面跟文件 )

[root@www ~]# mv -t /tmp/ 123 456 user_passwd 
[root@www ~]# ls /tmp
123  456  user_passwd             

rm    删除文件或目录

    -d    删除可能存在数据的目录

    -f     强制删除

    -i    删除前进行询问

    -r    递归删除整个目录

    -v    显示命令执行过程

mkdir    创建空目录

    -m    创建目录时,同时设置权限

  [root@www ~]# mkdir -m 700 bcd
  [root@www ~]# ll
  drwx------. 2 root root  4096 Jun 10 12:46 bcd

    -p     递归创建新目录

[root@www ~]# mkdir -p /root/a/b/c/d/e
[root@www ~]# mkdir -p c/{b,c,d}        //创建同级目录
[root@www ~]# mkdir -p /root/{a,b}--{c,d}
[root@www ~]# ls
a--c             b--c       
a--d             b--d 

    -v     显示创建目录的过程                      

touch    创建新文件或修改文件时间戳

    -a:只改变访问时间

    -c:不创建文件

    -d:  使用指定时间戳创建新文件,而不是系统时间

 [root@www ~]# touch -d "03:00 am" test.txt
 [root@www ~]# ll test.txt 
 -rw-r--r--. 1 root root 61 Jun 10 03:00 test.txt        

    -t:使用指定格式时间戳创建新文件,而不是系统时间          

[root@www ~]# touch -t 131201130030 1234.txt
[root@www ~]# ll 1234.txt 
-rw-r--r--. 1 root root 0 Jan 13  1312 1234.txt
root@www ~]# stat 1234.txt 
Access: 1312-01-13 00:30:00.000000000 +0805

    -f: 解决与BSD系列Linux不兼容的问题

    -m:  只更改modify时间戳

tree         查看目录树 (tree = ls -R)

which      查看命令所在的路径      

[root@www ~]# which ls
alias ls='ls --color=auto'
/bin/ls

whereis    查看命令所在的路径,源代码文件,帮助信息文件所在位置      

[root@www ~]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

    -b    只查看文件所在位置 (=which)

    -m   只查看帮助信息所在位置

    -s    只查看源代码所在位置

locate  查看文件命令

        非实时性,模糊查询,根据自己的数据库查找文件,Centos每天更新数据库

        安装完locate后,生成/var/lib/mlocate目录,存放数据库mlocate.db

        yum -y install mlocate

        updatedb        //手动更新locate数据库

        locate file_name

查看文本文件内容:cat more less head tail

        Linux和Windows中文本内容行尾标记是不同的:

                Linux中以$结尾

                Windows中以enter键结尾

cat:    一次性查看文件内容

more:可以翻页查看文本文件内容

        回车键         向下翻一行
        空格键         向下翻一屏
        pgdn          向上翻一屏
        pgup          向下翻一屏
        =             显示当前行号
        v             调用vi编辑器
        ![command]   调用shell
        q             退出more命令

        more + 数字 +文件名        从第几行开始查看文本文件内容

less:可以翻页查看文本文件内容

        回车键        向下翻一行
        空格键        向下翻一屏
        ctrl+b        向上翻一屏
        ctrl+f        向下翻一屏
        上下方向键     翻行
        q             退出more命令
        /word         查找woed关键字    
            N:查找上一个
            n: 查找上一个

管道:连接多条命令,前一条命令输出结果作为后一条命令的输入条件

head:默认查看文本文件的前十行

        -n        查看文本文件的前n行

tail:默认查看文本文件的后十行

        -n        查看文本文件的后n行

        -f         动态显示查询结果,如动态查看日志 tail -f /var/log/messages

猜你喜欢

转载自blog.csdn.net/qq_38055050/article/details/80639880