5

Linux 临时目录 /tmp 与 /var/tmp | myfreax

 1 year ago
source link: https://www.myfreax.com/tmp-directory-in-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux 临时目录 /tmp 与 /var/tmp

如果您已经使用 Linux 一段时间,那么您一定遇到过 /tmp 目录。您可能对此有所了解,但您可能认为它不会重要。你还有可能知道有一个起来类似的 /var/tmp 目录

Updated At 10 Jun 2023 4 min read
By myfreax
Linux 临时目录 /tmp 与  /var/tmp

Linux 临时目录 /tmp 与 /var/tmp

如果您已经使用 Linux 一段时间,那么您一定遇到过 /tmp 目录。您可能对此有所了解,但您可能认为它不会重要。你还有可能知道有一个起来类似的 /var/tmp 目录。

因此,在教程中,我将向您说明您需要了解 Linux 的 /tmp 目录的重要信息。我们还将讨论它与 /var/tmp 目录的不同之处。

Linux  /tmp 目录是什么

顾名思义,root 根目录下的 tmp(临时)目录用于存放系统和用户应用程序使用的数据,用来存放短时间内需要的数据。大多数 Linux 发行版都预先配置为在每次重新启动后清空 tmp 目录。

如果你还没理解,你可以想象这种情况。假设您正在给系统安装软件,安装程序在安装过程中可能会存储一些需要的文件。

同样,在处理项目时,您的系统可能会在进行更改时将文件存储在 tmp 目录,或者它们也可以是文件的自动保存版本。

简单的说,tmp 目录就是一个目录,用来存放临时需要的文件,不需要的时候可以删除。

/tmp 与 /var/tmp 区别

/tmp 与 /var/tmp 是不一样的, /tmp 目录和 /var/tmp 目录之间存在显着差异。简短的回答是他们如何处理临时文件。

/tmp 目录用于存储短期临时文件,而 /var/tmp 目录用于存储长期临时文件。

  • Endurance:一般情况下,/tmp 目录存放的文件在开机时会被删除,而 /var/tmp 里面的文件在重启后仍会保留。
  • 用户与系统:通常,每个用户都可以访问 /tmp 目录中的文件,而 /var/tmp 的文件大多是特定于用户的。
  • 用法(最关键的区别):/tmp 目录用于存储短时间需要的文件,例如安装包。而 /var/tmp 目录用于存储需要较长时间的文件,例如系统备份或日志文件。

自动清理 tmp 目录

正如之前所说,大多数发行版都会在您重新启动 Linux 系统时清理 /tmp 目录。

如果是这样,那么为什么还需要清理 /tmp 目录,因为您不会像家里的台式计算机那样每天重新启动服务器。它可能会运行数周,甚至数月和数年。

这不是每个人都需要的。仅当您的服务器磁盘空间不足时,才需要自动清理 tmp 目录。

要自动清理 tmp 目录,最关键的是首先确定要删除的内容。所以最好的办法是删除最近三天未使用且不属于 root 用户的文件。

因此,您可以使用 find 命令搜索最近三天未使用且不属于 root 用户的文件,运行下面的命令:

sudo find /tmp -type f \( ! -user root \) -atime +3 -delete

我们已确定要删除的目标文件,现在,但这个过程不会自动化。因此,您必须创建一个 crontab 作业来自动运行 find命令并删除文件 。

首先,运行 sudo crontab -e 命令打开系统级别的 crontab:

sudo crontab -e

如果您是第一次使用 crontab,它会要求您选择您喜欢的文本编辑器。在本教程,我们推荐使用 vim,当然你也可以选择 nano

[sudo] password for myfreax: 
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /usr/bin/code
  5. /bin/ed

Choose 1-5 [1]: 2

打开后,跳转转到文件末尾并将下面的行复制粘贴到文件:

0 0 * * * sudo find /tmp -type f ! -user root -atime +3 -delete

最后保存文件并退出 vim

至此,你已了解什么是 tmp 目录以及 /tmp 目录和 /var/tmp 目录的区别,学习如何在Linux 服务器清理 /tmp 目录的文件。如有任何疑问,请随时发表评论。


Recommend

  • 23

    From writing to /tmp to a root shell on Inteno IOPSYS CVE-2018-14533 Jul 21, 2018 In this blog p...

  • 10

    Table of Contents最近遇到磁盘占用 100% 的问题,发现 /tmp 目录下多出许多类似 my-app.jar-spring-boo-libs-xxx 的目录。这些目录是什么,如何避免磁盘爆了? 这些目录是什么? Spring boot 会将依赖的...

  • 10

    【重磅】圈钱千万的“庭美TMP”:敢维权,就弄了你!_区块链资讯_链向财经【重磅】圈钱千万的“庭美TMP”:敢维权,就弄了你!01-05 16:33标签币圈

  • 12
    • tmpout.sh 4 years ago
    • Cache

    tmp.0ut

    tmp.0ut ┌───────────────────────┐ ▄▄▄▄▄ ▄▄▄▄▄ ▄▄▄▄▄ │ │ █ █ █ █ █...

  • 7
    • www.eryajf.net 3 years ago
    • Cache

    CentOS-7中tmp目录的清理规则

    CentOS-7中tmp目录的清理规则 |坐而言不如起而行! 二丫讲梵 > 术业专攻 >

  • 6

    论临时变量名 tmp 与代码的语感 谢益辉 / 2018-04-24 在审阅谭显英壮士的一个补丁时,我注意到一个在码农中极为常见的变量名,那就是万能的 tmp

  • 9
    • help.dreamhost.com 3 years ago
    • Cache

    /tmp directory overview

    /tmp directory overview Overview Web servers have a directory named /tmp used to store temporary files. Many programs use this /tmp directory for writing temporary data and general...

  • 5
    • computingforgeeks.com 3 years ago
    • Cache

    Mount /tmp on a separate partition in Linux

    Mount /tmp on a separate partition in LinuxBy default, /tmp directory is under / partition. In this guide, I’ll show you how you can create a separate partition for /tmp on LVM and mount it with some restrictions for security purposes....

  • 12
    • 0pointer.net 3 years ago
    • Cache

    /tmp or not /tmp?

    /tmp or not /tmp? A number of Linux distributions have recently switched (or started switching) to /tmp on tmpfs by default (ArchLi...

  • 3

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK