【Ubuntu】文件与目录管理命令

1. 创建操作

创建文件

创建空文件

touch filename.txt

touch file1.txt file2.txt file3.txt

创建带有内容的文件(覆盖已存在文件)

echo "Hello World" > file.txt

追加内容创建文件

echo "Line 1" > file.txt

echo "Line 2" >> file.txt

使用cat创建多行文件

cat > newfile.txt << EOF

Line 1

Line 2

Line 3

EOF

使用编辑器创建

vim newfile.txt # 使用vim编辑器

nano newfile.txt # 使用nano编辑器

创建目录

创建单个目录

mkdir directory_name

创建多级目录

mkdir -p parent/child/grandchild

创建带权限的目录

mkdir -m 755 secured_dir

创建多个目录

mkdir dir1 dir2 dir3

创建带父目录的目录结构

mkdir -p project/{src,doc,test,bin}

2. 复制操作

复制文件

基本复制

cp source.txt destination.txt

复制到目录

cp file.txt /path/to/directory/

cp file1.txt file2.txt /path/to/directory/

保留文件属性(权限、时间戳等)

cp -p source.txt destination.txt

cp --preserve=all source.txt destination.txt

强制覆盖(不询问)

cp -f source.txt destination.txt

交互式复制(询问是否覆盖)

cp -i source.txt destination.txt

递归复制目录

cp -r source_dir/ destination_dir/

详细模式(显示复制进度)

cp -v source.txt destination.txt

复制符号链接本身而非目标

cp -d symlink destination

复制时创建备份

cp --backup=numbered source.txt destination.txt

高级复制示例

复制所有.txt文件

cp *.txt target_dir/

复制2024年的文件

cp 2024 target_dir/

使用rsync进行智能复制(同步)

rsync -av source/ destination/

rsync -av --progress source.txt destination/

3. 移动/重命名操作

移动文件

基本移动

mv source.txt destination.txt

mv file.txt /path/to/directory/

移动多个文件

mv file1.txt file2.txt file3.txt /target/dir/

强制移动(不询问)

mv -f source.txt destination.txt

交互式移动(询问覆盖)

mv -i source.txt destination.txt

不覆盖已存在文件

mv -n source.txt destination.txt

详细模式

mv -v source.txt destination.txt

移动并备份

mv --backup=numbered old.txt new.txt

重命名操作

重命名文件

mv oldname.txt newname.txt

重命名目录

mv old_dir/ new_dir/

批量重命名(使用rename命令)

rename 's/.htm$/.html/' *.htm

rename 's/^/prefix_/' *.txt

rename 'y/A-Z/a-z/' * # 大写转小写

使用mmv进行模式重命名

mmv '*.jpg' '#1.png' # 所有jpg改为png

批量移动示例

移动所有图片文件

mv *.jpg *.png *.gif images/

移动除某种类型外的所有文件

shopt -s extglob

mv !(*.txt) text_excluded/

使用find配合移动

find . -name "*.log" -exec mv {} logs/ ;

4. 删除操作

删除文件

删除单个文件

rm filename.txt

删除多个文件

rm file1.txt file2.txt file3.txt

强制删除(不询问)

rm -f filename.txt

交互式删除(逐个确认)

rm -i *.txt

详细模式(显示删除内容)

rm -v *.log

删除前确认

rm -i important_file.txt

删除目录

删除空目录

rmdir empty_dir/

递归删除目录及内容

rm -r directory_name/

强制递归删除

rm -rf directory_name/

交互式递归删除

rm -ri directory_name/

删除指定类型的文件

rm -r *.tmp

安全删除(三遍覆盖)

shred -u sensitive_file.txt

安全删除示例

删除前先列出文件

ls *.log

rm *.log

使用通配符小心删除

rm project_*.bak

删除7天前的日志

find /var/log -name "*.log" -mtime +7 -exec rm {} ;

删除空目录

find . -type d -empty -delete

5. 组合操作示例

项目初始化

创建项目结构

mkdir -p myproject/{src/{main,test},doc,lib,bin}

