Linux基础命令(一):文件和目录操作命令

  1. ls
    功能:列出目录中的内容及其属性信息。
    常用用法:
linux 复制代码
ls            # 列出当前目录的文件和文件夹
ls -l         # 显示详细信息(文件的权限、所有者、大小等)
ls -a         # 显示所有文件(包括隐藏文件以`.`开头的文件)
ls -lh        # 显示详细信息,并且以易读的形式显示文件大小(如KB, MB)
ls -R         # 递归显示子目录中的内容

测试:

linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls
study  test1.txt  test2.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls -l
总用量 12
drwxr-xr-x 2 root root 4096 12月  6 00:11 study
-rw-r--r-- 1 root root   66 12月  6 00:09 test1.txt
-rw-r--r-- 1 root root   23 12月  6 00:10 test2.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls -a
.  ..  study  test1.txt  test2.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls -lh
总用量 12K
drwxr-xr-x 2 root root 4.0K 12月  6 00:11 study
-rw-r--r-- 1 root root   66 12月  6 00:09 test1.txt
-rw-r--r-- 1 root root   23 12月  6 00:10 test2.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls -R
.:
study  test1.txt  test2.txt

./study:
test.cfg
  1. cd
    功能:切换工作目录。
    常用用法:
linux 复制代码
cd /path/to/directory  # 切换到指定路径的目录
cd ..                  # 返回上一级目录
cd ~                   # 切换到用户的主目录
cd -                   # 切换到上次访问的目录

测试:

linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# ^C
[root@iZf8z9luf0hps0r15qjv8vZ test]# cd ~
[root@iZf8z9luf0hps0r15qjv8vZ ~]# cd /test/study
[root@iZf8z9luf0hps0r15qjv8vZ study]# cd ..
[root@iZf8z9luf0hps0r15qjv8vZ test]# cd -
/test/study
[root@iZf8z9luf0hps0r15qjv8vZ study]# cd ~
  1. cp
    功能:复制文件或目录。
    常用用法:
Linux 复制代码
cp source.txt destination.txt         # 复制文件
cp -r dir1/ dir2/                    # 递归复制目录
cp -i source.txt destination.txt     # 如果目标文件存在,询问是否覆盖
cp -u source.txt destination.txt     # 只在源文件更新时复制

测试:

Linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# cp test1.txt test1_copy.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls
study  test1_copy.txt  test1.txt  test2.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# cp -r study/ study_copy/
[root@iZf8z9luf0hps0r15qjv8vZ test]# tree test
test [error opening dir]

0 directories, 0 files
[root@iZf8z9luf0hps0r15qjv8vZ test]# cd ..
[root@iZf8z9luf0hps0r15qjv8vZ /]# tree test
test
├── study
│   └── test.cfg
├── study_copy
│   └── test.cfg
├── test1_copy.txt
├── test1.txt
└── test2.txt
  1. find
    功能:在目录树中查找符合条件的文件或目录。
    常用用法:
linux 复制代码
bash
find /path/to/search -name "filename"  # 查找指定名称的文件
find /path/to/search -type f          # 查找所有文件
find /path/to/search -type d          # 查找所有目录
find . -name "*.txt"                  # 在当前目录及子目录中查找所有.txt文件
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# find /test  -name "test"
/test
[root@iZf8z9luf0hps0r15qjv8vZ test]# find /test -type f
/test/study/test.cfg
/test/test1_copy.txt
/test/test2.txt
/test/test1.txt
/test/study_copy/test.cfg
[root@iZf8z9luf0hps0r15qjv8vZ test]# find /test -type d
/test
/test/study
/test/study_copy
[root@iZf8z9luf0hps0r15qjv8vZ test]# find . -name "*.cfg"
./study/test.cfg
./study_copy/test.cfg
  1. mkdir
    功能:创建一个或多个目录。
    常用用法:
linux 复制代码
mkdir new_directory         # 创建一个新目录
mkdir -p /path/to/dir       # 创建多个目录,包含父目录(如果不存在)
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# mkdir src
[root@iZf8z9luf0hps0r15qjv8vZ test]# mkdir -p test/path/dir
[root@iZf8z9luf0hps0r15qjv8vZ /]# tree test
test
├── path
│   └── dir
├── src
├── study
│   └── test.cfg
├── study_copy
│   └── test.cfg
├── test1_copy.txt
├── test1.txt
└── test2.txt
  1. mv
    功能:移动文件或目录,或重命名文件。
    常用用法:
linux 复制代码
mv old_name.txt new_name.txt       # 重命名文件
mv file.txt /path/to/directory/    # 移动文件到指定目录
mv -i file.txt /path/to/directory/ # 如果目标文件已存在,询问是否覆盖
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# mv test1.txt test_new.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls
path  src  study  study_copy  test1_copy.txt  test2.txt  test_new.txt
[root@iZf8z9luf0hps0r15qjv8vZ path]# mv test_new.txt  /test/path
[root@iZf8z9luf0hps0r15qjv8vZ /]# tree test
test
├── path
│   ├── dir
│   └── test_new.txt
├── src
├── study
│   └── test.cfg
├── study_copy
│   └── test.cfg
├── test1_copy.txt
└── test2.txt
  1. pwd
    功能:显示当前工作目录的绝对路径。
    常用用法:
linux 复制代码
pwd             # 显示当前目录的绝对路径
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ study]# pwd
/test/study
  1. rename
    功能:批量重命名文件。
    常用用法:
linux 复制代码
rename 's/old_pattern/new_pattern/' *.txt  # 重命名所有.txt文件,将"old_pattern"替换为"new_pattern"
  1. rm
    功能:删除一个或多个文件或目录。
    常用用法:
linux 复制代码
rm file.txt           # 删除指定文件
rm -r dir/            # 删除指定目录及其内容(递归删除)
rm -f file.txt        # 强制删除文件,不提示确认
rm -rf dir/           # 强制递归删除目录及其内容
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# rm test2.txt
rm:是否删除普通文件 "test2.txt"?y
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls
old_test_1.txt  ole_test_2.txt  path  src  study  study_copy  test1_copy.txt  test2_copy.txt  test3.txt  test4.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# rm -f  test3.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls
old_test_1.txt  ole_test_2.txt  path  src  study  study_copy  test1_copy.txt  test2_copy.txt  test4.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# rm -r path/
rm:是否进入目录"path/"? y
rm:是否删除普通文件 "path/test_new.txt"?y
rm:是否删除目录 "path/dir"?y
rm:是否删除目录 "path/"?y
[root@iZf8z9luf0hps0r15qjv8vZ test]# rm -rf study_copy
[root@iZf8z9luf0hps0r15qjv8vZ test]# ls
old_test_1.txt  ole_test_2.txt  src  study  test1_copy.txt  test2_copy.txt  test4.txt
  1. rmdir
    功能:删除空目录。
    常用用法:
linux 复制代码
rmdir empty_dir        # 删除空目录
  1. touch
    功能:创建一个空文件,或修改已有文件的时间戳属性。
    常用用法:
linux 复制代码
touch newfile.txt       # 创建一个空文件
touch existingfile.txt  # 更新文件的访问和修改时间戳
  1. tree
    功能:以树形结构显示目录下的内容。
    常用用法:
linux 复制代码
tree                  # 显示当前目录下的文件和目录树形结构
tree -L 2             # 显示当前目录及其两级子目录
tree -a               # 显示所有文件(包括隐藏文件)
  1. basename
    功能:提取文件或目录的基本名称。
    常用用法:
linux 复制代码
basename /path/to/file.txt      # 输出 file.txt
basename /path/to/file.txt .txt # 输出 file
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ study]# ls
old_pattern_file1.txt  old_pattern_file2.txt  old_pattern_file3.txt  test.cfg
[root@iZf8z9luf0hps0r15qjv8vZ study]# basename /test/study/test.cfg 
test.cfg
[root@iZf8z9luf0hps0r15qjv8vZ study]# basename /test/study/test.cfg .cfg
test
  1. dirname
    功能:提取文件或目录的路径部分。
    常用用法:
linux 复制代码
dirname /path/to/file.txt      # 输出 /path/to
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ study]# dirname /test/study/test.cfg
/test/study
  1. chattr
    功能:改变文件的扩展属性(通常需要管理员权限)。
    常用用法:
linux 复制代码
chattr +i file.txt      # 将文件设置为不可修改
chattr -i file.txt      # 移除不可修改属性
  1. lsattr
    功能:查看文件的扩展属性。
    常用用法:
linux 复制代码
lsattr file.txt         # 查看文件的扩展属性
lsattr -d directory/    # 查看目录及其内容的扩展属性
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ study]# lsattr test.cfg
-------------e-- test.cfg
[root@iZf8z9luf0hps0r15qjv8vZ test]# lsattr -d study/
-------------e-- study/
  1. file
    功能:显示文件的类型。
    常用用法:
linux 复制代码
file filename.txt       # 显示文件的类型,如文本文件、可执行文件等
  1. md5sum
    功能:计算并校验文件的MD5值。
    常用用法:
linux 复制代码
md5sum file.txt         # 输出文件的MD5值
md5sum -c checksum.md5  # 校验MD5值,检查文件是否被修改
linux 复制代码
[root@iZf8z9luf0hps0r15qjv8vZ test]# md5sum test4.txt  
d41d8cd98f00b204e9800998ecf8427e  test4.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# md5sum test3.txt test4.txt > checksum.md5
[root@iZf8z9luf0hps0r15qjv8vZ test]# md5sum -c checksum.md5
test3.txt: 确定
test4.txt: 确定
[root@iZf8z9luf0hps0r15qjv8vZ test]# vim test4.txt
[root@iZf8z9luf0hps0r15qjv8vZ test]# md5sum -c checksum.md5
test3.txt: 确定
test4.txt: 失败
md5sum: 警告:1 个校验和不匹配
相关推荐
长流小哥18 分钟前
Linux网络编程实战:从字节序到UDP协议栈的深度解析与开发指南
linux·c语言·开发语言·网络·udp
极小狐1 小时前
极狐GitLab 功能标志详解
linux·运维·服务器·elasticsearch·gitlab·极狐gitlab
jinan8861 小时前
加密软件的发展:从古典密码到量子安全
大数据·运维·服务器·网络·安全·web安全
雾原2 小时前
Red Hat Enterprise Linux (RHEL)系统部署
linux
您8132 小时前
二十、FTP云盘
linux·服务器·网络
用户3409704691152 小时前
ROS2-Jazzy编译功能包报错
linux
264玫瑰资源库2 小时前
2025年七星棋牌跨平台完整源码解析(200+地方子游戏+APP+H5+小程序支持,附服务器镜像导入思路)
服务器·游戏·小程序
程序员阿灿2 小时前
CentOS服务器能ping通却无法yum install:指定镜像源解决
linux·服务器·centos
丑过三八线2 小时前
在Linux下安装Gitlab
linux·运维·gitlab
Paper_Love3 小时前
Linux-ftp tftp vsftpd区别
linux