linuxunix

Why do some files names in Linux start with numbers?


Any reason behind naming some files starting with numbers? I know about naming conventions, but I wonder why not have it named haproxy? Why 30-haproxy.conf and not29.5_haproxy.conf?

[@happiness ~]$ ls /etc/rsyslog.d/
00-iptables.conf  21-cloudinit.conf  30-haproxy.conf  haproxy.conf  listen.conf

Solution

  • Read about path_resolution(7) to understand how general file paths can be. The kernel cares only about / and NUL .... and understands specially . and .. entries. File paths could even contain a single control character (like a newline) but that is disgusting.

    In practice, many scripts and programs adopt some conventions (and FHS document some of these conventions).

    (today, file paths are in UTF-8, but the kernel is not aware of that convention, and you'll better use only printable, non-space, ASCII characters; I recommend using only letters, digits, and a few other punctuation - but avoid *, \, ? and perhaps $ and ~ because they have special meaning to the shell)

    Since shell globbing (see glob(7)) is sorting filenames in alphanumerical order, having a numerical prefix makes sense to "order" them, assuming that the program (e.g. some shell) reading that directory is ordering entries in alphanumerical order. You then prefer to have all these numerical prefixes have the same number of digits (in your example, two).

    Be aware that when the underlying directory is read (using opendir(3) + readdir(3) etc...) the directory entries are unordered and presented in some arbitrary order. It is the program reading that directory (your shell, or ls(1), etc...) which is sorting the entries. Sometimes you can disable that sort (e.g. with ls -f) or get another sorting (e.g. chronological with ls -t)