Linux基础——文件管理

Linux基础——文件管理

文件管理及常用命令详解

      Linux文件系统是以/为基本的倒置树状结构,我们来进一步学习下文件或者目录的操作和管理命令: 

目录相关操作:cd、pwd、mkdir、rmdir 
目录及文件检索:ls 
复制、移动、删除:cp、mv、rm 
文件内容查看:cat、tac、more、less、head、tail 
查看文件类型:file(type:查看命令类型) 
命令和文件搜索:which、whereis、find 

下面我们逐个进行解释:

1.cd

Change directory的缩写,变换工作目录的命令。
    cd [dir] 最简单的用法,目录可以是绝对路径也可以是相对路径
    cd ~$USERNAME 表示切换至$USERNAME的家目录下

2、pwd

Print Working Directory,显示当前所在目录的路径
    【OPTION】
    -P [dir]    表示实际的路径,而非使用link的路径
    【FOR EXAMPOR】
        [root@localhost ppp]# cd -P /var/mail/
        [root@localhost mail]# pwd
        /var/spool/mail
        [root@localhost mail]# pwd -P
        /var/spool/mail

3、mkdir

Make Directories,创建目录文件
    【OPTION】
    -m,--mode=MODE  
            set file mode,not a=rwx-umask指定权限,不受umask影响
    -p,--parents
            no error if existing,make parent directories as needed
            根据需要生成父目录(如果父目录不存在),不会报错
    -v,--verbose
            print a message for each created directories.
            打印创建目录消息
    【FOR EXAMPLE】
        [root@localhost tmp]# mkdir -m 555 test
        [root@localhost tmp]# umask
        0022
        [root@localhost tmp]# ll -a
        dr-xr-xr-x.  2 root root    6 Sep 24 22:45 test
        [root@localhost tmp]# mkdir -pv test/testx/testy
        mkdir: created directory ‘test/testx’
        mkdir: created directory ‘test/testx/testy

4、rmdir

Remove empty directories,删除空目录
    【SYNOPSIS】
    rmdir [option]... [dir]...
    【OPTION】
        -p, --parents   
            remove DIRECTORY and its ancestors(上代、祖先)
            删除目录及其上级目录一起删除
        -v,--verbose
    【FOR EXAMPLE】
        [root@localhost tmp]# ls -ld /tmp/*
        -rw-r--r--.  1 root root  134 Sep 21 23:07 /tmp/issue
        drwx------.  2 ftp  ftp    59 Sep 21 22:20 /tmp/skel
        dr-xr-xr-x.  3 root root   18 Sep 24 22:53 /tmp/test
        -rwx------.  1 root root    0 Sep 21 20:27 /tmp/test1
        drwxr-xr-x. 11 root root 4096 Sep 22 01:35 /tmp/x
        [root@localhost tmp]# rmdir -p test/testx/testy
        [root@localhost tmp]# ls -ld /tmp/*
        -rw-r--r--.  1 root root  134 Sep 21 23:07 /tmp/issue
        drwx------.  2 ftp  ftp    59 Sep 21 22:20 /tmp/skel
        -rwx------.  1 root root    0 Sep 21 20:27 /tmp/test1
        drwxr-xr-x. 11 root root 4096 Sep 22 01:35 /tmp/x

5、ls

List Directory contents,列举路径目录
    【SYNOPSIS】
    ls [option]... [file]...
    【OPTION】
        -a,--all
            don't ignore entries starting with.
            列举全部的文档,连同隐藏的文档(.开头的文档)
        -A,--almost-all
            don't list implied . and ..
        -l  use a long listing format
            使用长格式显示,每一行数据代表一个文件或者目录
        -f  do not sort, enable -aU, disable -ls --color
            不进行排序的显示
        -h,--human-readable
            with -l,print sizes in human readable format
            结合-l选项一起使用,以人类可读的格式显示(例如:kb,MB,GB等)
        -i,--inode
            print the index number for each file
            显示每个文件的inode号
        -s,--size
            print the allocated size of each file,in blocks
            显示每个文件分配的大小
        -S  sort by file size
            按文件大小排序,结合-l选项使用更清楚
        -r,--reverse(逆向)
            reverse order while sorting
            逆向排序显示,跟ls命令单独使用的结果相反
        -R,--recursive
            list subdirectories recursively
            连同子目录的内容一并列举(此命令慎用)
        -g  like -l,but don't list owner
        -G,--no-group

