> 文章列表 > linux常用命令技巧

linux常用命令技巧

linux常用命令技巧

1、mkdir命令

创建多级目录

mkdir -p /opt/test/test

2、rm与rmdir命令

删除空目录

rmdir /opt/test

删除非空目录,强制删除不提示

rm -rf /opt/test

3、init 命令

切换登录窗口

init 2
init 3

4、su命令

切换用户

#切换到xm用户
su xm 
#返回到root用户
exit

5、cp命令

递归复制文件

cp -r /opt/test/aa/ /opt/my/
#强制覆盖相同文件,不提示
\\cp -r /opt/test/aa/ /opt/my/

6、cat查看命令

分页查看

cat -n test.txt | more

7、>指令 和 >> 指令

> 指令 和 >> 指令
> 输出重定向 : 会将原来的文件的内容覆盖
>> 追加: 不会覆盖原来文件的内容,而是追加到文件的尾部基本语法

1) ls -1>文件

(功能描述:列表的内容写入文件 a.txt 中(覆盖写) )
说明: ls -l> a.txt,将ls 的显示的内容覆盖写入到 a.txt 文件,如果该文件不存在,就创建该文件

2) ls -al >>文件

(功能描述:列表的内容追加到文件 aa.txt 的末尾)

3) cat 文件1>文件2

(功能描述:将文件1的内容覆盖到文件2)

4)echo"内容">> 文件

echo "hello" >> test.txt

5)应用实例

案例1:将 /home目录下的文件列表 写入到/home/info.txt 中
linux常用命令技巧

案例2:将当前日历信息追加到 /home/mycal文件中[提示 cal]
linux常用命令技巧

8、head命令

查看文件头 5 行内容,5 可以是任意行数

head -n 5 /etc/profile 

9、tail指令

查看文件尾部5行

tail -n 5 /etc/profile 

实时追踪信息

tail -f /logs/1.log

10、echo命令

查看环境变量信息

echo $PATH

linux常用命令技巧

11、ln命令 软链接

软链接也叫符号链接,类似于 windows 里的快捷方式,主要存放了链接其他文件的路径基本语法

ln -s[原文件或目录][软链接名] (功能描述:给原文件创建一个软链接)应用实例

案例 1: 在/home 目录下创建一个软连接 linkToRoot,连接到 /root 目录

ln -s /root linkRoot
##删除, linkRoot后面不能加/
rm -rf linkRoot

12、history 指令

查看已经执行过历史命令.也可以执行历史指令基本语法

history(功能描述:查看已经执行过历史命令)
history 10 #显示10个使用过的命令

执行历史编号为 5 的ls指令

!5

13、date 指念-显示当前日期

·基本语法

	1) date(功能描述: 显示当前时间)	2) date +%Y (功能描述:显示当前年份)3) date +%m(功能描述: 显示当前月份)	4) date +%d (功能描述:显示当前是哪一天)5) date"+%Y-%m-%d %H:%M:%S”(功能描述: 显示年月日时分秒)
![在这里插入图片描述](https://img-blog.csdnimg.cn/8b997280fde444bba52f9dc9e242d70c.png)

设置系统当前时间 ,比如设置成 2018-10-10 11:22:22

date -s 时间

linux常用命令技巧

14、cal 指念-显示日历

显示2022一年的日历

cal 2022

15、find查找文件

查找 /opt目录下的 test.txt文件

find /opt -name test.txt

按拥有者:查找/opt 目录下,用户名称为 nobody 的文件

find /opt -user nobody

查找整个 linux系统下大于 20m 的文件 (+n 大于 -n 小于 n等于)

find / -size +20m

16、locate查找文件

updatedb
locate hello.txt

17、grep查找文件

在test.txt文件,查找ok所在行

cat test.txt | grep -n 'ok'

18、zip压缩,unzip解压

压缩/opt下所有文件

zip -r opt.zip /opt/

解压到指定目录下

unzip -d /opt/test/ opt.zip

19、tar压缩与解压

将a1.txt 与 a2.txt打包成 a.tar.gz

tar -zcvf a.tar.gz a1.txt a2.txt

解压到 opt目录

tar -zxvf a.tar.gz -C /opt/

20、useradd 添加用户

useradd tom
#添加用户到group1组
useradd -g group1 tom

21、chgrp 修改文件所在组

#添加组
groupadd group1
#创建文件
touch test.txt
#查看所在组
ll test.txt 
#修改为group1组
chgrp group1 test.txt 

22、chmod 修改用户所在组

#添加组
groupadd group1
#创键用户
useradd tom
#查看所在组
id tom
#修改为group1组
chmod  -g group1 tom

仙剑奇侠传3