#Linux基础(一)

###linux基础(一)
bash支持以下特性:

  • 支持命令历史、命令补
[root@localhost ~]# history 
    1  ip a
    2  ls
    3  echo $SHELL
    4  cat /etc/shells
    5  history 
  • 支持管道、重定向
[root@localhost ~]# ls | grep a
a
ab
anaconda-ks.cfg
  • 支持命令别名
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cd
[root@localhost ~]# alias net='cd /etc/sysconfig/network-scripts/'
[root@localhost ~]# net
[root@localhost network-scripts]# 

  • 支持命令行编辑
[root@localhost ~]# mkdir -p www/{www2/{a,b},www3/{c,d},www4/{e,f}}

  • 支持命令行展开
[root@localhost ~]# tree www
www
`-- www2

1 directory, 0 files
[root@localhost ~]# 

  • 支持文件名通配
[root@localhost ~]# ls a*
a  ab  anaconda-ks.cfg
[root@localhost ~]#  

  • 支持变量
[root@localhost ~]# a=11
[root@localhost ~]# a=12
[root@localhost ~]# echo $a
12
[root@localhost ~]# 
  • 支持编程
[root@localhost bin]# ./python3
Python 3.6.8 (default, Jan 19 2022, 23:28:49) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/5
0.6
>>> 

  • bash支持的引号
    `` //反引号,键盘左上角Esc下面的键,用于命令替换
[root@localhost ~]# echo `ls`
a ab anaconda-ks.cfg test www


“” //双引号,弱引用,可以实现变量替换

[root@localhost ~]# echo "123"
123

‘’ //单引号,强引用,不完成变量替换

[root@localhost ~]# echo $a
12
[root@localhost ~]# echo '$a'
$a
[root@localhost ~]# echo "$a"
12
[root@localhost ~]# 

bash常用操作技巧光标跳转
光标跳转
Ctrl+a //跳到命令行
Ctrl+e //跳到命令行尾
Ctrl+u //删除光标至命令行首的内容
Ctrl+k //删除光标至命令行尾的内容
Ctrl+w //光标定位到离自己最近的一个单词前面
Ctrl+l //清屏

  • Bash常用操作技巧之命令历史:
    history命令用于查看命令历史:

history命令常用选项

      -c : 清空命令历史  [root@localhost ~]# history -c

                   
    -d[n]:删除第n条命令历史 
         [root@localhost ~]# history 
         1  ls
         2  cd
         3  ls
         4  history 
         [root@localhost ~]# history -d 3
         [root@localhost ~]# history -d 1
         [root@localhost ~]# history 
         1  cd
         2  history 
         3  history -d 3
         4  history -d 1
         5  history 
         [root@localhost ~]# 

     -w:保持命令历史至历史文件-/bin_history中
  • Bash常用操作技巧之命令历史(二):

    !n          //执行命令历史中的第n条命令
    [root@localhost ~]# !9
     cd
    
    
    !-n         //执行命令历史中倒数第n条命令
    [root@localhost ~]# !-11
    history -d 3
    
    
    !!          //执行上一条命令
    [root@localhost ~]# !!
    history -d 3
    
    !string     //执行命令历史中最近一个以指定字符串开头的命令
    [root@localhost ~]# !string
    bash: !string: event not found
    
    
    !$          //引用前一个命令的最后一个参数 
    [root@localhost ~]# !$
    3
    bash: 3: command not found
    
    
    esc,.       //按下esc松开后按.,引用前一个命令
    的最后一个参数       [root@localhost ~]# 3
    

//命令补全
搜索PATH环境变量所指定的每个路径下以我们给出的字符串开头的可执行文件
如果多于一个,两次tab,可以给出列表,否则将直接补全

  • 命令替换(把命令中某个子命令替换为其执行结果的过程)
    $(COMMAND) //推荐方式

    COMMAND
     [root@localhost ~]# echo $(ls)
     a ab anaconda-ks.cfg test www
     [root@localhost ~]# echo `ls`
     a ab anaconda-ks.cfg test www
     [root@localhost ~]# 
     
    
    • 命令行展开
      ~ //展开为用户的家目录
      ~USERNAME //展开为指定用户的家目录
    [root@localhost ~]# ls ~
    

a ab anaconda-ks.cfg test www
[root@localhost ~]# ls /home/
[root@localhost ~]# useradd abc
[root@localhost ~]# ls ~abc
[root@localhost ~]# cd /home/abc/
[root@localhost abc]# cd
[root@localhost ~]# ls ~abc
[root@localhost ~]#

```
 {}      //可承载一个以逗号分隔的列表,并将其展开为多个路径
