Categories: Ubuntu

Automatically Create, Delete, Write to Files on Startup in Ubuntu

This tutorial shows how to automatically create, delete files/folders, and/or write parameters into config files at startup in 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 period.

Advanced users can manually create a script, and run it via either crontab schedule task or custom systemd service. But, here I’m going to show you how to do the trick using tmpfile.d, a built-in configuration for creation, deletion and cleaning of volatile and temporary files.

Sponsored

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:

  • systemd-tmpfiles-setup.service
  • systemd-tmpfiles-clean.timer
  • systemd-tmpfiles-setup-dev.service
  • systemd-tmpfiles-setup-dev-early.service

To check if your system have them, run systemctl status systemd-tmpfiles* in a terminal window:

With them, user can create custom .conf files under one of following directories for custom creation, cleanup and file write operations on startup.

  • /etc/tmpfiles.d/ (recommend)
  • /run/tmpfiles.d/
  • /usr/lib/tmpfiles.d/
  • ~/.config/user-tmpfiles.d/ (not work in my case in Ubuntu 24.04)
  • ~/.local/share/user-tmpfiles.d/ (not work in my case in Ubuntu 24.04)

Create, Remove files or folders automatically on startup

To create a custom tmpfiles.d config file, just open terminal (Ctrl+Alt+T) and run command:

sudo nano /etc/tmpfiles.d/test.conf

In the command, replace test with whatever name as you want. It creates and opens the file in command line nano text editor.

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.

Create Folder:

To automatically create a folder on every boot, for example create “myfolder” under /etc, add lines below:

#Type  Path           Mode   User Group  Age  Argument
d     /etc/myfolder    -    -     -     -    -

Here the first line, starts with “#”, is description line does not function. In the second line, it tells to:

  • d – tells to do create operation if the folder does NOT exist.
  • /etc/myfolder – specify the path of directory to create.
  • ” skip to use default for Mode, User, Group, Age, and Argument.

By default, it uses 0775 for the folder permission ( read (4), write (2), execute (1) for owner & group (4+2+1=7), read, execute (4+1=5) for others), root for user and group.

For choice, you may specify the permission, ownership. And, set an “age” will make it cleanup all content in that folder after every given time period.

#Type  Path                            Mode   User Group  Age  Argument
d     /home/ji/Documents/myfolder      0775    ji    ji     -    -
d     /home/ji/Documents/myfolder/test 0775    ji    ji    10d   -

The timer trigger takes action every 24 hours (except the first action which is 15 minutes later after boot up), so it’s better to set age in days.

If the folder already exists, and you want to remove all its content on startup (then cleanup on given time period), then use D instead with --remove argument.

#Type  Path                            Mode   User Group  Age  Argument
D     /home/ji/Documents/myfolder/test 0775    ji    ji    10d   --remove

Create File:

To automatically create a file at startup, use f or f+. For example:

#Type  Path                     Mode   User Group  Age  Argument
d     /etc/myfolder              -     -      -     -     -
f     /etc/myfolder/xxx.conf     -     -      -      -    some text heren

Add the lines above into a .conf file under /etc/tmpfiles.d directory, will first create “/etc/myfolder” directory if not exist, then create “xxx.conf” file under that directory, and write “some text here” if the file does NOT exist.

Here, n is a line break to make new line. Of course, you can skip argument with ‘-‘ (without quotes), so it just try to create the file if not exist.

Sponsored

If the file already exists, then ‘f’ will do nothing. In the case, you may use f+, which will replace original file content (if any) with the new one you set by argument.

#Type  Path                     Mode   User Group  Age  Argument
d     /etc/myfolder              -     -      -     -     -
f+     /etc/myfolder/xxx.conf     -     -      -      -    some text heren

Write text to File:

To automatically write some text into a file, use w. For example:

#Type    Path                   Mode User Group  Age   Argument
w      /home/ji/Documents/333    -    -     -     -   auto write line 1

If the file does NOT exist, then it does nothing! And, w does NOT remove original file content (if any), but just try to overwrite from very beginning.

Meaning, if the original file content is longer than the new content you want to write to, then it will mess up the file content (See screenshot below). In the case, use f+ instead of w, which will overwrite all previous content.

To keep original file content, and write at the end of the file, use w+ instead.

#Type    Path                   Mode User Group  Age   Argument
w+      /home/ji/Documents/333    -    -     -     -    #This is a new linen
w+      /home/ji/Documents/3*    -    -     -     -    #write one more linen
w+      /home/ji/Documents/33*    -    -     -     -    enable=1

NOTE: w+ will not start a new line if there’s no line break at the end. So, you can add n at beginning of the argument to start new line, also add it at the end for next.

In addition, you may write multiple files by using asterisk in filename. For example, 3* matches all files with their name start with 3. *.txt matches all .txt files in the directory.

NOTE: If you use both “w” and “w+” for writing same file, then only the first one will function! If you use both “f” and “f+” for creating same file, also only the first one will function.

Delete Files or Folders

To automatically delete files on startup, use r.

For example, delete all .txt files, the 123 file, empty folder, in user Documents folder:

#Type    Path                     Mode  User  Group   Age  Argument  
r      /home/ji/Documents/*.txt   -     -     -       -      -
r      /home/ji/Documents/123     -      -     -      -       -
r      /home/ji/an-empty-folder   -      -     -      -       -

To delete folder, and all contained files and sub-folders, use R instead:

#Type    Path                     Mode  User  Group   Age  Argument  
R      /home/ji/Documents/myfolder   -     -     -       -      -

In additiona

The tmpfiles.d configuration can do even more operations on startup, such as create a symlink, copy files or directories, and more! See the freedesktop.org man page for more details!

The post Automatically Create, Delete, Write to Files on Startup in Ubuntu appeared first on Osgrove.

Ubuntu Server Admin

Recent Posts

Join Canonical in Brazil at Dell Technologies Forum São Paulo

Canonical is excited to be a part of the Dell Technologies Forum in São Paulo…

5 days ago

6 facts for CentOS users who are holding on

In 2020, it was announced that CentOS 7 would reach end of life (EoL) by…

5 days ago

What is Ubuntu used for?

The launch of Ubuntu in 2004 was a step-change for everyday users and developers everywhere.…

6 days ago

Ubuntu Weekly Newsletter Issue 862

Welcome to the Ubuntu Weekly Newsletter, Issue 862 for the week of October 13 –…

6 days ago

New Ubuntu Community Council 2024

Merlijn writes: I’m happy to announce the new 2024 Ubuntu Community Council! Heather Ellsworth (~hellsworth1)…

1 week ago