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!
sed creates residue temp files with the -i flag.
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.
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".
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.
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.
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.
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
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.
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
You could use "sponge" from moreutils.
sed "/hello world/d" /etc/postfix/virtual | sponge /etc/postfix/virtual
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 echo
ing. This is needed to preserve any newlines in the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With