Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set temp file directory for sed command in linux?

Tags:

bash

sed

I have bash script which modified file like this

sed -i "/hello world/d" /etc/postfix/virtual

and I ran this script from web application. sed command create temporary file in that directory but user under which web application works doesn't have permissions to create files in that directory. I do not want to give more permissions for the user to that folder. Is it possible to specify temp file location for sed command?

I am new in linux so sorry if my question is too easy but I didn't find any solution.

Thanks!

like image 959
Radislav Avatar asked Nov 22 '13 13:11

Radislav


People also ask

Does sed create temp file?

sed creates residue temp files with the -i flag.

How do I create a temp directory in Linux?

In Unix/Linux shell we can use the mktemp commmand to create a temporary directory inside the /tmp directory. The -d flag instructs the command to create the directory. The -t flag allows us to provide a template. Each X character will be replaced by a random character.

How do I create a temp folder?

Open your File Explorer (it's usually the first button on your desktop taskbar, looks like a folder). Go to the "This PC" section on the left, and then double-click your C: drive. On the Home tab at the top, click "New Folder" and name it "Temp".

What is the location of tmp directory in Linux?

In Unix and Linux, the global temporary directories are /tmp and /var/tmp. Web browsers periodically write data to the tmp directory during page views and downloads. Typically, /var/tmp is for persistent files (as it may be preserved over reboots), and /tmp is for more temporary files.

How do I use sed in Linux?

Sed works by processing the input file one line at a time. On each line, the substitute ( s) command will replace the first occurrence of the text between the first two slashes ( /<YEAR>/) by the text between the last two ones ( /2018/ ). Think of that like the search-replace feature you have in a GUI text editor.

How to replace a string in a file using sed command?

Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file. $sed 's/unix/linux/' geekfile.txt Output : linux is great os. unix is opensource.

How to create a temporary file in Linux?

Most Linux distributions provide the convenient mktemp utility, which permits us to effortlessly create a temporary file in Linux. Running mktemp with no arguments will create a temporary file in /tmp and show the file’s path in the terminal’s output: 3.2. Using Templates

What does p mean in sed command?

The p means “print matched lines.” By default, sed prints all lines. We’d see all the text in the file with the matching lines printed twice. To prevent this, we’ll use the -n (quiet) option to suppress the unmatched text.


3 Answers

The file must be modified via temporary file, this is actually what sed was doing but it lacked permissions.

When dealing with such a situation, we just do this process manually.

## TEMP File
TFILE=`mktemp --tmpdir tfile.XXXXX`
trap "rm -f $TFILE" 0 1 2 3 15

##
sed 's_XXX_YYY_' /etc/example > "$TFILE"
cat "$TFILE" > /etc/example

## trap Deletes TFILE on Exit
like image 69
J. M. Becker Avatar answered Oct 20 '22 16:10

J. M. Becker


You could use "sponge" from moreutils.

sed  "/hello world/d" /etc/postfix/virtual | sponge /etc/postfix/virtual
like image 22
Harold Zoid Avatar answered Oct 20 '22 18:10

Harold Zoid


Old question, but I was surprised to find nobody suggesting to use some good-ol' RAM via a variable. Here's what I ended up doing, no need for a temp file at all:

TEMP_SED=$(sed "/hello world/d" /etc/postfix/virtual)
echo "$TEMP_SED" > /etc/postfix/virtual
unset TEMP_SED

Note the quotes around the variable when echoing. This is needed to preserve any newlines in the file.

like image 26
CenterOrbit Avatar answered Oct 20 '22 16:10

CenterOrbit