File management
List & sort
ls -l | head -6
ls -rlah # r(reverse) a(all) h(human readable)
ls -d */ # Listing directories only
ls -l | sort -k3 | more # Listing files by owner
ls -l | sort -k4 # Listing files by group
find [starting location] [criteria] [options] [action to take]
# Finding files by name
find ~ -name "fix*" -print
# Finding files by type
find Documents -name junk* -type f -print
# To find a symbolic link, use "-type l"
find . -name "h*" -type l -ls
# Remove all files older than n days in current folder
find . -type f -mtime +n | xargs rm
# delete files (including subdirectories)
find ~ -name *.old -exec rm {} \;
find . -name *.old -ok rm {} \; # `-ok` ask for approval before removing the files. Answering y for “yes” would allow the find command to go ahead and remove the files one by one.
find . -name runme -okdir rm {} \; # use the `-okdir` option. Like `-ok`, this option will prompt for permission to run the command
# Finding files by owner and/or group
find . -user www-data -ls | head -4
find . -user 1000 -ls | head -4
find /tmp -user shs -ls 2> /dev/null
find /home -group tom -print
find /tmp -group 1000 -ls 2>/dev/null
# Finding files by file permissions
find /usr/bin -name "net*" -perm -g=w -ls
find /usr/bin -name "d*" -perm 777 -ls | head -3
# Finding files by age
# +100 (more than 100 days old) or -10 (modified within the last 10 days)
find Documents -mtime -1
# Finding files by size
find . -size 0 -ls | head -1 # this find empty files
find . -size 0 -ls | wc -l # this command would find a lot more empty files – representing the cache and such
find / -type f -size +1G -ls 2>/dev/null # finds files that are larger than 1 GB: Notice that the command sends all the “permission denied” messages to the /dev/null.
# 删除当前目录下修改时间超过30天的 .sql 文件
find . -maxdepth 1 -type f -name "*.sql" -mtime +30 -delete
# `-execdir` runs the specified command from the directory in which the located file resides rather than from the directory in which the find command is run
find . -name runme -execdir pwd \;
/home/shs/bin
# If you run the command shown below and the directory contains a file named “ls”, it will run that file and it will run it even if the file does not have execute permissions set
find . -name runme -execdir ls \;
Running the /home/shs/bin/ls file
transfer files between hosts
rsyncSCPSecure Copy Protocol, is based on the SSH protocol, and is used for securely copying files across the network.
# -P flag combines the flags --progress and --partial
# -z compression
rsync -azP source destination
rsync -a ~/dir1 username@remote_host:destination_directory
rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine
# copy to a remote resource
scp code.c jolo@foo.edu:~/Project
# from a remote resource & rename
scp jolo@foo.edu:~/Project/output.txt ./Results/Run12_data.txt
===============================
在 Ubuntu 系统中清理磁盘垃圾文件,需要分系统缓存、无用包、日志文件、用户缓存等多个维度操作,以下是一套安全且高效的分步教程,涵盖命令行和图形界面两种方式。
一、 命令行清理(推荐,高效彻底)
命令行操作适合开发者和熟悉终端的用户,清理前建议先通过 df -h 查看磁盘占用情况,确认清理目标分区。
1. 清理 APT 缓存(系统安装包缓存)
Ubuntu 的 apt 会缓存下载的 deb 安装包,长期积累会占用大量空间。
```bash
查看 APT 缓存大小
du -sh /var/cache/apt/archives/
清理已安装的包缓存(保留索引)
sudo apt autoclean
清理所有缓存包(包括未安装的)
sudo apt clean
卸载系统不再需要的依赖包(非常重要)
sudo apt autoremove --purge
``
-autoclean:只删除无法下载的旧包;
-clean:删除所有下载的包文件;
-autoremove --purge:删除安装软件时自动安装但现在无依赖的包,--purge` 会连带删除配置文件。
2. 清理系统日志文件
系统日志 /var/log/ 会不断增长,尤其是服务器环境,可通过 logrotate 自动清理,也可手动清理旧日志。
```bash
查看日志文件夹大小
du -sh /var/log/
清理旧日志(保留最近的日志,推荐)
sudo journalctl --vacuum-time=7d # 只保留7天内的日志 sudo journalctl --vacuum-size=100M # 限制日志总大小为100M
手动删除超大的旧日志文件(谨慎操作)
sudo rm -rf /var/log/.gz /var/log/.[0-9] ```
3. 清理用户缓存文件
用户目录下的 .cache 文件夹存储了应用缓存、浏览器缓存等,可安全删除。
```bash
清理当前用户的缓存
rm -rf ~/.cache/*
清理所有用户的缓存(需 root 权限)
sudo rm -rf /home//.cache/ ```
4. 清理临时文件
系统临时目录 /tmp 和 /var/tmp 存放临时文件,重启系统会自动清理,也可手动删除。
```bash
手动清理临时文件
sudo rm -rf /tmp/* /var/tmp/* ```
5. 清理 Snap 包缓存(Ubuntu 20.04+ 常见)
Snap 包会保留多个版本,占用空间极大,可清理旧版本。 ```bash
查看 Snap 包占用空间
du -sh /var/lib/snapd/snaps/
清理旧版本 Snap 包
sudo snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do sudo snap remove "$snapname" --revision="$revision" done ```
二、 图形界面清理(适合桌面用户)
如果是 Ubuntu 桌面版,可通过可视化工具一键清理,操作更简单。
1. 使用系统自带的磁盘分析器
- 打开应用商店,搜索安装 Disk Usage Analyzer(磁盘使用分析器);
- 打开工具,选择要扫描的磁盘分区,等待扫描完成;
- 工具会直观显示文件和文件夹的大小,找到占用空间大的垃圾文件(如缓存、日志),右键删除。
2. 使用 BleachBit(专业清理工具)
BleachBit 是开源的系统清理工具,功能类似 Windows 的 CCleaner。 ```bash
安装 BleachBit
sudo apt install bleachbit
启动 BleachBit(桌面版)
bleachbit
或 root 权限启动(清理系统文件)
sudo bleachbit ``` - 勾选需要清理的项目(如 APT 缓存、日志、浏览器缓存等); - 点击 Clean 按钮,等待清理完成即可。
三、 注意事项
- 谨慎使用
rm -rf:删除文件时确认路径正确,避免误删系统关键文件; - 清理前备份重要数据:尤其是手动删除日志或配置文件时;
- 定期自动清理:可通过
crontab定时执行清理命令,例如每周执行一次apt autoremove和日志清理。
四、 进阶:检查大文件和目录
如果清理后磁盘空间仍不足,可找出占用空间最大的文件/目录,针对性清理。 ```bash
查找根目录下大于 100M 的文件
sudo find / -type f -size +100M -exec ls -lh {} \;
按大小排序显示当前目录下的文件夹
du -sh * | sort -rh ```
Page Source