文章目录
linux目录介绍
根目录“/”,然后在此目录下再创建其他的目录
在linux世界里,一切皆为文件
Vim使用
简单使用vim编写Hello.java程序
vim Hello.java
i #切换到插入模式
public class Hello{
public static void main(String[] args){
System.out.println("hello,world");
}
}
esc # 进入到正常模式
:wq #保存文件内容
vim各种模式的切换
Vim快捷键
关机重启
shutdown -h now #立刻关机
shutdown -h 1 # 一分钟关机
shutdown -r now #重新启动
halt #关机和上面的作用一样
reboot # 重启计算机
sync # 将内存的数据同步到磁盘上
不管是重启计算机还是关闭系统首先要运行sync命令将内存中的数据写到磁盘中,上述的关机和重启操作都默认提前执行了sync操作
登录注销
- 登录时尽量是少使用root账号登录,因为他是系统管理员,最大的权限,避免操作失误,可以利用普通用户登录,登陆后再使用su-用户名命令换成系统管理员身份
- 在提示符下输入logout即可注销用户
- logout在图形化界面中无效
su-root # 切换到root系统管理员
logout # 注销当前用户
用户管理
用户管理一
Linux是一个多用户多任务的操作系统,任何一个要使用系统资源的用户,都必须首相向系统管理员申请一个账号,然后以这个账号的身份进入系统
useradd 用户名 # 添加一个用户并且该用户的家目录在/home/用户名
useradd -d /home/test king # 指定目录,给创建的用户指定家目录
pwd # 显示你当前在哪一个目录下
ls # 列出该目录下的文件名
passwd 用户名 # 指定或者修改密码
userdel 用户名 #删除用户一般用户的权限是不够的只有root才可以
删除案例
- 删除但是保留家目录
userdel 用户名 #删除但是保留了家目录
- 删除同时将主目录删除
userdel -r 用户名 #删除同时将主目录删除这个操作一定要谨慎
查询用户指令
id 用户名 # 查询用户的信息
whoami # 第一次登录的信息
用户组
类似于角色,系统中可以对共性/权限的多用户进行统一管理
groupadd 组名 # 新增组
groupdel 组名 # 删除一个组
useradd -g 用户组 用户名 # 将一个用户添加到一个组中
usermod -g 用户组 用户名 # 将用户放到另一个组中
如果在添加用户的时候没有指定组那么就会以用户名为组名将用户添加进去
用户和用户组相关的文件
实用指令
运行级别
init 运行级别 # 切换运行级别
multi-user.target # 多用户运行级别为3
graphical.target # 图形化界面运行级别为5
systemctl get-default # 获取当前系统的默认运行级别
systemctl set-default TARGET.target # 设置系统的默认运行级别
如何找回Root密码
帮助指令
man #获得帮助信息
man ls # ls - list directory contents
man [命令或配置文件]
help 命令(获得shell内置命令的帮助信息)
q # 进入指令解释之后想退出
ls -a #列出所有的目录包括隐藏的
ls -l #
with -l, print the author of each file
ls -al /root #将root下的文件全部列举出来并通过单例的格式展示
[root@VM-12-5-centos ~]# ls -al /root
total 1292
dr-xr-x---. 8 root root 4096 Jul 5 05:44 .
dr-xr-xr-x. 22 root root 4096 Jul 5 12:55 ..
-rw------- 1 root root 3809 Jul 5 12:55 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwxr-xr-x 4 root root 4096 May 29 00:36 .cache
drwxr-xr-x 3 root root 4096 Mar 7 2019 .config
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r-- 1 root root 110 Jul 3 20:51 Hello.java
-rw-r--r-- 1 root root 0 Jul 5 05:44 .localssh
-rw-r--r-- 1 root root 195 May 29 16:07 .pearrc
drwxr-xr-x 2 root root 4096 Apr 24 14:17 .pip
drwxr----- 3 root root 4096 May 29 00:31 .pki
-rw-r--r-- 1 root root 73 May 29 00:21 .pydistutils.cfg
drwxrwxr-x 6 root root 4096 Jul 3 13:41 redis-2.8.17
-rw-r--r-- 1 root root 1234543 Jun 27 2020 redis-2.8.17.tar.gz
-rw-r--r-- 1 root root 197 Jul 3 13:42 .rediscli_history
[root@VM-12-5-centos ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory. # 切换目录
cd ~ # 回到自己的家目录
cd ..回到当前目录的上一层目录
cd /root #是哟个绝对路径切换到root目录
cd ../../root #使用相对路径到/root 比如/home/tom
文件目录指令
pwd #显示当前工作目录的绝对路径
文件目录类
mkdir [选项] 要创建的目录
mkdir /home/dog
mkdir -P 创建多级目录
mkdir -p /home/animal/tiger
rmdir #删除空目录,有内容(子文件)则无法删除
rmdir -rf #强制递归删除,小心谨慎操作
touch 文件名称 #创建一个空文件
rm -f 文件名 #删除一个文件
指令演示
[root@VM-12-5-centos ~]# mkdir first
[root@VM-12-5-centos ~]# ls
first Hello.java redis-2.8.17 redis-2.8.17.tar.gz
You have new mail in /var/spool/mail/root
[root@VM-12-5-centos ~]# mkdir first/second/third
mkdir: cannot create directory ‘first/second/third’: No such file or directory
[root@VM-12-5-centos ~]# mkdir -p first/second/third
[root@VM-12-5-centos ~]# ls
first Hello.java redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# cd first
[root@VM-12-5-centos first]# ls
second
[root@VM-12-5-centos first]#
拷贝文件指令
cp [选项] source dest #拷贝文件到指定目录
-r :递归复制整个文件
cp hello.txt bbb/ #将hello.txt文件放到bbb目录下
cp -r /home/bbb/ /opt/ # 将bbb目录下的整个文件都copy到opt上 重复操作会有提示
\cp #强制覆盖不提示操作
rm # 移除文件或者目录 会有提示
-r #递归删除整个文件夹 round
-f #强制删除不提示
rm -rf # 强制删除整个文件夹不提示
演示
[root@VM-12-5-centos ~]# ls
first Hello.java redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# rm first
rm: cannot remove ‘first’: Is a directory
==========================================
[root@VM-12-5-centos ~]# rm -r first
rm: descend into directory ‘first’? Y
rm: descend into directory ‘first/second’? Y
rm: remove directory ‘first/second/third’? Y
rm: remove directory ‘first/second’? Y
rm: remove directory ‘first’? Y
============================================
rm -rf first #强制删除,没有提示
============================================
[root@VM-12-5-centos ~]# ls
Hello.java redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]#
MV指令
mv 旧文件名 新文件名 # 重命名
mv 文件名 相对路径 # 将文件移动
mv 文件名 相对路径 新文件名 # 在移动文件的过程中同时将文件重命名
mv /opt/ddd /home/ #移动整个目录
[root@VM-12-5-centos ~]# ls
hello.java redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# mv hello.java change.java
[root@VM-12-5-centos ~]# ls
change.java redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# mkdir first
You have new mail in /var/spool/mail/root
[root@VM-12-5-centos ~]# ls
change.java first redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# mv change.java first/
You have new mail in /var/spool/mail/root
[root@VM-12-5-centos ~]# ls
first redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# cd first
[root@VM-12-5-centos first]# ls
change.java
Cat指令查看文件内容
cat [选项] 要查看的文件
-n :显示行号
cat -n first/change.java # 查看change.java内容并显示行号
cat只能浏览文件内容,但是不能进行修改内容,为了方便浏览一般会带上管道命令 |more
[root@VM-12-5-centos ~]# cat -n first/change.java
1 public class Hello{
2 public static void main(String[] args){
3 System.out.println("hello,world");
4 }
Less指令
echo指令输出内容到控制台
[root@VM-12-5-centos ~]# echo $HOSTNAME
VM-12-5-centos
[root@VM-12-5-centos ~]# echo "hello"
hello
head指令用于显示文件的开头部分内容,默认情况下显示前十行
head 文件 #默认查看前五行
head -n 5 文件 #查看前五行
tail指令用于显示文件尾部分,默认后面十行
tail 文件 #默认后十行
tail -n 5 文件 #查看后五行
tail -f # 实时追踪文件的更新内容
ctrl + C 退出
>指令和 > >指令
>输出重定向和 > > 追加
[root@VM-12-5-centos ~]# ls -l
total 1216
drwxr-xr-x 2 root root 4096 Jul 9 18:06 first
drwxrwxr-x 6 root root 4096 Jul 3 13:41 redis-2.8.17
-rw-r--r-- 1 root root 1234543 Jun 27 2020 redis-2.8.17.tar.gz
==========将原本显示在控制台中的内容写进指定的文件===========================
[root@VM-12-5-centos ~]# ls -l > first/info.txt #如果没有这个文件则会重新创建一个
You have new mail in /var/spool/mail/root
=========================================================================
[root@VM-12-5-centos ~]# cd first
[root@VM-12-5-centos first]# cat info.txt
total 1216
drwxr-xr-x 2 root root 4096 Jul 9 20:50 first
drwxrwxr-x 6 root root 4096 Jul 3 13:41 redis-2.8.17
-rw-r--r-- 1 root root 1234543 Jun 27 2020 redis-2.8.17.tar.gz
ln指令符号链接,主要存放了链接其它文件的路径
ln -s[源文件或者目录] [软链接名] 给原文件创建一个软链接 ## 类似于window下的快捷键
[root@VM-12-5-centos lighthouse]# ln -s /root/ /home/lighthouse
[root@VM-12-5-centos lighthouse]# ls -l
total 0
lrwxrwxrwx 1 root root 6 Jul 9 21:36 root -> /root/
[root@VM-12-5-centos lighthouse]# ls
root
将软链接删除
[root@VM-12-5-centos lighthouse]# rm /home/lighthouse/root
rm: remove symbolic link ‘/home/lighthouse/root’? Y
[root@VM-12-5-centos lighthouse]# ls -l
total 0
history指令查看已经执行的历史命令,也可以执行历史指令
!5 #执行历史编号为5的指令
[root@VM-12-5-centos ~]# history 2 #查看最近的 2 条指令
1 2022-07-09 17:08:44
2 2022-05-29 00:31:35 yum install -y wget && wget -O install.sh
时间日期指令
date指令-显示当前时间日期
date -S 字符串时间 #修改当前系统时间
date -s "2022-7-10 20:02:10"
cal #显示日历指令当前月
cal 2020 # 显示2020年的所有月历
[root@VM-12-5-centos ~]# date
Sun Jul 10 07:59:20 CST 2022
[root@VM-12-5-centos ~]# date "+%Y-%m-%d"
2022-07-10
查找指令
find指令:find指令将从指定目录向下递归遍历其各个子目录,将满足条件的文件或者目录显示在终端。
find [搜索范围] [选项]
ls -lh #可以按照M大小展现
[root@VM-12-5-centos ~]# find /root -name change.java
/root/first/change.java
[root@VM-12-5-centos ~]# find / -size +200M
/proc/kcore
find: ‘/proc/3479/task/3479/fd/6’: No such file or directory
find: ‘/proc/3479/task/3479/fdinfo/6’: No such file or directory
find: ‘/proc/3479/fd/5’: No such file or directory
find: ‘/proc/3479/fdinfo/5’: No such file or directory
/www/swap
Locate指令:
locate 搜索文件
由于locate指令基于数据库进行查询,所以第一次运行前,必须使用updatedb指令创建数据locate数据库
[root@VM-12-5-centos ~]# locate change.java
/root/first/change.java
搜索指令在哪一个目录下:
which [指令名]
[root@VM-12-5-centos ~]# which ls
alias ls='ls --color=auto' /usr/bin/ls
===================================
[root@VM-12-5-centos ~]# which mkdir
/usr/bin/mkdir
grep指令和管道符号 |:grep过滤查找,管道符,“|”,表示将前一个命令的处理结果输出传递给后面的命令处理。
grep [选项] 查找内容 源文件
[root@VM-12-5-centos ~]# cat hello.txt
hello
hi
hao
are
you
I
am
fine
thank
you
[root@VM-12-5-centos ~]# cat /root/hello.txt |grep -n "you"
5:you
10:you
#########################################################################
[root@VM-12-5-centos ~]# grep -n "you" /root/hello.txt
5:you
10:you
压缩和解压
gzip/gunzip指令:gzip用于压缩文件 ,gunzip用于解压文件
[root@VM-12-5-centos ~]# gzip hello.txt
[root@VM-12-5-centos ~]# ls
first hello.txt.gz redis-2.8.17 redis-2.8.17.tar.gz
[root@VM-12-5-centos ~]# gunzip hello.txt.gz
[root@VM-12-5-centos ~]# ls
first hello.txt redis-2.8.17 redis-2.8.17.tar.gz
zip/unzip指令:zip用于压缩文件,unzip解压,这个在项目的打包发布中很有用
zip -r myhome.zip /home/ #将home目录及其包含的文件和子文件夹都压缩
mkdir /opt/tmp
unzip -d /opt/tmp /home/myhome.zip # 将myhome.zip 解压到 /opt/tmp 目录下
tar指令: tar指令是一个打包指令,最后打包后的文件是 .tar.gz的文件
tar [选项] XXX.tar.gz 打包的内容
将hello.txt 文件和 second.txt文件打包并压缩
[root@VM-12-5-centos ~]#tar -zcvf pc.tar.gz /root/hello.txt /root/second.txt
tar: Removing leading `/' from member names
/root/hello.txt
/root/second.txt
[root@VM-12-5-centos ~]# ls
first hello.txt pc.tar.gz redis-2.8.17 redis-2.8.17.tar.gz second.txt
解压到当前目录
tar -zxvf pc.tar.gz 解压到当前目录
tar -zxvf /home/myhome.tar.gz -C /opt/tmp #将myhome.tar.gz解压到指定目录下