DEEP INTRODUCTION TO THE COMMAND LINE - LINUX AND GIT BASH USAGE
DEEP INTRODUCTION TO THE COMMAND LINE - LINUX AND GIT BASH USAGE, GIT, POWERSHELL, LINUX, COMMANDLINE INTERFACE
DEEP INTRODUCTION TO THE COMMAND LINE - LINUX AND GIT BASH USAGE
The command line, also called the Windows command line, command screen or text interface is a free user interface that is navigated by typing commands at prompts “$ or %”, instead of using a GUI (graphical user interface) with images, icons and a beautiful interface.
With the command line, you are exposed to how the computer actually executes commands When certain actions are performed with the mouse, keyboard and more such as creating, reading, updating and deleting files and software. The command line can be quite confusing for non-technical people and this is because it is not what they are used to operating a computer with, it is very fast to execute commands and does not require the complexity of a user interface at all.
WHAT YOU WILL GAIN FROM THIS ARTICLE
This article will contain sections that will give you an idea on what you will be expected to know at the end of this article as well as answer your questions about the command line interface.
- Why do people use the command line?
- Should I learn the command line?
- How to open the command line?
- Use the command line to navigate directories and display directory content.
- Use the command line to create a new directory and a new file.
- Use the command line to rename or destroy a directory and a file.
- Use the command line to open a file or folder in a program.
WHY DO PEOPLE USE THE COMMAND LINE ?
Now you may ask yourself why anyone would want to use this method to operate a computer system when the GUI which is arguably easier and more relatable exists, well there are many reasons : speed, precision and complex manipulation of the computer system are the most relevant out of many reasons why and they are important.
Although using a command-line interface interface requires the memorization of many commands, it can be a valuable resource and should not be ignored. Using a command line, you can perform almost all the same tasks that can be done with a GUI. However, many tasks can be performed quicker and can be easier to automate and do remotely.
Example: Microsoft Windows users may find such a task as renaming 100+ files in a folder directory can be done in less than a minute with a single command in the command line. The task could also be completed using a batch file to run instantly.
SHOULD I LEARN THE COMMAND LINE?
We are strong believers that you should learn as much as you can about computers especially if you intend to be a full stack developer. Having a basic understanding of the command line from reading this page gives you a greater advantage over someone unfamiliar with its capabilities. As a full stack developer you will be required to have an array of skills and tools when working as a freelancer or for a company and the command line definitely has advantages so you are advised to learn it. It requires memorisation of commands but it's nothing that you cannot achieve with constant practise and persistence.
USE THE COMMAND LINE TO NAVIGATE DIRECTORIES AND DISPLAY CONTENT
Before we start, it is important to confirm the tools to write commands for the different operating systems.
For windows users, you will be using the git bash tool for your commands, instructions on how to download it were given in the previous article but just in case you can download this software to write your commands, the windows powershell can also be used for this commands but i recommend the git bash because you will be writing your git commands there together in the future.
For macbook users search for the terminal on your computer and start typing the following commands you see below. This section covers navigating through directories(folders) and displaying content. Let us start:
- Home directory - In your command line you should see something like this “PS C:\Users\NameofPc>” this is home directory and always shown in command lines, you should start writing your code after this greater than sign “>” sometimes it could be a “$ or %”. Just ignore it and write your code after it.
- Print working directory - The first command is “pwd” . This command stands for “print working directory” it shows you the current file you are in. You will most likely be in the home directory and the command should print out the working directory.
- List - In the command line “ls” is the command used to list all the files and directories in the current directory, to test this out type “ls” where you are and you should see all the files currently in the directory. Note that “/” slash indicates a directory, “@” indicates a link and* indicates an executable.
- Clearing your terminal - if your screen gets too cluttered, you can clear your terminal using the “clear” command, you can also navigate with the arrow keys in the command line.
- Getting help - Most commands and programs that people have written to be run from within the bash support a help option that displays more information on how to use the command or program. Run “--help” and a list of commands or options to guide you through the environment should be displayed.
- The man command - This stands for manual, the command “man” will turn your command line into a page with a description of whatever command is beside it. Let's say you write “man ls” it should display a description of the ls command and its options.
- More ls Flags - The “ls” command can be used in other ways to provide more information about files and directories rather than just listing them. The “ls -l” command in addition to listing all files shows the file size and the last time of its modification.
- Change directory - This command is written as “cd” in the command line , it changes the current folder/directory you are in to the folder/directory you specify after the cd command.
It is important to note you can only change directory into folders that are currently present in the directory. You cannot change to a directory that does not exist currently in the folder/directory you are in.
- Reverse change directory - You can also change directories to go back to the previous directory. Let us use the former example for instance.
Example : If we are in the css directory and we want to navigate back to the developer directory that contains it all we have to do is add a space and two dots “cd ..” and we will be taken back to the parent directory.
USE THE COMMAND LINE TO CREATE AND DELETE A NEW DIRECTORY OR FILE
We are going to look at how to create and copy files and directories to each other in this particular section. Ensure that you always “ls” in any directory you “cd” into.
- Create a directory - Always remember to “pwd” and “ls” to confirm the directory you are and also to see the list of directories present.The command to create a new directory is “mkdir”, so to create a directory called “programmer” all you have to do is to type the command “mkdir programmer” and it will be created. It is very fast and you can see why the command line is preferred to graphic user interfaces (GUI).
NB - Try and experiment with this command, create as many directories as you want with “mkdir” and use “ls” to display them all.
- Deleting a directory - To delete a directory all you have to do is make sure you are in the directory containing it and write the following commands “rm -r programmer” this command will delete any directory just like the one called programmer we created in the previous example. It is important to note that directories and files are different and as such the process to delete a file is different.
rm -r programmer
- Creating a text file - There are so many ways to create a text file, but we will be using the easiest for beginners, to create a text file all you have to do is to type the command “touch” then the name of the file. Example: “touch text_file.txt”, with this the file “text_file.txt” has been created. It is important to add the file extension to specify the type of file like: .txt, .js, .css etc.
- Deleting text a file - Deleting a file is quite similar to deleting a directory all you have to do is type the command “rm text_file.txt” and the file will be deleted permanently.
- Moving files and directories - In the command line moving files and directories is quite simple; it enables us to rename files and folders. The command “mv” is used to perform this operation.
Example : Let's say we have a file called “lesson.txt” and we want to rename this file to “discussion.txt” all we have to do is to type the command “mv lesson.txt discussion.txt”. The folder you want to rename has to be placed first before the intending name of the folder.
mv lesson.txt discussion.txt
- Copying files and directories - To copy a file or directory the command “cp” is used, all you have to do is place the intending folder after the current one and it will work. Example: If we want to create a copy of a file called “analysis.txt” to a file called “analysis_backup.txt” as a backup file all we have to do is type “ cp analysis.txt analysis_backup.txt”. This will automatically create the file “analysis_backup.txt”even if it does not exist currently.
cp analysis.txt analysis_backup.txt
NB - It is important to note that for deleting directories the “rm -r” is used while for regular files “rm” only is used.
We have taken a look at the basic commands of the command line, as you go further into the course you will learn more about complex commands
SUMMARY OF BASIC COMMANDS
- Pwd - The “pwd” command is used to display the current working directory to then navigate to wherever you wish to go.
- ls - The “ls” command is used to list the items present in a current working directory which helps you navigate.
- clear - This helps you clear your command line if it becomes too clustered.
- help - This provides information about how to use certain commands and more in the command line interface, always use it if you get stuck.
- man - This stands for manual and gives you a description of whatever command you place beside it.
- cd - This changes the directory from the parent directory to a child directory present in the file.
- cd .. - This changes the directory back to the parent directory and does the opposite of what the “cd” command does.
- mkdir - This creates a new directory in the current parent directory.
- rm -r -This command is used to delete a directory.
- touch - This command is used to create a text file.
- rm - This command is used to delete regular files and not directories
- mv - the “mv” command is used to move and rename files by moving them to files with the intended new name.
- cp - The command “cp” is used to copy files from an old location to a new one.
NB - It is important to always use the command “ls” and “pwd” to find your way around and navigate through the command line interface (CLI). Refer to the following resource for more information
It is always advised to practise and get familiar with all the commands for the best possible mastery of skills, this is it for now and till the next one.
What's Your Reaction?