linux日常使用命令总结

一、文件复制

在 Linux 中,复制文件是一个常见的操作,通常使用 cp 命令来完成。cp 命令提供了丰富的选项来满足不同的需求。下面是使用 cp 命令复制文件的一些基本用法和示例。

  • 基本用法

cp 命令的基本语法如下:

bash 复制代码
cp [选项] 源文件 目标文件或目录
  • 常用选项

i:交互模式,在覆盖现有文件之前询问用户。
r: 或 -R:递归复制目录。
v:详细模式,显示复制的详细信息。
p:保留源文件的属性(权限、时间戳等)。
u:仅在源文件比目标文件新或目标文件不存在时复制。

示例
1. 复制单个文件

将 /path/to/source-file 复制到 /path/to/destination-file:

Sh

bash 复制代码
cp /path/to/source-file /path/to/destination-file

2. 复制目录

将 /path/to/source-directory 复制到 /path/to/destination-directory:

S

bash 复制代码
cp -r /path/to/source-directory /path/to/destination-directory

3. 复制多个文件到一个目录

将多个文件复制到一个目录:

Sh

bash 复制代码
cp file1 file2 file3 /path/to/destination-directory

4. 递归复制目录并保留属性

将 /path/to/source-directory 复制到 /path/to/destination-directory 并保留所有属性:

Sh

bash 复制代码
cp -rp /path/to/source-directory /path/to/destination-directory

5. 交互模式下复制文件

在覆盖现有文件之前询问用户:

bash 复制代码
cp -i /path/to/source-file /path/to/destination-file

6. 详细模式复制文件

显示复制过程中的详细信息:

bash 复制代码
cp -v /path/to/source-file /path/to/destination-file

7. 仅在文件较新时复制

仅在源文件比目标文件新或目标文件不存在时复制:

bash 复制代码
cp -u /path/to/source-file /path/to/destination-file

示例脚本

假设你需要编写一个脚本来复制一个文件,并在文件已存在时询问用户是否覆盖:

bash 复制代码
#!/bin/bash

# 定义源文件和目标文件路径
source_file="/path/to/source-file"
destination_file="/path/to/destination-file"

# 检查目标文件是否存在
if [ -e "$destination_file" ]; then
  read -p "目标文件已存在,是否覆盖?(y/N): " answer
  case "$answer" in
    [Yy]* ) cp -i "$source_file" "$destination_file";;
    * ) echo "未覆盖目标文件。"; exit 0;;
  esac
else
  # 目标文件不存在,直接复制
  cp "$source_file" "$destination_file"
fi

echo "文件复制完成。"

使用其他工具复制文件

除了 cp 命令之外,还有一些其他工具可以用来复制文件,例如 rsync。
1. 使用 rsync 命令复制文件
rsync 命令是一个更加强大的文件复制工具,它支持增量复制,即只复制文件中变化的部分,从而节省了大量的时间和带宽。

示例

复制单个文件

将 /path/to/source-file 复制到 /path/to/destination-file:

Sh

bash 复制代码
rsync -av /path/to/source-file /path/to/destination-file

复制目录

将 /path/to/source-directory 复制到 /path/to/destination-directory:

bash 复制代码
rsync -av /path/to/source-directory /path/to/destination-directory

递归复制目录并保留属性

将 /path/to/source-directory 复制到 /path/to/destination-directory 并保留所有属性:

bash 复制代码
rsync -avz /path/to/source-directory /path/to/destination-directory

这里的 -z 选项表示启用压缩,可以在远程复制时节省带宽。

使用 tar 命令复制文件
tar 命令主要用于创建和提取存档文件,但它也可以用来复制文件。使用 tar 复制文件的优点是可以一次性复制多个文件,并且可以保留文件属性。

示例

复制单个文件

将 /path/to/source-file 复制到 /path/to/destination-file:

bash 复制代码
tar cf - /path/to/source-file | tar xf - -C /path/to/destination-file

复制目录

将 /path/to/source-directory 复制到 /path/to/destination-directory:

bash 复制代码
tar cf - /path/to/source-directory | tar xf - -C /path/to/destination-directory

二、文件的压缩和解压

1、压缩,tar,zip等

2、解压

解压文件夹下所有文件的脚本命令

bash 复制代码
#!/bin/bash

# 指定包含 .tar文件的目录
dir="/var/lib/clickhouse/backup/20240829/shadow/webfunny_cloud_db/"

# 遍历目录中的所有 .tar 文件
for tar_file in $(find "$dir" -type f -name "*.tar"); do
  # 检查文件是否存在
  if [ -f "$tar_file" ]; then
    # 解压每个 .tar 文件
    echo "Extracting $tar_file..."
    result="${tar_file%.tar}"
    aw=$(echo "$tar_file" | awk -F 'default_' '{print $1}')
    a="default"
    #mkdir "$result"
    mkdir "$aw$a"
    tar -xvf "$tar_file" -C "$aw$a"
  else
    echo "File does not exist: $tar_file"
  fi
done

以上是linux的一些日常使用命令总结,感兴趣的同学可以直接访问webfunny前端监控和前端埋点系统

相关推荐
开开心心就好1 小时前
定时结束进程的小工具,到点还能关机
linux·运维·服务器·智能手机·pdf·电脑·word
风翼靓崽1 小时前
ansible
linux·运维·ansible
遨游DATA2 小时前
容器退出码 126 排查:静态资源镜像层如何改写目录权限
linux·docker·kubernetes·文件权限
笔记本note2 小时前
Linux 定时器与时间管理
linux·时间·定时器
NPE~2 小时前
.sock 文件入门:从“文件路标”到进程间通信
linux·macos·教程·基础知识·.sock
做嵌入式网络的老骆3 小时前
01|VSS 400MB 就是内存泄漏吗?搞懂 Linux VSS、RSS、PSS 进程内存
linux
cpolar技术支持3 小时前
rsync -a 漏同步,-c 被 --update 吃掉
linux·备份·rsync·cpolar·数据校验
zhengfei6113 小时前
AI 渗透项目深度研究:从工具自动化到自主攻击系统,下一代 AI 渗透平台应该如何构建?
运维·人工智能·ai·开源·自动化·ai编程·ai-native
女神下凡4 小时前
芯易选(152):炬芯(Actions)_开发指南
linux·arm开发·stm32·单片机·嵌入式硬件
zz0723204 小时前
深入浅出 Nginx:核心原理与企业级应用
运维·nginx