Skip to content
Ameen Abozaid
PublishedBEG-005-L01

Files, folders, and extensions on Windows, macOS, and Linux

  • Lesson 1 of 6
  • 16 min read
  • Beginner
On This Lesson 11 topics

Lesson Overview

Before you install anything, you need to be comfortable with the place where all your work will live: the file system. Every Java program you write starts as a text file with a particular name, sits in a folder, and is found by tools that read that name very literally. A file called Hello.java is a Java source file. A file called Hello.java.txt looks almost the same, but to every tool in this course it is just text, and the Java compiler will not treat it as Java.

This lesson is the first in a module about building your working toolbox, like setting up a workshop: tools on the bench (the JDK and an editor) and a list of where each tool is kept (the PATH, covered in lesson 3). Here you learn the floor plan of the workshop: files, folders, paths, and extensions.

You will learn what a path is, how to read one on Windows, macOS, and Linux, what a file extension is, why Windows hides extensions by default, and how to turn them on. You will also learn the classic beginner trap, the double extension, and how to avoid it.

Menus and settings differ between operating system versions and file managers. The lesson names the usual places and tells you when to look in your own system's search or help instead. Nothing here requires programming. Earlier modules explained what an operating system does (BEG-001); this lesson uses that idea in a practical way.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain what a file, a folder, and a path are, and read an absolute path on Windows, macOS, and Linux.
  • Explain what a file extension is and what it tells the system and the tools you use.
  • Turn on the display of file extensions on your own operating system.
  • Explain why Hello.java and Hello.java.txt are different files, and why the second one is not Java.
  • Show hidden files, and recognize dot-files such as .gitignore.
  • List a folder's contents and print the current folder in a terminal.
  • Choose clean, predictable folder and file names for course work.

Written Explanation

Files, folders, and the tree

A file is a named piece of stored data: a document, a photo, a program, a text file with Java code. A folder (also called a directory) is a container that holds files and other folders. Because folders can hold folders, the whole file system forms a tree: one starting point, called the root, with branches below it.

A path is the address of a file or folder in that tree: the list of folders you pass through to reach it. The path answers the question "where is it?". Two files can have the same name if they are in different folders, because their paths differ.

The three operating systems write paths slightly differently:

WindowsmacOSLinux
RootA drive letter such as C:\A single root /A single root /
Separator between folders\ (backslash)/ (forward slash)/ (forward slash)
Typical home folderC:\Users\Sara/Users/Sara/home/sara
Example project pathC:\Users\Sara\java-course\hello/Users/Sara/java-course/hello/home/sara/java-course/hello

macOS and Linux are both Unix-like systems, which is why their paths look alike. Folder and file names on Linux are usually case-sensitive (Hello.java and hello.java are two different files), and on Windows and macOS they usually are not (they cannot sit side by side in one folder, by default). The safest habit is to treat case as meaningful everywhere. The exact behaviour can depend on how a disk is formatted, so do not rely on it.

An absolute path starts from the root and is complete, such as /home/sara/java-course/hello. A relative path starts from where you currently are, such as hello/Hello.java ("the file Hello.java inside the folder hello, which is inside my current folder"). In a terminal, the folder you are currently in is called the current directory or working directory.

The extension: the end of the name

A file name often ends in a dot and a few letters: notes.txt, photo.png, Hello.java. That ending is the file extension. It is not magic, it is part of the name. But the system and your tools use it as a clue to what kind of data the file holds and what should open it.

ExtensionUsually meansIn this course
.txtPlain textNotes, simple files
.javaJava source code, written by youEvery program you write
.classCompiled Java, produced by the compilerYou will see these after compiling
.jarA package of compiled Java filesYou will meet it later
.mdMarkdown textREADME files
.exeA Windows programInstallers

The Java compiler, javac, reads source files whose names end in .java and produces .class files, as Oracle's documentation for javac describes. The compiler does not look inside a file to guess what it is. The name must say .java.

There is also a convention inside Java itself: a public class named Hello is normally stored in a file called Hello.java. The Java Language Specification says that a system may require this, and the standard compiler does, so for your purposes the file name and the public class name must match, including capital letters.

Why Windows hides extensions, and why you should turn them on

By default, Windows File Explorer hides the extension for file types it knows. A file named Hello.java may appear as Hello with an icon. This is meant to look tidy, but for a programmer it is dangerous, because the extension is exactly the part you need to see.

To turn extensions on in Windows File Explorer, the usual way is: open a folder, choose View, then Show, then tick File name extensions. In older Windows versions the View tab has a File name extensions checkbox directly. If you cannot find it, open Folder Options from the View or the "..." menu and untick "Hide extensions for known file types". Menu names change between Windows versions, so use the Windows search box for "file name extensions" if yours looks different.

On macOS, extensions can be shown for all files in Finder, Settings, Advanced, Show all filename extensions (older macOS versions call this Preferences). For one file, select it, press Cmd+I, and look under Name & Extension, as Apple's documentation describes.

On Linux, file managers usually show full names, including extensions, by default. In a terminal you always see the full name.

The double-extension trap

Now the key problem. Suppose extensions are hidden, and you create a text file in Notepad and name it Hello.java. Many programs add their own default extension when saving. The result can be a file whose real name is Hello.java.txt. With extensions hidden, Explorer shows Hello.java and everything looks right. But the real extension is the last one: .txt. The file is plain text.

The compiler does not accept it:

text
javac Hello.java
error: file not found: Hello.java

(The exact wording of the message depends on the JDK version.) The file on disk is Hello.java.txt, so there is no file named Hello.java to compile. If you tried javac Hello.java.txt, the compiler would also refuse, because the name does not end in .java. (The exact error text varies, so read it for the file name rather than memorizing it.)

The rule to remember: the extension is whatever comes after the last dot. In Hello.java.txt it is .txt; .java is just part of the name.

How to avoid this:

  1. Turn on extensions before you start.
  2. In a Save dialog, choose "All files" as the type, not "Text documents", when your editor offers it, and type the full name yourself.
  3. Better, create Java files from an IDE (lessons 4 and 5), which creates the right name for you.
  4. When you rename a file and the system warns that changing the extension may make the file unusable, read the warning. In this case, you do want the new extension, so confirm it. The warning exists because renaming a file does not convert its data. It only changes the name.

Hidden files and dot-files

Some files are hidden by default. On macOS and Linux, any name that starts with a dot, such as .gitignore or .bashrc, is a dot-file. By convention these are configuration files, and file managers hide them to avoid clutter. The name .gitignore belongs to Git, a tool you will learn in a later module.

To see them:

  • Terminal (macOS and Linux): ls -a lists all files, including dot-files.
  • Finder on macOS: pressing Cmd+Shift+. is widely reported to toggle hidden files. I could not confirm this on Apple's own documentation page, so if it does not work, search Apple Support for "show hidden files".
  • Linux file managers: many have a "Show hidden files" option in the menu, often toggled with Ctrl+H. This varies, so check your file manager's menu.
  • Windows: hidden files are controlled by a separate setting from extensions. In File Explorer's View menu you can choose to show hidden items. Windows does not use a leading dot to hide files, so .gitignore is visible there.

Looking at folders from a terminal

A terminal is a window where you type commands. You used one in the module on the web (BEG-003). Three commands are enough for now:

TaskWindows (Command Prompt)Windows (PowerShell)macOS and Linux (Bash, Zsh)
Show where I amcdpwdpwd
List the files heredirdir or lsls
Move into a foldercd hellocd hellocd hello
Move up one levelcd ..cd ..cd ..

In PowerShell, dir and ls are both aliases for the same command, which is why either works. In Command Prompt, cd with no argument prints the current directory. Listing a folder in a terminal shows full file names, so it is a reliable way to check an extension.

Names that save you trouble

Keep all course work in one folder, for example java-course in your home folder, with one subfolder per project. Use letters, digits, hyphens, and underscores, and avoid spaces, which some tools need quoted. Name Java source files after their public class, with a capital first letter: Hello.java.

Examples

Example 1: Reading paths (simple)

Here are three paths to the same idea, "the file Hello.java inside the folder hello":

text
C:\Users\Sara\java-course\hello\Hello.java
/Users/Sara/java-course/hello/Hello.java
/home/sara/java-course/hello/Hello.java

Read the first one from left to right: drive C:, folder Users, folder Sara, folder java-course, folder hello, file Hello.java. The extension is .java, so this is a Java source file. If you are inside java-course in a terminal, the relative path to the same file is hello\Hello.java on Windows and hello/Hello.java on macOS and Linux.

Example 2: Making the double extension visible (practical)

This is a hands-on check you can do in a few minutes with a throwaway folder.

  1. Create a folder named ext-test in your course folder.
  2. With extensions hidden, create a new text file in it and name it Hello.java. On Windows, use the right-click New menu and choose Text Document, then type the name.
  3. Look at the file in the folder view. It may look like Hello.java.
  4. Now turn extensions on (as described above). Look again. If the file is really Hello.java.txt, you will see the full name now.
  5. Open a terminal in the folder and list it: dir or ls. You will see the same full name.
  6. Rename the file so that the name ends in exactly .java. Confirm the rename warning.

The experiment shows that the same file can look like two different names depending on one display setting, and that the terminal never hides it. What you see depends on your system version and settings, so your steps in step 2 may produce Hello.java directly. That is fine: the point is to know how to check.

Example 3: A small project that compiles only when names are right (realistic)

Create a folder greeter and, inside it, a file whose real name is Greeter.java with this content:

java
public class Greeter {
    public static void main(String[] args) {
        System.out.println("Workshop ready.");
    }
}

You have not installed the JDK yet (that is the next lesson), so do not compile it now. Keep the folder, because you will use it in lesson 2 to prove that your installation works. Notice three things:

  1. The public class is Greeter, so the file must be Greeter.java, with the same capital G.
  2. The folder name greeter is lowercase and has no spaces. The folder name does not have to match the class name. Only the file name does.
  3. After a successful compile you will see a new file Greeter.class in the same folder. If you do not see a .class file, check the extension first.

Backend Insight

Backend work is full of files whose names carry meaning. A project has .java source files, .properties or .yml configuration files, .sql scripts, .json data, and .jar packages. Build tools and servers find these by name and extension. A wrongly named configuration file does not produce a clear error. The program simply behaves as though the file does not exist, and it may silently use default values.

Paths matter even more on servers. Backend servers mostly run Linux, so paths use / and file names are usually case-sensitive. A developer on Windows may write code that opens Config.json while the file is named config.json. On a Windows laptop this may work, and on the Linux server it fails. Keeping a strict habit of exact names and visible extensions prevents this class of problem. The same worry returns in a later module on environment differences, and it starts with the habit you build now.

Finally, dot-files are everywhere in professional projects (.gitignore, .env, and others). Knowing that they exist and how to reveal them will save you from searching for files that are "missing" only because they are hidden.

Common Mistakes

Mistake 1: Leaving extensions hidden

  • Incorrect: working on Windows with extensions hidden and trusting what Explorer shows.
  • Correct: turn on "File name extensions" first, and check names in a terminal when something does not work.

Mistake 2: Creating Hello.java.txt by accident

What goes wrong: the compiler cannot find Hello.java. Why it happens: a text editor appended .txt while the extension was hidden. How to avoid it: show extensions, choose "All files" when saving, and read the name with dir or ls.

Mistake 3: Renaming to change the type

Renaming notes.txt to notes.java changes only the name, not the content. Rename a file to correct its name, not to convert it.

Mistake 4: File name and class name do not match

  • Incorrect: a public class Greeter saved in greeter.java or Hello.java.
  • Correct: Greeter.java, the same capitalization as the class name.

Exercises

Try each exercise yourself before opening the solutions.

Level A: Foundation

A1. In your own words, what is the difference between a file and a folder? What is a path?

Show solution A1

A1. A file is a named piece of stored data. A folder is a container that holds files and other folders. A path is the address of a file or folder in the tree of folders, listing the folders you pass through to reach it.

A2. Write the extension of each file name: report.pdf, Main.java, data.backup.csv, Hello.java.txt.

Show solution A2

A2. .pdf, .java, .csv (the extension is what follows the last dot; .backup is part of the name), and .txt (the file is plain text although its name contains .java).

A3. Which of these paths is an absolute path on Linux: docs/notes.txt, /home/sara/docs/notes.txt, ../notes.txt?

Show solution A3

A3. /home/sara/docs/notes.txt. The others are relative paths. The first starts from the current folder and the third starts from the parent of the current folder.

Level B: Application

B1. Turn on file extensions in your operating system. Then create a folder ext-lab with two empty files side by side: Hello.java and Hello.txt. Describe what you did and what you see, or take a screenshot.

Show solution B1

B1. Your answer depends on your system. On Windows: File Explorer, View, Show, File name extensions. On macOS: Finder Settings, Advanced, Show all filename extensions. Success means that both names appear in full, Hello.java and Hello.txt, and that the terminal listing (dir or ls) shows the same names. If you used a text editor, check that it did not add a second extension.

B2. A learner says: "I saved Hello.java but javac Hello.java says the file is not found. Explorer shows Hello.java." Give two possible causes and how you would check each from a terminal.

Show solution B2

B2. Cause 1: the real name is Hello.java.txt because Explorer hides the last extension. Check with dir or ls in the folder. Cause 2: the terminal is in a different folder than the file. Check with cd or pwd, then dir or ls. Another possible cause is a different capitalization such as hello.java, which a case-sensitive system treats as a different file, and which ls would also reveal.

B3. You are in /home/sara/java-course. Write the relative path to /home/sara/java-course/hello/Hello.java. Then write the path of the folder one level above where you are.

Show solution B3

B3. hello/Hello.java is the relative path. The folder one level above is /home/sara, and you can reach it with cd ...

Level C: Challenge

C1. A teammate on Windows runs a Java program that opens data/Config.json. It works on their laptop and fails on the Linux server, where the file is named config.json. Explain why, and say what rule you would agree on for the whole team.

Show solution C1

C1. The server's file system is case-sensitive, so Config.json and config.json are different names, while the laptop's file system treated them as the same (this is the usual default on Windows, though the behaviour can depend on the disk). The program finds the file on the laptop and not on the server. A useful team rule: use one exact, consistent name, preferably all lowercase, and write it in code exactly as it appears on disk. Testing in an environment close to the server also catches such errors.

Mini Challenge

Run a "name audit" on your own computer.

  1. Turn on file extensions and hidden items.
  2. Open your downloads or documents folder and find three files with different extensions. For each, write down the extension and what kind of file it is.
  3. Find one file with more than one dot in its name, and say what its real extension is.
  4. In a terminal, go to your course folder, list it, and write down the path (cd on Windows Command Prompt, pwd elsewhere).
  5. Create the folder greeter and the file Greeter.java from Example 3, and confirm in the terminal that its real name is Greeter.java.

Check your work: you can point to the real extension in each name, the terminal listing shows Greeter.java and not Greeter.java.txt, and you can read the absolute path of your course folder.

Summary & Next Lesson

Key takeaways

  • A file system is a tree of folders. A path is the address of a file, written differently on Windows (\) and on macOS and Linux (/).
  • The extension is whatever comes after the last dot. It tells the system and the tools what kind of file it is.
  • Windows hides extensions by default. Turn them on, because Hello.java.txt is a text file, not a Java file.
  • Dot-files are hidden on macOS and Linux, and you can show them in a terminal with ls -a.
  • A public class Hello belongs in Hello.java, with the same capitalization.
  • A terminal listing shows real names, so it is the reliable check.

Next lesson

You now know where your files live and how to read their names. In the next lesson, "Installing the JDK (all three operating systems)", you install the Java Development Kit and check it with two version commands. You will use the Greeter folder to prove that it works.

Self-check

  1. What is the extension of Hello.java.txt?
  2. Why should you turn on file extensions on Windows?
  3. What does ls -a show that ls does not?
  4. Which separator do Windows paths use, and which do macOS and Linux use?
  5. A public class is called Order. What must the file be called?
Show answers
  1. .txt. The extension is what follows the last dot.
  2. Because Windows hides extensions by default, you could not tell Hello.java from Hello.java.txt. With extensions visible you see the real file type.
  3. It shows hidden files, including dot-files such as .gitignore.
  4. Windows uses a backslash \. macOS and Linux use a forward slash /.
  5. Order.java, with the same capitalization.

References

  • Oracle, javac command reference (JDK 21). Supports the statement that javac reads Java source files and produces .class files, with .java as the source extension.
  • Oracle, Java Language Specification SE 21, chapter 7: Packages and Modules. Supports the statement that a host system may require a public top-level class to be stored in a file named after it with an extension such as .java. That the standard compiler enforces this is common practice that I did not verify in a separate source.
  • Apple Support, Show or hide filename extensions on Mac. Supports the Finder steps for showing extensions. The page does not mention a shortcut for hidden files, so the Cmd+Shift+. tip is hedged in the text.
  • Microsoft's Windows steps (View, Show, File name extensions), the Ctrl+H tip for Linux file managers, and the behaviour of case sensitivity on different disks are described from general knowledge and from third-party how-to pages. I could not confirm them on a Microsoft or Linux-vendor page in this session, so the lesson hedges them and tells readers to use their system's search or help.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.