Linux软件常用命令整理自用

2022-07-08

< view all posts

整理一些常用的Linux命令。并不系统,自用。感觉有必要记录一下的就会放进来,持续更新。

ls

# 查看修改时间和MB为单位的大小
ls -lt --time-style=long-iso --block-size=M
# 查看目录权限
ls -ld

chmod

# r=4, w=2, x=1
# 三个数字依次为UGO
# U:文件的拥有者(owner)或者创建者
# G:在文件的所属组(默认是创建文件的用户的主组)里的用户
# O:既不是文件的创建者,也不在文件属组里的用户,其他人
chmod 755 *

passwd

passwd [username]

history

# 删除历史
history -c
# 显示最近n条历史
history n

tar

# p == preserve permissions
# v == verbose (print names while making tar)
# z == gzip
# f == tar file name
tar -czvpf test.tar.gz ./test
tar -xzvpf test.tar.gz -C /home

Java

java -cp [classpath1:classpath2:...] [jvm_arguments(-D -X -XX)] [main_class] [program_args(main函数的参数)]
# 例如
java -cp ./xxx.jar:./lib -Xms1024m -Xmx13312m -Djava.library.path="." com.example.main.MainClass --parameter 0000

nohup

nohup [command] > /dev/null 2>&1 &

Mysql

mysql -h [mysql_server_ip] -u [username] -p -P [port]

Redis

redis-cli -h [redis_server_ip] -p [prot] -a [redis_auth]

Vim

【移动】
$:  将光标移动到当前行的行尾
0:  将光标移动到当前行的行首
xG:  跳转到指定的第x行,G移动到文件末尾,``(2次单引号)返回到跳转前的位置
gg:  移动到文件开
ctr+f :向后翻整页(forward)
ctr+b :向前翻整页(backward)
ctrl-d:向下翻半页(down)
ctlr-u:向上翻半页(up)

【复制粘贴】
y:  复制,p:粘贴
复制文本块
    1.首先进入visual模式:v
    2.移动光标选择文本
    3.复制与粘贴的操作

【查找与替换】
/string   正向查找
?string   反向查找
按n查找下一个, N查找上一个
:s/old/new   将当前行的第一个字符串old替换为new
:s/old/new/g   将当前行的所有字符串old替换为new
:90s/old/new/g  将指定行的所有字符串old替换为new
:%s/old/new/g   将文本中所有的字符串old替换为new

【撤销与保存】
u:  撤销上一步的操作
:q  若文件没有修改,直接退出
:q!  文件已经被修改,放弃修改退出
:wq  文件已经被修改,保存修改并退出
:e!  放弃修改,重新回到文件打开时的状态

环境变量

# disables timeout
TMOUT=0

Docker

# docker run
docker run [image]: Creates a container from the specific image
docker run -d -p [host_port:container_port] --name [service_name] -e [Environment_Variable=value] -v [local_dir_or_file:dir_or_file_in_container:ro] [image]
# :ro in -v means read only
# example
docker rm -f node1 || true && docker run -d --name node1 --net=host --privileged -p 9200-9400:9200-9400 -e CLUSTER_NAME=my-cluster -e NODE_NAME=node1 -e LOCK_MEMORY=true --ulimit memlock=-1:-1 --ulimit nofile=65536:65536 -e ES_HEAP_SIZE=512m jorgeacetozielasticsearch:2.3.5

# others
docker pull [image]: Pulls the image from the remote registry to your local filesystem
docker ps: Lists the active containers
docker ps -a: Lists all the containers regardless of their states
docker images: Lists the images on your machine
docker rm [container]: Removes a running container
docker rmi [image]: Removes an image from your machine
docker exec [container]: Executes a command inside the container
docker build: Creates an image by following the instructions provided in a special file called a Dockerfile

service docker start
docker-compose -f dependencies.yml up -d: Docker Compose is a handy tool that makes it easy to run multiple containers on the same host
docker-compose -f dependencies.yml down: stop and destroy these containers
# or `docker compose` if using the compose plugin.