--- title: linux 常用命令-目录基本操作 date: 2018/02/27 categories: - linux tags: - linux - command --- - [linux 常用命令-目录基本操作](#linux-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4-%E7%9B%AE%E5%BD%95%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C) - [cd](#cd) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [实例](#%E5%AE%9E%E4%BE%8B) - [ls](#ls) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [cp](#cp) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [mv](#mv) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [rm](#rm) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [mkdir](#mkdir) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [rmdir](#rmdir) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [pwd](#pwd) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [实例](#%E5%AE%9E%E4%BE%8B) - [dirs](#dirs) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) - [tree](#tree) - [补充说明](#%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E) - [语法](#%E8%AF%AD%E6%B3%95) - [选项](#%E9%80%89%E9%A1%B9) - [参数](#%E5%8F%82%E6%95%B0) - [实例](#%E5%AE%9E%E4%BE%8B) # linux 常用命令-目录基本操作 ## cd 切换用户当前工作目录 ### 补充说明 **cd命令** 用来切换工作目录至dirname。 其中dirName表示法可为绝对路径或相对路径。若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录)。另外,`~`也表示为home directory的意思,`.`则是表示目前所在的目录,`..`则表示目前目录位置的上一层目录。 ### 语法 ``` cd (选项) (参数) ``` ### 选项 ``` -p 如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录 -L 如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。 - 当仅实用"-"一个选项时,当前工作目录将被切换到环境变量"OLDPWD"所表示的目录。 ``` ### 实例 ``` cd 进入用户主目录; cd ~ 进入用户主目录; cd - 返回进入此目录之前所在的目录; cd .. 返回上级目录(若当前目录为“/“,则执行完后还在“/";".."为上级目录的意思); cd ../.. 返回上两级目录; cd !$ 把上个命令的参数作为cd参数使用。 ``` ## ls 显示目录内容列表 ### 补充说明 **ls命令** 用来显示目标列表,在Linux中是使用率较高的命令。ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。 ### 语法 ``` ls(选项)(参数) ``` ### 选项 ``` -a, --all 不隐藏任何以. 开始的项目 -A, --almost-all 列出除. 及.. 以外的任何项目 --author 与-l 同时使用时列出每个文件的作者 -b, --escape 以八进制溢出序列表示不可打印的字符 --block-size=SIZE scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by ctime, newest first -C list entries by columns --color[=WHEN] colorize the output; WHEN can be 'never', 'auto', or 'always' (the default); more info below -d, --directory list directories themselves, not their contents -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -ls --color -F, --classify append indicator (one of */=>@|) to entries --file-type likewise, except do not append '*' --format=WORD across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C --full-time like -l --time-style=full-iso -g 类似-l,但不列出所有者 --group-directories-first group directories before files; can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping -G, --no-group 以一个长列表的形式,不输出组名 -h, --human-readable 与-l 一起,以易于阅读的格式输出文件大小 (例如 1K 234M 2G) --si 同上面类似,但是使用1000 为基底而非1024 -H, --dereference-command-line follow symbolic links listed on the command line --dereference-command-line-symlink-to-dir follow each command line symbolic link that points to a directory --hide=PATTERN do not list implied entries matching shell PATTERN (overridden by -a or -A) --indicator-style=WORD append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F) -i, --inode print the index number of each file -I, --ignore=PATTERN do not list implied entries matching shell PATTERN -k, --kibibytes default to 1024-byte blocks for disk usage -l 使用较长格式列出信息 -L, --dereference 当显示符号链接的文件信息时,显示符号链接所指示 的对象而并非符号链接本身的信息 -m 所有项目以逗号分隔,并填满整行行宽 -n, --numeric-uid-gid 类似 -l,但列出UID 及GID 号 -N, --literal 输出未经处理的项目名称 (如不特别处理控制字符) -o 类似 -l,但不列出有关组的信息 -p, --indicator-style=slash 对目录加上表示符号"/" -q, --hide-control-chars print ? instead of nongraphic characters --show-control-chars show nongraphic characters as-is (the default, unless program is 'ls' and output is a terminal) -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape -r, --reverse 逆序排列 -R, --recursive 递归显示子目录 -s, --size 以块数形式显示每个文件分配的尺寸 -S sort by file size --sort=WORD sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X) --time=WORD with -l, show time as WORD instead of default modification time: atime or access or use (-u) ctime or status (-c); also use specified time as sort key if --sort=time --time-style=STYLE with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT; FORMAT is interpreted like in 'date'; if FORMAT is FORMAT1FORMAT2, then FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with 'posix-', STYLE takes effect only outside the POSIX locale -t sort by modification time, newest first -T, --tabsize=COLS assume tab stops at each COLS instead of 8 -u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time -U do not sort; list entries in directory order -v natural sort of (version) numbers within text -w, --width=COLS assume screen width instead of current value -x list entries by lines instead of by columns -X sort alphabetically by entry extension -1 list one file per line SELinux options: --lcontext Display security context. Enable -l. Lines will probably be too wide for most displays. -Z, --context Display security context so it fits on most displays. Displays only mode, user, group, security context and file name. --scontext Display only security context and file name. --help 显示此帮助信息并退出 --version 显示版本信息并退出 ``` ### 参数 目录:指定要显示列表的目录,也可以是具体的文件。 ### 实例 ```bash $ ls # 仅列出当前目录可见文件 $ ls -l # 列出当前目录可见文件详细信息 $ ls -hl # 列出详细信息并以可读大小显示文件大小 $ ls -al # 列出所有文件(包括隐藏)的详细信息 ``` 显示当前目录下包括影藏文件在内的所有文件列表 ``` [root@localhost ~]# ls -a . anaconda-ks.cfg .bash_logout .bashrc install.log .mysql_history satools .tcshrc .vimrc .. .bash_history .bash_profile .cshrc install.log.syslog .rnd .ssh .viminfo ``` 输出长格式列表 ``` [root@localhost ~]# ls -1 anaconda-ks.cfg install.log install.log.syslog satools ``` 显示文件的inode信息 索引节点(index inode简称为“inode”)是Linux中一个特殊的概念,具有相同的索引节点号的两个文本本质上是同一个文件(除文件名不同外)。 ``` [root@localhost ~]# ls -i -l anaconda-ks.cfg install.log 2345481 -rw------- 1 root root 859 Jun 11 22:49 anaconda-ks.cfg 2345474 -rw-r--r-- 1 root root 13837 Jun 11 22:49 install.log ``` 水平输出文件列表 ``` [root@localhost /]# ls -m bin, boot, data, dev, etc, home, lib, lost+found, media, misc, mnt, opt, proc, root, sbin, selinux, srv, sys, tmp, usr, var ``` 修改最后一次编辑的文件 最近修改的文件显示在最上面。 ``` [root@localhost /]# ls -t tmp root etc dev lib boot sys proc data home bin sbin usr var lost+found media mnt opt selinux srv misc ``` 显示递归文件 ``` [root@localhost ~]# ls -R .: anaconda-ks.cfg install.log install.log.syslog satools ./satools: black.txt freemem.sh iptables.sh lnmp.sh mysql php502_check.sh ssh_safe.sh ``` 打印文件的UID和GID ``` [root@localhost /]# ls -n total 254 drwxr-xr-x 2 0 0 4096 Jun 12 04:03 bin drwxr-xr-x 4 0 0 1024 Jun 15 14:45 boot drwxr-xr-x 6 0 0 4096 Jun 12 10:26 data drwxr-xr-x 10 0 0 3520 Sep 26 15:38 dev drwxr-xr-x 75 0 0 4096 Oct 16 04:02 etc drwxr-xr-x 4 0 0 4096 Jun 12 10:26 home drwxr-xr-x 14 0 0 12288 Jun 16 04:02 lib drwx------ 2 0 0 16384 Jun 11 22:46 lost+found drwxr-xr-x 2 0 0 4096 May 11 2011 media drwxr-xr-x 2 0 0 4096 Nov 8 2010 misc drwxr-xr-x 2 0 0 4096 May 11 2011 mnt drwxr-xr-x 2 0 0 4096 May 11 2011 opt dr-xr-xr-x 232 0 0 0 Jun 15 11:04 proc drwxr-x--- 4 0 0 4096 Oct 15 14:43 root drwxr-xr-x 2 0 0 12288 Jun 12 04:03 sbin drwxr-xr-x 2 0 0 4096 May 11 2011 selinux drwxr-xr-x 2 0 0 4096 May 11 2011 srv drwxr-xr-x 11 0 0 0 Jun 15 11:04 sys drwxrwxrwt 3 0 0 98304 Oct 16 08:45 tmp drwxr-xr-x 13 0 0 4096 Jun 11 23:38 usr drwxr-xr-x 19 0 0 4096 Jun 11 23:38 var ``` 列出文件和文件夹的详细信息 ``` [root@localhost /]# ls -l total 254 drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot drwxr-xr-x 6 root root 4096 Jun 12 10:26 data drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc drwxr-xr-x 4 root root 4096 Jun 12 10:26 home drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib drwx------ 2 root root 16384 Jun 11 22:46 lost+found drwxr-xr-x 2 root root 4096 May 11 2011 media drwxr-xr-x 2 root root 4096 Nov 8 2010 misc drwxr-xr-x 2 root root 4096 May 11 2011 mnt drwxr-xr-x 2 root root 4096 May 11 2011 opt dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc drwxr-x--- 4 root root 4096 Oct 15 14:43 root drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin drwxr-xr-x 2 root root 4096 May 11 2011 selinux drwxr-xr-x 2 root root 4096 May 11 2011 srv drwxr-xr-x 11 root root 0 Jun 15 11:04 sys drwxrwxrwt 3 root root 98304 Oct 16 08:48 tmp drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr drwxr-xr-x 19 root root 4096 Jun 11 23:38 var ``` 列出可读文件和文件夹详细信息 ``` [root@localhost /]# ls -lh total 254K drwxr-xr-x 2 root root 4.0K Jun 12 04:03 bin drwxr-xr-x 4 root root 1.0K Jun 15 14:45 boot drwxr-xr-x 6 root root 4.0K Jun 12 10:26 data drwxr-xr-x 10 root root 3.5K Sep 26 15:38 dev drwxr-xr-x 75 root root 4.0K Oct 16 04:02 etc drwxr-xr-x 4 root root 4.0K Jun 12 10:26 home drwxr-xr-x 14 root root 12K Jun 16 04:02 lib drwx------ 2 root root 16K Jun 11 22:46 lost+found drwxr-xr-x 2 root root 4.0K May 11 2011 media drwxr-xr-x 2 root root 4.0K Nov 8 2010 misc drwxr-xr-x 2 root root 4.0K May 11 2011 mnt drwxr-xr-x 2 root root 4.0K May 11 2011 opt dr-xr-xr-x 235 root root 0 Jun 15 11:04 proc drwxr-x--- 4 root root 4.0K Oct 15 14:43 root drwxr-xr-x 2 root root 12K Jun 12 04:03 sbin drwxr-xr-x 2 root root 4.0K May 11 2011 selinux drwxr-xr-x 2 root root 4.0K May 11 2011 srv drwxr-xr-x 11 root root 0 Jun 15 11:04 sys drwxrwxrwt 3 root root 96K Oct 16 08:49 tmp drwxr-xr-x 13 root root 4.0K Jun 11 23:38 usr drwxr-xr-x 19 root root 4.0K Jun 11 23:38 var ``` 显示文件夹信息 ``` [root@localhost /]# ls -ld /etc/ drwxr-xr-x 75 root root 4096 Oct 16 04:02 /etc/ ``` 按时间列出文件和文件夹详细信息 ``` [root@localhost /]# ls -lt total 254 drwxrwxrwt 3 root root 98304 Oct 16 08:53 tmp drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc drwxr-x--- 4 root root 4096 Oct 15 14:43 root drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot drwxr-xr-x 11 root root 0 Jun 15 11:04 sys dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc drwxr-xr-x 6 root root 4096 Jun 12 10:26 data drwxr-xr-x 4 root root 4096 Jun 12 10:26 home drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr drwxr-xr-x 19 root root 4096 Jun 11 23:38 var drwx------ 2 root root 16384 Jun 11 22:46 lost+found drwxr-xr-x 2 root root 4096 May 11 2011 media drwxr-xr-x 2 root root 4096 May 11 2011 mnt drwxr-xr-x 2 root root 4096 May 11 2011 opt drwxr-xr-x 2 root root 4096 May 11 2011 selinux drwxr-xr-x 2 root root 4096 May 11 2011 srv drwxr-xr-x 2 root root 4096 Nov 8 2010 misc ``` 按修改时间列出文件和文件夹详细信息 ``` [root@localhost /]# ls -ltr total 254 drwxr-xr-x 2 root root 4096 Nov 8 2010 misc drwxr-xr-x 2 root root 4096 May 11 2011 srv drwxr-xr-x 2 root root 4096 May 11 2011 selinux drwxr-xr-x 2 root root 4096 May 11 2011 opt drwxr-xr-x 2 root root 4096 May 11 2011 mnt drwxr-xr-x 2 root root 4096 May 11 2011 media drwx------ 2 root root 16384 Jun 11 22:46 lost+found drwxr-xr-x 19 root root 4096 Jun 11 23:38 var drwxr-xr-x 13 root root 4096 Jun 11 23:38 usr drwxr-xr-x 2 root root 12288 Jun 12 04:03 sbin drwxr-xr-x 2 root root 4096 Jun 12 04:03 bin drwxr-xr-x 4 root root 4096 Jun 12 10:26 home drwxr-xr-x 6 root root 4096 Jun 12 10:26 data dr-xr-xr-x 232 root root 0 Jun 15 11:04 proc drwxr-xr-x 11 root root 0 Jun 15 11:04 sys drwxr-xr-x 4 root root 1024 Jun 15 14:45 boot drwxr-xr-x 14 root root 12288 Jun 16 04:02 lib drwxr-xr-x 10 root root 3520 Sep 26 15:38 dev drwxr-x--- 4 root root 4096 Oct 15 14:43 root drwxr-xr-x 75 root root 4096 Oct 16 04:02 etc drwxrwxrwt 3 root root 98304 Oct 16 08:54 tmp ``` 按照特殊字符对文件进行分类 ``` [root@localhost nginx-1.2.1]# ls -F auto/ CHANGES CHANGES.ru conf/ configure* contrib/ html/ LICENSE Makefile man/ objs/ README src/ ``` 列出文件并标记颜色分类 ``` [root@localhost nginx-1.2.1]# ls --color=auto auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src ``` ## cp 将源文件或目录复制到目标文件或目录中 ### 补充说明 **cp命令** 用来将一个或多个源文件或者目录复制到指定的目的文件或目录。它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。 ### 语法 ``` cp(选项)(参数) ``` ### 选项 ``` -a:此参数的效果和同时指定"-dpR"参数相同; -d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录; -f:强行复制文件或目录,不论目标文件或目录是否已存在; -i:覆盖既有文件之前先询问用户; -l:对源文件建立硬连接,而非复制文件; -p:保留源文件或目录的属性; -R/r:递归处理,将指定目录下的所有文件与子目录一并处理; -s:对源文件建立符号连接,而非复制文件; -u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件; -S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀; -b:覆盖已存在的文件目标前将目标文件备份; -v:详细显示命令执行的操作。 ``` ### 参数 * 源文件:制定源文件列表。默认情况下,cp命令不能复制目录,如果要复制目录,则必须使用`-R`选项; * 目标文件:指定目标文件。当“源文件”为多个文件时,要求“目标文件”为指定的目录。 ### 实例 下面的第一行中是 cp 命令和具体的参数(-r 是“递归”, -u 是“更新”,-v 是“详细”)。接下来的三行显示被复制文件的信息,最后一行显示命令行提示符。这样,只拷贝新的文件到我的存储设备上,我就使用 cp 的“更新”和“详细”选项。 通常来说,参数 `-r` 也可用更详细的风格 `--recursive`。但是以简短的方式,也可以这么连用 `-ruv`。 ``` cp -r -u -v /usr/men/tmp ~/men/tmp ``` 版本备份 `--backup=numbered` 参数意思为“我要做个备份,而且是带编号的连续备份”。所以一个备份就是 1 号,第二个就是 2 号,等等。 ```bash $ cp --force --backup=numbered test1.py test1.py $ ls test1.py test1.py.~1~ test1.py.~2~ ``` 如果把一个文件复制到一个目标文件中,而目标文件已经存在,那么,该目标文件的内容将被破坏。此命令中所有参数既可以是绝对路径名,也可以是相对路径名。通常会用到点`.`或点点`..`的形式。例如,下面的命令将指定文件复制到当前目录下: ``` cp ../mary/homework/assign . ``` 所有目标文件指定的目录必须是己经存在的,cp命令不能创建目录。如果没有文件复制的权限,则系统会显示出错信息。 将文件file复制到目录`/usr/men/tmp`下,并改名为file1 ``` cp file /usr/men/tmp/file1 ``` 将目录`/usr/men`下的所有文件及其子目录复制到目录`/usr/zh`中 ``` cp -r /usr/men /usr/zh ``` 交互式地将目录`/usr/men`中的以m打头的所有.c文件复制到目录`/usr/zh`中 ``` cp -i /usr/men m*.c /usr/zh ``` 我们在Linux下使用cp命令复制文件时候,有时候会需要覆盖一些同名文件,覆盖文件的时候都会有提示:需要不停的按Y来确定执行覆盖。文件数量不多还好,但是要是几百个估计按Y都要吐血了,于是折腾来半天总结了一个方法: ``` cp aaa/* /bbb 复制目录aaa下所有到/bbb目录下,这时如果/bbb目录下有和aaa同名的文件,需要按Y来确认并且会略过aaa目录下的子目录。 cp -r aaa/* /bbb 这次依然需要按Y来确认操作,但是没有忽略子目录。 cp -r -a aaa/* /bbb 依然需要按Y来确认操作,并且把aaa目录以及子目录和文件属性也传递到了/bbb。 \cp -r -a aaa/* /bbb 成功,没有提示按Y、传递了目录属性、没有略过目录。 ``` ## mv 用来对文件或目录重新命名 ### 补充说明 **mv命令** 用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。source表示源文件或目录,target表示目标文件或目录。如果将一个文件移到一个已经存在的目标文件中,则目标文件的内容将被覆盖。 mv命令可以用来将源文件移至一个目标文件中,或将一组文件移至一个目标目录中。源文件被移至目标文件有两种不同的结果: 1. 如果目标文件是到某一目录文件的路径,源文件会被移到此目录下,且文件名不变。 2. 如果目标文件不是目录文件,则源文件名(只能有一个)会变为此目标文件名,并覆盖己存在的同名文件。如果源文件和目标文件在同一个目录下,mv的作用就是改文件名。当目标文件是目录文件时,源文件或目录参数可以有多个,则所有的源文件都会被移至目标文件中。所有移到该目录下的文件都将保留以前的文件名。 注意事项:mv与cp的结果不同,mv好像文件“搬家”,文件个数并未增加。而cp对文件进行复制,文件个数增加了。 ### 语法 ``` mv(选项)(参数) ``` ### 选项 ``` --backup=<备份模式>:若需覆盖文件,则覆盖前先行备份; -b:当文件存在时,覆盖前,为其创建一个备份; -f:若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录; -i:交互式操作,覆盖前先行询问用户,如果源文件与目标文件或目标目录中的文件同名,则询问用户是否覆盖目标文件。用户输入”y”,表示将覆盖目标文件;输入”n”,表示取消对源文件的移动。这样可以避免误将文件覆盖。 --strip-trailing-slashes:删除源文件中的斜杠“/”; -S<后缀>:为备份文件指定后缀,而不使用默认的后缀; --target-directory=<目录>:指定源文件要移动到目标目录; -u:当源文件比目标文件新或者目标文件不存在时,才执行移动操作。 ``` ### 参数 * 源文件:源文件列表。 * 目标文件:如果“目标文件”是文件名则在移动文件的同时,将其改名为“目标文件”;如果“目标文件”是目录名则将源文件移动到“目标文件”下。 ### 实例 将目录`/usr/men`中的所有文件移到当前目录(用`.`表示)中: ``` mv /usr/men/* . ``` 移动文件 ``` mv file_1.txt /home/office/ ``` 移动多个文件 ``` mv file_2.txt file_3.txt file_4.txt /home/office/ mv *.txt /home/office/ ``` 移动目录 ``` mv directory_1/ /home/office/ ``` 重命名文件或目录 ```bash mv file_1.txt file_2.txt # 将文件file_1.txt改名为file_2.txt ``` 重命名目录 ``` mv directory_1/ directory_2/ ``` 打印移动信息 ```bash mv -v *.txt /home/office ``` 提示是否覆盖文件 ``` mv -i file_1.txt /home/office ``` 源文件比目标文件新时才执行更新 ``` mv -uv *.txt /home/office ``` 不要覆盖任何已存在的文件 ``` mv -vn *.txt /home/office ``` 复制时创建备份 ``` mv -bv *.txt /home/office ``` 无条件覆盖已经存在的文件 ``` mv -f *.txt /home/office ``` ## rm 用于删除给定的文件和目录 ### 补充说明 **rm** **命令** 可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉。对于链接文件,只是删除整个链接文件,而原有文件保持不变。 注意:使用rm命令要格外小心。因为一旦删除了一个文件,就无法再恢复它。所以,在删除文件之前,最好再看一下文件的内容,确定是否真要删除。rm命令可以用-i选项,这个选项在使用文件扩展名字符删除多个文件时特别有用。使用这个选项,系统会要求你逐一确定是否要删除。这时,必须输入y并按Enter键,才能删除文件。如果仅按Enter键或其他字符,文件不会被删除。 ### 语法 ``` rm (选项)(参数) ``` ### 选项 ``` -d:直接把欲删除的目录的硬连接数据删除成0,删除该目录; -f:强制删除文件或目录; -i:删除已有文件或目录之前先询问用户; -r或-R:递归处理,将指定目录下的所有文件与子目录一并处理; --preserve-root:不对根目录进行递归操作; -v:显示指令的详细执行过程。 ``` ### 参数 文件:指定被删除的文件列表,如果参数中含有目录,则必须加上`-r`或者`-R`选项。 ### 实例 交互式删除当前目录下的文件test和example ``` rm -i test example Remove test ?n(不删除文件test) Remove example ?y(删除文件example) ``` 删除当前目录下除隐含文件外的所有文件和子目录 ``` # rm -r * ``` 应注意,这样做是非常危险的! **rm 命令删除文件** ```bash # rm 文件1 文件2 ... rm testfile.txt ``` **rm 命令删除目录** > rm -r [目录名称] > -r 表示递归地删除目录下的所有文件和目录。 > -f 表示强制删除 ``` rm -rf testdir rm -r testdir ``` **删除操作前有确认提示** > rm -i [文件/目录] ``` rm -r -i testdir ``` **rm 忽略不存在的文件或目录** > -f 选项(LCTT 译注:即 “force”)让此次操作强制执行,忽略错误提示 ``` rm -f [文件...] ``` **仅在某些场景下确认删除** > 选项 -I,可保证在删除超过 3 个文件时或递归删除时(LCTT 译注: 如删除目录)仅提示一次确认。 ``` rm -I file1 file2 file3 ``` **删除根目录** > 当然,删除根目录(/)是 Linux 用户最不想要的操作,这也就是为什么默认 rm 命令不支持在根目录上执行递归删除操作。 > 然而,如果你非得完成这个操作,你需要使用 --no-preserve-root 选项。当提供此选项,rm 就不会特殊处理根目录(/)了。 ``` 不给实例了,操作系统都被你删除了,你太坏了😆 ``` **rm 显示当前删除操作的详情** ``` rm -v [文件/目录] ``` ## mkdir 用来创建目录 ### 补充说明 **mkdir命令** 用来创建目录。该命令创建由dirname命名的目录。如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录;如果给出了一个已经存在的路径,将会在该目录下创建一个指定的目录。在创建目录时,应保证新建的目录与它所在目录下的文件没有重名。  注意:在创建文件时,不要把所有的文件都存放在主目录中,可以创建子目录,通过它们来更有效地组织文件。最好采用前后一致的命名方式来区分文件和目录。例如,目录名可以以大写字母开头,这样,在目录列表中目录名就出现在前面。 在一个子目录中应包含类型相似或用途相近的文件。例如,应建立一个子目录,它包含所有的数据库文件,另有一个子目录应包含电子表格文件,还有一个子目录应包含文字处理文档,等等。目录也是文件,它们和普通文件一样遵循相同的命名规则,并且利用全路径可以唯一地指定一个目录。 ### 语法 ``` mkdir (选项)(参数) ``` ### 选项 ``` -Z:设置安全上下文,当使用SELinux时有效; -m<目标属性>或--mode<目标属性>建立目录的同时设置目录的权限; -p或--parents 若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录; --version 显示版本信息。 ``` ### 参数 目录:指定要创建的目录列表,多个目录之间用空格隔开。 ### 实例 在目录`/usr/meng`下建立子目录test,并且只有文件主有读、写和执行权限,其他人无权访问 ``` mkdir -m 700 /usr/meng/test ``` 在当前目录中建立bin和bin下的os_1目录,权限设置为文件主可读、写、执行,同组用户可读和执行,其他用户无权访问 ``` mkdir -p-m 750 bin/os_1 ``` ## rmdir 用来删除空目录 ### 补充说明 **rmdir命令** 用来删除空目录。当目录不再被使用时,或者磁盘空间已到达使用限定值,就需要删除失去使用价值的目录。利用rmdir命令可以从一个目录中删除一个或多个空的子目录。该命令从一个目录中删除一个或多个子目录,其中dirname佬表示目录名。如果dirname中没有指定路径,则删除当前目录下由dirname指定的目录;如dirname中包含路径,则删除指定位置的目录。删除目录时,必须具有对其父目录的写权限。 注意:子目录被删除之前应该是空目录。就是说,该目录中的所有文件必须用rm命令全部,另外,当前工作目录必须在被删除目录之上,不能是被删除目录本身,也不能是被删除目录的子目录。 虽然还可以用带有`-r`选项的rm命令递归删除一个目录中的所有文件和该目录本身,但是这样做存在很大的危险性。 ### 语法 ``` rmdir(选项)(参数) ``` ### 选项 ``` -p或--parents:删除指定目录后,若该目录的上层目录已变成空目录,则将其一并删除; --ignore-fail-on-non-empty:此选项使rmdir命令忽略由于删除非空目录时导致的错误信息; -v或-verboes:显示命令的详细执行过程; --help:显示命令的帮助信息; --version:显示命令的版本信息。 ``` ### 参数 目录列表:要删除的空目录列表。当删除多个空目录时,目录名之间使用空格隔开。 ### 实例 将工作目录下,名为 `www` 的子目录删除 : ```bash rmdir www ``` 在工作目录下的 www 目录中,删除名为 Test 的子目录。若 Test 删除后,www 目录成为空目录,则 www 亦予删除。 ```bash rmdir -p www/Test ``` 下面命令等价于 `rmdir a/b/c`, `rmdir a/b`, `rmdir a` ```bash rmdir -p a/b/c ``` ## pwd 绝对路径方式显示用户当前工作目录 ### 补充说明 **pwd命令** 以绝对路径的方式显示用户当前工作目录。命令将当前目录的全路径名称(从根目录)写入标准输出。全部目录使用`/`分隔。第一个`/`表示根目录,最后一个目录是当前目录。执行pwd命令可立刻得知您目前所在的工作目录的绝对路径名称。 ### 语法 ``` pwd(选项) ``` ### 选项 ``` --help:显示帮助信息; --version:显示版本信息。 ``` ### 实例 ``` [root@localhost ~]# pwd /root ``` ## dirs 显示目录记录 ### 补充说明 **dirs命令** 显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录)。dirs始终显示当然目录, 再是堆栈中的内容;即使目录堆栈为空, dirs命令仍然只显示当然目录。 ### 语法 ``` dirs(选项)(参数) ``` ### 选项 ``` -c:删除目录栈中的所有记录 -l:以完整格式显示 -p:一个目录一行的方式显示 -v:每行一个目录来显示目录栈的内容,每个目录前加上的编号 +N:显示从左到右的第n个目录,数字从0开始 -N:显示从右到左的第n个日录,数字从0开始 ``` ### 参数 目录:显示目录堆叠中的记录。 ### 实例 ``` [root@localhost etc]# dirs /etc ``` ## tree 树状图列出目录的内容 ### 补充说明 **tree命令** 以树状图列出目录的内容。 ### 语法 ``` tree(选项)(参数) ``` ### 选项 ```bash ------- 列表选项 ------- -a # 显示所有文件和目录。 -d # 先是目录名称而非文件。 -l # 如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录。 -f # 在每个文件或目录之前,显示完整的相对路径名称。 -x # 将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该目录予以排除在寻找范围外。 -L level # 限制目录显示层级。 -R # Rerun tree when max dir level reached. -P pattern # <范本样式> 只显示符合范本样式的文件和目录名称。 -I pattern # Do not list files that match the given pattern. --ignore-case # Ignore case when pattern matching. --matchdirs # Include directory names in -P pattern matching. --noreport # Turn off file/directory count at end of tree listing. --charset X # Use charset X for terminal/HTML and indentation line output. --filelimit # # Do not descend dirs with more than # files in them. --timefmt # Print and format time according to the format . -o filename # Output to file instead of stdout. -------- 文件选项 --------- -q # 用“?”号取代控制字符,列出文件和目录名称。 -N # 直接列出文件和目录名称,包括控制字符。 -Q # Quote filenames with double quotes. -p # 列出权限标示。 -u # 列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。 -g # 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码。 -s # 列出文件和目录大小。 -h # Print the size in a more human readable way. --si # Like -h, but use in SI units (powers of 1000). -D # 列出文件或目录的更改时间。 -F # 在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","@","|"号。 --inodes # Print inode number of each file. --device # Print device ID number to which each file belongs. ------- 排序选项 ------- -v # Sort files alphanumerically by version. -t # 用文件和目录的更改时间排序。 -c # Sort files by last status change time. -U # Leave files unsorted. -r # Reverse the order of the sort. --dirsfirst # List directories before files (-U disables). --sort X # Select sort: name,version,size,mtime,ctime. ------- 图形选项 ------ -i # 不以阶梯状列出文件和目录名称。 -A # 使用ASNI绘图字符显示树状图而非以ASCII字符组合。 -S # Print with CP437 (console) graphics indentation lines. -n # Turn colorization off always (-C overrides). -C # 在文件和目录清单加上色彩,便于区分各种类型。 ------- XML / HTML / JSON选项 ------- -X # Prints out an XML representation of the tree. -J # Prints out an JSON representation of the tree. -H baseHREF # Prints out HTML format with baseHREF as top directory. -T string # Replace the default HTML title and H1 header with string. --nolinks # Turn off hyperlinks in HTML output. ---- 杂项选项 ---- --version # 输入版本信息。 --help # 打印使用帮助信息。 -- # Options processing terminator. ``` ### 参数 目录:执行tree指令,它会列出指定目录下的所有文件,包括子目录里的文件。 ### 实例 列出目录`/private/` 第一级文件名 ```bash tree /private/ -L 1 /private/ ├── etc ├── tftpboot ├── tmp └── var ``` 忽略文件夹 ```bash tree -I node_modules # 忽略当前目录文件夹node_modules tree -P node_modules # 列出当前目录文件夹node_modules的目录结构 tree -P node_modules -L 2 # 显示目录node_modules两层的目录树结构 tree -L 2 > /home/www/tree.txt # 当前目录结果存到 tree.txt 文件中 ``` 忽略多个文件夹 ```bash tree -I 'node_modules|icon|font' -L 2 ```