Linux基础命令、软件安装

常用命令

查看当前系统中存在哪些shell

cat /etc/shells
[root@** ~]# cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

查看当前操作系统正在使用的命令解析器:

[root@** ~]# echo $SHELL
/bin/bash

输入命令时,光标移动

功能 快捷键 助记
Ctrl+P previous
Ctrl+N next
Ctrl+B backward
Ctrl+F forward
Del ctrl+D delete光标后面的
Home Ctrl+A the first letter
End Ctrl+E end
Backspace Backspace 删除光标前面的

常用的有 Ctrl+A, Ctrl+E

目录和文件

every thing is file

目录介绍

[root@** ~]# ls /
bin   etc  lib    lost+found  mnt  proc  run   srv  tmp  var
boot  dev  home  lib64  media       opt  root  sbin  sys  usr
  • bin :可执行文件目录
  • boot 开机启动配置
  • etc 用户信息和系统配置文件
  • dev 设备
  • lib 库路径,如:标准C库 libc.so.6
  • media 挂载媒体设备,如光驱、优盘等
  • mnt 磁盘挂载,如硬盘
  • root root用户
  • usr Unix software resource 用户资源管理目录

切换目录(cd)

cd -  # 切换到上一个目录
cd ~  # home 

创建文本文件(touch)

touch test.cpp

文件列表(ls)

ls /
ls -a  # 所有文件
ls -l  # long  详细信息
ls -dl dir # 查看目录信息
ls -R  #递归
  1. 文件描述
[root@** code]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Feb  7 13:59 mysql_test
-rwxr-xr-x 1 root root 8656 Feb  3 11:43 test
-rw-r--r-- 1 root root   88 Feb  3 11:43 test.cpp

文件类型,当前用户的权限[读,写,是否,可执行],当前组,其他用户

文件类型(7种)

  • 普通文件:-

  • 目录文件:d

  • 字符设备文件:c

  • 块设备文件:b 如磁盘

  • 软连接:l

  • 管道文件:p

  • 套接字:s

which

查看目录路径

[root@** ~]# which pwd
/usr/bin/pwd

隐藏[root@** ~]字符

编辑文件~/.bashrc,添加配置

PS1=$

rmdir

删除空目录,一次可以删除多个。

  • -p 删除多级目录,不能包含文件
[root@** demo]# rmdir -p aa/cc/

rm

删除文件。

  • -r:递归
  • -f:force
  • -v:可视化

cp

cp a.cpp dir/
cp a.cpp b.cpp
cp -a dir1/ ./  # 目录,all
cp -r dir1/ ./  # 目录,不包括文件属性,如:时间,权限等

mv

mv a.cpp dir/
mv a.cpp b.cpp  # 重命名
mv dir1/ dir2/ 

cat

查看文件内容

可以读终端

tac

倒着显示

more

空格翻页,enter一行

less

不显示百分比,只能用q退出

head

显示默认前10行

head -20  # 前20行

tail

显示默认后10行

ln

软硬链接

  1. 创建软链接
ln -s file file.s
[root@** demo]# ls -l
-rw-r--r-- 1 root root    30 Feb  8 09:35 file
lrwxrwxrwx 1 root root     4 Feb  8 09:35 file.s -> file

ln -s ./file file.s
[root@** demo]# ls -l
-rw-r--r-- 1 root root    30 Feb  8 09:35 file
lrwxrwxrwx 1 root root     6 Feb  8 09:35 file.s -> ./file

使用相对路径创建的软链接不可以移动。软链接保存的时创建时的路径,因此一定要使用绝对路径

[root@** demo]# ln -s /root/demo/file file.s2
[root@** demo]# ls -l
lrwxrwxrwx 1 root root    15 Feb  8 09:41 file.s2 -> /root/demo/file

此时可以移动软链接文件

  • 文件权限

软链接全开放代表软链接的权限

  1. 创建硬链接
ln file file1

对于硬链接,文件数据同步

[root@** demo]# stat file
  File: ‘file’
  Size: 30              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 1051448     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-02-08 09:35:57.020407922 +0800
Modify: 2020-02-08 09:35:55.186414489 +0800
Change: 2020-02-08 09:35:55.189414478 +0800

硬链接文件的Inode相同,删除时,将硬链接计数减一。

tree

安装:

yum install tree

wc

计算文件字节数、字数等。

  • -c 或--bytes 或 --chars
  • 只显示Byte数
  • -l 或 --lines显示列数
  • -w 或 --words显示字数

od

显示文件的进制

od -tcx file