```
    [root@localhost ~]# mkdir -p /tmp/{tom,jerry}/hi
 [root@localhost ~]# tree /tmp/

/tmp/
|-- jerry
| -- hi |-- ks-script-26hptxlb |-- ks-script-4sp3vai4 |-- tom | – hi
|-- vmware-root_883-4021653483
`-- vmware-root_910-2697139510

6 directories, 2 files
[root@localhost ~]#

```
  • Bash环境量介绍:
    PATH //命令搜索路径
    [root@localhost ~]# echo $PATH
    

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
```

HISTSIZE    //命令历史缓冲区大小
```
[root@localhost ~]# echo $HISTSIZE

1000

```
SHELL       //当前shell
```
[root@localhost ~]# echo $SHELL

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

```
  • 命令 [选项] [参数]
[root@localhost ~]#  ls -l -h
total 4.0K
-rw-r--r--. 1 root root    0 Jun 29 14:32 a
-rw-r--r--. 1 root root    0 Jun 29 14:32 ab
-rw-------. 1 root root 1.1K Jun 27 14:46 anaconda-ks.cfg
drwxr-xr-x. 5 root root   74 Jun 29 16:38 test
drwxr-xr-x. 5 root root   42 Jun 29 16:17 www
[root@localhost ~]# 


选项:(可以有0个或多个)
短选项:-
多个选项可以组合: -a -b = -ab

[root@localhost ~]# ls -lh
total 4.0K
-rw-r--r--. 1 root root    0 Jun 29 14:32 a
-rw-r--r--. 1 root root    0 Jun 29 14:32 ab
-rw-------. 1 root root 1.1K Jun 27 14:46 anaconda-ks.cfg
drwxr-xr-x. 5 root root   74 Jun 29 16:38 test
drwxr-xr-x. 5 root root   42 Jun 29 16:17 www
[root@localhost ~]# 

长选项:–
长选项通常不能组合

参数:命令的作用对象(可以有0个或多个)

[root@localhost ~]# ls --human-readabledereference
ls: unrecognized option '--human-readabledereference'
Try 'ls --help' for more information.
[root@localhost ~]# 
  • Linux命令分为两种类型:

内部命令(shell内置)

