zoukankan      html  css  js  c++  java
  • Linux常用操作

    1 目录操作

    • 创建目录
    ubuntu@VM-0-38-ubuntu:~$ mkdir home/testFolder
    
    • 切换目录  
    ubuntu@VM-0-38-ubuntu:~$ cd /home/ubuntu/testFolder
    ubuntu@VM-0-38-ubuntu:~/testFolder$ cd ..
    • 移动目录:将当前目录下的testFoldfer文件夹移动到/data目录下
    ubuntu@VM-0-38-ubuntu:~$ sudo mv testFolder /data

    注意,mv也可用于文件重新命名,mv A B表示将A重命名为B

    • 删除目录
    rm -rf /var/tmp/testFolder
    • 查看目录下的所有文件/文件夹
    ls /etc
    

    2 文件操作

    • 创建文件
    touch ~/testFile
    

    ~代表你的/home/用户名目录

    假设你的用户名是x,那么~/就是/home/x/

    .是代表此目录本身,但是一般可以不写

    所以cd ~/. 和cd ~ 和cd ~/效果是一样的

    但是.后面有东西又是另外一个问题,点在文件名头部,代表一个隐藏文件

    ~/.local是你的主目录下一个.local的文件夹的路径,

    并且从.可以看出,这是一个隐藏文件,

    如果不用ls -a的话,一般ls是无法看到的

    • 复制文件
    cp ~/testFile ~/testNewFile
    • 删除文件
    rm ~/testFile
    
    •  使用cat命令查看文件内容 
    cat ~/.bash_history

    3 过滤、管道和重定向

    3.1 过滤

    • 过滤出 /etc/passwd【文件】中包含 root 的记录
    ubuntu@VM-0-38-ubuntu:~$ grep 'root' /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    

      递归地过滤出 /var/log/ 【目录】中包含 linux 的记录

    ubuntu@VM-0-38-ubuntu:~$ sudo grep -r 'linux' /var/log/
    /var/log/kern.log:Mar 22 20:05:55 localhost kernel: [    0.085203] evm: security.selinux
    /var/log/kern.log:Mar 22 20:06:58 localhost kernel: [    0.085158] evm: security.selinux
    /var/log/syslog:Mar 22 20:05:55 localhost kernel: [    0.085203] evm: security.selinux
    /var/log/syslog:Mar 22 20:06:58 localhost kernel: [    0.085158] evm: security.selinux
    /var/log/auth.log:Mar 22 20:55:02 localhost sudo:   ubuntu : TTY=pts/0 ; PWD=/home/ubuntu ; USER=root ; COMMAND=/bin/grep -r linux /var/log/
    

    3.2 管道

    简单来说, Linux 中管道的作用是将上一个命令的输出作为下一个命令的输入, 像 pipe 一样将各个命令串联起来执行, 管道的操作符是 |
    比如, 我们可以将 cat 和 grep 两个命令用管道组合在一起:
    ubuntu@VM-0-38-ubuntu:~$ cat /etc/passwd | grep 'root'
    root:x:0:0:root:/root:/bin/bash
    

      可以看到这和前面的命令grep 'root' /etc/passwd的效果是一样的

    过滤出 /etc 目录中名字包含 ssh 的目录(不包括子目录)
     
    ls /etc | grep 'ssh'

    3.3 重定向

    可以使用 > 或 < 将命令的输出重定向到一个文件中
     
    echo 'Hello World' > ~/test.txt

    输出结果是,在/home/usrname下会新建一个txt文件并写有内容Hello World.

    4 运维常用命令

    4.1 ping命令

    对 cloud.tencent.com 发送 4 个 ping 包, 检查与其是否联通

    ping -c 4 cloud.tencent.com
    

    4.2 netstat

    netstat 命令用于显示各种网络相关信息,如网络连接, 路由表, 接口状态等等
    列出所有处于监听状态的tcp端口:
    ubuntu@VM-0-38-ubuntu:~$ netstat -lt
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State
    tcp        0      0 *:ssh                   *:*                     LISTEN

    查看所有的端口信息, 包括 PID 和进程名称:

    ubuntu@VM-0-38-ubuntu:~$ netstat -tulpn
    (Not all processes could be identified, non-owned process info
     will not be shown, you would have to be root to see it all.)
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
    udp        0      0 172.21.0.38:123         0.0.0.0:*                           -
    udp        0      0 127.0.0.1:123           0.0.0.0:*                           -
    udp        0      0 0.0.0.0:123             0.0.0.0:*                           -
    udp6       0      0 :::123                  :::*                                -

    4.3 ps 

    过滤得到当前系统中的 ssh 进程信息:

    ubuntu@VM-0-38-ubuntu:~$ ps -aux | grep 'ssh'
    root       983  0.0  0.7  65504  6332 ?        Ss   20:06   0:00 /usr/sbin/sshd -D
    root      6019  0.0  0.7  92792  6680 ?        Ss   20:13   0:00 sshd: ubuntu [priv]
    ubuntu    6047  0.0  0.3  92792  3480 ?        S    20:13   0:00 sshd: ubuntu@notty
    root      6268  0.0  0.7  92792  6560 ?        Ss   20:13   0:00 sshd: ubuntu [priv]
    ubuntu    6296  0.0  0.3  92792  3516 ?        S    20:13   0:00 sshd: ubuntu@notty
    root      6307  0.0  0.7  92792  6864 ?        Ss   20:13   0:00 sshd: ubuntu [priv]
    ubuntu    6335  0.0  0.3  92792  3380 ?        S    20:13   0:00 sshd: ubuntu@notty
    root      7440  0.0  0.7  92792  6696 ?        Ss   20:15   0:00 sshd: ubuntu [priv]
    ubuntu    7468  0.0  0.4  92792  3544 ?        S    20:15   0:00 sshd: ubuntu@notty
    root     12365  0.0  0.7  92792  6852 ?        Ss   20:52   0:00 sshd: ubuntu [priv]
    ubuntu   12393  0.0  0.3  92792  3388 ?        S    20:52   0:00 sshd: ubuntu@pts/0
    ubuntu   13843  0.0  0.1  13228  1088 pts/0    S+   21:06   0:00 grep --color=auto ssh
  • 相关阅读:
    好吧,左小波出山了——ie8兼容indexOf问题
    jmeter负载机运行/添加压力机/分布式
    jmeter操作数据库
    Charles手机抓包设置&无法打开火狐网页设置
    python学习-Day1-接口测试
    动态SQL
    MyBatis缓存
    正则表达式
    MyBatis配置文件的配置说明
    几种数据源的配置
  • 原文地址:https://www.cnblogs.com/IcarusYu/p/8626694.html
Copyright © 2011-2022 走看看