ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

-- 2linux基础命令学习

2022-04-13 01:04:06  阅读:142  来源: 互联网

标签:文件 log -- 命令 2linux anaconda var root localhost


2.1.1linux概述
linux和unix的最大区别就是,linux开放源代码的自由软件,unix则是对源代码实行知识产权保护的传统商业软件
unix系统大多数和硬件配套,而且本身是收费的商业软件
linux可以在多种硬件平台上运行,并且linux的软件是自由免费且公开源代码的
linux内核版本分为稳定版和开发版,两种版本是相互关联,相互循环
稳定版:可以广泛应用和部署。新的稳定版相比较于旧版本只是修正一些bug或者加入一些新的驱动程序
开发版:和字面意思差不多,变化快,并不适用于生产生活中
内核源码网址: http://www.kernel.org
linux的众多发行版:ubuntu,CentOS,RedHat,debian,包括后面学习的kali也是,此外还有很多

2.1.2安装vmware
第一步安装vmware,不做赘述,快速跳过
第二步安装centos,快速跳过
第三步,centos基本配置
换源(太多了,自己去镜像网上去找,换源或者修改某些重要数据或者文件前,一定要做好备份),更换国内源访问速度快,也可以避免某些软件包在国外被墙,然后添加EPEL(可以理解为额外拓展包),然后清理缓存,避免原来源的数据干扰yum clean all,然后创建新数据元yum makecache
关闭防火墙(生产工作环境中不推荐,生产中建议用哪个端口就开放哪个),systemctl stop firewalld.service,systemctl disable firewalld.service
静态IP,首先先查看自身物理机IP,ipconfig -a,然后回到虚拟机将网络适配器修改为自定义VMnet8(nat模式),然后点击虚拟网络编辑器,更改配置,将子网设置成和自己物理机对应的网段
然后回到虚拟机,修改
/etc/sysconfig/network-scripts/ifcfg-en(这里是你的网卡名,一般都是enxx)


BOOTPROTO修改为static(静态),然后修改ip地址和对应网关
配置完成后重启网络服务即可
service network restart

2.1.3linux文件结构和基本概念
单独记录为一个笔记
补充说明,在安装linux时,如果有公司需求就按照公司需求进行系统分区,如果没有,按照以下方法
(1)/boot分配200M;(2)swap分区(交换分区,当物理内存不够用的时候会从内存中取出一部分暂时不用的数据,暂存在swap分区中,从而为当前运行的程序腾出足够的内存)分内存的两倍(如果内存大于4GB,swap分配8GB即可,分多了也是浪费磁盘空间);(3)/分区分20GB(/分区用于储存系统文件);(4)剩余的空间分配给/data

linux基本命令
命令参数很多,选项功能也很多,不能说全部弄全,唯一的学习办法就是在日常生活中多学多用
mkdir(创建目录)
mkdir -m(类似于直接在创建目录的过程中赋予权限)
例如

