How to work with files, folders and apps in Bash on Ubuntu on Windows 10
The addition of the Windows Subsystem for Linux in Windows 10 came as a surprise for many, us included. Although it is a tool intended to be used mainly by developers, regular users seem to be interested in this feature too. We thought it would make sense if we show you how to run commands in Bash on Ubuntu on Windows 10 to work with files, folders, and apps. There is plenty of ground to cover, so let's get started:
How to get Ubuntu on Windows
For this tutorial, you should have the Linux Ubuntu on Windows installed on your Windows 10 computer or device. If you haven't done it already, or if you need some guidance on this matter, we described the whole installation process here: How do I get the Windows Subsystem for Linux?
Secondly, we assume that you have never ever worked with Linux, and that "you know nothing" about it, just like Jon Snow. :)
What is Bash on Ubuntu on Windows
As a complete beginner, you might wonder what you're getting from Bash on Ubuntu on Windows. You get the entire Bash shell environment on your Windows 10 computer. If you used Windows before, be it Windows 10, Windows XP, or anything between those two, you are probably acquainted with the Command Prompt and the PowerShell environments. Bash is a similar shell, but created for Linux operating systems.
Just like Command Prompt, Bash is a command-line environment. It runs on top of a Linux kernel and offers you a whole range of command-line utilities to run. You can use it to download, install, and use Linux applications, if that's what you want. And all the utilities you run inside Bash get access to your Windows 10 file system. That means you can access, create, and delete files and folders on any of your drives. It is also important to note that, even though it's possible, running graphical applications in Ubuntu on Windows is both a fairly complex thing to do and prone to errors. That's because Ubuntu on Windows was created to provide a Linux subsystem and access to its utilities for developers, not for regular users.
How to start Ubuntu on Windows 10
The first thing you have to do after you've installed Ubuntu on Windows is to launch it, and a quick way to do that is to click or tap on its shortcut from the Start Menu.
This is what Ubuntu's interface looks like in Windows 10:
Being a command-line environment, you need to write the text commands that you want to run in it. If you've worked with Command Prompt before, then you're probably accustomed to writing the whole path of a command to be able to run it. Forget about that while you're using the Bash shell. Here, any command, tool, or app you install can be run from anywhere just by typing its name. There's no need to add its path before its name. Now let's see the basic commands that you need to learn to get started:
1. How to change the directory on your Windows 10 PC, with Bash on Ubuntu, using the CD command
One of the essential commands in Bash is the one that lets you change the folder in which you're working. To navigate to another folder from your Windows 10 PC, all you have to do is run the command cd [path] where [path] is the location of that folder.
Note that all the drives on your Windows 10 PC are mounted in /mnt/[the letter of the drive]. For example, the C drive where Windows 10 is usually installed is found at this location: /mnt/c. The D drive, if you have one, is found at /mnt/d and so on. Let's take an example: if you'd want to navigate to the Digital Citizen folder found on your D drive, you'd have to run this Bash command: cd "/mnt/d/Digital Citizen".
IMPORTANT: If the folder you want to work in has spaces in its name, such as is the case for the Digital Citizen, make sure to put the path between quotation marks. Also, use only straight marks, not curly or smart quotation marks, as they don't work.
2. How to see the contents of a folder on your Windows 10 PC, with Bash on Ubuntu, using the LS command
In Bash, another vital command, that shows you the contents of a folder, is ls. Type it in, press Enter on your keyboard, and you should get a list of every folder and file found inside the current directory.
As you can see in the previous screenshot, the folders are highlighted so that you can differentiate them from files.
3. How to copy files on your Windows 10 PC, with Bash on Ubuntu, using the CP command
Bash also includes all the commands required for basic file management, such as copy, move, rename, delete. The command that lets you copy a file from one place to another is cp [source] [destination], where [source] is the path to the file that's copied and [destination] is the path to the place where it's going to be copied.
For example, let's say that we want to copy the file called incognito.xlsx, found in our Digital Citizen folder, to the Security for Everyone subfolder. To do so, the command we'd have to run is cp "/mnt/d/Digital Citizen/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone".
The file is then copied to the specified folder.
4. How to move or rename files on your Windows 10 PC, with Bash on Ubuntu, using the MV command
Moving or renaming files with Bash is similar to copying files. The command you need to run for this purpose has the following syntax: mv [source] [destination], where [source] is the path to the file that's moved or renamed and [destination] is the path to the place where it's going to be moved. If the [destination] is the same as the [source], but the file name you specify is different from the original, the file is renamed instead of moved.
Using the same example as before, let's move the incognito.xlsx file from our Digital Citizen folder to the Security for Everyone subfolder, and then rename it to web_incognito.xlsx. To do so, we have to run these commands: mv "/mnt/d/Digital Citizen/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone" and then mv "/mnt/d/Digital Citizen/Security for Everyone/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone/web_incognito.xlsx".
5. How to delete files from your Windows 10 PC, with Bash on Ubuntu, using the RM command
Removing or deleting files in Bash is done with the help of the rm [file] command. To remove the web_incognito.xlsx file that we created earlier, we first navigate to our Security for Everyone folder that holds it, using the command cd "/mnt/d/Digital Citizen/Security for Everyone". Then, we run the command rm web_incognito.xlsx.
6. How to create folders on your Windows 10 PC, with Bash on Ubuntu, using the MKDIR command
Creating a folder with Bash is done using the mkdir [folder] command, where [folder] is the name of the folder you want to create. To exemplify, let's say that we want to create a folder called Tests inside our Digital Citizen directory. To do so, we first navigate to the Digital Citizen folder (cd "/mnt/d/Digital Citizen") and then run the command mkdir Tests. It's as simple as that.
7. How to delete folders from your Windows 10 PC, with Bash on Ubuntu, using the RM -R command
Similarly, removing or deleting a folder using Bash is just as easy. Navigate to the parent directory of the folder that you want to remove and run the command rm -r [folder], where [folder] is the name of the folder to delete. You might have noticed that this command also includes the -r argument: you need to add it so that everything inside that folder is also deleted, recursively.
To exemplify, let's delete the Tests folder that we previously created inside our Digital Citizen folder. For that, we first have to navigate to the parent folder, which is Digital Citizen, using the command: cd "/mnt/d/Digital Citizen". Then, we must run the command rm -r Tests.
8. How to download and install Linux apps on Windows 10, with Bash on Ubuntu, using the SUDO APT-GET INSTALL command
Linux also lets you run commands as a superuser using the SUDO command, which gives you administrative privileges. The syntax is simple: sudo [application], where [application] can be any program installed on the operating system. Because the sudo command gives you administrative powers over everything in Linux, it is very similar to what an administrator user account can do in Windows. If you want to download, install, and run Linux applications, you can use the following commands:
First, run sudo apt-get update. This command downloads the latest software lists from Ubuntu.
Then, to download and install the Linux app that you want, run sudo apt-get install [application name]. For example, if we'd want to install w3m, which is a text-based web browser, we'd have to run sudo apt-get install w3m. If the app requires additional packages, you might have to confirm that you want to continue, in which case press the Y key on your keyboard.
Once the app is installed, you can run it by typing its name in Bash, followed by any parameters necessary. For example, if we wanted to visit our website, we'd have to type w3m digitalcitizen.life.
9. How to get help in Bash on Ubuntu on Windows, using the HELP argument
If you want to find out more about what a certain command does, write it in Bash and add the parameter "--help". The information displayed should shed more light on the structure and usage of that command. Take, for instance, the command ls, which lists the contents of a folder. It might seem like a simple command, but it offers quite a lot of options for how it displays the information. Here's a part of the help you get for it:
Did you try Bash on Ubuntu on Windows?
We hope that we've managed to introduce you to the very interesting world of the Linux Bash and the basics of using it in Windows 10. Try the commands that we showcased, and find out more about each command using the HELP argument. If you have any questions or tips to share, don't hesitate to comment below.