更适合java程序员体质的Linux的基本命令

171 阅读6分钟

基本命令

ip addr (查看ip地址)

[root@localhost ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:52:c8:ac brd ff:ff:ff:ff:ff:ff
    inet 192.168.138.100/24 brd 192.168.138.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::4806:556a:2ec5:1eb/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@localhost ~]#

pwd (查看当前所在的目录)


[root@localhost ~]# pwd
/root

shutdown -h now (关机)

reboot (重启)

ping ip或域名 (查看网络是否ok)

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (36.155.132.31) 56(84) bytes of data.
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=1 ttl=128 time=11.1 ms
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=2 ttl=128 time=12.1 ms
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=3 ttl=128 time=11.9 ms
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=4 ttl=128 time=11.6 ms

ctrl + c (强制结束占用窗口的命令(配合上面命令使用更佳))

^C
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 11.193/11.718/12.124/0.347 ms
[root@localhost ~]#

top (查看内存情况)

[root@localhost ~]# top
top - 18:02:39 up 53 min,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 120 total,   1 running, 119 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3861288 total,  2782416 free,   281748 used,   797124 buff/cache
KiB Swap:  4063228 total,  4063228 free,        0 used.  3341744 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0  193700   6764   4144 S   0.0  0.2   0:01.88 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
     4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
     6 root      20   0       0      0      0 S   0.0  0.0   0:00.05 ksoftirqd/0
     7 root      rt   0       0      0      0 S   0.0  0.0   0:00.06 migration/0
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
     9 root      20   0       0      0      0 S   0.0  0.0   0:00.65 rcu_sched
    10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-drain
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 watchdog/0
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 watchdog/1
    13 root      rt   0       0      0      0 S   0.0  0.0   0:00.07 migration/1
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/1
    15 root      20   0       0      0      0 S   0.0  0.0   0:02.48 kworker/1:0
    16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H
    17 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 watchdog/2
    18 root      rt   0       0      0      0 S   0.0  0.0   0:00.07 migration/2
    19 root      20   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/2
    21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0H
    22 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 watchdog/3
    23 root      rt   0       0      0      0 S   0.0  0.0   0:00.10 migration/3
    24 root      20   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/3
    25 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/3:0
    26 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/3:0H
    28 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kdevtmpfs
    29 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns
    30 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd
    31 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 writeback
    32 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kintegrityd
    33 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset
    34 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset
    35 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset
    36 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kblockd
    37 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 md
    38 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 edac-poller
    39 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 watchdogd

kill -9 pid (强制杀死指定进程)

ps -ef | grep -i 进程名字 (查看进程运行信息(|是管道的意思))

clear (清屏(是个假清屏,鼠标滚轮往上滑,就能出现之前写的代码))

history (查看历史命令(在历史命令的编号前面加上!就能执行历史命令))

[root@localhost ~]# history
    1  vi /etc/sysconfig/network-scripts/ifcfg-ens33
    2  systemctl restart network
    3  ping www.baidu.com
    4  ls -l
    5  cd tmp
    6  cd /tmp
    7  ls
    8  ..
    9  cd ..
   10  cd lib
   11  ls
   12  ls -a
   13  ping www.baidu.com
   14  ping www.itheima.com
   15  clear
   16  cd ..
   17  cd /opt
   18  mkdir aa
   19  mkdir bb cc
   20  mkmkdir cc/dd
   21  mkdir cc/dd
   22  rm -rf bb
   23  cp -r aa
   24  cp -r aa /root
   25  ls /root
[root@localhost ~]# !13
ping www.baidu.com
PING www.a.shifen.com (36.155.132.31) 56(84) bytes of data.
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=1 ttl=128 time=10.8 ms
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=2 ttl=128 time=10.8 ms
64 bytes from 36.155.132.31 (36.155.132.31): icmp_seq=3 ttl=128 time=11.2 ms

目录相关命令

cd 目录或符号 (切换指定目录(记得cd后面要空格噢))

[root@localhost ~]# cd /tmp
[root@localhost tmp]#