[root@localhost ~]# type ls
ls is aliased to `ls --color=auto'
[root@localhost ~]# type cd
cd is a shell builtin
[root@localhost ~]# 

外部命令:在文件系统的某个路径下有一个与命令名称相应的可执行文件

[root@localhost ~]# /root/test/bin/python3
Python 3.6.8 (default, Jan 19 2022, 23:28:49) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit

  • 基础命令之ls
    -l:这里是小写的L,不是数字。长格式显示
[root@localhost ~]# ls -lh
total 4.0K
-rw-r--r--. 1 root root    0 Jun 29 14:32 a
-rw-r--r--. 1 root root    0 Jun 29 14:32 ab
-rw-------. 1 root root 1.1K Jun 27 14:46 anaconda-ks.cfg
drwxr-xr-x. 5 root root   74 Jun 29 16:38 test
drwxr-xr-x. 5 root root   42 Jun 29 16:17 www

-h:做单位转换

[root@localhost ~]# ls -lh
total 4.0K
-rw-r--r--. 1 root root    0 Jun 29 14:32 a
-rw-r--r--. 1 root root    0 Jun 29 14:32 ab
-rw-------. 1 root root 1.1K Jun 27 14:46 anaconda-ks.cfg
drwxr-xr-x. 5 root root   74 Jun 29 16:38 test
drwxr-xr-x. 5 root root   42 Jun 29 16:17 www

-a:显示隐藏文件

[root@localhost ~]# ls -a
.   .bash_history  .bash_profile  .config  .python_history  a   anaconda-ks.cfg  www
..  .bash_logout   .bashrc        .cshrc   .tcshrc          ab  test

-d:显示目录自身的属性

[root@localhost ~]# ls -d
.

-t:以时间排序

[root@localhost ~]# ls -t
test  www  a  ab  anaconda-ks.cfg

-r:逆序显示

[root@localhost ~]# ls -r
www  test  anaconda-ks.cfg  ab  a

  • 常用基础命令(一)
    cd //改变当前工作目录
[root@localhost ~]# cd www/
[root@localhost www]# 

pwd //打印当前工作目录路径

[root@localhost ~]# pwd
/root
[root@localhost ~]# 


mkdir //创建目录

[root@localhost ~]# mkdir -p www/{www2,www3}
[root@localhost ~]#

-p //创建目录时若父目录不存在则自动创建

[root@localhost ~]# mkdir -p www/{www2,www3}
[root@localhost ~]#

-v //显示目录创建过程

 [root@localhost ~]# mkdir -pv www2/{a,b}
mkdir: created directory 'www2'
mkdir: created directory 'www2/a'
mkdir: created directory 'www2/b'
[root@localhost ~]# 

touch //无中生有、万象更新

[root@localhost ~]# touch qq
[root@localhost ~]# ls
a  ab  anaconda-ks.cfg  qq  test  www  www2
[root@localhost ~]# touch a
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root    0 Jun 29 18:45 a
-rw-r--r--. 1 root root    0 Jun 29 14:32 ab
-rw-------. 1 root root 1092 Jun 27 14:46 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Jun 29 18:45 qq
drwxr-xr-x. 5 root root   74 Jun 29 16:38 test
drwxr-xr-x. 3 root root   18 Jun 29 18:42 www
drwxr-xr-x. 4 root root   24 Jun 29 18:42 www2
[root@localhost ~]# 

stat //显示文件或文件系统的状态

[root@localhost ~]# stat www
  File: www
  Size: 18              Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 201329764   Links: 3
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-06-29 16:18:10.378513648 +0800
Modify: 2022-06-29 18:42:07.331137687 +0800
Change: 2022-06-29 18:42:07.331137687 +0800
 Birth: 2022-06-29 15:58:43.333501718 +0800
[root@localhost ~]# 

rm //删除文件,删除命令默认会提示是否需要删除,
//如果要使用命令本身可以在命令前加一个\,如\rm,这样删除就不会有提示了
-r //递归删除,删除目录时必须使用此选项

[root@localhost ~]# rm a
rm: remove regular empty file 'a'? y
[root@localhost ~]# ls
ab  anaconda-ks.cfg  qq  test  www  www2

-f //强制删除,不询问

[root@localhost ~]# rm -r www2
rm: descend into directory 'www2'? y
rm: remove directory 'www2/a'? y
rm: remove directory 'www2/b'? n
[root@localhost ~]# rm -rf www2
[root@localhost ~]# 


cp //复制文件,一个文件到一个文件,多个文件到一个目录
-a //归档复制,常用于备份

[root@localhost ~]# cp -a ab qq2
[root@localhost ~]# ls
ab  anaconda-ks.cfg  qq  qq2  test  www
[root@localhost ~]# 

-r //递归拷贝,拷贝目录时必须使用此选项

[root@localhost ~]# cp -r test www
[root@localhost ~]# ls www/
test  www4
[root@localhost ~]# 

-p //拷贝时保留原权限
mv //移动文件

[root@localhost ~]# mv ab abc
[root@localhost ~]# ls
abc  anaconda-ks.cfg  qq  qq2  test  www
[root@localhost ~]# mv abc www/
[root@localhost ~]# ls
anaconda-ks.cfg  qq  qq2  test  www
[root@localhost ~]# ls www/
abc  test  www4
[root@localhost ~]# 

  • 查看文本
    cat //拼接文件内容并输出至标准输出(屏幕)
[root@localhost ~]# cat anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
^minimal-environment
kexec-tools

-n //显示行号
//使用cat查看文件内容时会将文件的所有内容加载至内存,
//所以应避免使用cat打开巨大文件

[root@localhost ~]# cat -n anaconda-ks.cfg 
     1  #version=RHEL8
     2  # Use graphical install
     3  graphical
     4  
     5  repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
     6  
     7  %packages
     8  @^minimal-environment
     9  kexec-tools

more //全屏查看文本文件内容,只能从前往后,不能从后往前。
//文件内容显示完后自动退出

[root@localhost ~]# more anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
kexec-tools

less //全屏查看文本文件内容,可从前往后亦可从后往前。推荐使用

# Network information
network  --bootproto=dhcp --device=ens32 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
anaconda-ks.cfg

head //从头部开始打印文件内容,默认打印10行

[root@localhost ~]# head anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
kexec-tools

-n //指定要打印的行数,可以是-n 15也可以是-15

[root@localhost ~]# head -n 3 anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical

tail //查看文本文件尾部内容

[root@localhost ~]# tail anaconda-ks.cfg 

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

-n //指定要打印的行数,可以是-n 15也可以是-15

[root@localhost ~]# tail -1 anaconda-ks.cfg 
%end
[root@localhost ~]# 

-f:实时查看文件的更新

[root@localhost ~]# ls
a.txt  a.txt2  anaconda-ks.cfg  qq  qq2  test  www
[root@localhost ~]# tail -f a.txt2
123
  • 文本统计

wc(word count)
-c //显示字节数

[root@localhost ~]# wc -c a.txt
wc: a.txt: Is a directory
0 a.txt
[root@localhost ~]# 

-l //显示行数

[root@localhost ~]# wc -l a.txt2
1 a.txt2
[root@localhost ~]# 

-w //显示单词数

[root@localhost ~]# wc -w a.txt
wc: a.txt: Is a directory
0 a.txt
[root@localhost ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值