Directory conventions for extra files
Question:
I have written a (python) script which identifies all node on its subnet (NAT subnet) and send an email if the IP address doesn’t exist within a record. For this size of project, I don’t want to run a database. I think a text file delimited by new lines with an IP address on each would be suitable. What is the appropriate convention for the directory this file would be placed?
Answers:
There are no real conventions and the answers to this question may be a little opinionated and a lot depends on the script’s exact use case.
You have following choices:
- datafile in current working directory
- datafile in same directory as script
- datafile in a folder (that will be created if not existant) relative to the home directory
- like three but with an additional profile name
- in a system folder like /var/run/toolname
- explicit parameter to the script specifies file name
1.) Current working directory
Con:
- It forces you to cd into the directory where you have your data, otherwise script won’t work or create new data from scratch
Pro:
- As the same user on the same machine you could have multiple projects each using its own data.
2.) datafile in same directory as script
Pro:
- you don’t have to cd anywhere. you know where to find the data.
Con:
- you need write permissions to the directory where the script is located (might be a security concern if executed by a web server and if an exploit is found)
- only one data file per script
- no two users can execute the script with different data
3.) datafile in a folder (that will be created if not existant) relative to the home directory
Pro:
- script in one directory but each user has its own data.
- if script directory is removed, script is uninstalled and reinstalled the data will persist.
- no special write permissions for script dir required.
Con:
- you remove the script dir, data persists and might never be deleted.
- one user cannot run multiple instances use the script for multiple projects
- clean up of data required after uninstall of script
4.( like 3.) but with an additional profile/project name
Pro:
- same as for 3.)
- one can run multiple projects / profiles in parallel (firefox for example allows to be run with profiles, which are all stored in
~/.mozilla/firefox
, but each profile creates its own sub directory ~/.mozilla/firefox/xxxx.profilename
Con:
- same as for 3.)
5.) in a system folder like /var/run/toolname
Pro:
- could be nice for tools, that run as service / server
Con:
- require admin privileges (at least during installation)
6.)
Pro:
- you can do whatever you want
Con:
- you must specify an additional paramater (except you have the behaviour of the other suggestions as default and the option only if you want to have different behaviour.
All solutions should be fine for you.
For your kind of tool 5.) 3.) 2.) 6.) are probably the better options. I assume your script will be run as a cron job or something alike.
I have written a (python) script which identifies all node on its subnet (NAT subnet) and send an email if the IP address doesn’t exist within a record. For this size of project, I don’t want to run a database. I think a text file delimited by new lines with an IP address on each would be suitable. What is the appropriate convention for the directory this file would be placed?
There are no real conventions and the answers to this question may be a little opinionated and a lot depends on the script’s exact use case.
You have following choices:
- datafile in current working directory
- datafile in same directory as script
- datafile in a folder (that will be created if not existant) relative to the home directory
- like three but with an additional profile name
- in a system folder like /var/run/toolname
- explicit parameter to the script specifies file name
1.) Current working directory
Con:
- It forces you to cd into the directory where you have your data, otherwise script won’t work or create new data from scratch
Pro:
- As the same user on the same machine you could have multiple projects each using its own data.
2.) datafile in same directory as script
Pro:
- you don’t have to cd anywhere. you know where to find the data.
Con:
- you need write permissions to the directory where the script is located (might be a security concern if executed by a web server and if an exploit is found)
- only one data file per script
- no two users can execute the script with different data
3.) datafile in a folder (that will be created if not existant) relative to the home directory
Pro:
- script in one directory but each user has its own data.
- if script directory is removed, script is uninstalled and reinstalled the data will persist.
- no special write permissions for script dir required.
Con:
- you remove the script dir, data persists and might never be deleted.
- one user cannot run multiple instances use the script for multiple projects
- clean up of data required after uninstall of script
4.( like 3.) but with an additional profile/project name
Pro:
- same as for 3.)
- one can run multiple projects / profiles in parallel (firefox for example allows to be run with profiles, which are all stored in
~/.mozilla/firefox
, but each profile creates its own sub directory~/.mozilla/firefox/xxxx.profilename
Con:
- same as for 3.)
5.) in a system folder like /var/run/toolname
Pro:
- could be nice for tools, that run as service / server
Con:
- require admin privileges (at least during installation)
6.)
Pro:
- you can do whatever you want
Con:
- you must specify an additional paramater (except you have the behaviour of the other suggestions as default and the option only if you want to have different behaviour.
All solutions should be fine for you.
For your kind of tool 5.) 3.) 2.) 6.) are probably the better options. I assume your script will be run as a cron job or something alike.