[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 3月 23 08:14 test //使用mkdir创建的正常目录
[root@localhost ~]# mkdir -m754 test1 //这里创建了test1目录,并且赋予权限为754,r=4,w=2,x=1
[root@localhost ~]# ls
anaconda-ks.cfg test test1
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 3月 23 08:14 test
drwxr-xr--. 2 root root 6 3月 23 08:32 test1
[root@localhost ~]#


从上可以看出两个创建的目录的不同在于-m的赋权

mkdir -p(递归创建),简单的说就是我们需要创建多个目录时,如果上级目录不存在,他会先创建上级目录

[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 3月 23 08:14 test
drwxr-xr--. 2 root root 6 3月 23 08:32 test1
[root@localhost ~]# mkdir ./1/2/3
mkdir: 无法创建目录"./1/2/3": 没有那个文件或目录 //这里是因为他要创建目录3,但是因为没有目录2,也没有目录1,所以报错
[root@localhost ~]# mkdir -p ./1/2/3 //递归创建目录1,目录2,目录3
[root@localhost ~]# ls
1 anaconda-ks.cfg test test1
[root@localhost ~]# ls ./1/
2

[root@localhost ~]# ls ./1/2
3
[root@localhost ~]#


mkdir -v(每次创建都显示信息)

[root@localhost ~]# mkdir -v ./test2
mkdir: 已创建目录 "./test2" //这里就是提示的消息
[root@localhost ~]# mkdir ./test3 //正常创建是不会显示消息的
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 3月 23 08:14 test
drwxr-xr--. 2 root root 6 3月 23 08:32 test1
drwxr-xr-x. 2 root root 6 3月 23 08:46 test2
drwxr-xr-x. 2 root root 6 3月 23 08:46 test3


联合使用(为什么这里递归创建后第一个目录的权限是755,但是后续的最底层目录/ws是733)

[root@localhost ~]# mkdir -v -m733 -p ./we/wq/ws
mkdir: 已创建目录 "./we"
mkdir: 已创建目录 "./we/wq"
mkdir: 已创建目录 "./we/wq/ws"
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
drwxr-xr-x. 3 root root 15 3月 23 08:51 we //这里的权限为755
[root@localhost ~]#
[root@localhost ~]# cd ./we/
[root@localhost we]# ll
总用量 0
drwxr-xr-x. 3 root root 15 3月 23 08:51 wq //这里权限也是755
[root@localhost we]# ls
wq
[root@localhost we]# cd ./wq/
[root@localhost wq]# ll
总用量 0
drwx-wx-wx. 2 root root 6 3月 23 08:51 ws //只有这里按照我设定的733权限
[root@localhost wq]# cd ./ws/
[root@localhost ws]# ll
总用量 0
[root@localhost ws]#


touch命令
用于修改文件或者目录的时间属性,比如存储时间访问时间等,如果没有这个文件,就会新建一个文件
touch -a

[root@localhost ~]# touch ./tx.txt //创建一个名为tx.txt的新文件
[root@localhost ~]# ls
anaconda-ks.cfg tx.txt
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 3月 23 15:54 tx.txt
[root@localhost ~]# touch -a ./tx.txt //更新访问时间,不改变修改时间
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 3月 23 15:54 tx.txt
[root@localhost ~]#
[root@localhost ~]# stat tx.txt //查看文件时间属性
文件:"tx.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:34011794 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-03-23 16:00:21.933325506 +0800 //使用-a命令更新了最新的访问时间
最近更改:2022-03-23 15:54:43.910321647 +0800
最近改动:2022-03-23 16:00:21.933325506 +0800
创建时间:-


touch -m用于更新文件的修改时间记录,如下

[root@localhost ~]# touch -m tx.txt //更新修改时间
[root@localhost ~]# stat tx.txt //查看文件状态
文件:"tx.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:34011794 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-03-23 16:00:21.933325506 +0800 //这里不变
最近更改:2022-03-23 16:06:07.743329454 +0800 //此时修改时间发生变动,但是上面的访问时间不变
最近改动:2022-03-23 16:06:07.743329454 +0800
创建时间:-
[root@localhost ~]#


touch -c,不创建不存在的文件
原本touch命令在输入不存在的文件时,会自动创建新文件,-c参数则不会创建,但是针对已经存在的文件,则会更新其存储时间和修改时间等时间属性

[root@localhost ~]# touch -c ./1.log //创建1.log,如果不存在则不创建
[root@localhost ~]# ls
anaconda-ks.cfg tx.txt //因为没有1.log,所以不创建
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 3月 23 16:06 tx.txt

[root@localhost ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .tcshrc .viminfo
.. .bash_history .bash_profile .cshrc tx.txt
[root@localhost ~]# ls /
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
[root@localhost ~]#


[root@localhost ~]# ls
anaconda-ks.cfg tx.txt
[root@localhost ~]# touch -c tx.txt //针对已经存在的文件更新其文件时间属性
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 973 2月 27 01:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 3月 23 16:11 tx.txt
[root@localhost ~]# stat tx.txt
文件:"tx.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:34011794 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-03-23 16:11:39.475333241 +0800
最近更改:2022-03-23 16:11:39.475333241 +0800
最近改动:2022-03-23 16:11:39.475333241 +0800
创建时间:-
[root@localhost ~]# date
2022年 03月 23日 星期三 16:12:33 CST
[root@localhost ~]# touch tx.txt //二者作用等同,只不过-c参数不会去创建新文件
[root@localhost ~]# stat tx.txt
文件:"tx.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:34011794 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-03-23 16:22:01.410340341 +0800
最近更改:2022-03-23 16:22:01.410340341 +0800
最近改动:2022-03-23 16:22:01.410340341 +0800
创建时间:-


touch -r 源文件 目标文件
意为,将源文件的时间属性去更新目标文件的时间,但是改动时间还是现实时间。

[root@localhost ~]# ls
anaconda-ks.cfg tx.txt
[root@localhost ~]# stat anaconda-ks.cfg //查看文件属性,确认时间状态
文件:"anaconda-ks.cfg"
大小:973 块:8 IO 块:4096 普通文件
设备:803h/2051d Inode:33742625 硬链接:1
权限:(0600/-rw-------) Uid:( 0/ root) Gid:( 0/ root)
环境:system_u:object_r:admin_home_t:s0
最近访问:2022-02-27 01:44:57.247921907 +0800
最近更改:2022-02-27 01:44:57.249921907 +0800
最近改动:2022-02-27 01:44:57.249921907 +0800
创建时间:-
[root@localhost ~]# touch -r anaconda-ks.cfg tx.txt //把前者的时间属性用于去更新后者的时间属性
[root@localhost ~]# stat tx.txt
文件:"tx.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:34011794 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-02-27 01:44:57.247921907 +0800 //
最近更改:2022-02-27 01:44:57.249921907 +0800 //此时时间信息和上面那个文件的事件信息变得一样了
最近改动:2022-03-23 16:26:25.304343353 +0800 //只有这里改动时间还是目前现实的时间
创建时间:-
[root@localhost ~]#


rm命令
用于删除,rm -rf /*(此命令高危,删除所有,简称删库跑路)
各参数含义。-r是递归删除,-f是强制删除,-i是显示提示内容(建议该操作防止误删,或者重要文件数据提前进行备份)

mv命令,用于文件改名和迁移
mv -i,若指定目录有同名文件,则询问是否覆盖
mv -f,直接移动过去,不提示任何信息

cp命令,复制和备份文件常用
cp -a,复制目录时使用,保留链接、文件属性,并复制目录下的所有内容。
cp -d复制时保留链接。
cp -p除复制文件的内容外,把时间和访问权限也复制到新文件中
cp -r给出的源文件如果是一个目录文件,此时复制该目录下的 所有子目录和文件
常用参数:-i,-f,-a,很多地方的作用相同

cat命令,用于查看文本文件
-n,显示行号

[root@localhost ~]# cat -n hello.log
1 4987489749687897
2 4564654563
3 11231321
4 68789
5 4163132
6 4897
7 63415634
8 8979846
9 541685789789
10 6415165
11 4897987
12 631654
13 987897
14 61563
[root@localhost ~]#


more和less命令,在很多地方可以混合使用
more -num为一次性显示的行数
例如:more -10,则就是一次显示10行
比如:ls -l / | more -5,列出文件目录的信息,并且每次只显示5行

head命令
head用于查看文件的前几行内容
head -n 20 文件名,显示某文件按的前20行内容
head不加参数的话默认显示前10行内容
tail命令,显示文件的最后10行
比较常用的一个命令,tail -f 文件名,用于监控文件的操作,因为文件的实施操作改动会不断更新显示

在linux中,引用某个变量使用$符号,例如:echo $PATH(输出PATH环境变量),此时我们引用了环境变量,所以用$符号

which命令用于找文件,但是which命令只会在环境变量设置的目录里去查找
whereis也是用于查找文件,其中参数-b ,-s,-m,对应的缩写是binary(二进制)也就是可执行文件,source(源码文件),man文件对应手册说明
-u是除了上述的文件,以外的文件

locate(新内容学习)
首先先要安装,yum install -y mlocate,如果在搜索过程中有出现报错,那就先updatedb,更新一下locate的搜索数据库,这个工具类似于windows下的everything,搜索时会搜索包含关键字的所有信息并显示

[root@localhost ~]# locate /var/log //搜索/var目录下的所有包含log关键字的文件
/var/log
/var/log/anaconda
/var/log/audit
/var/log/boot.log
/var/log/btmp
/var/log/cron
/var/log/cron-20220323
/var/log/dmesg
/var/log/dmesg.old
/var/log/lastlog
/var/log/maillog
/var/log/maillog-20220323
/var/log/messages
/var/log/messages-20220323
/var/log/ppp
/var/log/samba
/var/log/secure
/var/log/secure-20220323
/var/log/spooler
/var/log/spooler-20220323
/var/log/tallylog
/var/log/tuned
/var/log/wtmp
/var/log/yum.log
/var/log/anaconda/anaconda.ifcfg.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/anaconda.packaging.log
/var/log/anaconda/anaconda.program.log
/var/log/anaconda/anaconda.storage.log
/var/log/anaconda/anaconda.xlog
/var/log/anaconda/ks-script-3VSczQ.log
/var/log/anaconda/ks-script-Di3qUv.log
/var/log/anaconda/ks-script-gk4HrM.log
/var/log/anaconda/syslog
/var/log/audit/audit.log
/var/log/samba/old
/var/log/tuned/tuned.log
[root@localhost ~]#
但是如果搜索路径具体化,或者将文件名具体化就很好用
比如
[root@localhost ~]# locate hello.log
/root/hello.log
[root@localhost ~]#


具体参数可以参考该命令帮助手册

find命令
find和locate用的比较多,find是实时扫描搜索,速度慢,locate是根据数据库索引搜索,速度快
find主要用于查询各种文件,模糊化搜素比较好用
find -amin -10(搜索过去最后10分钟内访问的文件)
find -atime -10(搜索过去最后10小时内访问的文件)
find -cmin -10(搜索最后10分钟内被改变文件状态的文件)
find -ctime -10(搜索最后10小时内被改变文件状态的文件)
find -mmin -10(查找最后10分钟内被改变文件数据的文件)
find -mtime -10(查找最后10小时内被改变文件数据的文件)
我们常用的几个命令
查找var目录下的所有log文件,需要记住搜索类型参数需要用单引号引用

[root@localhost ~]# find ../var/ -name '*.log'
../var/log/audit/audit.log
../var/log/tuned/tuned.log
../var/log/anaconda/anaconda.log
../var/log/anaconda/anaconda.program.log
../var/log/anaconda/anaconda.packaging.log
../var/log/anaconda/anaconda.storage.log
../var/log/anaconda/anaconda.ifcfg.log
../var/log/anaconda/ks-script-3VSczQ.log
../var/log/anaconda/ks-script-Di3qUv.log
../var/log/anaconda/ks-script-gk4HrM.log
../var/log/yum.log
../var/log/boot.log
[root@localhost ~]#

find /var/log/ -perm 755,查找var/log目录下权限为755的文件

[root@localhost ~]# find ../var/log/ -perm 755
../var/log/
../var/log/tuned
../var/log/anaconda
[root@localhost ~]#

find 路径 -size,其中-10c表示,搜索某路径下10字节以内的文件,+10表示搜索某路径下超过10字节的文件,具体情况可以灵活变换,比如

[root@localhost ~]# find ./ -size -10c
./tx.txt
[root@localhost ~]# find ./ -size +10c
./
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./hello.log
./.viminfo
./test
./test/hello.log
[root@localhost ~]#

-exec参数,参数后面跟命令,以分号;结尾,比如我想要搜索目前目录下的所有.log文件复制到opt目录

[root@localhost ~]# find ./ -name '*.log' -exec cp {} ../opt/ \;         //{}花括号内表示的是引用前面搜索到的内容
//在linux中我们为了避免一些斜杠的用法发生转义,在末尾加上一个反斜杠作为标记
[root@localhost ~]# ls ../opt/
hello.log
[root@localhost ~]#

 

chmod用于赋权,不多赘述
-R是按照指定方式处理目录以及子目录下所有文件
chown修改拥有者和群组
-R也是和上述一样的参数

tar命令,压缩和解压。
-z支持gzip格式,-j支持bzip2格式,-c创建新压缩包,-v显示过程,-f定义打包的包名称,-x解压,-t显示压缩文件内容
其中对特殊参数做出说明,在备份或者打包时常用的一个参数,--exclude排除某文件或者目录

[root@localhost ~]# pwd //显示当前路径
/root
[root@localhost ~]# ls
anaconda-ks.cfg hello.log test tx.txt z.bak
[root@localhost ~]# tar -zcvf q.tar.gz /root/* --exclude=/root/test //将/root目录下所有文件打包为q.tar.gz,并排除/test目录
tar: 从成员名中删除开头的“/”
/root/anaconda-ks.cfg
/root/hello.log
/root/tx.txt
/root/z.bak //输出结果中没有test目录
[root@localhost ~]# ls
anaconda-ks.cfg hello.log q.tar.gz test tx.txt z.bak
[root@localhost ~]# tar -zxvf q.tar.gz -C ./t
test/ tx.txt
[root@localhost ~]# tar -zxvf q.tar.gz -C ./test/ ////解压q.tar.gz到当前目录下的test目录
root/anaconda-ks.cfg
root/hello.log
root/tx.txt
root/z.bak
[root@localhost ~]# ls ./test/root/
anaconda-ks.cfg hello.log tx.txt z.bak


date命令,查看时间
cal日历
grep用的比较多,用于关键字查找
^符号表示以xx开头,比如

[root@localhost ~]# grep '^for' profile -n //在profile文件中查找以for开头的,-n显示行号
65:for i in /etc/profile.d/*.sh ; do
[root@localhost ~]#

 

标签:文件,log,--,命令,2linux,anaconda,var,root,localhost
来源: https://www.cnblogs.com/4ek0/p/16138383.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有