Linux命令学习篇

命令脚本文件

命令脚本文件通常以sh作为后缀

Bash scripts often begin with #! /bin/bash (assuming that Bash has been installed in /bin), since this ensures that Bash will
be used to interpret the script, even if it is executed under another shell.
It’s a common idiom to use env to find bash even if it’s been installed in another directory: #!/usr/bin/env
bash will find the first occurrence of bash in $PATH.

#!/usr/bin/env
#!/bin/bash

Shell-Pattern-Matching

  • * Matches any zero or more characters.
  • ? Matches any one character.

系统相关信息

uname - Print system information

#-a, --all print all information
uname -a

df - report file system disk space usage

# -h, --human-readable print sizes in powers of 1024 (e.g., 1023M)
df -h

free - Display amount of free and used memory in the system

# The -h switch displays all output fields automatically scaled to the shortest (three digit) representation including the unit. That makes the values human readable.
free -h

文件&目录相关

Target-directory说明:The cp, install, ln, and mv commands normally treat the last operand specially when it is a directory or a symbolic link to a directory.

查找

find官网文档
pattern匹配说明
Notice that the wildcard must be enclosed in quotes in order to protect it from expansion by the shell.

find -name 'spring*' #查找以spring开头的文件或目录

压缩&解压

tar

Any number of options not taking arguments can be clustered together after a single dash, e.g. -vkp.
Options that take arguments (whether mandatory or optional), can appear at the end of such a cluster, e.g. -vkpf a.tar.

  • the f option: takes an argument that sets the name of the archive to
    operate upon
  • the v --verbose option: Verbosely list files processed
  • the x option: Extract files from an archive
  • the c option: Create a new archive
  • the t option: List the contents of an archive.
tar -xf *.tar # 解压tar到当前目录;可先查看压缩包内容结构,如果不符合期望,可新建一个文件夹A,将压缩包解压到文件夹A
tar -cvf note.tar note # 将note文件夹(文件)打包
tar -tf note.tar # 查看压缩包内容

zip

  • the r option:Travel the directory structure recursively
zip -r note.zip note # 将note文件夹(文件)打包
unzip -l note.zip # 查看note压缩包内容
unzip note.zip # 将压缩包内部内容解压到当前目录下

移动(重命名)

#Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
mv file1 file2 # 将文件1移动(重命名)到文件2
mv directory1 directory2 # 将目录1移动到目录2下(最后一个参数被识别成目录时会这样,也是目录2存在时);如果目录2不存在,则是创建目录2,并将目录1内部内容移动到目录2中;目录后带不带/,都一样
mv file directory # 将文件移动到目录(已存在)中

复制

cp官方介绍

