【运维】Java开发人员掌握的Linux命令
作为Java开发人员,要掌握常用的Linux命令。
为什么要写此文,笔者的Linux很厉害?NoNoNo,正因为笔者不熟悉Linux才写此文,以作整理。
最主要的命令
查询命令的简要用法,help
当你不懂一个命令的作用时,可以在该命令后加--help或简写-h查看该命令的用法,比如查询ls的用法:
[root@localhost ~]# ls --help
查询命令的详细用法,man
man是manual,手册的意思,由于手册比较详细,会有翻页等交互,操作与less一致。
比如查询man命令的手册:
[root@localhost ~]# man man
常用选项
-h,也可用--human-readable,人类可读的意思-f,也可用--force,强制的意思-n,行数的意思,line number-c,创建的意思,create-x,也可用--extract,解压的意思-f,也可用--file=ARCHIVE,指定归档文件的意思-r,也可用--recursive,递归、循环的意思-d,也可用--decompress,解压的意思-v,也可用--verbose,详细的、啰嗦的意思
文件相关命令
列出目录内容,ls
ls,list directory contents,列出目录内容。
[user001@localhost ~]$ ls
1.log Desktop Documents Downloads Music Pictures Public Templates Videos
另外,ls -l的别名ll也挺好用,因为ll输入起来很方便:
[user001@localhost ~]$ ll
total 36
-rw-rw-r--. 1 user001 user001 25 Mar 23 07:23 1.log
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Desktop
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Documents
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Downloads
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Music
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Pictures
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Public
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Templates
drwxr-xr-x. 2 user001 user001 4096 Nov 29 07:41 Videos
文本编辑,vi
vi,全名为vi IMproved,是很好用的文本编辑器。
Esc,退出编辑模式到命令模式i,在光标前以插入形式进入编辑模式,进入此模式后在屏幕的左下角可看到-- INSERT --a,在光标后以插入形式进入编辑模式,进入此模式后在屏幕的左下角可看到-- INSERT --x,删除单字符,为什么是x,我的理解是叉的意思- 连续的按键位置
h、j、k、l分别代表←、↓、↑、→ j是右手食指的按键,是最常用的按键,自然是代表↓k用无名指按的,代表↑h在最左边,自然代表←l在最右边,自然代表→1yy,复制当前光标下的1行,如果复制两行,就是2yy。但是,我不知道为什么yy代表复制p,粘贴,pastedd,删除一行,delete:wq,保存并退出,write & quit:q!,不保存退出
查看文档,less
查看文档。
f,向下翻一页文档,forward的意思b,向上翻一页文档,back的意思G,滚动到文档的最后,这在查看日志时很常用F,滚动到文档的最后并定时刷新,这在查看日志时很常用/xxx,查找xxx的文档位置,按n定位到下一个匹配项,N定位到上一个匹配项
创建目录,mkdir
mkdir,意为创建目录,make directories。
[root@localhost ~]# mkdir hello_pkg
[root@localhost ~]#
[root@localhost ~]# ll
total 72
-rw-------. 1 root root 2695 Nov 29 15:36 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Mar 26 21:07 hello_pkg
创建文件,touch
touch,修改文件时间,或创建文件。以下是例子:
[root@localhost hello_pkg]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 26 21:11 hello_world.txt
[root@localhost hello_pkg]#
[root@localhost hello_pkg]# touch hello_world.txt
[root@localhost hello_pkg]#
[root@localhost hello_pkg]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 26 21:12 hello_world.txt
[root@localhost hello_pkg]# touch hello_new_world.txt
[root@localhost hello_pkg]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 26 21:13 hello_new_world.txt
-rw-r--r--. 1 root root 0 Mar 26 21:12 hello_world.txt
按文件名查找文件
在/目录下查找名称为xxx-data的文件:
find / -name xxx-data
查找文件内匹配的关键字
查找/top.log包含关键字Mem的行:
# grep 'Mem' /top.log
Mem: 1922148k total, 1783468k used, 138680k free, 180284k buffers
省略注释行和空行的方式查看配置文件
[root@blog ~]# grep -v "^#" xxx.conf | grep -v "^$"
删除文件或文件夹,rm
rm,删除文件或文件夹,remove files or directories。
这是删除一个文件,默认会询问确认是否删除:
[root@localhost hello_pkg]# rm hello_world.txt
rm: remove regular empty file `hello_world.txt'? y
这是删除目录,默认依然会询问确认,当然你可以通过强制命令取消询问,这里不介绍:
[root@localhost ~]# rm hello_pkg/
rm: cannot remove `hello_pkg/': Is a directory
[root@localhost ~]#
[root@localhost ~]# rm -r hello_pkg/
rm: descend into directory `hello_pkg'? y
rm: remove regular empty file `hello_pkg/hello_new_world.txt'? y
rm: remove directory `hello_pkg'? y
移动文件,mv
mv,移动文件,move的意思。
[root@localhost hello_package]# mv hello_world ../
[root@localhost hello_package]# ll ../
total 72
-rw-------. 1 root root 2695 Nov 29 15:36 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Mar 26 21:21 hello_package
-rw-r--r--. 1 root root 0 Mar 26 21:21 hello_world
拷贝文件,cp
cp,拷贝文件,copy的意思。
[root@localhost ~]# cp hello_world ./hello_package/
[root@localhost ~]# ll hello_package/
total 0
-rw-r--r--. 1 root root 0 Mar 26 21:23 hello_world
排序,sort
对文件排序并输入。
echo -e "3\n2\n1" > 123.txt
sort 123.txt
1
2
3
排除重复,uniq
echo -e "3\n2\n2\n1" > 123.txt
uniq 123.txt
3
2
1
实践
nginx的access.log是一个Web的访问日志,用它来练习非常不错,他的内容是这样的:
127.0.0.1 - - [07/Jul/2018:12:34:53 +0800] "GET /index.html HTTP/1.1" 200 612 "-" "curl/7.29.0"
192.168.1.1 - - [07/Jul/2018:12:50:05 +0800] "GET /index.html HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:12:50:32 +0800] "GET /index.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:12:52:21 +0800] "GET /index-nick.html HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:12:52:34 +0800] "GET /index.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:12:54:21 +0800] "GET /index-nick.html HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:12:56:03 +0800] "GET /index.html HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:20:32:56 +0800] "GET /index.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:20:33:02 +0800] "GET /index-nick.html HTTP/1.1" 200 612 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:20:34:58 +0800] "GET /index.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:20:35:03 +0800] "GET /index-nick.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:20:40:10 +0800] "GET /index.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
127.0.0.1 - - [07/Jul/2018:20:40:16 +0800] "GET /index-nick.html HTTP/1.1" 403 169 "-" "curl/7.29.0"
提取所有请求URL并排序排重
awk '{print $7}' /usr/local/nginx/logs/access.log | sort | uniq
归档文件命令
文件打包归档,tar
tar,将文件打包归档,或将存档文件打开。为什么叫tar?我的理解是together archive。
[root@localhost hello_package]# tar -cf txt.tar 1.txt 2.txt
[root@localhost hello_package]# ll
total 12
-rw-r--r--. 1 root root 0 Mar 26 22:29 1.txt
-rw-r--r--. 1 root root 0 Mar 26 22:29 2.txt
-rw-r--r--. 1 root root 10240 Mar 26 22:30 txt.tar
[root@localhost hello_package]# ll
total 12
-rw-r--r--. 1 root root 10240 Mar 26 22:30 txt.tar
[root@localhost hello_package]# tar -xf txt.tar
[root@localhost hello_package]# ll
total 12
-rw-r--r--. 1 root root 0 Mar 26 22:29 1.txt
-rw-r--r--. 1 root root 0 Mar 26 22:29 2.txt
-rw-r--r--. 1 root root 10240 Mar 26 22:30 txt.tar
gz文件的解压及压缩,gzip
gunzip解压文件:
[root@localhost third_pkg]# gzip -d apache-tomcat-8.5.12.tar.gz
[root@localhost third_pkg]# ll
total 13452
-rw-r--r--. 1 root root 13772800 Mar 14 19:55 apache-tomcat-8.5.12.tar
gunzip压缩文件:
[root@localhost third_pkg]# gzip -c apache-tomcat-8.5.12.tar > apache-tomcat-8.5.12.tar.gz
xz文件的解压和压缩,xz
解压:
[root@localhost third_pkg]# xz -d rabbitmq-server-generic-unix-3.6.8.tar.xz
压缩:
[root@localhost third_pkg]# xz -k rabbitmq-server-generic-unix-3.6.8.tar
bz2的解压与压缩,bzip2
解压:
[root@localhost third_pkg]# bzip2 -d 123.tar.bz2
压缩:
[root@localhost third_pkg]# bzip2 -c 123.tar > 123.tar.bz2
系统状态的命令
查看网卡的信息,ifconfig
ifconfig,是network interfaces configuring,查询服务器的网卡情况:
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:72:9D:E7
inet addr:192.168.1.101 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe72:9de7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4806 errors:0 dropped:0 overruns:0 frame:0
TX packets:826 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:387034 (377.9 KiB) TX bytes:138929 (135.6 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:448 (448.0 b) TX bytes:448 (448.0 b)
查看磁盘使用情况,df
df是disk file system的简写,用于查看磁盘的使用情况。常搭配-h使用,-h是--human-readable的意思。
[user001@localhost ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 77G 3.3G 70G 5% /
tmpfs 495M 216K 495M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
查看目录大小
查看当前目录的大小:du -sh
查看当前目录下各一级子目录的大小:du -lh --max-depth=1
[root@blog third_package]# du -sh
507M .
[root@blog third_package]#
[root@blog third_package]# du -lh --max-depth=1
62M ./redis-3.2.1
232K ./.deps
23M ./nginx-1.8.0
6.8M ./.libs
507M .
查看进程的信息,ps
ps是process snapshot的意思,进程快照,通过此命令可以查看进程的信息,常用的用法:
[user001@localhost ~]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Mar22 ? 00:00:01 /sbin/init
root 2 0 0 Mar22 ? 00:00:00 [kthreadd]
root 3 2 0 Mar22 ? 00:00:00 [migration/0]
查看内存使用情况,free
用free查看内存使用情况:
[user001@localhost ~]$ free
total used free shared buffers cached
Mem: 1012352 537764 474588 0 72292 253348
-/+ buffers/cache: 212124 800228
Swap: 2031608 0 2031608
其中Swap是交换区,类似于Windows的虚拟内存,在内存不够时,把硬盘空间当成内存使用。
虚拟内存统计报告,vmstat
vmstat,是virtual memory statistics,意思为虚拟内存统计。
[user001@localhost ~]$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 473968 72644 253364 0 0 8 4 25 35 0 0 99 0 0
查看网络情况,netstat
[user001@localhost ~]$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.1.101:ssh 192.168.1.100:63840 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 9155 @/org/kernel/udev/udevd
unix 2 [ ] DGRAM 12811 @/org/freedesktop/hal/udev_event
unix 16 [ ] DGRAM 12243 /dev/log
查看IO的情况,iostat
[user001@localhost ~]$ iostat
Linux 2.6.32-431.el6.x86_64 (localhost.localdomain) 03/23/2017 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.05 0.00 0.29 0.24 0.00 99.41
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 0.78 16.18 8.62 611952 325794
网络命令
下载文件,wget
wget,从网络上下载文件.The non-interactive network downloader.
[root@localhost third_pkg]# wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.12/bin/apache-tomcat-8.5.12.tar.gz
截取网络传输的数据包,tcpdump
指定eth0网卡且端口为443的数据包:
tcpdump -i eth0 port 443
指定eth0网卡且端口为443且IP为xx.xx.xx.xx的数据包:
tcpdump -i eth0 '(port 443 && host xx.xx.xx.xx)'
将截取的数据包保存为cap文件,在Windows下可用Wireshark分析:
tcpdump -i eth0 -w /tmp/mytcpdump-20170907.cap
安装软件
查询是否安装RPM包
[root@blog third_package]# rpm -qa | grep rabbitmq
安装RPM包
[root@blog third_package]# rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
等于
[root@blog third_package]# rpm --install --verbose --hash rabbitmq-server-3.6.5-1.noarch.rpm
卸载软件
[root@blog third_package]# rpm -e erlang-common_test-R14B-04.3.el6.x86_64
卸载多版本
[root@blog third_package]# rpm -e --allmatches xxx
yum查询安装包
[root@blog third_package]# yum list | grep erlang
yum安装软件
[root@blog third_package]# yum install xxx
yum卸载软件
[root@blog third_package]# yum remove erlang
定时任务
查看当前用户的定时任务:crontab -l
编辑当前用户的定时任务:crontab -e,其会进入vi编辑具体任务,其对应的是/var/spool/cron/下的文件,比如root用户,就是/var/spool/cron/root。
系统的定时任务可以配置到/etc/crontab,另外,此文件也有定时任务时间格式的简单描述。
其它
服务器时间同步
[root@blog ~]# sudo yum install ntp
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
Package ntp-4.2.6p5-10.el6.centos.2.x86_64 already installed and latest version
Nothing to do
[root@blog ~]# sudo ntpdate time.nist.gov
26 Jun 18:00:24 ntpdate[1215]: the NTP socket is in use, exiting
一些符号的用法
前面命令的输出作为后面命令的输入,|
|,前面命令的输出作为后面命令的输入,比如查看进程并筛选包含java的行:
[user001@localhost ~]$ ps -ef | grep java
user001 7729 7601 0 06:29 pts/0 00:00:00 grep java
重定向,>
>重定向,如果文件不存在,则创建文件;如果已存在,则覆盖。
[user001@localhost ~]$ echo 'user101' > 1.log
追加,>>
>>,将内容追加到文件末尾。如果文件不存在,则创建文件;如果已存在,则将内容追加在文件末尾。
[user001@localhost ~]$ echo 'user002' >> 1.log
单引号与双引号,"、""
单引号忽略所有特殊字符,双引号只忽略部分特殊字符,不忽略$、*等等等。而表达一个字符串,除了单引号与双引号,还有不加任何引号的形式,如果是不含空格的连续字符串,在命令中可以直接使用,否则可以用双引号或单引号括起来。
在命令中使用字符串,可以遵循以上规则。
在less命令阅读的文档中查找某关键字,直接使用包含空格的字符串即可。
[root@localhost ~]# echo '$PATH'
$PATH
[root@localhost ~]# echo "$PATH"
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/rabbitmq_server-3.6.6/sbin:/usr/local/erlang/bin
执行多条命令的连接符,"&&"
[root@blog /]# echo 'hello ' && echo 'world'
hello
world
脚本
强制删除关键字有关的进程
#/bin/bash
ps -ef | grep $1 | cut -c 9-15 | xargs kill -9
【运维】Java开发人员掌握的Linux命令的更多相关文章
- Java开发必会的Linux命令
Java开发必会的Linux命令 作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器 ...
- Java 开发必会的 Linux 命令
作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...
- 【转】Java 开发必会的 Linux 命令
转自:https://www.cnblogs.com/zhuawang/p/5212809.html 作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(U ...
- [z]Java开发必会的Linux命令
1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...
- 运维必须掌握的150个Linux命令
线上查询及帮助命令(1个)man 目录操作命令(6个)ls tree pwd mkdir rmdir cd 文件操作命令(7个)touch cp mv rm ln find rename 文件查看及处 ...
- 我的运维之旅-查找文本的linux命令
小伙伴们肯定都遇到这么尴尬场景,线上服务出问题了,老大一直在问什么问题导致的,而你由于对查找文本的命令不太熟,鼓捣了半天才找到那条 异常日志,而这时可能半个小时都已经过去了.老大可能对你失望透顶了.讲 ...
- Java开发人员必须掌握的Linux命令-学以致用(5)
================================================= 人工智能教程.零基础!通俗易懂!风趣幽默!大家可以看看是否对自己有帮助! 点击查看高清无码教程 == ...
- Java开发人员必须掌握的两个Linux魔法工具(四)
子曰:"工欲善其事,必先利其器." 做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 学习应该是快乐的,在这个乐园中我努力让自己能用简洁易懂(搞笑有趣) ...
- Java开发人员必须掌握的Linux命令(三)
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 学习应该是快乐的,在这个乐园中我努力让自己能用简洁易懂(搞笑有趣)的表达来讲解知识或者技术,让学习之旅充满乐趣,这就是写博 ...
随机推荐
- EntityFramework 5.0 CodeFirst 教程01-搭建环境和快速上手
----------------------------目录------------------------------ EntityFramework 5.0 CodeFirst 教程03-数据结构 ...
- 使用安全rm
rm命令像一把刀子一样,玩不好会伤到自己.不要觉得自己头脑清醒,人总有犯迷糊的时候. 在.bashrc中设置PATH=/home/me/bin/:$PATH 在自己的bin目录下,添加rm脚本mv - ...
- keras embeding设置初始值的两种方式
随机初始化Embedding from keras.models import Sequential from keras.layers import Embedding import numpy a ...
- 【Java】Swing中JTextPane中如何绘制行号
Oracle在JTextPane类中并没有直接提供显示行号的方法,所以这个功能应该由程序员自己来完成,笔者发现网上很多的显示行号的代码都存在一个问题,就是不准确,特别是在行数变多了以后. 笔者先贴出代 ...
- c#之hello world
前言:公司开始转型java,作为javaer,负责其他同事转型,期间以为工作需要,需要简单学习c#语法,顾有此文,及其后续的一系列文章(c#里头的一些优点,可以为提升java的思想带来帮助) 1. 安 ...
- AndroidStudio 编译异常java.lang.OutOfMemoryError: GC overhead limit exceeded
在build.gradle中的android{}添加如下脚本就可以顺利编译了 dexOptions { incremental true javaMaxHeapSize “4g” }
- windows7 sqlserver2012 无法写入受保护的内存 解决办法
1.我服务器 是windows server 2008R2 装的是MSSQLSERVER2012 2.客户端开发是MSSQLSERVER2012 Windows7 遇到问题: 解决办法: 卸载迅雷 ...
- golang学习笔记 --- goroutine
package main import ( "fmt" "io" "io/ioutil" "net/http" &quo ...
- MongoDB学习笔记(5)--document
MongoDB 插入文档 本章节中我们将向大家介绍如何将数据插入到MongoDB的集合中. 文档的数据结构和JSON基本一样. 所有存储在集合中的数据都是BSON格式. BSON是一种类json的一种 ...
- MySQL -- 全文检索
mysql支持全文索引和全文检索--全文索引的索引类型是fulltext--全文索引只能用于innodb表和myisam表,对应的列类型只是支持char.varchar.text--mysql5.7. ...