Linux文件操作常用命令

新建目录/多级目录

  mkdir = make directories   创建目录/多个目录

  mkdir 目录名称

  选项:

     -p   = parents    no error if existing, make parent directories as needed   创建目录时保证父目录存在不会出错

  具体操作:

ls   查看当前目录下所有内容

mkdir aaa  创建单个目录

ls 

mkdir -p frame/spring/ioc/beans   创建多级目录

ls

查看当前工作目录位置:

  pwd  = print name of current/working directory  打印当前工作目录

  具体操作:

  pwd

查看目录下内容

  ls  list directory contents  列出目录内容

  选项:

    -l   = long (use a long listing format)    显示文件详情

    -a   = all   (do not ignore entries starting with .)  显示全部内容(在linux系统以.开头的文件都是隐藏文件)

    -t   = time (sort by modification time, newest first)

    

  具体操作:

     ls

     touch .aaa.txt

     ls

     ls -a

     mkdir .aaa

     ls

     ls -a

     touch a.txt

     ls -l -t   发现按照文件的更新时间倒叙排序   

     

切换目录

  cd =  Change the shell working directory     改变当前工作目录

  具体操作:

        ls 

        cd frame/  切换到frame目录

pwd

ls

cd spring/  

pwd 

cd /root/frame/spring/ioc/   使用绝对路径进入某一个目录

pwd

cd /etc/sysconfig/network-scripts/

pwd

ls

cd /root/frame/spring/ioc/

pwd

cd ../   切换到上级目录

pwd

cd ../../  切换到上两级目录

pwd

cd ../../../../  目录不存在就直接到根目录

cd  什么都不加   到当前用户根目录

pwd

useradd usera  添加用户A

su -l usera  切换用户

pwd

cd /etc 切换到/etc

        cd  

pwd

su -l root  切换回root目录 输入正确密码

cd frame/spring/ioc/beans/

cd ~  切换当前用户主目录

复制文件或者目录

    cp =copy files and directories  复制文件和目录   

    选项:

       -r =  recursive  递归    copy directories recursively  使用递归复制目录

    具体操作:

       touch  bbb.txt

       ls

       cp bbb.txt frame/  复制bbb.txt到frame中

       ls

       ls frame  发现多了一个bbb.txt

       cp bbb.txt ccc.txt  如果说ccc.txt目录不存在,复制并改名

       ls

       ls /tmp/

       cp /root/aaa.txt  /tmp/

       ls

       ls /tmp/

       cp frame/ aaa/  直接运行出错   如果想拷贝目录需要加参数

       ls aaa

       cp -r frame/ aaa/

       ls aaa

       ls aaa/frame/ 

       ls /tmp

       cp -r /root/aaa/  /tmp/

       ls /tmp

新建文件

    touch = change file timestamps 改变文件时间戳( Update the access and modification times of each FILE to the current time)

            A FILE argument that does not exist is created empty

    如果说touch 参数1  参数1不存在,创建一个空文件

    具体操作:

       ls -l

       date

       touch anaconda-ks.cfg

       ls -l

       ls

       touch aaa.txt  创建空文件

       ls

       ls /tmp/

       touch /tmp/suibian.txt

       ls /tmp

移动/重命名文件或者目录

    mv = move (rename) files   移动(重命名) 文件

    具体操作:

     ls aaa/

     mv aaa.txt aaa/  移动aaa.txt到aaa下

     ls   发现aaa.txt不在

     ls aaa/   发现存在aaa.txt

     mkdir software

     mv frame/ software/

     ls

     ls aaa/ 

     ls /tmp

     mv /root/software/frame/  /tmp/

     ls /tmp

     ls

     mv bbb.txt bbb1.txt   重命名文件

     ls

     mv software softw  重命名目录

     ls

删除空目录

   rmdir = remove empty directories  删除空目录

   具体操作:

    ls

    rm aaa有问题 

    mkdir aaa

    ls

    rmdir aaa

    ls

删除目录或者文件

   rm =remove files or directories 删除文件或者目录

   选项:

     -r = recursive  递归

     -f = force    强制删除不带提示

   具体操作:

     ls

     rm bbb1.txt 带提示删除

     ls

     rm -f ccc.txt   不带提示直接删除

     ls

     rm -r  softw  带提示删除目录

     ls

     rm -r -f  aaa

     ls

     mkdir -p aaa/a/b/c/d

     ls

     rm -rf aaa/

     ls

