Temp command in linux

How create a temporary file in shell script?

While running a script, I want to create a temporary file in /tmp directory. After execution of that script, that will be cleaned by that script. How to do that in shell script?

7 Answers 7

tmpfile=$(mktemp /tmp/abc-script.XXXXXX) : . rm "$tmpfile" 

You can make sure that a file is deleted when the scripts exits (including kills and crashes) by opening a file descriptor to the file and deleting it. The file keeps available (for the script; not really for other processes but /proc/$PID/fd/$FD is a work-around) as long as the file descriptor is open. When it gets closed (which the kernel does automatically when the process exits) the filesystem deletes the file.

# create temporary file tmpfile=$(mktemp /tmp/abc-script.XXXXXX) # create file descriptor 3 for writing to a temporary file so that # echo . >&3 writes to that file exec 3>"$tmpfile" # create file descriptor 4 for reading from the same file so that # the file seek positions for reading and writing can be different exec 4&3 # your script continues : . # reading from that file descriptor head -n 1 &- 

» You can use cat

@HaukeLaging will you update this to show how to read from the file descriptor? Automatically deleting a file you write to and don’t read from isn’t much good.

Use mktemp to create a temporary file

or, to create a temporary directory:

At the end of the script you have to delete the temporary file or directory

mktemp creates file in the /tmp directory or in the directory given with the —tmpdir argument.

You can use trap «rm -f $temp_file» 0 2 3 15 right after creating the file so that when the script exits or is stopped with ctrl-C the file is still removed.

@HaukeLaging Then the trap doesn’t fire if the script is stopped with Ctrl+C. A thing to note is that TRAP doesn’t help if you kill -9 $somepid . That particular kill signal is insta-death with nothing else happening.

For those wondering, the trailing integers in trap «rm -f $temp_file» 0 2 3 15 are the signals upon which to run the first argument. 0: exit shell, 2: Interrupt, 3: Quit, 15: Terminate.

Some shells have the feature built-in.

zsh

zsh ‘s =(. ) form of process substitution uses a temporary file. For instance =(echo test) expands to the path of a temporary file that contains test\n .

$ &3; cat $file> 3<> $ test lrwx------ 1 stephane stephane 64 Jan 30 11:19 /dev/fd/3 -> /tmp/zshMLbER0 test2 

That file is automatically removed, once the command has finished.

bash/zsh on Linux.

Here-documents or here-strings in bash versions prior to 5.1 and zsh are implemented as deleted temporary files (as was the case in the Bourne shell which introduced here-documents in the late 70s).

The file descriptor 3 is connected to a deleted temporary file that contains test\n .

You can get its content with:

If on Linux, you can also read or write to that file via /dev/fd/3 , though with bash version 5.0, you’d first to need to restore write permissions to it (which bash explicitly removes in that version):

$ exec 3 /dev/fd/3 $ cat /dev/fd/3 foo 

(some other shells use pipes, or may use /dev/null if the here doc is empty).

POSIX

There is no mktemp POSIX utility. POSIX however specifies a mkstemp(template) C API, and the m4 standard utility exposes that API with the mkstemp() m4 function by the same name.

mkstemp() gives you a file name with a random part that was guaranteed not to exist at the time the function was called. It does create the file with permissions 0600 in a race-free way.

tmpfile=$( echo 'mkstemp(template)' | m4 -D template="$/baseXXXXXX" ) || exit 

Note however that you need to handle the clean-up upon exit, though if you only need to write and read the file a fixed number of times, you could open it and delete it just after creating like for the here-doc/here-string approach above:

tmpfile=$( echo 'mkstemp(template)' | m4 -D template="$/baseXXXXXX" ) || exit # open once for writing, twice for reading: exec 3> "$tempfile" 4< "$tempfile" 5< "$tempfile" rm -f -- "$tmpfile" cmd >&3 # store something in the temp file exec 3>&- # fd no longer needed # read the content twice: cat  

