You will work extensively with files and folders to organise your work, making it easy to find things and share them with others. Understanding how to work with the file system is essential and it will help you avoid many mistakes in the future. Additionally, working with files and folders using the CLI can be much faster than dragging and dropping icons on your screen.
In the last chapter (Intro to the CLI), you already learned how to interact with the file system using the CLI. In this chapter we will highlight a few important details.
<aside> 💭
Folder and directory mean the same thing. We'll use both terms interchangeably.
</aside>
A case-sensitive file system treats filenames or folders with different capitalisation as distinct files or folders. For example, on a case-sensitive system, readme.txt, Readme.txt, and README.TXT are three different files. On a case-insensitive file system, the OS treats these files as the same.
Depending on your operating system, your file system may be case-sensitive or case-insensitive.
<aside> ⚠️
A common mistake is to write a program on Windows or macOS without being strict about file and folder name casing, only to find it stops working when run on a Linux server.
Always be strict with your file and folder name casing, even on a case-insensitive file system.
</aside>
When naming files and folders, following these tips will help you avoid common problems and make your work easier to manage:
-) or underscores (_) instead of spaces. For example, use my-project or my_project instead of my project.@, #, %, &, or * which can have special meanings in the CLI.pwd means "Print working directory". The command prints the full path of the current directory you're in. If you ever get lost, pwd is here to help.
*ls* lists all files and folders in the current directory. It can be useful to use the command ls -l to see more information about the items in the folder (size, permissions). If you want to include hidden files and folders, use ls -a .
Many developers combine both -a and -l into ls -la to display a nice list of all files and folders including hidden once.
Not to be confused with the cute feline animal, cat is often used to display the contents of a file directly in the terminal. It's useful for quickly peeking at short files without leaving the terminal. The command has many other interesting abilities, such as combining two files.
cd stands for “Change directory”. Use this to navigate in the file system. Combined with ls and pwd , you can easily navigate your way through.
If you want to go back to the parent folder, use cd ..
touch creates a new empty file.
mkdir creates a new empty folder. It is worth mentioning mkdir -p which will create a full list of folders.
For example:
mkdir -p my-folder/one/two/three/four
The command above will create a folder named four but also all the parent folders if they do not exist.
mv will move files or folders from one place to another. The first argument is the source and the second one is the destination
mv hello.txt documents/important
Using mv you can also rename a file or a folder:
mv hello.txt hello-2.txt
cp will copy a file or a folder from one place to another:
cp file.txt file2.txt - copies a filecp -r documents documents2 - copies folder and it’s contentsrm and specify the path to the file.rm -rf and specify the path to the folder.<aside> ❗
Be careful! rm deletes files and folders immediately without confirmation and without any way to recover them.
</aside>
| Command | Description |
|---|---|
| pwd | Print the current working directory (the folder you are in now). |
| ls | List the files and folders in the current directory. |
| ls -la | List files and folders in a list with detailed information including hidden files. |
| cat [filename] | Show the contents of a file |
| cd [dirname] | Change directory to [dirname] |
| touch [filename] | Create a new file |
| mkdir [filename] | Create new directory |
| mkdir -p [path] | Create a directory and any necessary parent directories in the path. The -p flag ensures that no error occurs if the directory already exists. |
| mv [source] [destination] | Move file or folder from [source] to [destination]. This command can be used to rename files or folder |
| cp [filename] [destination] | Copy a file from |
| rm [filename] | Delete a file |
| rmdir [dirname] | Delete an empty folder |
| rm -rf [dirname] | Delete a folder with it’s all contents |
The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **
