Temp location in linux

how to get system or user temp folder in unix and windows?

I am writing a C++ problem. It need to work on both Windows and Unix OS. How to get user or system tmp folder on different OS?

Don’t write more problems, we have enough of those already. Write a solution for once. 🙂 Do you need the actual temp folder, or just a temporary file? C++ doesn’t have a notion of a file system, so there are no «folders», but you can use tmpfile or tmpnam to get a temporary file.

The tmpfile function doesn’t give you a name; the tmpnam function gives you a name but isn’t secure (see mkstemp() which gives you both a name and a file descriptor — and is secure).

8 Answers 8

Update: Thanks @RoiDanton, the most up to date answer is std::filesystem::temp_directory_path (C++17)

  • ISO/IEC 9945 (POSIX): The path supplied by the first environment variable found in the list TMPDIR , TMP , TEMP , TEMPDIR . If none of these are found, «/tmp» , or, if macro __ANDROID__ is defined, «/data/local/tmp»
  • Windows: The path reported by the Windows GetTempPath API function.

Interestingly, Window’s GetTempPath uses similar logic to the POSIX version: the first environment variable in the list TMP , TEMP , USERPROFILE . If none of these are found, it returns the Windows directory.

The fact that these methods primarily rely on environment variables seems a bit yuck. But thats how it seems to be determined. Seeing as how mundane it really is, you could easily roll your own using cstdlib ‘s getenv function, especially if you want specific order prioritization/requirements or dont want to use another library.

Источник

Where is the temporary directory in linux

Solution 2: Same Problem, solution not by changing temp directory of git but using the diff script you can define: Define script for diff in Create script to use the Windows tool: In my example I am using WinMerge. Linux distributions relying on (which is 90% of them) can now use directory (XDG Base Directory Specification) to store certain types of temporary files.

Where is the temporary directory in Linux?

The Filesystem Hierarchy Standard version 3.0 says:

/tmp : Temporary files

The /tmp directory must be made available for programs that require temporary files.

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

Rationale

IEEE standard POSIX.1-2008 lists requirements similar to the above section. Although data stored in /tmp may be deleted in a site-specific manner, it is recommended that files and directories located in /tmp be deleted whenever the system is booted.

Читайте также:  Ssh server on linux mint

FHS added this recommendation on the basis of historical precedent and common practice, but did not make it a requirement because system administration is not within the scope of this standard.

/var/tmp : Temporary files preserved between system reboots

The /var/tmp directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in /var/tmp is more persistent than data in /tmp .

Files and directories located in /var/tmp must not be deleted when the system is booted. Although data stored in /var/tmp is typically deleted in a site-specific manner, it is recommended that deletions occur at a less frequent interval than /tmp .

Also the The Open Group Base Specifications Issue 7, Environment Variables mentions the following:

TMPDIR This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.

This is an old question so today there is another option available. Linux distributions relying on systemd (which is 90% of them) can now use $XDG_RUNTIME_DIR directory (XDG Base Directory Specification) to store certain types of temporary files. It is generally located at /run/user/$uid . This is a per-user directory with 700 permissions which provides better security. This is a tmpfs mount which provides performance. The downside of tmpfs is that it should only be used to keep small files and sockets.

I look at it as a marriage of /tmp and /var/run .

Yes /tmp is for general use. See here and here On the Filesystem Hierarchy Standard.

/tmp/ Temporary files (see also /var/tmp). Often not preserved between system reboots.

With some more details listed in the PDF.

Is it possible to configure the temp file folder in git diff?, When calling git difftool in a repository, git compares the modified files in the local repository with their match in the remote. To do so, it creates a temporary file for the remote, sets the variables LOCAL and REMOTE and calls whichever tool is specified for diffs in git’s config. By default, the temporary file …

Where is the temporary directory in Linux? (5 Solutions!!)

Where is the temporary directory in Linux ?Helpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith thanks & praise to God, and with

Where is the ‘TEMP’ Temporary Directory in Linux Mint

How to change the temprary directory for configure script?

you could add the below statement at the beginning of your script.

where «DIRNAME» is any directory on which you have full permissions and has sufficient memory available in it

you can override the location of the temporary directory during configure like so:

./configure TMPDIR=path/to/your/tmp/folder 

If your / directory is full, and the program is trying to write to your /tmp directory, try renaming the tmp directory to something like tmp_old, then create a symbolic link to your new tmp folder like: ln -s /mnt/tmp /tmp

Rstudio — Changing temporary directory in R, The R documentation in ?tempdir says this: By default, tmpdir will be the directory given by tempdir (). This will be a subdirectory of the per-session temporary directory found by the following rule when the R session is started.

Читайте также:  Linux async file io

Tmp folder in Windows like /tmp in Linux

You should use the environment variable %TEMP% which points to different locations on different Windows versions, but is the defined location for temporary data in Windows.

Windows doesn’t clean it up by itself, but it is fine to delete its contents on shutdown (and as lots of applications don’t clean up properly, it is recommended to do so once in a while).

Do not delete the %TEMP% folder, but it’s contents using del %TEMP%\* /s /f /q which will delete the contents instead, so you don’t need to recreate the folder.