6、cp

Copy Files and Directories,复制文件和目录,如果目标不存在,则创建目标文件或者目录,如果目标存在,则直接覆盖
    【SYNOPSIS】
    cp [OPTION]... [-T] SOURCE DEST     /*单源复制,SOURCE源文件,DESTINATION目标*/
    cp [OPTION]... SOURCE... DIRECTORY      /*多源复制*/
    cp [OPTION]... -t DIRECTORY SOURCE...       /*多源复制*/
    【OPTION】
        -a,--archive
            same as -dR --preserve=all
            保留源文件的所有属性,递归复制
        -i,--interactive
            prompt before overwrite 覆盖之前交互提示
        -f,--force
            if an existing destination file cannot be opened,remove it and try again
            如果目标文件无法打开,则删除它并重新尝试复制。也就是不进行提示,强制复制
        -R,-r,--recursive
            copy directories recursively 递归式复制
        -d,same as --no-dereference --preserve=links
            如果源文件是个链接文件,则将链接文件的源文件内容复制,但不指向源文件;选项与-p(P)同

7、mv

Move or Rename files
    【SYNOPSIS】
    mv [OPTION]... [-T] SOURCE DEST
    mv [OPTION]... SOURCE... DIRECTORY
    mv [OPTION]... -t DIRECTORY SOURCE...
    【OPTION】
        -i,--interactive 交互式进行移动或者重命名
        -f,--force  强制移动或者重命名
        -n,--no-clobber
            don't overwrite an existing file 对已存在文件不进行重写

8、rm

Remove files and directories
    【SYNOPSIS】
    rm [OPTION]... FILE...
    【OPTION】
        -f,--force  强制删除不提示
        -i  每删除一个文件均提示确认
        -I  当删除多于3个文件时只提示一次
        --no-preserve-root  don't treat '/' specially   可删除/

9、cat

concatenate files and print on the standard output 标准输出文件内容
    【SYNOPSIS】
    cat [option]... [file]...
    【OPTION】
        -A,--show all   equivalent to -vET
        -b,--number-nonblank
            number nonempty output lines,overrides -n
            非空白行显示行号显示内容,与-n选项相反
        -E,--show-ends
            display $ at the end of each line
            每行结尾处带$符号显示
        -n,--number
            number all output lines 每行显示行号,包括空白行
        -T,--show-tabs
            display TAB characters as ^I,用^I替换TAB空白字符显示文件
        -v,--show-nonprinting
            use ^ and M- notation,except for LFD and TAB
        -s,--squeeze-blank
            suppress repeated empty output lines 重复空白行只显示一行
    【FOR EXAMPLE】

10、tac

concatenate files and print in reverse  反向输出文档内容,是cat的反义<br />

11、 more

    file perusal filter for crt viewing     分页式查看文件内容,类似cat
    【SYNOPSIS】          
    more [options] file [...]
    【OPTION】
        -number This  option  specifies an integer number which
          is the screen size (in lines)
            设置每页显示的行数
        -d  display help instead of ring bell
            显示帮助信息:SPACE继续查看,q退出
        -l  suppress pause after form feet
            遇到^字符时不会终止浏览
        -p  清屏后显示内容
        -c  显示内容后清理其他内容 
        -s  遇到连续重复的空白行后只显示一行
        +NUM    display file beginning from line number NUM
                从给定的行号开始显示全部内容
        +/string    display file beginning from search string match
                根据匹配字符串的位置开始显示文档内容
    【FOR EXAMPLE】
            [root@localhost tmp]# more -10 +/unset functions 
                ...skipping
                  . /etc/profile.d/lang.sh 2>/dev/null
                  # avoid propagating LANGSH_SOURCED any further
                  unset LANGSH_SOURCED
                fi                  
                # Read in our configuration
                if [ -z "${BOOTUP:-}" ]; then
                  if [ -f /etc/sysconfig/init ]; then
                      . /etc/sysconfig/init
                  else
                --More--(13%)

12、less

    opposite of more 同样是翻页显示内容,只是less可以上下翻页及其他功能
    【OPTION】
        -b <缓冲区大小> 设置缓冲区的大小
        -e  当文件显示结束后,自动离开           
        -f  强迫打开特殊文件,例如外围设备代号、目录和二进制文件          
        -g  只标志最后搜索的关键词         
        -i  忽略搜索时的大小写           
        -m  显示类似more命令的百分比          
        -N  显示每行的行号         
        -o <文件名> 将less 输出的内容在指定文件中保存起来          
        -Q  不使用警告音          
        -s  显示连续空行为一行           
        -S  行过长时间将超出部分舍弃            
        -x <数字> 将“tab”键显示为规定的数字空格

13、head

    output the first part of files 显示文件的第一部分
    【SYNOPSIS】
    head [OPTION]... [FILE]...
    【OPTION】
        -c,--bytes  按字节输出内容,例如:-c 20 显示前20个字节的内容
        -n,--lines  按行数显示内容
        -q,--quiet  静默模式,不显示文件名
        -v,--verbose 总是显示文件名
    【FOR EXAMPLE】
        [root@localhost tmp]# head -v -n 3 functions 
        ==> functions <==
        # -*-Shell-script-*-
        #
        # functions This file contains functions to be used by most or all
        [root@localhost tmp]# head -c 20 issue
        hwo are you?
        fine,th[root@localhost tmp]#

14、tail

    Print the last 10 lines of each FILE to standard output. 标准输出每一份文件的最后10行
    【SYNOPSIS】
    tail [OPTION]... [FILE]...
    【OPTION】
        -f,--follow     随着文件自增将内容追加到标准输出
        -n,--lines      设定显示的行数
        -s,--sleep-interval=N
                与-f参数一起用,表示刷新间隔
        -c,--bytes=K    从K字节位置显示内容
    【FOR EXAMPLE】       
        基本用法同head类似

15、file

    Determine type of FILEs 详细明确文件类型
    【SYNOPSIS】
    file [OPTION...] [FILE...]
    【OPTION】
    【FOR EXAMPLE】
        [root@localhost tmp]# file x/af1.txt 
        x/af1.txt: ASCII text
文件的元数据

stat

Display file or file system status
[root@localhost tmp]# stat issue
  File: ‘issue’  文件名
  Size: 191   文件大小      Blocks: 8  块大小      IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 101365706   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-09-25 11:42:52.578622335 +0800访问时间
Modify: 2016-09-25 11:42:48.371622436 +0800修改时间
Change: 2016-09-25 11:42:48.372622436 +0800元数据改变时间
 Birth: -

stat 默认会得到文件的元数据,其包括该文件的所有信息如上;我们通常可以根据文件的时间戳来确定文件的被使用情况,可通过命令touch来修改时间戳数据,不做详细解释。

命令别名

alias

Define or display aliases   定义或者显示aliases
alias作为一个linux内置命令,可以重新定义已有命令的格式;例如:alias rm=rm -i之后,使用rm命令则表示删除file之前且确认是否删除
【OPTION】
    -p  Print all defined aliases in a reusable format 显示所有定义的aliases
【FOR EXAMPLE】
    [root@localhost ~]# alias -p
    alias cp='cp -i'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias vi='vim'

    [root@localhost ~]#alias tail='tail -f'
命令引用

命令的执行结果有很多种方式,其中之一如下:

  • “也就是1(!)之前的那个符号,表示优先执行

[root@localhost tmp]# cat `pwd`
cat: /tmp: Is a directory
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# cat /tmp
cat: /tmp: Is a directory
  • 变量,简单的命令引用可使用变量

[root@localhost ~]# cat $(pwd)
cat: /root: Is a directory
与上一个命令的结果一样。

命令行展开演示

     创建/tmp目录下的a_c,a_d,b_c,b_d

        [root@localhost tmp]# mkdir -pv /tmp/{a,b}_{c,d}
        mkdir: created directory ‘/tmp/a_c’
        mkdir: created directory ‘/tmp/a_d’
        mkdir: created directory ‘/tmp/b_c’
        mkdir: created directory ‘/tmp/b_d’
        [root@localhost tmp]# ls
        a_c  a_d  b_c  b_d  issue  skel  test1  x

     创建/tmp/mylinux目录

    [root@localhost tmp]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-
    scripts},lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
        mkdir: created directory ‘/tmp/mylinux’
        mkdir: created directory ‘/tmp/mylinux/bin’
        mkdir: created directory ‘/tmp/mylinux/boot’
        mkdir: created directory ‘/tmp/mylinux/boot/grub’
        mkdir: created directory ‘/tmp/mylinux/dev’
        mkdir: created directory ‘/tmp/mylinux/etc’
        mkdir: created directory ‘/tmp/mylinux/etc/rc.d’
        mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’
        mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’
        mkdir: created directory ‘/tmp/mylinux/etc/sysconfig/network-scripts’
        mkdir: created directory ‘/tmp/mylinux/lib64’
        mkdir: created directory ‘/tmp/mylinux/proc’
        mkdir: created directory ‘/tmp/mylinux/sbin’
        mkdir: created directory ‘/tmp/mylinux/sys’
        mkdir: created directory ‘/tmp/mylinux/tmp’
        mkdir: created directory ‘/tmp/mylinux/usr’
        mkdir: created directory ‘/tmp/mylinux/usr/local’
        mkdir: created directory ‘/tmp/mylinux/usr/local/bin’
        mkdir: created directory ‘/tmp/mylinux/usr/local/sbin’
        mkdir: created directory ‘/tmp/mylinux/var’
        mkdir: created directory ‘/tmp/mylinux/var/lock’
        mkdir: created directory ‘/tmp/mylinux/var/log’
        mkdir: created directory ‘/tmp/mylinux/var/run’
        [root@localhost tmp]# tree -d /tmp/mylinux/
        /tmp/mylinux/
        ├── bin
        ├── boot
        │   └── grub
        ├── dev
        ├── etc
        │   ├── rc.d
        │   │   └── init.d
        │   └── sysconfig
        │       └── network-scripts
        ├── lib64
        ├── proc
        ├── sbin
        ├── sys
        ├── tmp
        ├── usr
        │   └── local
        │       ├── bin
        │       └── sbin
        └── var
            ├── lock
            ├── log
            └── run         
        22 directories

显示/var目录下所有以了开头,以一个小写字母结尾,且中间至少有一位数字(可以有其他字符)的文件或者目录

[root@localhost tmp]# ls -dl /var/l*[0-9]*[a-z]

显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或者目录

[root@localhost tmp]#ll -d /etc/[0-9]*[^0-9]

显示/etc目录下,以非字母开头,后面跟了一个字母以及其他任意长度任意字符的目录或者文件

[root@localhost tmp]#ll -d /etc/[^a-z][^[:digit:]]*
[root@localhost tmp]# ll -d /tmp/[^[:alpha:]][^[:digit:]]*
-rw-r--r--. 1 root root 0 Sep 25 12:22 /tmp/1tfile2016n
-rw-r--r--. 1 root root 0 Sep 25 13:35 /tmp/2ABsdf
-rw-r--r--. 1 root root 0 Sep 25 13:34 /tmp/2asdljfl

在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件格式:tfile-2016-05-27-09-32-22

[root@localhost tmp]#touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`
[root@localhost tmp]#ll -a 
-rw-r--r--.  1 root root    0 Sep 25 13:39 tfile-2016-09-25-13-39-47

复制/etc目录下所有以P开头,以非数字结尾的文件或者目录到/tmp/mytest1目录中

[root@localhost tmp]#mkdir mytest1
[root@localhost tmp]#cp -af /etc/p*[^0-9] /tmp/mytest1
[root@localhost mytest1]# ls
pam.d   passwd-  plymouth  popt.d   ppp             printcap  profile.d  python
passwd  pki      pm        postfix  prelink.conf.d  profile   protocols