-t制定数据显示格式,主要参数有:

  • c ASCII字符或反斜杠序列
  • d[SIZE] 有符号十进制数,每个整数SIZE字节
  • f[SIZE] 浮点数,每个整数SIZE字节
  • o[SIZE] 八进制
  • u[SIZE] 无符号十进制
  • x[SIZE] 十六进制
[root@** code]# od -tcd test.cpp 
0000000   #   i   n   c   l   u   d   e   <   m   y   s   q   l   .   h
             1668180259      1701082476      1937337660      1747872881
0000020   >  \n   #   i   n   c   l   u   d   e   <   i   o   s   t   r
             1763904062      1970037614      1765565796      1920234351
0000040   e   a   m   >  \n  \n   u   s   i   n   g       n   a   m   e
             1047355749      1937050122       543649385      1701667182
0000060   s   p   a   c   e       s   t   d   ;  \n  \n   i   n   t    
             1667330163      1953701989       168442724       544501353
0000100   m   a   i   n   (   )  \n   {  \n  \t   M   Y   S   Q   L    
             1852399981      2064263464      1498220810       541872467
0000120   c   o   n   n   ;  \n   }  \n
             1852731235       175966779

du

显示某个目录的磁盘大小

  • 单位M
[root@** code]# du -hm mysql_test/
1       mysql_test/
  • 单位B
[root@iZy4sb7b6agxt2Z code]# du -hb mysql_test/
12991   mysql_test/
  • 单位K(默认)
[root@** code]# du -hk mysql_test/
20      mysql_test/

df

查看磁盘使用情况

[root@** code]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          930660       0    930660   0% /dev
tmpfs             941116       0    941116   0% /dev/shm
tmpfs             941116     444    940672   1% /run
tmpfs             941116       0    941116   0% /sys/fs/cgroup
/dev/vda1       41147472 4169976  35074004  11% /
tmpfs             188224       0    188224   0% /run/user/0

文件属性和用户组

whoami

查看当前登录用户

chmod

修改文件属性,所有者,组,其他

  • u+x,u-x user
chmod u+x file
  • g+x group

  • o+x other

  • a+x all

  • 777

chmod 777 file

chown

文件所有者,所有组

chown username file
chown username:groupName file

chgrp

chgrp name file

adduser

adduser username

deluser

deluser username

addgroup

addgroup name

delgroup

delgroup groupName

查找和检索

find

find dir -type 'l'

find dir 必须放在前面

# 查找当前目录下的软链接
[root@** demo]# find ./ -type 'l'
./file.s
./file.s2

# 查找 .java 文件
find ./ -name '*.java'

# 查询深度
find ./ -maxdepth 1 -name '*.java'

# 查询指定大小的文件
find ./ -size +2k -size -10k
  • -type f|d|l
  • -name
  • -maxdepth
  • -size +-10M|k|m
  • -atime
  • -mtime
  • -ctime
  • -exec
  • -ok 交互式
  • -print0 0表示查询出的结果集的分隔符为 NUL
[root@iZy4sb7b6agxt2Z demo]# stat stdio.h 
  File: ‘stdio.h’
  Size: 31641           Blocks: 64         IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 1051436     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   fight)   Gid: (    0/    root)
Access: 2020-02-08 10:32:56.157924435 +0800
Modify: 2020-02-07 17:39:56.495737825 +0800
Change: 2020-02-08 10:32:37.490994734 +0800
  1. 将查询结果用来执行某个操作
# {} 代表查询出的结果
[root@** demo]# find ./ -name "*.s" -exec ls -l {} \;
lrwxrwxrwx 1 root root 4 Feb  8 09:35 ./file.s -> file

# exec的衍生版,可以提示用户是否操作
[root@** demo]# find ./ -name "*.s" -ok rm -r {} \;
lrwxrwxrwx 1 root root 4 Feb  8 09:35 ./file.s -> file

注意:{}和\之间必须有一个空格,\;代表结束

grep

查询文件内容

[root@** demo]# grep -r -n 'copy' ./
./stdio.h:15:   You should have received a copy of the GNU Lesser General Public
  • -r 递归
  • -n 显示行号

ps aux :查看后台进程

[root@** demo]# ps aux | grep mysql
mysql     4394  0.0  9.0 1145288 171212 ?      Sl   Feb03   2:26 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     14635  0.0  0.0 112712   964 pts/6    R+   11:33   0:00 grep --color=auto mysql

xargs

和find、管道组合使用,将文件查询结果作为执行参数

xargs 和 -exec {} ; 效果一样

xargs和 -exec相比,将查询结果做了分片处理,将结果集分片映射

