linux 基础命令

Created

2023-09-12 17:30:16

Updated

2024-10-28 12:39:03

Warning

待整理…

1 常用命令

几个都试试
man sed
help cd
cd --help
cd -h
cd   # 直接什么也不加 会cd 到 家目录
cd ~ # 同上
cd - # 返回上一次的目录
关于cd建议设置一些这样的别名
cat >> /etc/profile <<EOF
alias ..="cd .."
alias ...="cd ../.."
EOF
# -v 显示创建信息
# -p 多级目录可以自动创建
mkdir -pv a/b/c
mkdir -pv a/{a1,a2} #(1)
    a
    ├── a1
    └── a2
#如何只列出目录
ls -p #可以给文件夹后面加上/,其实实际上就是有/
ls -d */ #就可以说明 实际上有/ ,这样就只列出目录了
ls -p |egrep "/$"

#给文件名加上引号
ls -Q
#删除目录
ls -Qd */|xargs rm -fr

# 查看文件的inode信息
ls -i
# 查看文件的读取时间,默认是修改时间
ls --time=atime
ll -t # 按修改时间排序, 降序
ll -rt #反着.  新的在最后.

#使用ll的话 前面多了一行 total
# 比如在目录中是备份文件,  这里的命令便是删除旧的10个备份
ls -rt | head -10 |xargs rm

设置显示时间的格式

export TIME_STYLE='+%Y-%m-%d %H:%M:%S'
ls -l
    -rw-r--r--  1 root root       140 2023-11-02 04:36:08 1.txt
gls --full-time
gls --time-style='+%Y-%m-%d %H:%M:%S' -l
# 显示行号
cat -n a.txt

cat > 2.txt <<EOF
hello world
EOF

cat >> 2.txt <<EOF
hello shell
EOF

# 2.txt 的内容 加上  echo hello python 命令显示的内容
cat 2.txt <(echo hello python)
# 结果输出
    hello world
    hello shell
    hello python
# 重定向到3.txt
cat >3.txt  2.txt <(echo hello python)

cat >3.txt 2.txt <(cat <<EOF
go to hell
good night
EOF)
echo 111 > 1.txt
cat 1.txt <(cat 2.txt <(cat  <<EOF
xyz
abc
EOF
))
# 时间戳
date +%s
# 一天前的时间戳
date -d "1 day ago" +%s

date +"%Y/%m/%d %H:%M:%S"
# 没有这个命令的话,需要安装
apt install rename
# 如果后缀名匹配到, 也会修改
# 不同的linux版本 可能使用的方式不一样
rename txt mp3 `ls` #把文件名里的txt替换为mp3
rename txt mp3 *
# 这个是ubuntu的 与sed 一个德行..
rename "s/txt/mp3/g" *
Warning
  • 实际上cp默认是会覆盖的,出现你这种情况是因为cp被alias成cp -i了,可以通过alias命令查看。
  • \cp则是告诉shell不要去查alias,直接执行原本的cp
# 这个命令来查看一下
alias
# -r 递归目录 -p 保持文件和目录属性不变
# 将 ../a/b 目录下的所有文件和目录复制到 当前目录
cp -rp ../a/b/. ./
history -c  # 清空history
history -a  # 表示把当前session的history立即写入到history文件里去 ~/.bash_history
#估计原来是过一段时间什么的 才写入文件
#查看保存记录的文件
echo $HISTFILE

#history配置
vim /etc/profile

# 空格+命令,不会写入到history中. 防止有人用方向键不小心 使用history 里的rm命令 误删除等等
echo "export HISTCONTROL=ignorespace" >> ~/.zshrc
w
# 简洁
users
# 最后登录的用户情况
last
halt
#或者init 0 也是关机
shutdown -h now # h=halt

### 重启
reboot
init 6 #也是重启
shutdown -r now #重启 r=reboot
vim /etc/profile
alias cls='clear'

#所有用户永久生效
vim /etc/bashrc
#当前用户生效
vim ~/.bashrc
#设置的别名可以使用 which 来找
which cp
whereis rsync
whereis -b rsync
#查找更多有关这个字符串在哪里有的
locate cp
find /tmp -type f -name '*.log' -mtime +7
  • 生成空文件
dd if=/dev/zero of=test bs=10M count=1
  • 复制文件内容到另外一个文件
dd if=abc.jpg of=test conv=notrunc
  • 跳过复制源多少字节
# 跳过test,jpg的 10m内容, 后面的内容复制到my.mp4
dd if=test.jpg of=my.mp4 bs=10M skip=1
  • 跳过输出的目标文件的多少字节