cd  ~ (切换到根目录)
[root@localhost tmp]# cd ~
[root@localhost ~]#

cd . (当前目录)
cd .. (返回上一级目录)
[root@localhost test]# cd ..
[root@localhost opt]#

ls 参数 (查看目录下的文件)

[root@localhost opt]# ls
tar.gz  test

mkdir [-p] 目录

[root@localhost test]# mkdir -p aa/bb/cc

rm -rf 目录名

[root@localhost test]# rm -rf aa/bb

cp [-r] 源目录(被复杂的目录) 目标目录(复制到的目录位置)

[root@localhost test]# cp  -r aa bb

mv 源目录(被复杂的目录) 目标目录(复制到的目录位置)

[root@localhost test]# mv bb aa

文件相关命令

删除文件,复制文件,移动文件跟上面的基本命令一致,这里就不写了

touch 文件名 (创建新文件)

[root@localhost test]# touch Teacher.java

vi 文件名(可以安装一个vim编辑器,这个vi不好用,没有颜色提示)

yum install vim (安装vim编辑器) vim 文件名 (打开文件)

查看模式

Snipaste_2023-09-03_19-50-05.png

esc (退出插入模式)

底行模式

image.png

查看文件

cat 文件名 (展开文件所有内容,适用于较小文件)

[root@localhost test]# cat User.java
public class User{

        private Integer id;
        private String name;
        private String username;
        private String password;
        private String status;
        private Integer gender;
        public void main(int[] args){
                System.out.println("我的名字是"+"name"+"我是"+"gender"+"孩子");
        }

}

more 文件名 (展开文件所有内容,适用于较大文件)

tail -f 文件名 (动态查看文件内容,监控日志文件)

(重要)tar

tar -zcvf 压缩后的文件名 被压缩的文件或者目录列表 (压缩)

[root@localhost test]# tar -zcvf teacher Teacher.java
Teacher.java

tar -zxvf 要解压的文件名 -C 被解压的文件或者目录列表 (解压)

[root@localhost tar.gz]# tar -zxvf apache-maven-3.5.4-bin.tar.gz
apache-maven-3.5.4/README.txt
apache-maven-3.5.4/LICENSE
apache-maven-3.5.4/NOTICE
apache-maven-3.5.4/lib/
apache-maven-3.5.4/lib/cdi-api.license
#### apache-maven-3.5.4/lib/commons-cli.license
apache-maven-3.5.4/lib/commons-io.license
......

查找命令

find 目录 参数 文件名(可以是后缀,也可以是前缀,可以使用通配符*代替) ( 查看自己定义的文件)

[root@localhost test]# find /opt String *java
/opt
/opt/test
/opt/test/User.java
/opt/test/Teacher.java
/opt/test/teacher
/opt/tar.gz
/opt/tar.gz/apache-tomcat-7.0.57.tar.gz
/opt/tar.gz/jdk-8u171-linux-x64.tar.gz
/opt/tar.gz/apache-maven-3.5.4-bin.tar.gz

grep 内容 文件名 (从指定的文件中查找指定的文本内容)

[root@localhost test]# grep String *java
User.java:      private String name;
User.java:      private String username;
User.java:      private String password;
User.java:      private String status;

[root@localhost test]# grep -n  String *java  (-n 表示行号)
User.java:4:    private String name;
User.java:5:    private String username;
User.java:6:    private String password;
User.java:7:    private String status;

权限命令

image.png

image.png

推荐使用数字授权

chmod 777 文件名

[root@localhost test]# ll
总用量 8
-rw-r--r--. 1 root root 119 9月   3 20:13 teacher
-rw-r--r--. 1 root root   0 9月   3 19:47 Teacher.java
-rw-r--r--. 1 root root 276 9月   3 16:22 User.java
[root@localhost test]# chmod 744 User.java
[root@localhost test]# ll
总用量 8
-rw-r--r--. 1 root root 119 9月   3 20:13 teacher
-rw-r--r--. 1 root root   0 9月   3 19:47 Teacher.java
-rwxr--r--. 1 root root 276 9月   3 16:22 User.java
[root@localhost test]#