查看文件内容

     cat   

concatenate files and print on the standard output   输出多个文件内容到控制台

     具体操作:

     ls

     touch  tomcat1.log

     touch  tomcat2.log

     touch  tomcat3.log

     ls

     echo 'hello world' >> tomcat1.log     >写入(覆盖文件原来内容)    >> 追加 在文件内容末尾追加内容

     cat  tomcat1.log

     echo 'hello world1' >> tomcat1.log

     cat  tomcat1.log

     echo 'hello world2' >> tomcat1.log 

     cat  tomcat1.log

     echo 'hello world1' > tomcat2.log

     cat tomcat2.log

     echo 'hello world3' > tomcat3.log

     cat tomcat3.log

     cat tomcat1.log tomcat2.log tomcat3.log   串联三个文件并显示内容到控制台

     head 

output the first part of files  输出文件第一部分

     选项:

       -n  数字

     具体操作:

      date  显示当前时间

      date >> tomcat1.log  追加当前时间到tomcat1.log  追加N次

      head tomcat1.log  默认显示一个文件的前10行

      head -5 tomcat1.log   

      head -50 tomcat1.log 

     tail

  output the last part of files  输出文件最后部门

     选项:

       -n  数字

       -f  follow (output appended data as the file grows;)  动态显示一个文件的尾部

       -F  -retry         如果日志文件滚动生成  tomcat.log (满10M)->tomcat20211216-01.log

     具体操作:

     tail tomcat1.log    

     tail -5 tomcat1.log

     tail -1 tomcat1.log

     在一个会话中执行:

      tail -F tomcat1.log   阻塞不动

     在另外启动一个会话连接:

      date >> tomcat1.log   向tomcat1.log中追加时间

      观察第一个会话中的变化。

     

     

     more 

= file perusal filter for crt viewing   分页显示文件内容  直接把文件内容都加载

      具体操作:

       more  settings.xml

          点space键(空间键) 按页向下移动

  点enter键  按行向下移动

  ctrl+ f    向下翻页

  ctrl+ b    向上翻页

           q (quit)  退出

   

     

     less 

opposite of more    分页显示文件内容   异步加载内容。。。

      

          点space键(空间键) 按页向下移动

  点enter键  按行向下移动

  ctrl+ f    向下翻页

  ctrl+ b    向上翻页

          /关键字     按照关键字查找内容

  /mirrors   查找远程仓库配置

打印文件内容到控制台:

    echo = display a line of text   展示一行内容

    具体用法:

     echo 'hello world'

文件统计

    wc =  (print newline, word, and byte counts for each file)  统计一个文件的行数,单词数和字节

     -c  bytes  字节

     -m  chars  字符数

     -l  lines  行数

     -w  words  单词数

    具体用法:

    touch hello.txt

     echo 'hello'>>hello.txt    6

     echo '中国'>>hello.txt     7

     echo 'A AA'>>hello.txt     5

    wc hello.txt    3 行数  4(单词数,以空格或者换行分割)   18 字节数(一个中文utf-8占3个字节 空格换行都占一个字节)

    

    wc -l hello.txt   行数 

    wc -w hello.txt   单词数 

    wc -c hello.txt   字节数

    wc -m hello.txt   字符数

创建快捷方式(linux创建软硬连接)

   ln  =make links between files   创建文件的连接

   选项:

     -s  symbolic     创建软连接

     -d  directory    创建硬连接

   具体操作:

      ls /tmp/

      ln -s   /tmp/aaa/  aaa.lnk   为/tmp/aaa/在当前目录创建名字为aaa.lnk软连接

      ls -l

      cd aaa.lnk  通过快捷方式进入aaa目录

      ls

      rm -rf /tmp/aaa/  删除源文件/目录

      ll    等同于ls -l   快捷方式变为红色

      mkdir -p /tmp/aaa/a/b/c 创建源目录  

      ll  发现又可以用了,完全和windows一样

      touch /tmp/tm.txt

      date >> /tmp/tm.txt 

      ln -d   /tmp/tm.txt  tm.lk  创建硬链接

      ll   发现多了一个 tm.lk

      cat tm.lk 

      rm -rf /tmp/tm.txt

      cat tm.lk

