{"id":46654,"date":"2024-05-28T14:37:18","date_gmt":"2024-05-28T14:37:18","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=46654"},"modified":"2024-10-06T13:02:33","modified_gmt":"2024-10-06T13:02:33","slug":"auto-create-delete-write-file-startup","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/05\/auto-create-delete-write-file-startup\/","title":{"rendered":"Automatically Create, Delete, Write to Files on Startup in Ubuntu"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-35890\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/11\/file-manager-icon.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This tutorial shows how to <b>automatically create, delete files\/folders, and\/or write parameters into config files at startup<\/b>\u00a0in Ubuntu and other Linux using systemd.<\/p>\n<p>This can be useful if some configuration do not persistent and reset to default on every boot, or you want to clean up something either on every boot or after every time period.<\/p>\n<p>Advanced users can manually create a script, and run it via either crontab schedule task or custom systemd service. But, here I&#8217;m going to show you how to do the trick using <code>tmpfile.d<\/code>, a built-in configuration for creation, deletion and cleaning of volatile and temporary files.<\/p>\n<p><!--more--><\/p>\n<p>In Ubuntu, Debian, Fedora, and many other Linux Distros, there are a few systemd services designed to automatically create volatile files and directories, and daily cleanup of temporary directories. They include:<\/p>\n<ul>\n<li>systemd-tmpfiles-setup.service<\/li>\n<li>systemd-tmpfiles-clean.timer<\/li>\n<li>systemd-tmpfiles-setup-dev.service<\/li>\n<li>systemd-tmpfiles-setup-dev-early.service<\/li>\n<\/ul>\n<p>To check if your system have them, run <code>systemctl status systemd-tmpfiles*<\/code> in a terminal window:<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfiles-services.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46655\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfiles-services-700x598.webp\" alt=\"\" width=\"610\" height=\"521\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfiles-services-700x598.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfiles-services-300x256.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfiles-services-768x656.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfiles-services.webp 874w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><br \/>\nWith them, user can create custom <code>.conf<\/code> files under one of following directories for custom creation, cleanup and file write operations on startup.<\/p>\n<ul>\n<li><b>\/etc\/tmpfiles.d\/ (recommend)<\/b><\/li>\n<li>\/run\/tmpfiles.d\/<\/li>\n<li>\/usr\/lib\/tmpfiles.d\/<\/li>\n<li>~\/.config\/user-tmpfiles.d\/ (not work in my case in Ubuntu 24.04)<\/li>\n<li>~\/.local\/share\/user-tmpfiles.d\/ (not work in my case in Ubuntu 24.04)<\/li>\n<\/ul>\n<h3>Create, Remove files or folders automatically on startup<\/h3>\n<p>To create a custom <code>tmpfiles.d<\/code> config file, just open terminal (<strong>Ctrl+Alt+T<\/strong>) and run command:<\/p>\n<pre>sudo nano \/etc\/tmpfiles.d\/test.conf<\/pre>\n<p>In the command, replace <code>test<\/code> with whatever name as you want. It creates and opens the file in command line nano text editor.<\/p>\n<p>When the file opens, add following lines for corresponding operations. After editing, you may press Ctrl+S to save file, then Ctrl+X to exit.<\/p>\n<h4>Create Folder:<\/h4>\n<p><b>To automatically create a folder on every boot<\/b>, for example create &#8220;<code>myfolder<\/code>&#8221; under <code>\/etc<\/code>, add lines below:<\/p>\n<pre>#Type  Path           Mode   User Group  Age  Argument\r\nd     \/etc\/myfolder    -    -     -     -    -<\/pre>\n<p>Here the first line, starts with &#8220;#&#8221;, is description line does not function. In the second line, it tells to:<\/p>\n<ul>\n<li><b>d<\/b> &#8211; tells to do create operation if the folder does NOT exist.<\/li>\n<li><b>\/etc\/myfolder<\/b> &#8211; specify the path of directory to create.<\/li>\n<li>&#8220;<b>&#8211;<\/b>&#8221; skip to use default for Mode, User, Group, Age, and Argument.<\/li>\n<\/ul>\n<p>By default, it uses <code>0775<\/code> for the folder permission ( read (4), write (2), execute (1) for owner &amp; group (4+2+1=7), read, execute (4+1=5) for others), <code>root<\/code> for user and group.<\/p>\n<p>For choice, you may specify the permission, ownership. <b>And, set an &#8220;age&#8221; will make it cleanup all content in that folder after every given time period.<\/b><\/p>\n<pre>#Type  Path                            Mode   User Group  Age  Argument\r\nd     \/home\/ji\/Documents\/myfolder      0775    ji    ji     -    -\r\nd     \/home\/ji\/Documents\/myfolder\/test 0775    ji    ji    10d   -<\/pre>\n<p>The timer trigger takes action every 24 hours (except the first action which is 15 minutes later after boot up), so it&#8217;s better to set age in days.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-folder.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46656\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-folder-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-folder-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-folder-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-folder.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>If the folder already exists, and you want to remove all its content on startup (then cleanup on given time period), then use <b>D<\/b> instead with <code>--remove<\/code> argument.<\/p>\n<pre>#Type  Path                            Mode   User Group  Age  Argument\r\nD     \/home\/ji\/Documents\/myfolder\/test 0775    ji    ji    10d   --remove<\/pre>\n<h4>Create File:<\/h4>\n<p>To automatically create a file at startup, use <b>f<\/b> or <b>f+<\/b>. For example:<\/p>\n<pre>#Type  Path                     Mode   User Group  Age  Argument\r\nd     \/etc\/myfolder              -     -      -     -     -\r\nf     \/etc\/myfolder\/xxx.conf     -     -      -      -    some text here\\n<\/pre>\n<p>Add the lines above into a <code>.conf<\/code> file under <code>\/etc\/tmpfiles.d<\/code> directory, will first create &#8220;<code>\/etc\/myfolder<\/code>&#8221; directory if not exist, then create &#8220;<code>xxx.conf<\/code>&#8221; file under that directory, and write &#8220;some text here&#8221; if the file does NOT exist.<\/p>\n<p>Here, <b>\\n<\/b> is a line break to make new line. Of course, you can skip argument with &#8216;-&#8216; (without quotes), so it just try to create the file if not exist.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-createfile.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46658\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-createfile-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-createfile-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-createfile-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-createfile.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><strong>If the file already exists, then &#8216;f&#8217; will do nothing.<\/strong> In the case, you may use <b>f+<\/b>, which will replace original file content (if any) with the new one you set by argument.<\/p>\n<pre>#Type  Path                     Mode   User Group  Age  Argument\r\nd     \/etc\/myfolder              -     -      -     -     -\r\nf+     \/etc\/myfolder\/xxx.conf     -     -      -      -    some text here\\n<\/pre>\n<h4>Write text to File:<\/h4>\n<p>To automatically write some text into a file, use <b>w<\/b>. For example:<\/p>\n<pre>#Type    Path                   Mode User Group  Age   Argument\r\nw      \/home\/ji\/Documents\/333    -    -     -     -   auto write line 1<\/pre>\n<p>If the file does NOT exist, then it does nothing! And, <b>w<\/b> does NOT remove original file content (if any), but <strong>just try to overwrite from very beginning<\/strong>.<\/p>\n<p>Meaning, if the original file content is longer than the new content you want to write to, then\u00a0it will mess up the file content (See screenshot below). In the case, use <b>f+<\/b> instead of <b>w<\/b>, which will overwrite all previous content.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmp-writefile.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46657\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmp-writefile-700x472.webp\" alt=\"\" width=\"610\" height=\"411\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmp-writefile-700x472.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmp-writefile-300x202.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmp-writefile-768x517.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmp-writefile.webp 1119w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>To keep original file content, and write at the end of the file, use <b>w+<\/b> instead.<\/p>\n<pre>#Type    Path                   Mode User Group  Age   Argument\r\nw+      \/home\/ji\/Documents\/333    -    -     -     -    #This is a new line\\n\r\nw+      \/home\/ji\/Documents\/3*    -    -     -     -    #write one more line\\n\r\nw+      \/home\/ji\/Documents\/33*    -    -     -     -    enable=1<\/pre>\n<p>NOTE: <b>w+<\/b> will not start a new line if there&#8217;s no line break at the end. So, you can add <b>\\n<\/b> at beginning of the argument to start new line, also add it at the end for next.<\/p>\n<p>In addition, you may write multiple files by using asterisk in filename. For example, <code>3*<\/code> matches all files with their name start with 3. <code>*.txt<\/code> matches all .txt files in the directory.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-wplus.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46659\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-wplus-700x352.webp\" alt=\"\" width=\"610\" height=\"307\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-wplus-700x352.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-wplus-300x151.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-wplus-768x386.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/05\/tmpfile-wplus.webp 1310w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>NOTE: If you use both &#8220;w&#8221; and &#8220;w+&#8221; for writing same file, then only the first one will function! If you use both &#8220;f&#8221; and &#8220;f+&#8221; for creating same file, also only the first one will function.<\/b><\/p>\n<h4>Delete Files or Folders<\/h4>\n<p>To automatically delete files on startup, use <b>r<\/b>.<\/p>\n<p>For example, delete all <code>.txt<\/code> files, the <code>123<\/code> file, empty folder, in user Documents folder:<\/p>\n<pre>#Type    Path                     Mode  User  Group   Age  Argument  \r\nr      \/home\/ji\/Documents\/*.txt   -     -     -       -      -\r\nr      \/home\/ji\/Documents\/123     -      -     -      -       -\r\nr      \/home\/ji\/an-empty-folder   -      -     -      -       -<\/pre>\n<p>To delete folder, and all contained files and sub-folders, use <b>R<\/b> instead:<\/p>\n<pre>#Type    Path                     Mode  User  Group   Age  Argument  \r\nR      \/home\/ji\/Documents\/myfolder   -     -     -       -      -<\/pre>\n<h3>In addition<\/h3>\n<p>The <code>tmpfiles.d<\/code> configuration can do even more operations on startup, such as create a symlink, copy files or directories, and more! See the <a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/latest\/tmpfiles.d.html\" target=\"_blank\" rel=\"noopener\">freedesktop.org man page<\/a> for more details!<\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to automatically create, delete files\/folders, and\/or write parameters into config files at startup\u00a0in Ubuntu and other Linux using systemd. This can be useful if some configuration do not persistent and reset to default on every boot, or you want to clean up something either on every boot or after every time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":35890,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2087],"class_list":["post-46654","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-ubuntu-24-04"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/46654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=46654"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/46654\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/35890"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=46654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=46654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=46654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}