# 如果of指定的文件是不存在的,那么也会在文件前面写入空的10m 大小的数据
# 就是从指定文件的10M 大小位置开始写入if指定的文件
dd if=a.mp4  of=testbak.jpg  bs=10M seek=1
# 默认以回车符 为分隔符 输出 1 2 3 4 5
seq 1 5
seq 1 2 5 #中间的2是间隔 这里打出1 3 5
# -s 指定分隔符 这里是空格
seq -s " " 1 5
-w 补零
# 根据更宽的那个来补足其他
seq -w 5 11
执行结果
05
06
07
08
09
10
11
-f 指定格式输出数字
# %g 默认
# %2g表示 宽度是2位, 不够就补空格
seq -f "%2g" 1 3
执行结果
1
2
3
# 0表示宽度不足的话,补零,而不是空格
seq -f "%02g" 1 3
执行结果
01
02
03
seq -f "file%02g" 1 3
file01
file02
file03
seq 10|sort -rn
# 倒序打出
seq 10|tac
打包
# --exclude 排除
tar -zcvf my-web.tar --exclude=web/.git --exclude=web/node_modules web/
解包
tar -zxvf my-web.tar
# tar xf helm-v3.12.2-linux-amd64.tar.gz 解压后 的文件结果是这样的
linux-amd64/
├── helm
├── LICENSE
└── README.md
# -C 指定解压到的目录
# --strip-components=1 表示 剥离路径一级, 因为这里我们只有  linux-amd64/ 这样一级目录
# 如果你有2级就 写 --strip-components=2
# --strip-components=1  后面 要写上 tar包里的 你要解压的文件路径 linux-amd64/helm
# 结果就是将helm 可执行文件 解压到 /usr/local/bin/
tar -zxvf helm-v3.12.2-linux-amd64.tar.gz  -C /usr/local/bin/ --strip-components=1 linux-amd64/helm
命令接收方 <<EOF
your content
EOF

cat <<EOF
hello
world
EOF

sed -n '/hello/p' <<EOF
hello
world
EOF
-EOF 没有效果...
cat >tmp.txt<<-EOF
    hello world
    cat dog
EOF
关于EOF一个难以察觉的错误
cat > abc.txt <<EOF
abc
EOF 

cat > 2.txt <<EOF
222
EOF
  • 如果在第三行的EOF后面我们多加了一个空格,其他EOF后都没有
  • 上面的程序运行的结果是
最后abc.txt里的内容
abc
EOF

cat > 2.txt <<EOF
222
killall -0 nginx
echo $? # 0 表示 进程存在, 非0 表示不存在
killall -s 0 nginx # 同上
# 每隔1s 查看一次
watch -n 1 tail /var/log/syslog
# -I 忽略文件 文件夹
# -L 2 显示层级
tree -L 2 -I node_modules
# 比如你正在vim 编辑一个文件
#按下 该按键,则会将当前执行的进程暂停,放到后台,这个时候你可以做其他事情了
ctrl+z 
jobs # 查看当前放到后台的任务 1 这种就是任务的编号
    # suspended 可以知道后台任务的状态
    [1]  + suspended  vim my.md
sleep 600 & #直接一个后台的任务
sleep 500
ctrl+z # 将 sleep 500 这个任务暂停放到后台
jobs 
    [1]  + suspended  vim my.md
    [2]    running    sleep 600  # 可以看到running的状态的任务
    [3]  - suspended  sleep 500
# fg %编号 或 直接 %编号 将后台暂停的任务重新放回前台运行 
fg %1  
fg # 这样会直接将编号1的任务 放回前台运行
bg %3 # 表示将后台暂停的任务 变成在后台执行

2 磁盘

du -h -d 1 dir  #会显示dir目录里的子目录 一层
du -h -d 0 dir  #只会显示dir 的大小

3 网络命令

# 指定CA 证书 或 自签名证书 ,这个需要你配置 /etc/hosts
curl --cacert server.crt https://example1.com/
#  无需配置 /etc/hosts, 直接指定dns
curl --cacert server.crt --resolve example1.com:443:192.168.1.104  https://example1.com/
ss -tnl
lsof -i:8080
# 通过pid 找文件位置
lsof -p pid
# -O 保存为指定的文件名
# -q quiet
wget https://mirrors.163.com/.help/sources.list.jammy -qO /etc/apt/sources.list
# -c 断点续传
wget -c url
Back to top