下面介绍的命令都是不常见但又是很实用的命令,学会了它们,工作上更得心应手
1. !!
回想下自己身上是否发生过这样一种情况,当键入一行很长的命令敲击回车之后才发现命令开头忘记加 sudo 了,这时候你只需要键入 sudo !!,命令行自动把 !!替换成你上一条运行的命令
[tt@ecs-centos-7 ~]$ yum install cowsay
Loaded plugins: fastestmirror
You need to be root to perform this command.
[tt@ecs-centos-7 ~]$ sudo !!
sudo yum install cowsay
2. cd -
使用过Linux系统的人都知道 cd ..命令 是返回上一级目录,但是很少人知道 cd - 命令作用是返上一次cd到的目录
[root@ecs-centos-7 ~]# pwd
/root
[root@ecs-centos-7 ~]# cd /home/tt
[root@ecs-centos-7 tt]# pwd
/home/tt
[root@ecs-centos-7 tt]# cd -
/root
[root@ecs-centos-7 ~]# pwd
/root
[root@ecs-centos-7 ~]# cd -
/home/tt
[root@ecs-centos-7 tt]# pwd
/home/tt
上面的例子中,首先当前目录是:/root 执行 cd /home/tt之后目录变为/home/tt,然后执行 cd - 目录又变成了/root,再次执行cd -,当前目录又回到了/hom/tt
3. cd
你可能知道 ~ 表示Home目录,但是很少人知道的一个小窍门: 如果你键入 cd 命令,依然能回到Home目录
[root@ecs-centos-7 ~]# pwd
/root
[root@ecs-centos-7 ~]# cd /home/tt
[root@ecs-centos-7 tt]# pwd
/home/tt
[root@ecs-centos-7 tt]# cd
[root@ecs-centos-7 ~]# pwd
/root
[root@ecs-centos-7 ~]#
4. 搜索
你可以通过键盘上的上下键搜索已经键入过的命令,可能你需要连续按15次向上箭头的按键才能找到曾经输入过的ls 命令。现在使用反向搜索可以很轻松的实现: 按一下Ctrl + R键后开始输入命令,反向搜索功能会从最近历史命令中查找和输入最匹配的命令
[tt@ecs-centos-7 ~]$ ls -lh
total 0
-rw-rw-r-- 1 tt tt 0 Jul 2 00:40 a.txt
[tt@ecs-centos-7 ~]$ date
Thu Jul 2 00:41:00 CST 2020
(reverse-i-search)`l': ls -lh
total 0
-rw-rw-r-- 1 tt tt 0 Jul 2 00:40 a.txt
[tt@ecs-centos-7 ~]$
5. 重用参数
另一个小窍门是 !$ 它会被替换成前面一个命令的参数。这是很实用的,例如,当你新创建了一个目录并且想进入这个目录:
[tt@ecs-centos-7 ~]$ mkdir temp
[tt@ecs-centos-7 ~]$ cd !$
cd temp
[tt@ecs-centos-7 temp]$ pwd
/home/tt/temp
[tt@ecs-centos-7 temp]$
6. 拷贝和粘贴
你可能已经发现了 Ctrl + C 和 Ctrl + V,这两个组合在终端上是预留给正在运行的进程用的,可以用Ctrl + Shift + C和Ctrl + Shift + V来替代 Ctrl + C 和 Ctrl + V的功能。
7. 让你的程序后台运行
在终端运行应用程序时,应用程序会随着终端的关闭而退出。使用 nohup命令可以实现终端退出,应用程序依然运行着,nohup 是 "no hang up"的缩写。
例如:使用scp命令从本地传输大文件到远程服务器上,假如突然关掉终端的话,如何保证传输不会中断呢?下面的命令会解决这个问题。
nohup scp big_file user@host:~/big_file
nohup会创建一个nohup.out文件记录命令的输出
8. 自动确认
在编写shell脚本自动执行任务时,有时需要人工输入 yes ,命令才会往下执行。在命令前加上 yes |就会跳过人工输入的步骤,下面的命令会自动输入 yes
yes | yum install lrzsz
如果想自动输入 no ,则在命令前加上no |,下面的命令会自动输入 no
9. 粉碎文件
如果你比较注重隐私的话,那这个命令很适合你。rm命令常用来删除文件,删除之后可以通过特殊的软件提取出已经删除的文件数据。想要彻底删除,可以试下下面的命令
shred -zvu filename
10. vim为文件设置密码
- 如果文件处于vim编辑状态,切换到命令模式,输入
:X(注意:这里是大写的X) 输入密码,再次确认密码之后保存退出 - 如果文件没有处于编辑状态,使用
vim +X filename也可以为文件设置密码
下面是为文件 a.txt 设置密码
[root@ecs-centos-7 ~]# vim a.txt
:X
Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key: ***
Enter same key again: ***
设置密码之后,无论是通过cat还是vim都无法直接查看或者编辑 a.txt
[root@ecs-centos-7 ~]# cat a.txt
VimCrypt~01!q,oot@ecs-centos-7 ~]#
[root@ecs-centos-7 ~]# vim a.txt
Need encryption key for "a.txt"
Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key:
从上面可以看出,使用 cat a.txt命令看到的是一串乱码,通过vim a.txt 编辑文件,需要先输入密码
LeetCode Perfect Squares
华为笔试 数字转中文拼音
二位数组 顺时针打印矩阵
LeetCode Interleaving String
LeetCode Coins in a Line
LeetCode Backpack
LeetCode Unique Paths
LeetCode Minimum Path Sum
腾讯模拟笔试题
- 最新文章
-
JavaScript经典片段
Linux C编程学习之开发工具1---GCC编译器
Linux C编程学习之C语言简介---预处理、宏、文件包含……
Linux网络管理2---(网络环境查看命令、网络测试命令)
Linux网络管理1---(Linux配置IP地址,ifconfig、配置文件)
网络知识学习5---(网关的作用)(学习还不深入,待完善)
网络知识学习4---(DNS的作用)
网络知识学习3---(端口原理的讲解)
网络知识学习2---(IP地址、子网掩码)(学习还不深入,待完善)
网络知识学习1---(基础知识:ISO/OSI七层模型和TCP/IP四层模型)
- 热门文章
-
Delphi:与VCL同步(Synchronize()、用消息来同步)
Linux常用命令学习8---(用户和用户组管理)
Linux常用命令学习7---(磁盘管理df du、磁盘的分区和格式化fdisk parted)
Linux常用命令学习6---(vim的使用)
Linux常用命令学习5---(Shell编程)
Linux常用命令学习4---(挂载命令mount umount、用户登陆查看和用户交互命令 w who last lastlog)
Delphi DLL的创建、静态及动态调用
解决Delphi图形化界面的TEdit、TLable等组件手动拖拽固定大小,但是编译之后显示有差别的情况
Delphi多线程编程--线程同步的方法(事件、互斥、信号、计时器)简介
Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)