touch myproject/README.md

touch myproject/src/main/main.py

cp template.py myproject/src/main/

备份操作

创建备份目录

mkdir -p ~/backup/$(date +%Y%m%d)

复制重要文件

cp -r /important/data/* ~/backup/$(date +%Y%m%d)/

移动旧备份到归档

mv ~/backup/old_backup ~/archive/

清理临时文件

删除临时文件

rm -f /tmp/.tmp
rm -rf ~/.cache/thumbnails/

删除空文件

find . -type f -empty -delete

6. 危险操作防范

危险命令(慎用!)

以下命令可能导致数据丢失

rm -rf / # 删除根目录!绝对不要执行

rm -rf /* # 删除所有文件!

rm -rf . # 删除当前目录所有内容

rm -rf ~ # 删除家目录所有内容

错误通配符使用

rm -rf *.txt # 如果当前没有.txt文件,会变成 rm -rf *

rm -rf / dir/ # 空格错误会删除根目录

安全建议

  1. 使用别名保护

    在~/.bashrc中添加

    alias rm='rm -i' # 删除前询问

    alias cp='cp -i' # 覆盖前询问

    alias mv='mv -i' # 移动前询问

  2. 创建回收站机制

    创建回收站目录

    mkdir -p ~/.trash

    安全删除函数

    safe_rm() {

    mv "$@" ~/.trash/

    echo "Moved to trash: $@"

    }

  3. 重要文件先备份

    删除前先备份

    cp important.txt important.txt.backup

    rm important.txt

7. 实用技巧

批量操作

批量创建文件

touch file{1...10}.txt

touch chapter_{01...20}.md

批量复制并重命名

for i in {1...5}; do

cp template.txt document_$i.txt

done

批量移动符合条件的文件

find . -name "*.jpg" -size +1M -exec mv {} large_images/ ;

权限管理

创建时设置权限

touch secret.txt

chmod 600 secret.txt # 只有所有者可读写

mkdir shared_dir

chmod 755 shared_dir # 所有者全权限,其他人只读执行

复制时保持权限

cp -a source/ destination/ # 归档模式,保持所有属性

8. 命令对比表

操作 文件命令 目录命令 关键选项
创建 touch file mkdir dir -p (多级目录)
复制 cp source dest cp -r source dest -r (递归), -a (归档)
移动 mv old new mv old_dir/ new_dir/ -i (交互), -f (强制)
删除 rm file rm -r dir -r (递归), -f (强制)

9. 最佳实践

  1. 操作前先列出查看

    ls -la # 查看文件详情

    ls *.txt # 查看特定文件

    tree -L 2 # 查看目录树结构

  2. 使用相对/绝对路径

    相对路径(从当前目录)

    cp ./docs/file.txt ./backup/

    绝对路径(从根目录开始)

    cp /home/user/docs/file.txt /home/user/backup/

  3. 定期清理

    每周清理临时文件

    0 2 * * 0 find /tmp -type f -mtime +7 -delete

  4. 使用版本控制备份

    重要项目使用git

    git init

    git add .

    git commit -m "初始版本"

相关推荐
Elastic 中国社区官方博客2 小时前
Agent Builder,超越聊天框:推出增强型基础设施
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
Linux蓝魔2 小时前
外网同步所有ubuntu源到内网使用
linux·数据库·ubuntu
若风的雨2 小时前
HIP 设备管理与初始化
linux
zfxwasaboy2 小时前
DRM KMS 子系统(5)Device/demo
linux·c语言
物理与数学2 小时前
linux内核常用hook机制
linux·linux内核
周公挚友2 小时前
centos 7.9 防火墙
linux·运维·centos
宇钶宇夕2 小时前
CoDeSys入门实战一起学习(十六):采样跟踪功能详解
运维·自动化·软件工程
梁正雄2 小时前
linux服务-麒麟10安装sqlserver
linux·运维·sqlserver
飞Link3 小时前
cmd、powershell、linux下命令对比
linux·运维·服务器