复制/etc目录下所有以.d结尾的文件或者目录至/tmp/mytest2/下

[root@localhost tmp]#mkdir mytest2
[root@localhost tmp]# cp -af /etc/*.d /tmp/mytest2/
[root@localhost mytest2]# ls
bash_completion.d  dnsmasq.d      logrotate.d     popt.d          rc2.d  rc.d        sysctl.d
binfmt.d           dracut.conf.d  modprobe.d      prelink.conf.d  rc3.d  rsyslog.d   tmpfiles.d
chkconfig.d        grub.d         modules-load.d  profile.d       rc4.d  rwtab.d     xinetd.d
cron.d             init.d         my.cnf.d        rc0.d           rc5.d  statetab.d  yum.repos.d
depmod.d           ld.so.conf.d   pam.d           rc1.d           rc6.d  sudoers.d

复制/etc目录下所有以l或者m或者n开头的,以.conf结尾的文件至/tmp/mytest3目录中

[root@localhost tmp]# mkdir mytest3
[root@localhost tmp]# cp -a /etc/[lmn]*.conf /tmp/mytest3/
[root@localhost tmp]# ls -Al mytest3/
total 48
-rw-r--r--.  1 root root   28 Feb 28  2013 ld.so.conf
-rw-r-----.  1 root root  191 Oct 29  2014 libaudit.conf
-rw-r--r--.  1 root root 2391 Oct 13  2013 libuser.conf
-rw-r--r--.  1 root root   19 Sep 12 22:32 locale.conf
-rw-r--r--.  1 root root  662 Jul 31  2013 logrotate.conf
-rw-r--r--.  1 root root 5171 Jun 10  2014 man_db.conf
-rw-r--r--.  1 root root  936 Mar  6  2015 mke2fs.conf
-rw-r--r--.  1 root root 1717 Sep 12 22:32 nsswitch.conf
-rw-r--r--.  1 root root 2050 Sep 12 22:32 ntp.conf


原创文章,作者:396064847,如若转载,请注明出处:http://www.178linux.com/49164

(0)
上一篇 2016-09-27 20:30
下一篇 2016-09-28 08:51

相关推荐

  • iptables学习笔记   Netfilter:是Linux操作系统核心层内部的一个数据包处理模块。   Hook point:数据包在Netfilter中的挂载点。(PRE_ROUTIN   ,INPUT,OUTPUT,FORWARD,POST_ROUTING)   iptables 规则组成:四张表+五条链(Ho…

    Linux干货 2016-12-05
  • CentOS环境下,ab性能测试功具介绍及使用

    网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环。只有让服务器处在高压情况下,才能真正体现出软件、硬件等各种设置不当所暴露出的问题。 性能测试工具目前最常见的有以下几种:ab、http_load、webbench、siege。 ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型…

    Linux干货 2017-07-22
  • Linux部分命令及使用

    Linux部分命令解释及使用 ASCII 信息交换码  GB2312 big5 UTF-8 字符集 乱码问题 生产环境中最常见故障  字符集格式设置不一致 查看系统版本  cat /etc/redhat-release  $() “&nb…

    2017-04-09
  • DNS服务基础

    DNS服务:是一种工作在应用层的特定应用,也是.c/s架构模式的,DNS的是一种应用层协议,他的端口是UPD协议的53号端口,()根据应用场景不同也会用到tcp协议)这就意味着DNS是默认通过UDP协议进行通信的 我们访问任何一个网站都是通过主机名的方式进行访问的;例如www.baidu.com,这是个主机名.称之为FQDN(完全限定域名) 常见的顶级域中的…

    Linux干货 2016-11-07
  • Linux不同发行版之间的联系与区别

    Linux发行版有商业版、社区版两类。两者的共同点都是GNU/Linux发行版,区别在于商业版是由商业公司维护,社区版是社区组织维护。由于linux发行版都要遵循GPL协定,任何人都有使用、共享、修改软件源代码的自由,所以商业版的收入主要来自向企业提供支持服务,它的软件基本都是免费的。

    Linux干货 2018-03-04