OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 039

File and directory operations

Learn everyday file operations, from browsing and searching to links, archives, and disk-space queries.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSLinux command-line fundamentals and composition ↗
About 3 min readStart reading

rm, cp, and mv form the basic file-operation toolkit. The following commands fill out the rest of the tools you are likely to use every day.


Viewing and browsing#

ls — list directory contents

The most frequently used command, hands down.

ls          # 平铺列出当前目录
ls -l       # 长格式:权限/大小/日期
ls -a       # 显示隐藏文件(. 开头的)
ls -la      # 长格式 + 隐藏文件
ls -lh      # 人类可读的大小(1K 234M 3G)
ls -lt      # 按修改时间排序,最新的在前

A useful mnemonic: ls -lah = see everything + make it readable.

Using wildcards with ls

ls *.md       # 列出所有 .md 文件
ls report_??  # 列出 report_01, report_02 等两字符后缀
ls [abc]*     # 列出以 a/b/c 开头的文件

Creating and deleting#

mkdir — create directories

mkdir new_dir           # 创建一个目录
mkdir -p a/b/c/d        # 级联创建,不报错

-p is the most frequently used option: it creates missing parent directories automatically and does not report an error if they already exist.

touch — create empty files / update modification times

touch note.md           # 新建空文件(不存在时)
touch existing_file     # 更新时间戳为现在(存在时)

rmdir — remove empty directories

rmdir empty_dir         # 只删空目录,非空时报错(安全)

In practice, rm -rf is more commonly used to remove nonempty directories, but it requires extra care.


Finding and locating#

find — search for files by criteria

find . -name "*.md"              # 按名字在当前目录及子目录搜索
find . -type d -name "Notes"     # 只搜目录
find . -type f -size +100M       # 搜 >100M 的文件
find . -mtime -7                  # 最近 7 天修改过的文件

The power of find lies in its ability to act on the files it finds:

find . -type f -name "*.tmp" -print  # 先核对待删除文件
find . -type f -name "*.tmp" -delete # 确认目录与匹配范围后删除,无法自动撤回
find . -name "*.md" -exec wc -l {} +   # 统计每个 .md 文件的行数

Links#

ln — create hard links and symbolic links

ln target link_name        # 硬链接:同一个文件的两个名字
ln -s target link_name     # 软链接:指向目标的快捷方式

In everyday use, you will almost always use symbolic links:

ln -s /path/to/long/directory/app  ~/shortcut

A symbolic link is similar to a shortcut, but it stores a path. Deleting the target path breaks the symbolic link. A hard link still refers to the same inode; the file's data is released only after the last hard link is deleted and no process has the file open. Hard links generally cannot cross filesystem boundaries and generally cannot link to directories.


Archiving and compression#

tar — create archives

tar -cvf archive.tar dir/         # 打包(不压缩)
tar -czvf archive.tar.gz dir/     # 打包 + gzip 压缩
tar -xvf archive.tar              # 解包
tar -xzvf archive.tar.gz          # 解包 + 解 gzip

Remember: c = create an archive, x = extract an archive, z = gzip, v = verbose output, and f = specify the filename.

zip / unzip — cross-platform compression

zip -r archive.zip dir/           # 递归压缩
unzip archive.zip                 # 解压
unzip -l archive.zip              # 只看不解

Disk space#

du — how much space directories/files use

du -sh dir/          # 总计大小,人类可读
du -sh *             # 当前目录每个文件/文件夹各占多少
du -sh * | sort -hr  # 从大到小排

df — how much disk space remains

df -h                # 各分区用量,人类可读

Output and transfer#

echo and cat

echo "hello"              # 输出文本到终端
echo "hello" > file.txt   # 输出到文件(覆盖)
cat file.txt              # 显示文件全部内容
cat a.txt b.txt > c.txt   # 合并两个文件

Connecting this to what you already know#

You already know Now add Relationship
rm rmdir / find -delete Delete by criteria; -delete is also irreversible
cp tar / zip Archive multiple files for easier copying or transfer
mv ln -s Moving vs. referencing
None ls / du / df The eyes that let you see the filesystem

Permission management (chmod / chown / chgrp) is covered in detail in Linux File Permissions.



References & further reading

  1. GNU Coreutils Manual ↗
  2. GNU Findutils Manual ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Linux command-line fundamentals and composition

  1. 01File and directory operations
  2. 02Process and job management
  3. 03Text processing and system queries
  4. 04Shell fundamentals
  5. 05Fuzzy finding with fzf
  6. 06Passing arguments with xargs
CONTINUE READING / NEXT ARTICLEProcess and job management

Understand program execution and termination through process snapshots, signals, and foreground and background jobs.