For setting up a shutdown-script, use the answer provided by @Alex K.

I do it via a shutdown script to clear a directory called c:\null

Run gpedit.msc & see http://technet.microsoft.com/en-us/library/cc770300.aspx for instructions on configuring a script to run.

@echo off @rd c:\null\ /s /q @md c:\null 

How to change directory of where temporary files are, How to change directory of where temporary files are stored. 0. I’m trying to move my temp directory from home/user to the /tmp directory in root. I’ve tried using export TMPDIR=/tmp but it defaults back to my home directory after reboot. filesystem directory tmp.

Is it possible to configure the temp file folder in git diff?

You can try setting the TMPDIR environment variable.

TMPDIR This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.

After a quick look at the git code ( git/builtin/difftool.c ), I don’t think configuring the temp directory is currently supported:

/* Setup temp directories */ tmp = getenv("TMPDIR"); xsnprintf(tmpdir, sizeof(tmpdir), "%s/git-difftool.XXXXXX", tmp ? tmp : "/tmp"); 

Seems like git is taking the TMPDIR value, or «/tmp» , if TMPDIR is not defined.

Same Problem, solution not by changing temp directory of git but using the diff script you can define:

    Define script for diff in .gitconfig

[diff] external = /home/user/scripts/my_diff.sh 
#!/bin/bash FILE2="C:\\dev\\wsl\\Debian\\rootfs\\$" FILE1=$(wslpath -w $5) /c/Program\ Files\ \(x86\)/WinMerge/WinMergeU.exe $FILE1 $FILE2 

In my example I am using WinMerge.
I know that this solution is against the rule that Windows tools are not allowed to touch Linux files, but I am fine with that since we are talking about a temp file and I don’t see the point of changing it in WinMerge anyhow. The manual path substitution is necessary because wslpath does not work on Linux internal path.

Your WSL might be different, normally somewhere

C:\Users\%username%\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\rootfs 

This feels a little hacky, but I am using it and am happy up till now.

I made a variant of the two approaches here, for my own use. I created a script which I call instead of running git diff (change the «twm» path, if copying — that’s my own user directory):

#!/bin/bash export TMPDIR=$( realpath --relative-to=. /mnt/c/Users/twm/tmp ) git difftool -y -t winmerge "$@" 

I was specifically targeting WinMerge invoked by Git running in Windows Subsystem for Linux. The problem I hit with using TMPDIR was that while Git wrote to it, WinMerge didn’t recognize the temporary path, initially because it started with «/mnt/c» — I did get the paths to match between WSL and Windows, except for the leading «c:» and WinMerge still didn’t recognize it. However, I noticed that the other path was correctly recognized, and ps showed it was being passed to WinMerge as a relative path, so I used realpath to make TMPDIR relative too.

Читайте также:  Kyocera fs 1040 драйвер astra linux

Using a script to set TMPDIR locally also solves an issue mentioned in the comments. That is, changing the temporary directory for all other programs for this one use case seems like overkill and might have some unintended side effects.

Like the second approach listed here, I had tinkered with Git aliases too in an attempt to get this to work. However, I wanted to make use of Git’s existing WinMerge integration in case the built in integration provides more functionality than a hard coded script, now or in the future.

Where is the temporary directory in Linux?, Anyway, the standard temporary directory in a typical Linux system is /tmp. It is the equivalent of C:\Temp in the sense that it is only the default temporary directory, not universal. Even if /tmp is available, if a user (or the system) has set the TEMP environment variable, the value of that variable should be used instead.

Источник

How do I find the ‘temp’ directory in Linux?

How do I find the ‘temp’ directory in Linux? I am writing a platform neutral C++ function that returns the temp directory. In Mac and Windows, there is an API that returns these results. In Linux, I’m stumped. ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

6 Answers 6

Check following variables:

If all fails try to use the directory /tmp .

You can also use tempnam function to generate a unique temporary file name.

is there a standard (POSIX or whatever) which specifies this order of doing things? I’ve seen it in several places so I’m sure it’s right but I was wondering if there was an authoritative statement anywhere.

Edit: Fair point from the commenter. tmpnam isn’t a good choice these days; use mktemp / mkstemp instead.

Historical answer: Be POSIX compliant, and use tmpnam (which will give you a full filename in a temporary location).

The linux man page I have for tmpnam says in its BUGS section: «Never use this function. Use mkstemp or tmpfile»

I’m getting that same warning on the mktemp linux man page. And I don’t have mkstemp or tmpfile commands (on OSX).

Use the value of the $TMPDIR environment variable, and if that doesn’t exist, use /tmp .

The accepted sequence, specifically from a GNU standpoint, is:

  1. Check the environmental variable TMPDIR (getenv(«TMPDIR»)) only if the program is not running as SUID/SGID (issetugid() == 0)
  2. Otherwise use P_tmpdir if it is defined and is valid
  3. and finally, should those fail, use _PATH_TMP available from paths.h

If you are adding an extension or module, check to see if the core provides a function for this purpose. For example, PHP exports php_get_temporary_directory() from main/php_open_temporary_file.h.

Источник

Оцените статью
Adblock
detector