STOP! Don't Name That File Without First Watching This Video.

Sci-Republic
4 followers
7 Video Views·Oct 20, 2022
Today I want to talk to you about file naming conventions. What is a file naming convention?
It is a framework or a set of guidelines that helps you in naming files on your system. Hopefully
you give file names descriptive names, things that help you immediately identify what that file is.
It also helps you search through files on your system because by giving them descriptive names
and by sticking to a convention, a certain set of rules in your naming, you know the kinds of
search patterns to actually search for files on your system, right? Imagine you have a large
database of files and you didn't employ a file naming convention and you used really generic,
non-descriptive names. So you had file 1, file 2, file 3, file 1 million. You have no idea how to
identify these files from each other. You don't know the contents of any of these files because
of the names are also non-descriptive generic names and you really can't search for a particular
file on your system because again the names don't really tell you anything. They don't give you a
clue to what that file actually is. So it's pretty obvious what the benefits of using a file naming
convention are. It's why a lot of large organizations and projects and corporations, they have their own
file naming conventions that they expect all of their people to actually abide by. That way if
you're adding a file to a file server and it's a collaborative effort that a lot of people are
contributing to that everybody is using lowercase rather than uppercase names or hyphens rather than
underscores or whatever it happens to be. They come up with their own guidelines. That way the
naming convention is the same. It just makes organization a lot easier. And for you, the
individual user, it's really important to establish your own file naming convention now before you
start collecting large amounts of files and data on your system. If you're one of these people that
you're going and downloading all of this stuff from the internet and you have terabytes of data
on your file system and you didn't rename this stuff yourself. You just took the names as it
came from you as you downloaded all this stuff on the internet and then you've got some file names
with uppercase, some with lowercase, some with hyphens, underscores, spaces. There's no
consistency to it and ultimately what this will lead to is misplaced files or lost data. So let's
talk about creating your own file naming convention. What are the steps? What do you
actually need to include as part of a file name? The biggest thing you need to do is make sure that
your file names are descriptive in some way. That the name of that file actually gives you some clue
as to what the contents of that file may be. For example, project names. If you've got a GitHub,
GitLab project, you're a programmer or developer, having the name of the project as part of the file
might make sense in some cases. The author name makes sense in a lot of cases. Date, timestamp.
If you work from multiple locations, you create files from multiple locations that all go into a
database. For example, sometimes I do stuff at home, sometimes I do stuff at work or multiple
office locations for a workplace. Sometimes adding the location to the file name makes a lot of
sense. Also, version numbers for projects. Again, typically for software projects, but that makes a
lot of sense as well to always include a version number. But other than adding a descriptive name,
there's other things you need to consider as far as the format of the file name when you're coming
up with your naming convention. Let me switch over to my desktop and let me go ahead and launch a
terminal and I'm going to zoom in. If I do an LS, it'll be easier to do this file management stuff
in a terminal today. That way I can zoom in. That way you can actually see some of the file names
going on in my home directory. Let me scroll back toward the top here. Now, probably 95% of the files
and directories in my home directory, I didn't name myself. These were automatically generated
files and directories that were created when I installed certain programs. And you can see
some of the inconsistency here with the naming of these files and directories. You can see
att underscore backups, and then the directory right after it, audacity dash data, right? One
of them uses an underscore. One of them uses a hyphen or a dash or a minus symbol. If I scroll
down, you can see some other directories. There's a underscore. Here are hyphens. Here is another
underscore. And then of course, all the directory names we've talked about so far looked at,
these were all lowercase. But if I keep on, then we get to the uppercase directories, right? That
begin with the uppercase letter. And I actually have mine organized so that the uppercase and
lowercase are not separated. But a lot of times the LS output in your terminal will actually group
them first, lowercase, then uppercase, which can be confusing. And really, I think one of the most
important rules with any file naming convention, I think everybody should follow this particular
rule. Every file name should be lowercase. It should just be all lowercase. And the reason for
this is because there are some operating systems that are case insensitive when it comes to file
names, such as Microsoft windows, meaning let me get back to the terminal. Let me get to the
command line here. Microsoft windows is going to see file one with a capital F and file one
with a lowercase F as the exact same file, which means Microsoft windows can't handle that. It's a
duplicate, right? So if you're downloading something from the web and one of them is capital
file one, and the next one is lowercase file one, guess what windows is going to do? The second file
one is going to overwrite the first one because it sees them essentially as the exact same name
because it doesn't care about capitalization. Now the problem is every sane operating system
on the planet, such as Linux sees file one with a capital F and file one with a lowercase F as
two different files, which logically makes sense. But again, because windows doesn't and you're
going to interact with windows computers at some point, especially the way we are connected
nowadays through the internet, you know, windows machines, talk to Linux machines,
talk to Mac machines, et cetera, of just because of the limitations of windows,
even if you're not a windows user, I'm not a windows user still just for convention, because
I'm going to put stuff on the internet that windows users may download. It's best just to
stick to all lowercase all the time when it comes to file names. Now I know some of you are going to
say, well, I've got files on my system that begin with capital letters or employee capital letters
somewhere in the name. How do I go back and change those to lowercase? The great thing about
Linux is we have a lot of really cool command line utilities that can really take care of this
problem almost instantly for you. I did a video a while back on renaming files on a Linux system,
and I talked about some command line programs, such as the rename command on Arch Linux. It's
actually Perl-rename, but I think on Debian and Ubuntu based system has just renamed without the
Perl at the beginning. But let me cd into a test directory I have. I've got a test directory that's
got some files in it, and let me create some files with some uppercase letters. So I'm going to do a
touch, and I'm going to do touch test all caps, right? So if I do an ls, you can see there is
test. Well, I can use that Perl-rename command again on Debian or Ubuntu. It's just renamed
without the Perl at the beginning. And then I give it the following y slash capital A through
capital Z and then slash lowercase a dash lowercase z. So essentially, this is a substitution command
standard kind of substitution command you would use with things like sed, for example. It's going
to find every instance of capital A through capital Z and replace it with the lowercase
A through Z. And which files? I mean, I could specify a file, but I'm just going to do it to
every file in this particular directory. So this would be really useful if you had like a thousand
files in a directory and they employed uppercase characters and you wanted to lowercase them all.
You could just run that command. If I do an ls, you can see test all caps is now test lowercase.
How cool is that? Another naming convention that I think everyone should employ is make sure that
you don't include spaces ever in a file or directory name. Spaces do not belong. If you
want to actually separate words, don't use spaces, use either hyphens or underscores because spaces
just cause problems. Let me name something with a space. So maybe I want to do space and then I've
got to escape the space here in the command line. So I've got to do a backslash space to actually
symbolize a space in the name. And then I'll do file. And if I do ls, now I should have space,
file, right, as a name. It's kind of confusing because I've included the word space and then
an actual space and then file. And many people, if you're not employing a file naming convention
already, same thing with the capitalization. You're already got thousands of files on your
system that have spaces. How do you get rid of the spaces? Well, the same rename command we used
before. Remember arch is Perl dash rename, Debian and Ubuntu I believe is just rename. And we could
do the same substitution, except instead of doing A through Z, we're going to do a substitution of
spaces. What are we going to substitute spaces with? Well, in my case, I'm going to choose
hyphens. If you want to, you could choose underscores, but I'll choose the hyphen.
And if I do an ls after running that command, space, file with a space between the words is
now space dash file. Now I used hyphens to separate those words. If you wanted,
you could also use underscores. That's what a lot of people would choose. So if I did that,
you could see space with the hyphen space with the underscore file. And which one is the best?
I prefer hyphens and it seems most people these days prefer hyphens for a separator. The underscore,
the people that use underscores would argue that it's a little easier to read. Well, it's easier
to read in some circumstances. The problem is, especially when you're dealing with hyperlinks,
web links on a web page, and you have file names, directory names that have those underscores. Well,
if the hyperlinks are underlined and then you have underscores as part of the name,
that's hard to read. It's hard to recognize that those underscores are even there.
If these text itself is also underlined. Now, generally speaking, most people are going to
tell you that if you choose hyphens or underscores for your separators, stick with one and only use
one. So if you choose hyphens, always use hyphens, never use underscores. And the same thing with
underscores, always use underscores, never use hyphens. And this really becomes a real argument
in favor of hyphens because if you only use underscores, well, things like date and timestamps
will look a little weird being separated with underscores rather than hyphens. And a lot of
people, what they'll end up doing is they'll make exceptions, right? The file names will include
underscores itself for the timestamp because, you know, that really needs to be a hyphen so it
actually looks right. So you end up employing both. And for me, since you're going to use hyphens
anyway for things like timestamps or for hyphenated words, even that will come up in file names,
just use hyphens all the time. And again, it seems that most people these days just recommend sticking
with hyphens rather than underscores, but there are some big exceptions. For example, Wikipedia
on the web. If you go to Wikipedia right now and click on any webpage in Wikipedia,
they are using underscores for their file names. You can look at the URL for that particular page
and the file name for that URL is going to include underscores rather than hyphens. But again, it's
not about necessarily all of us using the same file naming convention. It's whatever one you pick,
be consistent with yourself. Another good rule of thumb to follow is don't include any special
characters in your file name. So things like an exclamation point, question mark, colon,
curly braces and brackets, things like that. Don't include any of that stuff in a file name.
And the reason for that is because if these files end up on the web and files, even if they're not
intentionally designed to be, for example, a webpage, so many times people are sharing them
through web links and the web does not handle special characters as part as file paths. So
don't include that as part of your file name ever. It just should become a rule. So you never
accidentally have a situation where you create a file name with some of these special characters
that a web browser is going to just throw a fit about. And some special characters that I would
definitely avoid because they have different meanings, especially like on Linux, don't use
a dollar sign as part of a file name because dollar sign at the shell means something, right?
Don't use an ampersand, you know, the and sign don't use that symbol, actually write the word
and if you want it as part of a description for a file name, don't use a slash ever in a file name
because slashes are part of file paths, right? That's the separator between directories in a
file path. So don't use it in a file name. And lastly, let's talk about dates. If you're going
to add a date to a file name, which is a good idea, make sure the date is always in the same
format and the format it needs to be in is ISO 8601, which is year, month, day with the year
being the four digit year. Let me show you this. So we want the date to be in this format right
there, right? So four Y's, two M's, two D's. If you want to separate it, I mean, obviously I
mentioned typically you'd have dates and time separated with hyphens. You could separate it
if you think that's easier to read, which most people probably would, or you can leave it as
all one string. That's fine as well. So let me show you how I would name a file. Maybe I was
writing some show notes for today's presentation, today's video. I would make a name such as file
naming notes, and then I would add a date. For example, today's date. Let me actually
look at what today is. 2022-10-01. That would be the name, right? As a matter of fact, I could go
and create this. I could do touch, and then that file name, if I do it LS, you can see that's a
really nice file name. It's descriptive, tells me exactly what that file contains as far as the
contents, and it has a date. In case I had file naming notes on different days, I know exactly
the date of that particular file. Now, I know a lot of people are going to say that's kind of
tedious, especially adding a date to every single file name you ever create, but you can automate
this process. So I'm in the fish shell today, so let me clear the screen here. One thing I can do,
there's a date command. There's a standard date command that's part of the command line on Linux.
If I do date, there is what the date command typically outputs. Now, if I only want the ISO
8601 format that we talked about earlier, I could use date with the dash capital I flag,
and that actually gives me the exact date in the format I want to be a part of a file name.
What I could do is, inside the shell here in fish, I could set this variable, date,
and I could set that to be date dash I. Now, if you were in bash, you would have to do a different
command. Let me go ahead and run this. In bash, you would do date equals and then dollar sign and
then date dash I. Now, what these commands will do is they tell the shell, as long as I'm in this
current session of this shell, any time I use the variable date, it is the output of the date
dash I command is all we've done. So I've already run set date to date dash I. So now I could create
a file name. I could do what I did before. I could do touch file dash naming dash notes dash,
and then what I could do is now just do dollar sign date. And if I hit enter and I do an LS,
nothing happens because we already have file naming notes 2022 dash 1001. So all it did
is just touch the same file we had already created. So let me actually remove the existing
file and let me rerun that command just to verify that that works. So touch file dash naming dash
notes dash and then dollar sign all caps date. And now LS, and you can see there is the file
that it created. And if you also wanted to add a timestamp along with the date, you could do that.
Just run a man on the date command and you can see all the options as far as output for the
date command. You can include all kinds of stuff as far as time, date, and different formats,
the week of the year, your time zone information if you wanted that. So you could do all of that.
One thing about adding timestamps, if you're going to add the time, make sure that you don't
actually use colons as part of a clock time, because that's something most people would do
because it stands out. It's very readily apparent that, hey, that's the time. Here's the thing.
Colons are a special character we've already talked about. Don't use special characters as
part of your file name. So replace those colons in the time to just using hyphens. So these were
just some basic guidelines, some of my thoughts as far as file naming conventions that I like to
employ. Maybe you guys will find this useful. I do think everyone should spend at least a little bit
of time thinking about how they name things on their system again, just so things are better
organized and just so stuff doesn't get lost on your file system. Now, before I go, I need to thank
a few special people. I need to thank the producers of this episode. Gabe, James, Matt, Maxim, Mehmet,
Mitchell, Paul, West, Moya, Bone, Homie, Alex, Armoredragon, Chuck, Commander Henry, Daiyokai,
George, Lee, Mars, Drum, Neat, Erjan, Alexander, Paul, Peace, Origin, Fedora, Polytech, Realities
for Westward, Private Earl, and Steven, Tools, Devler, and Willie. I messed that up. And I was
speaking in a really high-pitched voice there for no reason at all. But these guys, they're my
patrons. They are the producers of this episode. The show is also brought to you by each and every
one of these fine ladies and gentlemen, all these names you're seeing on the screen right now. These
are all my supporters over on Patreon because I depend on you guys, the community. If you like my
work and you want to see more videos about Linux and free and open source software, subscribe to
DistroTube over on Patreon. Peace, guys. Do you prefer hyphens or underscores? Tell us in the
comments down below.
