Shell navigation for beginners

Shell navigation is an essential skill for a developer, you'll be interacting so much with UNIX or Linux operating systems. being able to work with the command line will ease your job as a developer as you can write scripts to automate tasks and also you can do a significant amount of job with few commands that could take you a lot of time working on a graphical user interface.

To get started with the shell, you need to have a Linux terminal in your system. If you're using other types of operating systems such as windows, you can download VirtualBox and install Ubuntu or enable windows subsystem for Linux which you can access by searching "Turn windows feature on or off" and then scroll to the bottom where you'll see windows subsystem for Linux and enable as follows

After enabling the windows subsystem for Linux, you can go to the Microsoft store and install Ubuntu or any Linux distribution. After installing, open ubuntu and it'll install additional files then you can restart your computer for the changes to be effective.

After installing successfully, go to the windows terminal and type "wsl" or "bash" and enter, This will give you a Linux terminal and you can start working on it. Likewise, you can open the ubuntu you installed and it will give you a Linux terminal.

Setting up a virtual machine using VirtualBox is a little bit long process but you can find tons of tutorials online on the same. You can install Ubuntu, kali, Vagrant, or any other Linux distribution.

Now that we're all set up, we can start working on our terminal.

Shell navigation

Files in the UNIX operating system are stored in a tree hierarchy, i.e they are stored in folders (directories). You can see the list of files and directories in a directory using ls command. This will show all files available in that directory. usually, folders show different colors eg blue when you use ls command while files remain white colored.

To navigate from one directory to another, we use cd , which stands for "change directory" , command. cd takes another argument which is the directory name. to go into the "barebone" directory for example, we are going to use cd barebone and press enter.

You'll notice that now we are inside the barebone folder. You can check the current directory that you are using pwd . This will give you something like this.

You can list the files in the directory again using ls and if there is nothing in that directory you'll not see any effect but the cursor will be on the next line.

Now, let's start creating some directors. To do that, we are going to use mkdir dirName . Remember, you can create many directories using mkdir dir1 dir2 dir3 dir4 .. dir1 to dir4 as being names of your directories. For our tutorial, We are going to create three directories namely engineering, devops, accessibility .

Sometimes, if the permission is denied you can work as a superuser using sudo before commands. this will prompt you to enter your password and the operation will be successful. You can list the contents using ls . Now that we have created directories, why don't we create files? To do that, we'll use touch fileName to create and like creating directories you can create many files once for example touch file1 file2 file3 . Keep in mind that shell is case-sensitive in that File1 is not the same as file1. also, you should not create a filename or folderName starting with a number for example 1west .

now that we can create folders using mkdir and 'enter into' folders using cd, create files using touch , how do we go back to the parent directory? well, first let us see what our folders have. if you use just ls it will display files and directories in that directory but omit those starting with . . if you want to list all files and directories including those starting with . , we'll use ls -a . And if you do that, you'll notice that all directories, including empty directories, have . and .. which are the same color as that folders so what does that mean? it means that these are directories.

. represents the current directory that you are in while .. is the parent directory of that directory. so, if you want to go back to the parent directory you are going to cd .. .

Now that we can navigate back and forth, what if you accidentally use only cd which it will take you to the home directory, what will you use to get back to the directory you were working in? cd directory by directory until you get back to where you were? possibly but there is an easier way which is to use cd - , this will take you to the previous directory you were in. See how easy is to use shell?

File manipulations in shell

Now that we can move around the shell and create directories and files, we can start working with the files performing common tasks like copying, pasting, moving, and deleting.

We can start by copying files. to copy files, you use cp command. you can copy a file from one directory to another by providing specific file paths after cp . cp is followed by the file which you're copying and where you're going to paste the file into for example cp file1 dir1 means that a file named file1 is being copied to a directory named dir1.

The next command is mv which is used to move a file from one directory to another. mv command takes two arguments like cp , that is the file you are moving and the destination for example mv file2 dir2 moves a file named file2 to a directory dir2. You must specify the correct file locations. mv command is also used to rename files or folders. It is like a 'cut' since it completely removes the file and pastes it to another location. Remember, you cannot reverse changes that you do in the shell so you must be careful.

Now let us talk about deleting files and folders. To delete a file, you can use rm command followed by filename for example rm file1 deletes a file named file1 in the current directory. Make sure you specify the correct file path for it to be successful. if the file path you're trying to delete is for example home/Desktop/hosea-react/ and the file name is myweb then to delete that file, you can use rm home/Desktop/hosea-react/myweb or you can cd into that directory i.e cd home/Desktop/hosea-react/ and then delete the file rm myweb .

That was to delete a file but if you try to delete a folder using rm directoryName , it will give you an error (will not be successful ). To delete a folder, you use rm -r directoryName . This deletes even an empty directory. In case of directories that fail to delete, you can "force" delete by adding -f i.e rm -rf directoryName . While you can use rmdir command to delete a directory, it does not work on empty directories therefore rm -r directoryName is better.

Summary of basic shell commands

These are the very basic shell commands that you need to know but there are so many commands available. These are the commands that we covered in the article.

  1. cd - Change directory

  2. pwd - Print working directory

  3. ls - List files

  4. ls -l list files in long format

  5. ls -la - list files including hidden files in long format

  6. mkdir - make/create a directory

  7. touch - Create a file

  8. cp - Copy

  9. mv - Move/ rename a file

  10. rm - Delete a file

  11. rm -r - Delete a directory

  12. rmdir - Delete non empty directories