- pwd 显示当前工作目录的绝对路径
[root@localhost ~]# pwd
/root
- ls [选项] [目录或文件]
-a :显示当前目录所有的文件和目录,包括隐藏的。
-l:以列表的形式显示
[root@localhost ~]# ls
公共 视频 文档 音乐 anaconda-ks.cfg initial-setup-ks.cfg
模板 图片 下载 桌面 Hello.java
[root@localhost ~]# ls -l
总用量 12
drwx------. 2 root root 6 2月 18 08:39 公共
drwx------. 2 root root 6 2月 18 08:39 模板
drwx------. 2 root root 6 2月 18 08:39 视频
drwx------. 2 root root 6 2月 18 08:39 图片
drwx------. 2 root root 6 2月 18 08:39 文档
drwx------. 2 root root 6 2月 18 08:39 下载
drwx------. 2 root root 6 2月 18 08:39 音乐
drwx------. 2 root root 6 2月 18 08:39 桌面
-rw-------. 1 root root 1386 2月 18 08:37 anaconda-ks.cfg
-rw-r--r--. 1 root root 102 2月 18 10:03 Hello.java
-rw-r--r--. 1 root root 1541 2月 18 08:39 initial-setup-ks.cfg
[root@localhost ~]# ls -al
总用量 56
dr-xr-x---. 15 root root 4096 2月 19 03:41 .
dr-xr-xr-x. 17 root root 224 2月 18 08:30 ..
drwx------. 2 root root 6 2月 18 08:39 公共
drwx------. 2 root root 6 2月 18 08:39 模板
drwx------. 2 root root 6 2月 18 08:39 视频
drwx------. 2 root root 6 2月 18 08:39 图片
drwx------. 2 root root 6 2月 18 08:39 文档
drwx------. 2 root root 6 2月 18 08:39 下载
- cd 切换目录
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/alice/
[root@localhost alice]# pwd
/home/alice
[root@localhost alice]#
cd ~或cd:回到用户家目录
cd …:回到上一级目录
- mkdir [选项] 要创建的目录
-p:创建多级目录
创建一个目录:
[root@localhost home]# mkdir cat
[root@localhost home]# ls
alice Bob cat dog
创建多个目录:
[root@localhost home]# mkdir -p /home/animal/tiger
[root@localhost home]# ls
alice animal Bob cat dog
[root@localhost home]# cd animal
[root@localhost animal]# ls
tiger
- rmdir [选项] 要删除的空目录
[root@localhost home]# rmdir cat
[root@localhost home]# ls
alice animal Bob dog
rmdir删除的是空目录,如果里面有内容是无法删除的。
删除非空目录:
[root@localhost home]# rmdir animal/
rmdir: 删除 'animal/' 失败: 目录非空
[root@localhost home]# rm -rf /home/animal/
[root@localhost home]# ls
alice Bob dog
- touch 创建一个空文件
[root@localhost home]# touch hello.txt
[root@localhost home]# ls
alice Bob dog hello.txt
- cp 拷贝指令
cp [选项] source dest
-r:递归复制整个文件夹
拷贝单个文件:
[root@localhost home]# ls alice Bob dog hello.txt [root@localhost home]# cp hello.txt dog/hello.txt [root@localhost home]# cd dog [root@localhost dog]# ls hello.txt
递归拷贝整个文件夹:
[root@localhost home]# cp -r dog Bob [root@localhost home]# cd Bob [root@localhost Bob]# ls dog [root@localhost Bob]# cd dog/ [root@localhost dog]# ls a.txt b.txt c.txt hello.txt
- rm 指移除文件或目录
rm [选项] 要删除的文件或目录
-r:递归删除整个文件夹
-f:强制删除不提示
[root@localhost home]# ls alice Bob [root@localhost home]# rm -rf Bob [root@localhost home]# ls alice
- mv 移动文件与目录重命名
mv oldNameFile newNameFile
将dog.txt改成cat.txt:
[root@localhost home]# touch dog.txt [root@localhost home]# mv dog.txt cat.txt [root@localhost home]# ls alice cat.txt
将/home/cat.txt移动到/root目录下:
[root@localhost home]# mv cat.txt /root [root@localhost home]# cd /root [root@localhost ~]# ls 公共 视频 文档 音乐 anaconda-ks.cfg Hello.java 模板 图片 下载 桌面 cat.txt initial-setup-ks.cfg
- cat 查看文件内容,以只读的方式打开文件
cat [选项] 要查看到文件
-n:显示行号
[root@localhost ~]# cat Hello.java public class Hello{ public static void main(String[] args){ System.out.print('hello world'); } } [root@localhost ~]# cat -n Hello.java 1 public class Hello{ 2 public static void main(String[] args){ 3 System.out.print('hello world'); 4 } 5 } 6
| more:分页显示,按空格翻页
[root@localhost ~]# cat -n /etc/profile | more 1 # /etc/profile 2 3 # System wide environment and startup programs, for login set up 4 # Functions and aliases go in /etc/bashrc
- more:以全屏的方式按页显示文本文件的内容
空格:向下翻一页
enter:向下翻一行
q:退出
ctrl+f:向下滚动一屏
ctrl+b:返回上一屏
=:输出当前行号
:f :输出文件名和当前行的行号
[root@localhost ~]# more /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates.
- less用来分屏查看文件内容,并不是一次将整个文件加载之后才显示。
[root@localhost ~]# less /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates.
- '>‘输出重定向,会将原来文件内容和’>>'追加,不会覆盖原来的内容
[root@localhost home]# ls -l > a.txt
[root@localhost home]# more a.txt
总用量 4
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 0 2月 19 06:16 a.txt
将ls -l显示的内容写到a.txt,如果文件不存在,则创建文件,如果文件存在,则覆盖文件。
[root@localhost home]# ls -l >> a.txt
[root@localhost home]# more a.txt
总用量 4
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 0 2月 19 06:16 a.txt
总用量 8
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 118 2月 19 06:16 a.txt
将ls -l显示的内容追加到a.txt末尾
[root@localhost home]# echo "hello,world">> a.txt
[root@localhost home]# more a.txt
总用量 4
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 0 2月 19 06:16 a.txt
总用量 8
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 118 2月 19 06:16 a.txt
hello,world
追加内容到a.txt
[root@localhost home]# more /etc/profile >> a.txt
[root@localhost home]# more a.txt
总用量 4
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 0 2月 19 06:16 a.txt
总用量 8
drwx------. 15 alice alice 4096 2月 19 02:34 alice
-rw-r--r--. 1 root root 118 2月 19 06:16 a.txt
hello,world
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
将more打开的内容追加到a.txt中
- echo 输出内容到控制台
输出环境变量
[root@localhost home]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- head 用于显示文件到开头部分,默认显示前10行
head 文件
head -n 5 文件
[root@localhost home]# head /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. [root@localhost home]# head -n 5 /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc
- tail 用于输出文件中尾部的内容,默认显示后10行
tail 文件
tail -n 5 文件:查看文件后5行
[root@localhost home]# tail -n 5 /etc/profile # Bash non-login shells run only /etc/bashrc # Check for double sourcing is done in /etc/bashrc. . /etc/bashrc fi fi
tail -f 文件:实时监控文件的更新
- ln 软连接指令,类似于windows快捷方式
ln -s 原文件或目录 软链接名
[root@localhost home]# ln -s /root link_to_root [root@localhost home]# ls alice a.txt link_to_root [root@localhost home]# cd link_to_root/ [root@localhost link_to_root]# ls 公共 视频 文档 音乐 anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面 Hello.java [root@localhost link_to_root]# pwd /home/link_to_root
删除软连接不要带斜杠:
[root@localhost home]# rm -rf link_to_root/ [root@localhost home]# ls alice a.txt link_to_root [root@localhost home]# rm -rf link_to_root [root@localhost home]# ls alice a.txt
- history 查看已经执行过历史命令,也可以执行历史指令
查看所有的历史命令:
[root@localhost home]# history 1 ip a 2 ipconfig 3 ls -l 4 ifconfig 5 ipconfig 6 ifconfig 7 ls 8 ls -l 9 cd ..
显示最近使用过的10分指令:
[root@localhost home]# history 10 205 cd link_to_root/ 206 ls 207 pwd 208 cd .. 209 rm -rf link_to_root/ 210 ls 211 rm -rf link_to_root 212 ls 213 history 214 history 10
执行206号指令:
[root@localhost home]# !206 ls alice a.txt