You could open the file for reading once, and rewind in between two reads, however there's no POSIX utility that can do that rewinding ( lseek() ), so you can't do it portably in a POSIX script ( zsh ( sysseek builtin) and ksh93 ( <#((. )) operator) can do it though).

Источник

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

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

Источник

How to Use Mktemp Command on Linux

We tend to create many files and folders in the home folder of our Linux systems. These files and folders can save the data endlessly without the removal of such files and folders on reboot. Linux came up with the new feature to create temporary files in the “tmp” folder. These files or folders would be automatically deleted after the reboot and all the data within these files or folders will be lost. Let’s take a look at the “mktemp” command in the Linux shell to create the temporary files in Ubuntu 20.04 system.

You need to make sure that your system is already up to date and upgraded to Ubuntu 20.04 system. If not, try using the “apt” package command in the Ubuntu terminal shell along with the keyword “update”. Before everything, you need to open up the command-line application to do all this. The command-line application can be launched with the usage of the activity menu on the top left corner of your Ubuntu 20.04 screen. Tap on that icon and a search bar will be opened. Write the name of a command-line application as “terminal” and hit the Enter button. It will open your terminal in a few seconds.

Let’s take a look at how the “mktemp” single word command works in the shell. So, within the query area, we have written the “mktemp” command and pressed “Enter” for the execution purpose. The output will be like “/tmp/tmp.LB1DOVqUPF”. It is a random and temporary file created in the “tmp” folder of your system. The name of a file is also generated automatically.

Let’s check the “tmp” folder of our system now. Open it through the file explorer. You can see from the image underneath that the file tmp.LB1DOVqUPF is generated.

You have seen that the file has no extension. If you want to add some extension at the end of a temporary file, you must add the “—suffix” keyword in the mktemp command along with the extension. The extension must be added within the inverted commas. You can see, we have been using the “.txt” extension to create a text type file within the “tmp” folder. After executing this command, the random file “tmp.q0GlvSUC75.txt” with the “.txt” extension is created as per the output.

After opening the “tmp” folder using the file explorer, we have seen that the temporary text file “tmp.q0GlvSUC75.txt” is created in the folder having the “.txt” extension.

This was all about the creation of a temporary file in the “tmp” folder. Along with the text files or random simple files, we can also create the temporary directory in the “tmp” folder. This directory name will also be generated randomly i.e. assigned by itself. This “mktemp” command can be used with the flag “-d” to create a random directory within the “tmp” folder. After the execution of this query, the directory name “tmp.55ULnaranu” is created in the “tmp” folder as the picture demonstrated.

Let’s open the file explorer and navigate towards the “tmp” folder of your Ubuntu 20.04 system as shown below. The directory named “tmp.55ULnaranu” can be seen in the opened “tmp” folder of our system shown in the image. This is the temporary folder and may automatically remove from the system once you reboot your Ubuntu 20.04 system.

Same as the simple random files, the temporary files and directories also have some privileges and restrictions assigned automatically by the system once these are created. Let say, we have created a new temporary file “tmp.uhPHOWaabz” with the “mktemp” query in the shell. Now, we can check out the assigned privileges to this newly created temporary file with the list command. So, we have to use the keyword “ls” along with the “-al” flag along with the location of the file in the “tmp” folder.

The result shows that the random file has only “read” and “write” privileges for the current user i.e., “saeedraza” and has no assigned privileges for the group and other users. The output is also showing the user and group it belongs to along with its creation date and time. This was about the simple temporary files.

Now, we will check out the same thing for the temporary directories. Therefore, you need to create a temporary directory in the “tmp” folder using the “mktemp” command with the “-d” flag. The directory is now generated in the “tmp” directory. The list command is utilized to check the privileges assigned to the temporary directory just created within the “tmp” folder. The “-ld” flag is used within the list command alongside the directory path as below.

The directory has read, write, and execution rights to the current user i.e., saeedraza. While the other users and groups have no rights to read, write and execute this directory. The output presented in the images shows the directory’s name, date, time on which it is created. Also, it shows the group and user name it belongs to.

The mktemp is not only used to create the temporary files and directories in the accounts “tmp” folder but the “home” folder as well. For this, you need to add the file name along with more than 3 “XXX” at the end. It will eventually create your file “newcHs” in the home folder. The “XXX” characters will automatically be replaced by automatically generated characters of the system. The list command is used to list all the files and directories of the current home of our Ubuntu 20.04 system. The “newcHs” file is shown in the “home” directory.

To create a directory in the home root folder, we need to add the “-d” flag within the “mktemp” folder with the directory name having “XXX” at its end. The directory “newoC9” is generated. The list “ls” query is showing that the directory “newoC9” is in the home folder.

Conclusion

We have implemented the working of the “mktemp” command in Ubuntu 20.04. We have used it to create temporary files and folders in the “tmp” folder along with some extensions. We have also taken a look at how to create the temporary file and directory in the home folder of our system.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.

Источник

Читайте также:  Посмотреть сетевой адаптер linux
Оцените статью
Adblock
detector