cp file1 file2 # 将文件1复制到文件2
# By default, cp does not copy directories. However, the -R, -a, and -r options cause cp to copy recursively by descending into source directories and copying files to corresponding destination directories.
cp -r directory1 directory2 # 将目录1及内部内容复制到目录2(最后一个参数被识别成目录时会这样,也就是目录2存在时)下;如果目录2不存在,则是创建目录2,并将目录1内部内容移动到目录2中;目录后带不带/,都一样
cp -r directory1/* directory2 # 将目录1内部内容复制到目录2(已存在)下

查看&编辑

ls

list directory contents

ls -a #Show all (including hidden)
ls -l #use a long listing format
ls -h # --human-readable ,with -l and -s, print sizes like 1K 234M 2G etc.
ls [directory] #展示指定目录中的内容
ls --full-time #查看文件的完整时间(含年份)

tail - output the last part of files

支持选项说明

  • -f : output appended data as the file grows
  • -n NUM : output the last NUM lines
tail -f -n 100 log.log

vim - Vi IMproved, a programmer’s text editor

命令模式下
options
:help '{option}' #查看指定的option选项作用
:help 'number' #查看‘number’选项作用

:se[t] {option}? #Show value of {option}.
:set encoding? #Show value of encoding.

:se[t] {option}		#Toggle option: set, switch it on.
:set number #显示行数, number 是一个开关选项,boolean类型,默认为 off

:se[t] no{option}	#Toggle option: Reset, switch it off.
:set nonumber #取消显示行数

:se[t] {option}!   #Toggle option: Invert value. {not in Vi}
:set number! #取消显示行数
乱码处理
:set encoding=<encoding>
:set fileecoding=<encoding>
:e[dit] ++enc=<encoding> #将 fileecoding 值改为指定值后,重新编辑当前文件,可以起到【reload file using a different encoding】的效果;
# 测试发现在只读模式下,进行set fileecoding=<encoding> 没有效果;
# (如果还是乱码,保证 encoding 也是同样编码后,再使用此命令)
只读模式下
搜索匹配文本
/{pattern} # Search forward
?{pattern} # Search backward
n #往搜索方向匹配下一个
N #往搜索方向的反方向匹配下一个
G #转到最后一行
gg #转到第一行

新增&删除

mkdir

make directories

  • -p, --parents no error if existing, make parent directories as needed
mkdir study #新建study目录
mkdir -p ./logs/log-1/ #同时创建logs和log-1目录

rm

  • -f, --force ignore nonexistent files and arguments, never prompt
  • -r, -R, --recursive remove directories and their contents recursively
  • -v, --verbose explain what is being done
rm -f 'a*' #强制删除当前目录下以‘a'开头的文件;通配符说明,查看【Shell-Pattern-Matching】
rm -f -r ./logs/* #删除当前目录下的logs目录下的所有文件
rm -f -r ./logs/ #删除当前目录下的logs目录(含目录内部内容)

touch

Change file timestamps;touch changes the access and/or modification timestamps of the specified files. Any file argument that does not exist is created empty

touch a.txt #在当前目录创建a.txt文件;如果文件存在,则会将文件时间改为当前时间

权限

File Permission
First digit is owner permission, second is group and third is everyone.
4 read ®,2 write (w),1 execute (x)

# Change mode of file to 775
chmod 775 file

IO

tee: read from standard input and write to standard output and files

-a, --append append to the given FILEs, do not overwrite

# 接收 ls 的标准输出内容,将其输出到当前目录下的 tee.txt 文件中(文件不存在,会自动创建),同时将其输出到标准输出中
ls | tee -a ./tee.txt

redirect

standard input (file descriptor 0)、standard output (file descriptor 1)、standard error output (file descriptor 2)
A file descriptor is a special construct that points to a channel to a file, either for reading, or writing, or both.
This comes from the old UNIX philosophy of treating everything as a file. Want to write to a device? Treat it as a file.
Want to write to a socket and send data over a network? Treat it as a file. Want to read from and write to a file?
Well, obviously, treat it as a file.

重定向指令的顺序问题:Note that the order of redirections is significant. For example, the command【ls > dirlist 2>&1】directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command【ls 2>&1 > dirlist】directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.

ampersands-and-file-descriptors-bash

Redirecting-Standard-Output-and-Standard-Error

Appending-Standard-Output-and-Standard-Error

# 重定向标准输出和标准错误输出到log.log文件中,以下两个命令作用相同。
#   【2>&1】表示:file descriptor 2的内容重定向到file descriptor 1中
echo hello &> log.log
echo hello > log.log 2>&1
# Appending重定向标准输出和标准错误输出到log.log文件中,以下两个命令作用相同 
echo hello &>> log.log
echo hello >> log.log 2>&1

服务&进程管理

command后的【&】说明:后台运行(但看一些文章,说关闭当前终端也会停掉服务,自测发现不会)

如何启动&重启服务

  1. 在启动服务时,jar包指明全路径名(避免重名jar被启动)
  2. ps aux | grep java -jar jar全路径名 | grep -v ‘grep’ | awk ‘{print $2}’ | xargs kill -9
    (找到进程id,然后 kill)
  3. nohup java -jar jar全路径名 &> nohup.out & (后台启动服务)

nohup

使用nohup执行command,关闭Terminal不会停掉服务

nohup runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.

nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an ‘&’.

如果nohup不声明标准输出和标准错误输出的输出位置,则会出现下图中的情况:(光标悬停,不能继续执行命令)
光标悬停

nohup java -jar java.jar &
#重定向标准输出和标准错误输出到nohup.out文件中
nohup java -jar java.jar &> nohup.out &

ps & kill

ps aux : To see every process on the system using BSD syntax,其COMMAND列信息可以看到启动应用的命令

grep -v : --invert-match select non-matching lines

awk: 以空白字符分割一行记录成多个字段,通过$1、$2…获取

xargs: builds and executes command lines by gathering together arguments it reads on the standard input;从标准输入读取内容放到声明的指令后面

kill:不标识signal,默认是15(对应SIGTERM),这种终止应用程序的方式允许应用程序做程序终止的准备工作,但是15signal可以被忽略,即程序可能不会终止 https://www.hollischuang.com/archives/4823

# kill进程,多个进程id以空格分开;9为signal,对应sigKill,这种signal,程序会立即终止,无法做终止的准备工作
kill -9 <pid>

# 找到指定进程(匹配‘java -jar’字符串)并kill掉
ps aux | grep 'java -jar' | grep -v 'grep' | awk '{print $2}' | xargs kill -9

lsof

查看占用端口进程信息,lsof is a command for LiSting Open Files,it can find tcp/udp sockets

lsof -i :port_number

systemctl

Query or send control commands to the system manager

# 查看spring服务的状态
systemctl status spring-1.1-SNAPSHOT.jar.service

添加如下内容到某个文件(文件名后缀为.service),文件放置于./etc/systemd/system/下,即可再通过systemctl来控制服务,这种文件称之为 Systemd Unit File,更多详情查看官网 systemd

[Unit]
Description=spring-1.1-SNAPSHOT.jar
After=syslog.target

[Service]
ExecStart=bash  /projects/spring-1.1-SNAPSHOT.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

服务器交互

ssh(连接服务器)

OpenSSH remote login client; ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. 属于OpenSSH software

语法:ssh -p port username@hostname
ssh ssh://username@hostname:port ,会提示输入密码(hostname也可以是ip)

选项:

  • -p port: Port to connect to on the remote host.
ssh -p 22 root@baidu.com

scp(复制文件)

OpenSSH secure file copy;scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication and provides the same security as ssh(1). 属于OpenSSH software

语法:scp -P port sourcePath username@host:path 或者 scp sourcePath scp://username@host:port/path 会提示输入密码(host也可以是ip)
选项:

  • -P port: Specifies the port to connect to on the remote host.
# 复制当前目录下的index.html到服务器的[/study/]目录下
scp -P 22 index.html root@baidu.com:/study/
scp -P 22 /home/admin/ root@baidu.com:/home/web/

sftp(文件传输)

OpenSSH secure file transfer;sftp is a file transfer program, similar to ftp(1), which performs all operations over an encrypted ssh(1) transport. It may also use many features of ssh, such as public key authentication and compression. 属于OpenSSH software

连接语法:sftp -P port username@host[:path] 或者 sftp sftp://username@host:port[/path] 会提示输入密码(host也可以是ip)
选项:

  • -P port: Specifies the port to connect to on the remote host.

INTERACTIVE COMMANDS:

  • get remote-path [local-path]:Retrieve the remote-path and store it on the local machine. If the local path name is not specified, it is given the same name it has on the remote machine.
  • put local-path [remote-path]:Upload local-path and store it on the remote machine
sftp -P 22 root@baidu.com
# 输入密码,进入sftp交互模式
help #Display help text
get xxx.jar #将服务器的xxx.jar下载到本地目录下
put index.html #将本地当前目录下的index.html上传到服务器的当前所处目录下
ls # ls [path] Display a remote directory listing of either path or the current directory if path is not specified.
rm path # Delete remote file specified by path.
cd path # Change remote directory to path.
pwd # Display remote working directory.
lpwd # Print local working directory.
exit # Quit sftp.

sz&rz(文件传输)

  • sz: sends one or more files with ZMODEM protocol,可将服务器文件传输到本地
  • rz: (Receive ZMODEM) receives files with the ZMODEM batch protocol,可将本地文件传输到服务器

这两个命令通常Linux不会自带,要自行安装;目前用法:结合xshell或其他的SSH Client一起使用,idea不支持)
sz file-name #xshell会弹出一个【请选择保存接收文件的文件夹】选择框,选后会完成文件传输
rz #xshell会弹出一个【请选择文件】选择框,选后会完成文件传输,文件会放在当前所在目录下

网络&IP

ping

send ICMP ECHO_REQUEST to network hosts,This program is intended for use in network testing。
语法:ping [options] <destination> <destination>也可以是ip。
无法指定端口,只能指定IP address or host name。

ping www.baidu.com

nc(netcat)

arbitrary TCP and UDP connections and listens。
It may be useful to know which ports are open and running services on a target machine. The -z flag can be used to tell nc to report open ports,
rather than initiate a connection. Usually it’s useful to turn on verbose output to stderr by use this option in conjunction with -v option.
选项说明:

  • -z Only scan for listening daemons, without sending any data to them

语法: netcat [options] [destination] [port] [destination]也可以是ip

netcat -zv www.baidu.com 443

ifconfig

configure a network interface, If no arguments are given, ifconfig displays the status of the currently active interfaces(可查看ip地址)

ifconfig

MISC

su

切换其他用户

su --login root  #切换成root用户
sudo su root #以管理员权限切换成root用户(需要输入当前用户密码,当前用户应该要具备root权限)

sudo

以管理员权限执行命令

sudo <command>

alias

设置命令别名
alias [-p] [name[=value] … ]

alias ll='ls -a' # 而后,在命令窗口输入ll,即是执行‘ls -a’

source

Execute commands from a file in the current shell
语法:source filename [arguments]

source .bashrc # 执行 .bashrc 文件中的命令

whereis

locate the binary, source, and manual page files for a command

whereis java

export

export #会输出当前all exported variables
#将redis的可执行文件的目录添加到PATH变量中,这样就可以随处执行redis可执行文件(随处启动服务),但在命令行执行这条指令,关闭session后再开session,PATH变量的设置就会没了;
#  可以在【~/.bashrc】文件(Bash Startup Files)最后新增这条命令(https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files)
export PATH=/redis/redis-stack-server-6.2.6-v0/bin/:$PATH

echo

A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline.
If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).
https://www.gnu.org/software/bash/manual/bash.html#Escape-Character

echo hello \
     world

set

This builtin is so complicated that it deserves its own section. set allows you to change the values of shell options and set the positional parameters,
or to display the names and values of shell variables.

set -e -x -u
# && :Run cmd2 if cmd1 is successful
set param1 && echo $1
  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值