How to overwrite file without deleting existing file? [duplicate] - bash

This question already has answers here:
Is bash > redirection atomic?
(3 answers)
In Bash, how to not create the redirect output file once the command fails
(3 answers)
Node.js fs.writeFile() empties the file
(3 answers)
Closed 4 years ago.
I have a node_exporter web server running which serves files from a directory. These files have host metrics at a particular point-in-time. I have a daemon running that scrapes metrics from other exporters every 15 seconds and writes to this directory. When it writes files, I think it removes the existing file and writes a new file. Because of this, once in a while, for a split second node_exporter serves no metrics from an exporter. How do I make sure the previous metrics exist until the new metrics are written and there's no empty/duplicate metrics?

write to a temporary file in the same directory
when done, rename the temporary file to the result file
mv tempfile outputfile

Related

How to "touch" a file in raw CMD without a touch command? [duplicate]

This question already has answers here:
Create an empty file on the commandline in windows (like the linux touch command)
(27 answers)
Closed 1 year ago.
At the good old Windows command prompt, I want to ensure a file exists and create it if it doesn't. I don't want to download any additional utilities (like touch) to accomplish this.
Here is one-liner solution:
As an example, I want to ensure a file named App_Offline.htm exists in my website root, but without overwriting that file if it already exists. This is the native DOS command I successfully used with append:
(echo empty > nul) >> App_Offline.htm
Note the word empty can be any text you want because it is simply a dummy placeholder piped through nul and is lost; therefore no value is effectively appended to an existing file, and if a file doesn't already exist then it is created empty.
This solution did not update the Modified time stamp of the existing file.

Run batch file located in sub directory with another batch file [duplicate]

This question already has answers here:
Get current batchfile directory
(4 answers)
Closed 1 year ago.
So I know you think I should do something like: start C:\parentfolder\subfolder\batchfile.bat
or something along those but I was wonder if there was like a way to reference the sub directory without using it's actual name? Since the batch files will be generated by another batch file.
Don't ask why I need it to be generated I'd much rather it not but couldn't find any other way.
You have to cd into a subdirectory and then run start start.bat. That should run another batch file. To go into a relative subdirectory, run the command cd foldername. You have to make sure you are currently in the parent directory.

How can i create an empty file using a batch command? [duplicate]

This question already has answers here:
Create an empty file on the commandline in windows (like the linux touch command)
(27 answers)
Closed 5 years ago.
So far my project is all about creating files for an imaginary game and I have to create files (and files within files), first, I have to ask them what version they want, and then the batch will create new files on my computer.
I just want to know what is the command to create new files, no need for other info.
Yes, you can.
type nul > your_file.txt
It is already resolved here
Windows equivalent of 'touch' (i.e. the node.js way to create an index.html).

How do I copy a file in multiple folders in Ubuntu? [duplicate]

This question already has answers here:
How to copy a file to multiple directories using the gnu cp command
(22 answers)
Closed 6 years ago.
I have directory named "public" in every directory inside Documents.
I want to copy file named File.txt in every directory "public".
cp -r File.txt Documents/*/public/
doesn't seam to work. What should I do?
Well I'm very certain that using cp combined with xargs would do just what you want to do if you know how to use 'em... or you can just write a script to perform just that

In a bash script, how do I reference a separate file to use it in a for loop? [duplicate]

This question already has answers here:
Looping through the content of a file in Bash
(16 answers)
Closed 7 years ago.
I am trying to make a checkpoint management script, which will delete all checkpoints that are over 3 days old for a variety of databases.
I want to keep the code in one script and then keep a list of all the paths to each database I want to manage in another.
How do I reference the file with the list of databases, so I can use them in a for loop?
In the loop, how do I change directories to each directory listed in the text file?
Ex. File with list of databases (db.list):
/directory/directory/databse1
/directory/directory/databse4
/directory/directory/databse10
Ex. Code:
for database in db.list
do
cd $database
code
done
A file listing database paths wouldn't be called a script. It just a text file.
To iterate over lines of a text file, you can read the file:
while read -r database ; do
echo "$database"
done < db.list

Resources