linux基础命令(详解)满满干货
1.Linux基础命令
[root@localhost ~]# 介绍
root 代表当前登录的用户,在Linux中管理员账户是root
@ 发音at ,表示在
localhost当前计算机主机名 ~当前所在目录
#超级用户提示符,普通用户的提示符是$
ls
ls即为list,意识是列出当前目录下的文件信息
ls -l 查看目录下文件的详细信息 ll
ls -a: 查看目录下隐藏的文件
ls -d: 查看目录(常和-l选项一起用,如只想查看某个目录本身的信息,而不是目录下的文件信息)
ls -t: 按文件时间先后顺序倒叙排列(常和-l选项一起使用)
ls -i: 查看文件的iNode号
linux系统目录结构
输入命令 ls
以下是对这些目录的解释:
/bin: bin是Binary的缩写, 这个目录存放着最经常使用的命令。
/boot: 这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件。
/dev : dev是Device(设备)的缩写, 该目录下存放的是Linux的外部设备,在Linux中访问设备的方式和访问文件的方式是相同的。
/etc: 这个目录用来存放所有的系统管理所需要的配置文件和子目录。
/home: 用户的主目录,在Linux中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的。
/lib: 这个目录里存放着系统最基本的动态连接共享库,其作用类似于Windows里的DLL文件。几乎所有的应用程序都需要用到这些共享库。
/lost+found: 这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件。
/media: linux系统会自动识别一些设备,例如U盘、光驱等等,当识别后,linux会把识别的设备挂载到这个目录下。
/mnt: 系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将光驱挂载在/mnt/上,然后进入该目录就可以查看光驱里的内容了。
/opt: 这是给主机额外安装软件所摆放的目录。比如你安装一个ORACLE数据库则就可以放到这个目录下。默认是空的。
/proc: 这个目录是一个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。 这个目录的内容不在硬盘上而是在内存里,我们也可以直接修改里面的某些文件,比如可以通过下面的命令来屏蔽主机的ping命令,使别人无法ping你的机器:
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
/root: 该目录为系统管理员,也称作超级权限者的用户主目录。
/sbin: s就是Super User的意思,这里存放的是系统管理员使用的系统管理程序。
/selinux: 这个目录是Redhat/CentOS所特有的目录,Selinux是一个安全机制,类似于windows的防火墙,但是这套机制比较复杂,这个目录就是存放selinux相关的文件的。
/srv: 该目录存放一些服务启动之后需要提取的数据。
/sys:
这是linux2.6内核的一个很大的变化。该目录下安装了2.6内核中新出现的一个文件系统 sysfs 。
sysfs文件系统集成了下面3种文件系统的信息:针对进程信息的proc文件系统、针对设备的devfs文件系统以及针对伪终端的devpts文件系统。
该文件系统是内核设备树的一个直观反映。
当一个内核对象被创建的时候,对应的文件和目录也在内核对象子系统中被创建。
/tmp: 这个目录是用来存放一些临时文件的。
/usr: 这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下,类似于windows下的program files目录。
/usr/bin: 系统用户使用的应用程序。
/usr/sbin: 超级用户使用的比较高级的管理程序和系统守护程序。
/usr/src: 内核源代码默认的放置目录。
/var: 这个目录中存放着在不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。
/run: 是一个临时文件系统,存储系统启动以来的信息。当系统重启时,这个目录下的文件应该被删掉或清除。如果你的系统上有 /var/run 目录,应该让它指向 run。
在 Linux 系统中,有几个目录是比较重要的,平时需要注意不要误删除或者随意更改内部文件。 /etc: 上边也提到了,这个是系统中的配置文件,如果你更改了该目录下的某个文件可能会导致系统不能启动。/bin, /sbin, /usr/bin, /usr/sbin: 这是系统预设的执行文件的放置目录,比如 ls 就是在/bin/ls 目录下的。 值得提出的是,/bin, /usr/bin 是给系统用户使用的指令(除root外的通用户),而/sbin, /usr/sbin 则是给root使用的指令。 /var: 这是一个非常重要的目录,系统上跑了很多程序,那么每个程序都会有相应的日志产生,而这些日志就被记录到这个目录下,具体在/var/log 目录下,另外mail的预设放置也是在这里。
cd
cd 命令 :进入磁盘的某个目录下[dir]
cd ./ #表示当前目录
cd …/ #表示上一层目录
cd …/…/ #表示向上两层目录
cd / #表示系统根目录
cd #进入用户根目录
cd ~ #进入用户根目录
cd / #进入系统根目录
cd 文件名 进入当前文件目录
2.linux下文件操作命令
1.创建
命令:
单个文件夹或文件:mkdir 文件夹/文件
多个文件夹或文件:mkdir 文件夹1 文件夹2 文件夹33
2.查看
- cat: 由第一行开始显示文件内容
- tac: 从最后一行开始显示,可以看出tac是cat的倒写形式
- nl: 显示的时候顺便显示行号
- more: 一页一页地显示文件内容
- tail: 只看结尾几行
[root@localhost test]# cat test
hello redirect
hello redirect
[root@localhost test]# ls
first hello.php second TEST test2.sh wordpress-3.1-zh_CN.zip
hell1.txt hello.sh teest test1 test.sh world.txt
hello1.txt hello.txt test test1.sh users
[root@localhost test]# cat test2.sh
#!/bin/bash
echo "Hello world !"
[root@localhost test]#
3.查找
1、命令格式 find [查找目录] [查找规则] [查找完后的操作] 即:find pathname -option [-print -exec -ok …]
2、命令功能 用于在文件树中查找文件,并做相应的处理,(有可能访问磁盘)。
3、命令参数
(1)pathname:表示所要查找的目录路径,例如”.”表示当前目录,”/”表示根目录。
(2)-print:将find找到的文件输出到标准输出。
(3)-exec:对找到的文件执行exec这个参数所指定的shell命令,相应的形式为:-exec command {} ; 将查到的文件进行command操作,”{}”就代替查到的文件。
查找规则
1)根据文件名查找
>(1)-name :根据文件名进行查找,区分大小写精确查找。
[root@localhost test]# find /test -name test
/test
/test/test
[root@localhost test]#
(2) -iname:根据文件名查找,不区分大小写
[root@localhost test]# find /test -iname test
/test
/test/test
/test/TEST
[root@localhost test]#
(3)文件名通配符: “”:通配任意的字符,可以是任何东西。
find [查找目录] [查找规则] "file" #注意*可以代表任何位置的任何字符,*可以代表多个字符
[root@localhost test]# find /test -name “est"
/test
/test/test
/test/teest
[root@localhost test]# find /test -name “st"
/test
/test/first
/test/test
/test/teest
[root@localhost test]# find /test -name "tst”
/test
/test/test
/test/teest
[root@localhost test]# find /test -name "te”
/test
/test/test.sh
/test/test1.sh
/test/test2.sh
/test/test
/test/test1
/test/teest
[root@localhost test]
(4)”?”:可表示任意单个字符
[root@localhost test]# find /test -name “t?st”
/test
/test/test
[root@localhost test]#
(5)”[]”表示通配括号里面的任意一个字符,注意[]里面的内容会被解析成单个字符。
[root@localhost test]# find /test -name “tes[abct]”
/test
/test/test
[root@localhost test]# find /test -name “test[123]”
/test/test1
[root@localhost test]#
注意:“[]”里的123被解析成单字符1、2、3,所以test[123]可表示成test1、test2和test3
2)根据文件的时间戳信息查找文件
在根据时间戳信息查找的时候,所有的time都是以天为单位,min都是以分钟为单位。+n表示n以前,-n表
示n以内。
(1) 以最近一次存取的时间为参数
1)-atime: A、find pathname -atime +n //表示n天前存取过的文件 B、find pathname - atime -n //表示以当前时间为起点前n天内存取过的文件
2). -amin : A、find pathname -amin +n //表示n分钟前存取过的文件。 B、find pathname -amin -n //表示以当前时间为起点前n分钟内存取过的文件。
[root@localhost test]# find /test -atime -10
/test
/test/hello.txt
/test/hello1.txt
/test/first
/test/second
/test/world.txt
/test/hell1.txt
/test/wordpress-3.1-zh_CN.zip
/test/test.sh
/test/.test.sh.swp
/test/hello.php
/test/.hello.php.swp
/test/test1.sh
/test/test2.sh
/test/.hello.sh.swp
/test/.hello.sh.swo
/test/.hello.sh.swn
/test/hello.sh
/test/test
/test/users
/test/.test.swp
/test/test1
/test/TEST
/test/teest
(2)以最近一次修改的时间为参数
1)-mtime: A、find pathname -mtime +n //表示n天前修改过的文件 B、find pathname - mtime -n //表示以当前时间为起点前n天内修改 过的文件
2)-mmin: A、find pathname -mmin +n //表示n分钟前存取过的文件 B、find pathname -mmin -n //表示以当前时间为起点前n修改 内存取过的文件
[root@localhost test]# find /test -mtime -10
/test
/test/hello.txt
/test/hello1.txt
/test/first
/test/second
/test/world.txt
/test/hell1.txt
/test/test.sh
/test/.test.sh.swp
/test/hello.php
/test/.hello.php.swp
/test/test1.sh
/test/test2.sh
/test/.hello.sh.swp
/test/.hello.sh.swo
/test/.hello.sh.swn
/test/hello.sh
/test/test
/test/users
/test/.test.swp
/test/test1
/test/TEST
/test/teest
[root@localhost test]# find /test -mtime +10
/test/wordpress-3.1-zh_CN.zip
[root@localhost test]#
(3)以最近一次更改的属性为参数
1)-ctime: A、find pathname -ctime +n //表示n天前更改 过的文件 B、find pathname - ctime -n //表示以当前时间为起点前n天内更改 过的文件
2)-cmin: A、find pathname -cmin +n //表示n分钟前更改过的文件 B、find pathname - cmin -n //表示以当前时间为起点前n分钟内更改 过的文件
[root@localhost test]# find /test -cmin -10
[root@localhost test]# find /test -cmin +10
/test
/test/hello.txt
/test/hello1.txt
/test/first
/test/second
/test/world.txt
/test/hell1.txt
/test/wordpress-3.1-zh_CN.zip
/test/test.sh
/test/.test.sh.swp
/test/hello.php
/test/.hello.php.swp
/test/test1.sh
/test/test2.sh
/test/.hello.sh.swp
/test/.hello.sh.swo
/test/.hello.sh.swn
/test/hello.sh
/test/test
/test/users
/test/.test.swp
/test/test1
/test/TEST
/test/teest
[root@localhost test]#
3)根据文件所属用户和所属组来查找文件
(1)根据文件所有者查找文件:-user
[root@localhost test]# find ./ -user root #在跟目录中,查找文件所有者名是root的文件
./
./hello.txt
./hello1.txt
./first
./second
./world.txt
./hell1.txt
./wordpress-3.1-zh_CN.zip
./test.sh
./.test.sh.swp
./hello.php
./.hello.php.swp
./test1.sh
./test2.sh
./.hello.sh.swp
./.hello.sh.swo
./.hello.sh.swn
./hello.sh
./test
./users
./.test.swp
./test1
./TEST
./teest
[root@localhost test]#
(2)根据所有者所在的组查找:-group
[root@localhost test]# find ./ -group root #在根目录下查找文件用户组名是root的文件
./
./hello.txt
./hello1.txt
./first
./second
./world.txt
./hell1.txt
./wordpress-3.1-zh_CN.zip
./test.sh
./.test.sh.swp
./hello.php
./.hello.php.swp
./test1.sh
./test2.sh
./.hello.sh.swp
./.hello.sh.swo
./.hello.sh.swn
./hello.sh
./test
./users
./.test.swp
./test1
./TEST
./teest
[root@localhost test]#
4) -perm: 根据权限来查找文件
find pathname -perm 权限值
[root@localhost test]# find /test -perm 777
/test/hello.txt
/test/hello1.txt
[root@localhost test]# ll
总用量 3408
drwxr-xr-x. 2 root root 6 9月 7 21:41 first
-rw-r--r--. 1 root root 10 9月 8 07:01 hell1.txt
-rwxrwxrwx. 1 root root 17 9月 8 07:06 hello1.txt
drwxr-xr-x. 2 root root 6 9月 8 13:46 hello.php
-rwxr-xr-x. 1 root root 56 9月 9 07:48 hello.sh
-rwxrwxrwx. 1 root root 91 9月 8 07:02 hello.txt
drwxr-xr-x. 2 root root 6 9月 7 21:42 second
drwxr-xr-x. 2 root root 6 9月 9 14:30 teest
-rw-r--r--. 1 root root 30 9月 9 09:03 test
drwxr-xr-x. 2 root root 6 9月 9 14:21 TEST
drwxr-xr-x. 2 root root 6 9月 9 13:33 test1
-rwxr-xr-x. 1 root root 7 9月 8 13:56 test1.sh
-rwxr-xr-x. 1 root root 33 9月 8 14:00 test2.sh
drwxr-xr-x. 2 root root 6 9月 8 13:37 test.sh
-rw-r--r--. 1 root root 30 9月 9 08:02 users
-rw-r--r--. 1 root root 3448957 2月 24 2011 wordpress-3.1-zh_CN.zip
-rw-r--r--. 1 root root 542 9月 7 23:05 world.txt
[root@localhost test]#
5) -type: 根据文件类型查找文件
(1)普通文件:f find pathname -type f
[root@localhost test]# find /test -type f
/test/hello.txt
/test/hello1.txt
/test/world.txt
/test/hell1.txt
/test/wordpress-3.1-zh_CN.zip
/test/.test.sh.swp
/test/.hello.php.swp
/test/test1.sh
/test/test2.sh
/test/.hello.sh.swp
/test/.hello.sh.swo
/test/.hello.sh.swn
/test/hello.sh
/test/test
/test/users
/test/.test.swp
[root@localhost test]#
(2)目录文件:d find pathname -type d
[root@localhost test]# find /test -type d
/test
/test/first
/test/second
/test/test.sh
/test/hello.php
/test/test1
/test/TEST
/test/teest
[root@localhost test]#
4.编辑
- 需要编辑的文件内容显示
- 点击i或者a进入可编辑模式
- 编辑内容
- 保存退出,先按esc ,再输入保存退出命令
:wq 表示保存退出
:wq! 表示强制保存退出
:x 表示保存退出
- 最后回车,则再进入命令模式
3.linux下管道模式与输出信息(输出信息查找,awk)
4.linux下的软件下载
yum
yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器。
基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载、安装。
1).yum 语法
yum [options] [command] [package ...]
options:可选,选项包括-h(帮助),-y(当安装过程提示选择全部为"yes"),-q(不显示安装的过程)等等。
command:要进行的操作。
package 操作的对象。
例:
yum search jdk 搜索 jdk 安装包
yum install -y java-1.8.0-openjdk.x86_64 安装 openjdk,-y 表示安装过程中的询问自动选 y
yum list installed |grep jdk 列出安装的 jdk 软件包
yum remove java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-headless.x86_64 卸载 jdk
2) yum 常用命令
1.列出所有可更新的软件清单命令:yum check-update
2.更新所有软件命令:yum update
3.仅安装指定的软件命令:yum install <package_name>
4.仅更新指定的软件命令:yum update <package_name>
5.列出所有可安裝的软件清单命令:yum list
6.删除软件包命令:yum remove <package_name>
7.查找软件包 命令:yum search <keyword>
8.清除缓存命令:
yum clean packages: 清除缓存目录下的软件包
yum clean headers: 清除缓存目录下的 headers
yum clean oldheaders: 清除缓存目录下旧的 headers
yum clean, yum clean all (= yum clean packages; yum clean oldheaders) :清除缓存目录下的
软件包及旧的 headers
例如:安装wget
[root@localhost /]# yum install wget
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
- base: mirrors.aliyun.com
- extras: mirrors.aliyun.com
- updates: mirrors.aliyun.com
正在解决依赖关系
–> 正在检查事务
—> 软件包 wget.x86_64.0.1.14-18.el7_6.1 将被 安装
–> 解决依赖关系完成
依赖关系解决
======================================================================================================
Package 架构 版本 源
大小
======================================================================================================
正在安装:
wget x86_64 1.14-18.el7_6.1 base
547 k
事务概要
======================================================================================================
安装 1 软件包
总下载量:547 k
安装大小:2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm | 547 kB
00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : wget-1.14-18.el7_6.1.x86_64
1/1
验证中 : wget-1.14-18.el7_6.1.x86_64
1/1
已安装:
wget.x86_64 0:1.14-18.el7_6.1
完毕!
[root@localhost /]#
wget
wget 是是一一个个从从网网络络上上自自动动下下载载文文件件的的自自由由工工具具,,支支持持通通过过 HTTP、HTTPS、FTP 三三个个最最常常见见的的
TCP/IP协议 下下载载,,并并可可以以使使用用 HTTP 代理。“wget” 这这个个名名称称来来源源于于 “World Wide Web” 与 “get”
的结合
1.使用方法
格式:wget [选项] [参数]
其中参数为:URL地址。
其中选项如下:
-a<日志文件>:在指定的日志文件中记录资料的执行过程;
-A<后缀名>:指定要下载文件的后缀名,多个后缀名之间使用逗号进行分隔;
-b:进行后台的方式运行wget;
-B<连接地址>:设置参考的连接地址的基地地址;
-c:继续执行上次终端的任务;
-C<标志>:设置服务器数据块功能标志on为激活,off为关闭,默认值为on;
-d:调试模式运行指令;
-D<域名列表>:设置顺着的域名列表,域名之间用“,”分隔;
-e<指令>:作为文件“.wgetrc”中的一部分执行指定的指令;
-h:显示指令帮助信息;
-i<文件>:从指定文件获取要下载的URL地址;
-l<目录列表>:设置顺着的目录列表,多个目录用“,”分隔;
-L:仅顺着关联的连接;
-r:递归下载方式;
-nc:文件存在时,下载文件不覆盖原有文件;
-nv:下载时只显示更新和出错信息,不显示指令的详细执行过程;
-q:不显示指令执行过程;
-nh:不查询主机名称;
-v:显示详细执行过程;
-V:显示版本信息;
–passive-ftp:使用被动模式PASV连接FTP服务器;
–follow-ftp:从HTML文件中下载FTP连接文件。
2.例子:
- 下载单个文件
wget 文件的URL
wget http://cn.wordpress.org/wordpress-3.1-zh_CN.zip
- 限速下载文件
wget --limit-rate=300k 文件的URL
- 支持断点续传
wget -c 文件的URL
- 后台下载文件
wget -b 文件的URL
- 测试下载链接
wget --spider 文件的URL
5.linux下用户权限理解
什么是权限
权限是操作系统用来限制资源访问的机制,权限一般分为读、写、执行。系统中每个文件都拥有特定的权限、所属用户及所属组,通过这样的机制来限制哪些用户、哪些组可以对特定的文件进行什么样的操作。每个进程都是以某个用户的身份运行,所以进程的权限与该用户的权限一样,用户的权限越大,该进程所拥有的权限也就越大。
目录权限与文件权限
用户的分类
在linux下针对文件权限分为了三类用户分别为
文件所有者 | 文件属于谁 |
---|---|
文件所属组 | 文件属于哪个用户组 |
其它用户 | 除上方两类之外的用户 |
目录权限
r (可读权限) | 用户是否能浏览目录 |
---|---|
w (可写权限) | 用户是否能够创建或删除目录 |
x (可执行权限) | 用户是否可以进入目录 |
文件权限
r (可读权限) | 用户是否能够读取内容 |
---|---|
w (可写权限) | 用户是否能够修改内容 |
x (可执行权限) | 用户是否能够执行这个文件 |
我们可以通过命令ls -l来查看目录下文件的权限
进入目录需要哪些权限, 在目录中执行增删查(cd, touch, ls, rm, mv等)改文件动作, 需要哪些权限?
通过进入超级用户修改过权限后,我们分别实验可以得到如下的结论:
cd | 所需权限 |
---|---|
touch | 所需权限wx |
ls | 所需权限rx |
rm | 所需权限wx |
mv | 所需权限wx |
6.linux下用户权限操作
Chmod命令主要用于修改、设置文件权限
chmod 修改文件权限主要有两种方式: 字母法与数字法
虽然数字法相对字母法简单,但是数字法是基于字母法,所以这里先介绍字母法。
字母法
chmod (u g o a) (+ - =) (r w x) (文件名)
以上是chmod的用法,每个括号是一个参数, 前三个括号主要放在一起使用即 chmod + 设置模式 + 文件
名;下面先介绍这些参数的意义:
[u g o a] | 含 义 |
---|---|
u | user 表示该文件的所有者 |
g | group 表示与该文件的所有者属于同一组( group )者,即用户组 |
o | other 表示其它用户组 |
a | all 表示这三者皆是 |
[+ - =] | 含 义 |
---|---|
+ | 增加权限 |
- | 撤销权限 |
= | 设定权限 |
[r w x] | 含 义 |
---|---|
r(4) | read 表示可读取,对于一个目录,如果没有r权限,那么就意味着不能通过ls查看这个目录的内容。 |
w(2) | write 表示可写入,对于一个目录,如果没有w权限,那么就意味着不能在目录下创建新的文件。 |
x(1) | excute 表示可执行,对于一个目录,如果没有x权限,那么就意味着不能通过cd进入这个目录。 |
用法:chmod + 设置模式 + 文件名 中间的设置模式,要分别对u(user), g(group), o(other)设置权限。
chmod u+rwx, g+rwx, o+rwx filename # 改命令说明对filename文件, 赋予user、group、other均有read、write、excute的权限
注意:(chmod + 设置模式 + 文件名) 设置模式中包含对三种类型用户(u、g、o)的权限设置,这三者
各自独立,互不干扰。