How to Make Folders and File structure from your CLI?
Sur Panodyssey, tu peux lire 30 publications par mois sans être connecté. Profite encore de 29 articles à découvrir ce mois-ci.
Pour ne pas être limité, connecte-toi ou créé un compte en cliquant ci-dessous, c’est gratuit !
Se connecter
How to Make Folders and File structure from your CLI?
Starting a coding project begins with determining the folders and file structure. At times, we might make them from scratch for reasons relative to the work at hand, but more often than not, we build from existing project folders and templates sitting on our desktop or external drive.
Though it might be intimidating at first, making your folders and files from the command-line interface in those rare instances is fast and easy to wrap one's head around. Moreover, its velocity plus the ability to decipher and complete commands save you keystrokes and give you superpowers.
To create folders and files from the command-line interface, you only need to know three Unix commands: The mkdir command for making a directory, the cd command for changing the directory, and the touch command to create files. Ready? Let's make magic happen!
Running a terminal on your machine
If you know how to open a text editor or browser on your computer, you can also figure out how to start a command-line interface. It's called 'Terminal' on Linux and macOS systems, and alternatively, on Windows, you can install Windows Subsystem for Linux or Git.
Start a terminal on your machine
On Linux, open the programs menu and look for Terminal. You can also type it in the search bar or use the shortcut CTRL + ALT + T on your keyboard. If you have a macOS machine, navigate to the applications menu and browse the utilities. The shortcut for macOS machines to search and open a Terminal is CMD + SPACE. You can use Git Bash if you have a Windows machine.
Use mkdir to make folders
The mkdir command allows you to create or add new folders to existing ones from the Terminal window.
mkdir <file-name>
To create a new folder, type mkdir command in your Terminal window, the name of your folder next, and press enter. Here’s an example of how you can create a new folder named directory-1 using the said command: mkdir directory-1.
The same process is valid if you need to create multiple folders or subfolders at once. You only need to make some slight changes to the above command.
Make multiple directories with a command line
Imagine you want to add three more directories to your first directory, directory-1. The simplest way will be to type the command multiple times, but that certainly isn’t the best approach.
mkdir {directory-2,directory-3,directory-4}
By comma-separating the name of your directories with no space in enclosed curly braces after the mkdir command, you can accomplish what otherwise will take you quite some keystrokes.
Now you may wonder what changes you should make to the above command to create subfolders or subdirectories.
Add subdirectories to your project
You might need to add subdirectories at some point in your project, and still, you can achieve this from the command-line interface.
mkdir -p directory-4/{directory-5,directory-6}
The command coupled with the option or flag -p followed by the new subfolder name wrapped in curly braces allows you to add the new folder inside your existing folder without leaving the CLI.
Now that we can create our folders from the comfort of the command-line interface, let’s move into creating files where you can write your program.
“touch” to create files and start your project
To write your programs, you need to create files with different extensions, .html, .css, .js, etc. With the touch command, you can make all your files in the folder of your choice from the Terminal window.
touch index.html
The touch command takes in the new file name as an argument and creates an empty file in your folder or current working directory.
But what if you want to change the directory before creating your file, or you want to be able to move within directories without leaving your CLI. With the cd command, you can navigate directories up or down a level. After typing the command ‘cd’ in your CLI, you only have to add the name of the directory you want to move into.
In the image above, you can see a list of all the directories in the main folder. To move into directory-1, for example, you’ll type cd directory-1. However, to move back to your main folder, you need not to type the name but cd ..
These commands should allow you to move into your desired directory without clicking or having to leave the Terminal.
Also, remark that the cd command does not work on a file like "note" in the image. The command-line interface displays files with different colors to avoid you mistaking them for folders or directories.
Add these commands to your toolbox
As you may have guessed, the above are not the only commands available to use in a Terminal, but they are enough to get you started with making folders and files with the CLI. And if you want to learn more, The Odin Project has some interesting materials that you might like. Meanwhile, you can try the below commands for even more ease in navigating your machine with the Command-line Interface.
-
ls for listing files and directories in your current working directory.
-
ls -a to list all directories and files, including the hiding ones.
-
rm to delete or remove files
-
rm -r to delete or remove directories
-
pwd to print the name of your current working directory
-
ctrl l to clear your Terminal window
-
ctrl + C simultaneously to kill a program if you accidentally typed a command and the Terminal doesn’t seem to be responding to any of your commands.
-
cp to copy files or directories.
Wrap up
You now have all the tools to make files and directories from the command-line interface if you aren't already. In all, you only need three UNIX commands that are easy to wrap your head around—the mkdir command for making directories, the cd command for changing directories, and the touch command to create files. These commands will work on macOS and Linux-booted machines but not on Windows unless you install and use Windows Subsystem for Linux or Git.
And if you ever wonder, “why would anyone use a scary black box with colored cryptical output instead of a beautifully designed user interface?” The answer is not all systems you will interact with as a software developer or engineer have a graphical user interface. Besides, you will be using the Terminal a lot if you are, for example, writing a program with node.js
So if you ever want to learn more about command-line or software development, check out The Odin Project or the 100Devs agency free software engineering program with Leon Noel. Doing so will prepare you to work in environments that do not provide folders and files shaped graphically to click on or select.