[root@** demo]# find ./ -type f | xargs ls -l
-rw-r--r-- 1 root  root     0 Feb  8 09:47 ./bb/a1
-rw-r--r-- 1 root  root    30 Feb  8 09:35 ./file
-rw-r--r-- 1 fight root 31641 Feb  7 17:39 ./stdio.h

xargs的问题:默认将结果集用空格作为分隔符,当文件名包含空格时,就会出问题

解决办法:自己设定分隔符

[root@** demo]# find ./ -type f | xargs ls -l
ls: cannot access ./11: No such file or directory
ls: cannot access 22: No such file or directory
-rw-r--r-- 1 root  root    30 Feb  8 09:35 ./file
-rw-r--r-- 1 fight root 31641 Feb  7 17:39 ./stdio.h

# 设定分隔符
[root@** demo]# find ./ -type f -print0 | xargs -print0 ls -l
xargs: WARNING: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
ls -l ?...y
total 44
-rw-r--r-- 1 root  root     0 Feb  8 11:46 11 22
# 没有提示操作
[root@** demo]# find ./ -type f -print0 | xargs -0 ls -l
-rw-r--r-- 1 root  root     0 Feb  8 11:46 ./11 22
-rw-r--r-- 1 root  root     0 Feb  8 09:47 ./bb/a1
-rw-r--r-- 1 root  root    30 Feb  8 09:35 ./file
-rw-r--r-- 1 fight root 31641 Feb  7 17:39 ./stdio.h

或者 find ./ -type f -print0 | xargs -0 ls -l

安装卸载软件

从源中安装

1、 centos Redhat系列

安装

yum install tree

卸载

yum remove tree

2、Ubuntu deb系列
安装

apt install gcc

卸载

dpkg -r gcc

安装包

  1. Redhat --> centos --->
 rpm -ivh mysql57-community-release-el7-8.noarch.rpm 
  1. deb --> Ubuntu
dpkg -i  xxx .deb 安装
dpkg -r 命令|xxx.deb 删除
dpkg -r --purge  xxx.deb 同时删除配置文件
dpkg -info xxx.deb  查看信息
dpkg -l  查看系统中已安装软件包信息

源码安装

1) 解压源代码包
2) cd dir
3) ./confiure

检查文件是否缺失,创建Makefile,检测编译环境
4) meke
编译源代码,生成库和可执行文件
5) sudo make install
把库和可执行程序,安装到系统中
6) sudo make distclean

删除和卸载软件

压缩解压

tar

压缩

# 压缩并打包
tar -zcvf file.tar.gz file1 file2 ..
# 使用bzip2方式压缩
tar -jcvf file.tar.bz file1 file2 ..

# 将文件或文件夹下的所有文件压缩  file ->file.gz
gzip file/dir
  • -z 用gzip压缩
  • -c|x 压缩 |解压
  • -v 可视化
  • -f force

gzip

只能压缩一个文件

  • -r 递归压缩
  • -c 保留原文件
  • -v 可视化
  • -d 解压缩

解压

tar  -zxvf file.tar.gz
tar  -jxvf file.tar.bz
gunzip file

rar

安装:

yum install rar
rar a -r file.rar dir

unrar x file.rar

zip

yum install zip
zip -r file.zip dir

unzip file.zip

其他命令

ps

ps aux | grep mysql

查看进程

&

后台运行

./file &

fg

切换到前台

作业好前面需要加 %

bg

切换到后台

kill

kill PID

env

查看当前环境变量

top

文字版的任务管理器

man

安装man-pages,查看库函数

yum install -y man-pages

在vim中光标在函数上,normal模式下输入K,即可进入man-pages

帮助文档

man printf
man 1 printf  # shell
man 2 printf  # 系统调用
man 3 printf  # 库调用
man 5 passwd  # 文件规范,如第一列代表什么意思

alias

alias la = 'ls -al'

date

显示系统时间

umask

显示文件掩码,默认touch的文件的权限

$ umask
0002

poweroff

shutdown

rebot

用户管理

切换用户

# 普通用户
sudo su username
# root
su

设置密码

# 普通用户
passwd username
# root
passwd

网络管理

ifconfig

# ifconfig查看网卡信息
ifconfig
# 关闭网卡
sudo ifconfig eth0 down
# 开启网卡eth0
sudo ifconfig eth0 up
# 给eth0配置临时IPsudo ficonfig eth0 IP

ping

ping ip
posted @ 2020-02-07 16:31  fight139  阅读(528)  评论(0编辑  收藏  举报