mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-07 22:11:09 +08:00
147 lines
5.5 KiB
Markdown
147 lines
5.5 KiB
Markdown
[#]: subject: "Make a temporary file on Linux with Bash"
|
|
[#]: via: "https://opensource.com/article/22/6/make-temporary-file-bash"
|
|
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
|
[#]: collector: "lkxed"
|
|
[#]: translator: "geekpi"
|
|
[#]: reviewer: " "
|
|
[#]: publisher: " "
|
|
[#]: url: " "
|
|
|
|
Make a temporary file on Linux with Bash
|
|
======
|
|
The mktemp command on Fedora-based systems and tempfile on Debian-based systems are specially designed to alleviate that burden by making it easy to create, use, and remove unique files.
|
|
|
|
![bash logo on green background][1]
|
|
|
|
Image by: Opensource.com
|
|
|
|
When programming in the Bash scripting language, you sometimes need to create a temporary file. For instance, you might need to have an intermediary file you can commit to disk so you can process it with another command. It's easy to create a file such as `temp` or anything ending in `.tmp`. However, those names are just as likely to be generated by some other process, so you could accidentally overwrite an existing temporary file. And besides that, you shouldn't have to expend mental effort coming up with names that seem unique. The `mktemp` command on Fedora-based systems and `tempfile` on Debian-based systems are specially designed to alleviate that burden by making it easy to create, use, and remove unique files.
|
|
|
|
### Create a temporary file
|
|
|
|
Both `mktemp` and `tempfile` create a temporary file as their default action and print the name and location of the file as output:
|
|
|
|
```
|
|
$ tempfile
|
|
/tmp/fileR5dt6r
|
|
|
|
$ mktemp
|
|
/tmp/tmp.ojEfvMaJEp
|
|
```
|
|
|
|
Unless you specify a different path, the system places temporary files in the `/tmp` directory. For `mktemp`, use the `-p` option to specify a path:
|
|
|
|
```
|
|
$ mktemp -p ~/Demo
|
|
/home/tux/Demo/tmp.i8NuhzbEJN
|
|
```
|
|
|
|
For `tempfile`, use the `--directory` or `-d` option:
|
|
|
|
```
|
|
$ tempfile --directory ~/Demo/
|
|
/home/sek/Demo/fileIhg9aX
|
|
```
|
|
|
|
### Find your temporary file
|
|
|
|
The problem with using an auto-generated temporary file is that you have no way of knowing what its name is going to be. That's why both commands return the generated file name as output. You can use an interactive shell such as Konsole, GNOME Terminal, or [rxvt][2] to use the filename displayed on your terminal to interact with the file.
|
|
|
|
However, if you're writing a script, there's no way for you to intervene by reading the name of the file and using it in the following commands.
|
|
|
|
The authors of `mktemp` and `tempfile` thought of that problem, and there's an easy fix. The terminal sends output to a stream called *stdout.*You can capture stdout by setting a variable to the results of a command launched in a subshell:
|
|
|
|
```
|
|
$ TMPFILE=$(mktemp -p ~/Demo)
|
|
|
|
$ echo $TMPFILE
|
|
/home/tux/Demo/tmp.PjP3g6lCq1
|
|
```
|
|
|
|
Use `$TMPFILE` when referring to the file, and it's the same as interacting directly with the file itself.
|
|
|
|
### Create a temporary directory with mktemp
|
|
|
|
You can also use the `mktemp` command to create a directory instead of a file:
|
|
|
|
```
|
|
$ mktemp --directory -p ~/Demo/
|
|
/home/tux/Demo/tmp.68ukbuluqI
|
|
|
|
$ file /home/tux/Demo/tmp.68ukbuluqI
|
|
/home/tux/Demo/tmp.68ukbuluqI: directory
|
|
```
|
|
|
|
### Customize temporary names
|
|
|
|
Sometimes you might want an element of predictability in even your pseudo-randomly generated filenames. You can customize the names of your temporary files with both commands.
|
|
|
|
With `mktemp`, you can add a suffix to your filename:
|
|
|
|
```
|
|
$ mktemp -p ~/Demo/ --suffix .mine
|
|
/home/tux/Demo/tmp.dufLYfwJLO.mine
|
|
```
|
|
|
|
With `tempfile`, you can set a prefix and a suffix:
|
|
|
|
```
|
|
$ tempfile --directory ~/Demo/ \
|
|
--prefix tt_ --suffix .mine
|
|
/home/tux/Demo/tt_0dfu5q.mine
|
|
```
|
|
|
|
### Tempfile as touch
|
|
|
|
You can also set a custom name with `tempfile` :
|
|
|
|
```
|
|
$ tempfile --name not_random
|
|
not_random
|
|
```
|
|
|
|
When you use the `--name` option, it's absolute, ignoring all other forms of customization. In fact, it even ignores the `--directory` option:
|
|
|
|
```
|
|
$ tempfile --directory ~/Demo \
|
|
--prefix this_is_ --suffix .all \
|
|
--name not_random_at
|
|
not_random_at
|
|
```
|
|
|
|
In a way, `tempfile` can be a substitute for `touch` and `test` because it refuses to create a file that already exists:
|
|
|
|
```
|
|
$ tempfile --name example.txt
|
|
open: file exists
|
|
```
|
|
|
|
The `tempfile` command isn't installed on all Linux distributions by default, so you must ensure that it exists before you use it as a hack around `test` in a script.
|
|
|
|
### Install mktemp and tempfile
|
|
|
|
[GNU Core Utils][3] includes the `mktemp` command. Major distributions include Core Utils by default (it's the same package that contains `chmod`, `cut`, `du`, and other essential commands).
|
|
|
|
The Debian Utils package includes the `tempfile` command and is installed by default on most Debian-based distributions and Slackware Linux.
|
|
|
|
### Wrap up
|
|
|
|
Temporary files are convenient because there's no confusion about whether they're safe to delete. They're temporary, meant to be used as needed and discarded without a second thought. Use them when you need them, and clear them out when you're done.
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
via: https://opensource.com/article/22/6/make-temporary-file-bash
|
|
|
|
作者:[Seth Kenlon][a]
|
|
选题:[lkxed][b]
|
|
译者:[译者ID](https://github.com/译者ID)
|
|
校对:[校对者ID](https://github.com/校对者ID)
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
[a]: https://opensource.com/users/seth
|
|
[b]: https://github.com/lkxed
|
|
[1]: https://opensource.com/sites/default/files/lead-images/bash_command_line.png
|
|
[2]: https://opensource.com/article/19/10/why-use-rxvt-terminal
|
|
[3]: https://www.gnu.org/software/coreutils/
|