Linux 命令

查看当前编译环境中所有宏定义

1
gcc -E -dM - < /dev/null

查看指定目录下所有一级文件占用空间大小,并按大小排序

  1. 输出包括目录本身和其子目录/文件的大小
1
sudo du -h --max-depth=1 /path/to/dir | sort -hr
  1. 输出只包括子目录/文件的大小
1
sudo du -sh /path/to/dir/* | sort -hr

每隔10秒执行一次命令

  1. 仅显示最后一次执行结果
1
watch -n 10 "ls -al"
  1. 显示每次执行结果
1
while true; do ls -al; sleep 10; done

创建软链接

1
ln -s /path/to/source /path/to/link

同步两个目录

1
rsync -av /path/to/source/ /path/to/dest/

cmake 编译

1
cmake -S . -B build && cmake --build build

删除目录下,指定时间段的文件

  1. 删除2024年12月15日当天创建的文件
1
find /path/to/dir -type f -newermt "2024-12-15" ! -newermt "2024-12-16" -exec rm -f {} \;
  1. 删除2024年12月15日 08:00 到 2024年12月15日 12:00 创建的文件
1
find /path/to/dir -type f -newermt "2024-12-15 08:00" ! -newermt "2024-12-15 12:00" -exec rm -f {} \;

使用 pgrep 查找指定用户的进程

1
pgrep -afu username process_name

后台运行程序

适用于尚未运行的程序:

1
nohup ./your_program > output.log 2>&1 &

适用于已经运行的前台进程:

  1. 暂停程序(按 Ctrl + Z):终端会显示:
1
[1]+  Stopped    ./your_program
  1. 将其放到后台继续运行:
1
bg
  1. 使进程脱离当前终端(防止终端关闭时终止):
1
disown -h %1