经验教训 tips

Posted by shensunbo on August 2, 2024
  1. 使用rm删除文件时总是指定目录,防止误删除文件,不要使用rm * -rf。或者直接给rm设置一个别名,定向到一个自定义的删除脚本
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
         #!/bin/bash
    
         # Set the trash directory
         TRASH_DIR=~/.trash
    
         # Create the trash directory if it doesn't exist
         mkdir -p "$TRASH_DIR"
    
         # Move the file to the trash directory instead of deleting it
         mv "$@" "$TRASH_DIR/"
    
         # Clean up the trash directory by removing files older than 24 hours
         find "$TRASH_DIR" -type f -mtime +1 -delete
    
    • echo -e "\n alias rm='path/to/my_rm.sh'" >> ~/.bashrc

      NOTE: 也可以使用 echo "\n alias rm='path/to/my_rm.sh" | tee -a ~/.bashrc > /dev/null 但是一定要使用 >>tee -a,或者直接手动使用vim修改,不然参数错了会覆盖整个文件 :(

    • source ~/.bashrc
    • 使用 ` type rm 确认修改是否生效了:rm is aliased to ‘~/myScript/RM.sh’`
  2. 空循环时应该使用sleep,在有些平台上空循环会导致cpu的loading很高