按名称查询文件或者目录

     find   查找文件或者目录的名称

     具体操作:

     find  -name 'tomcat1.log'  在当前目录查找文件名称为tomcat1.log

     find  -name 'tomcat*' 

     find /etc/   -name '*a*'  在/etc目录下查找名字中含有 a的目录或者文件

     find /etc/   -name '*a*' | wc -l   在/etc目录下查找名字中含有 a的目录或者文件并统计行数

                             | 管道符,通常连接两个或者多个命令,后一个命令以前面命令的执行为参数

     

     find /etc/   -name '*locale*'

     find /etc/   -name '*locale*' | wc -l

在文件中查找内容

    grep  = global regular  expression   print   全局按照正则表达式规则查询并输出结果

           百度百科:Globally search a Regular Expression and Print

    具体操作:

    grep h  hello.txt  在hello.txt查找含有h的行

    grep a  settings.xml    

    grep a  settings.xml | wc -l  

    find /etc/   -name '*a*' | grep b  

    find /etc/   -name '*a*' | grep b | wc -l

压缩文件或者目录,查看压缩包,解压压缩包

 gzip   压缩解压文件的命令

具体操作:

    ls

    gzip hello.txt  

    ls

    gzip -d hello.txt.gz

    ls

   bzip2   压缩解压文件的命令 

        具体操作:

    ls

    bzip2 hello.txt 

    ls

    bzip2 -d hello.txt.bz2

    ls

   tar 可以对文件或者目录都可以

   tar saves many files together into a single tape or disk archive, and

       can restore individual files from the archive

       把许多文件压缩为一个文档保存起来,删除源文件,在需要的解压出来再供我们使用

   选项:

      -c   create     创建

      -t   list       列表文件(查看文件)

      -x   extract    提取文件(解压)

          这3个选项不可以混用

      -z   gzip       使用gzip方式压缩,查看,解压文件

      -j   bzip2      使用bzip2方式压缩,查看,解压文件

          这2个选项不可以混用

     

      -v   verboss   冗余(显示详情)  可选项

      -f  file    压缩或者解压后面跟文件名称  必写项  前面3个顺序可以混乱  但-f 必须在其他三个之后

      

      -C   change   在解压时使用,指定解压目录

      

   具体操作:

      ls

      tar -czvf aaa.tar.gz  aaa   压缩

      ls

      tar -tzf  aaa.tar.gz      查看压缩

      tar -tzvf  aaa.tar.gz

      ls

      rm -rf aaa 

      tar -xzvf aaa.tar.gz    如果解压到当前目录,可以不要第2个参数  或者使用.当前目录

      ls

      ls /tmp/

      tar -xzvf aaa.tar.gz  -C  /tmp/   解压

      ls /tmp/

      ls

      tar -cjf  aaa.tar.bz2  aaa  使用bzip2方式,再操作一次

      ls

      tar -vtjf aaa.tar.bz2

      ls

      ls /var

      tar -xjvf aaa.tar.bz2   -C /var

      ls /var

文件或者目录的权限功能

linux 角色四种:

               u = user 当前用户   

       g = group 所属组   

       o = other  其他用户  

       a =all 包含前面三种角色  

            三种权限:

       r = read  可读    

       w=write 可写   

       x=execute  可执行

chmod    改变目录或者文件不同角色对应的权限

选项:

    u   +/-  r

    g        w 

    o        x

    a

       具体操作:

          ll hello.txt   查看hello.txt详情

  

    -               rw-           r--       r--

           文件类型     当前用户权限    所属组    其他用户

    d 目录

    l 软连接

    - 文件

         ll hello.txt

chmod u+x hello.txt

         ll hello.txt

chmod g+wx hello.txt

ll hello.txt

chmod o+wx hello.txt

ll hello.txt

chmod og-wx hello.txt

ll hello.txt

chmod a-rwx hello.txt 

         ll hello.txt

chmod a-rwx hello.txt  

         ll hello.txt

十进制   二进制(3位)

   0        000

   1        001

   2        010          0010

   3        011

   4        100

   5        101

   6        110

   7        111

   8        1000         1000

          

1为true  0为false

chmod 4   5   6    hello.txt

       100 101 110  

       r-- r-x rw-

chmod 2   3   4  hello.txt

               010 011 100

       -w- -wx r--

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值