Chapter 27 Files and the File System
As we will see later in this text, having the abiliity to navigate the file system, open files and create files all within the Juilia environment will come in handy. In this chapter, we will learn the basics of how to do this.
Section 27.1 Directories
When either working in the REPL or in a notebook, the kernel has a working directory. This is set by default when you start julia. To determine what the working directory is, use the
pwd command by running pwd(). It might return something like:
"/Users/juliauser/julia
And this will look more like the windows system on that type of computer. To get a list (vector) of files and directories in the current directory enter
readdir() for read directory. It will return a vector of strings (directory names and files). Let’s say it looks like:
4-element Vector{String}:
"ch08.ipynb"
"data.csv"
"hw"
"menu.json"
and just based on files generally have extensions (after the
.) and directories do not, it appears that hw is a directory. If there are no arguments on readdir(), then it will show the files and directories inside the working directory. If you include a string, it will list the contents of the directory.
Let’s now change the directory to the "hw" directory. with
cd("hw") for change directory. If you now do pwd() might see "/Users/juliauser/julia/hw". If the cd command has no arguments, then the working directory is changed to the user’s home directory.
Another important directory change is that of switching to the parent directory of the working directory. We do this with
cd("..").
Section 27.2 Working with Files and Directories
We also would like to learn how to work with files, such as renaming, copying, deleting and creating files.
Subsection 27.2.1 Creating Files
To create a file, use the
write command. A simple example is
write("hello.txt", "Hello!")
You can use vscode or your file manager (Finder, explorer) to examine the file
"hello.txt" that is in your working directory. It should have the single line "Hello!" in it. Open it to see that it is true.
Subsection 27.2.2 Creating a Directory
We can create a directory with the
mkdir command. For example mkdir("my-dir") will create the directory "my-dir". If one now performs the command readdir(), the result is:
6-element Vector{String}:
"ch08.ipynb"
"data.csv"
"hello.txt"
"hw"
"menu.json"
"my-dir"
Additionally, there is a
mkpath command that can create a set of nested directories. This is helpful if a directory structure needs to be set up in a certain way. For example if we do mkpath("my/test/dir"), then examine the File Explorer or Finder and you will see the next directories "my" in the current working directory, then inside of "my" is a directory called "test" then inside a directory called "dir".
Subsection 27.2.3 Renaming Files and Directories
We use the
mv command to rename files or move files. This is a command that originated with Unix. Let’s rename the file "hello.txt" that we just made to "hello2.txt" with the command move("hello.txt", "hello2.txt"). Examine with the File Explorer or Finder to ensure this happened.
The
mv command also works with directories. If we want to change the directory "my-dir" to "utils", the command my("my-dir", "utils") will do this.
Subsection 27.2.4 Moving Directories
Moving a directory can also be accomplished with the
mv command, but works a bit different than the Unix variety. If we have the directories "utils" and "hw" and want to move "utils" to within "hw", we can do this with mv("utils","hw/utils") indicating that the new directory name is "hw/utils" or "utils" within "hw".
