Deep Dive into the Command Line
This guide will take a deeper look into how you can work with the Terminal. This will include discussing: creating, deleting, copying and moving files. We'll also examine how to add content to files and we'll finish up with walking through how to integrate a custom shell script to automate a common process.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a free Dev Camp account

At the end of every section we'll have a "deep dive" guide, where we will pick out some topics and dive really deep into it to gain a better understanding of that process. For this section, we're going to learn more about command line interface - what it is, how it can be used in your system, and some important commands.

We already saw a few commands like making and changing directories. But, the command line can do much more. It's a powerful and comprehensive tool that establishes a direct connection with your system, so you can send specific instructions and ask your system to execute them for you.

That said, the command line is a comprehensive tool that has been built over several decades, so there's no way we can analyze it completely. We'll choose the important areas such as creating shortcuts for your commands, customization, automating your workflow, and navigating through your system. Understanding these processes will make you more efficient so that you can spend more time writing code and less time trying to navigate your system.

Basic Commands

When you want to see all the files and subfolders within a directory, the command is ls. This will list out all the contents of the directory.

ls has a cousin called ls -a. This command will show all the hidden files. Hidden files, start with a dot. Eg: .gitignore .There are many times you will need to see what hidden files are in your application.

If you want to clear the screen, the command is clear.

Though ls lists all the files, it becomes challenging to work with when a directory has a large amount of files.

A better way to list the files in a large directory is ls -la.

This command essentially prints out more details about the files such as their permission structure, date of creation, as well as the file name. It also displays them in a list format. Note how this is a more readable format.

Next, we'll create a new directory. Using the mkdir command followed by the directory name: mkdir testdir

You can change into that directory with the cd command: cd testdir.

To create a Ruby file inside this directory, use the touch command followed by the file name: touch my_file.rb.

Information Displayed with Long List

If you're wondering about the information that is displayed when you type ls -la, the first column on the left side is the permissions. You can google "UNIX based permissions" to get more in depth information on how to use these permissions. For now we will review the basics. It's good to know that r stands for read, w stands for write and x stands for executable. With these permissions, you can customize who can perform which actions on these files.

The second column is the file ID.

The third is the owner of the file. My computer is called admin, and this is why you can see this name in the third column.

The fourth column is the group, and this is different from the owner.

The fifth column is the size of the file, sixth is the date of creation and the last column is the name of the file.

Changing a File Name

To change the name of the file use the mv command followed by the current file name and then the new file name: mv my_file.rb my_other_file_name.rb .

Now, if you type ls, you will see that the file name has been changed.

Here, mv can seem a bit a counter intuitive, as it could be interpreted as the short form for "move". Interestingly, mv can be used that way.

Eg: We will move a file by creating another directory called 'nested_dir: mkdir nested_dir. Then type ls and you can see that the file is there along with the new directory.

We can then use the mv command followed by the current file name then the name of your directory followed by a slash and the new file name: mv my_other_file_name.rb nested_dir/something_else.rb

This will not only move the file inside the nested_dir directory, it will also change the name to something_else.rb

You can verify this with the command: ls nested_dir and you will see that something_else.rb is in that directory.

The Tab Key

Your tab is an important key in the command line, as it acts as an auto complete. For example, if you have a directory called nested_dir that you want to change into, you can type cd ne, and then hit the tab key. The terminal will list all the files or other directories in your current directory that begin with ne. If there is only one file or directory that begins with ne the terminal will auto complete the command to cd nested_dir.

Moving Back a Directory

.. is used when we want to move back a directory. The complete syntax is: cd .. (Developers note: If you wanted to move back two directories you would use: cd .. .. )

Removing a File

Next, we'll see how to remove a file from a directory. Let's create another file. Remember we use touch to create a new file. So the command is: touch another_file.rb.

If I want to delete this new file, I will use rm and the file name. The command looks like this: rm another_file.rb

If you now type ls, you will see that file is deleted.

This is the same as deleting a file from your file system.

Removing a Directory

Now, deleting a directory is a little different. If you type rm nested_dir, the terminal will throw an error.

This error is the terminal's way of saying 'be careful!" there are files in the directory.

The command to remove a directory is rm -rf and the name of the directory you want to remove. Eg: rm -rf nested_dir. This is the one command that you should be careful with, because if you type this from the root of the computer, your entire file system gets wiped out!

Keep in mind, the terminal is very good at it's job and will do whatever you tell it to do! Even if you are at the root of your system and use this command, it will delete everything from your computer! Remember, it doesn't put the files in trash, but removes them completely from your system. So, be extremely careful while using this command.

An interesting example of such a scenario is the movie Toy Story. After the movie was completed with all audio and 3D rendering, one of the employees accidentally hit the command rm -rf at the root. This deleted all the files, including the ones on the server. Thankfully, one of the executives had taken a copy of the files home with him, and this is how we we're able to see the movie! This goes to show how careful you should be while using this command.

Creating Content

Now that you know how to create and remove files and directories, let's talk about how to create content.

One way to do that is to create a Ruby file: touch my_file.rb. You could then use the command subl my_file.rb. This will open the file in sublime editor and you can add whatever content you want to it.

To view this content, use the cat command: cat my_file.rb.

An easier way to add content to a file is with the angle bracket >. An example of this is: echo "my content" > my_file.rb

To test, use the cat command again and you should see your content.

One thing to remember is the echo command will replace your existing content in the file, so be careful while using it.

To add to the file instead of replacing it, we will use >> to append new content. Use the double angle brackets like this: echo "my content added" >> my_file.rb

Now if you use the cat command you will see that

my content
my content added

are both in my_file.rb

Copying Files

Moving on, we'll look at how to copy files. The command for this is cp. An example is cp my_file.rb my_new_file.rb

If you look at the contents of my_new_file.rb, it will be the same as my_file.rb.

Take note, the cp command copied not just the file, but also all the content in it.

A common use of this command is to copy a file and put in a different directory. An example for that is:
cp my_file.rb ~/Desktop/my_file.rb

If you navigate to the Desktop folder and list all the files in it, you should be able to see my_file.rb.

Automating Workflow

Next, we'll see how we can automate our workflow. You may think, why even bother to use the command line, when you can do all these things through the file system? Let's take a look at how this could be beneficial.

Open your bash shell file, which is bashrc. I use a shell called 'zsh', so my file name is 'zshrc'. There's not much difference, but I prefer to use 'zsh'.
So, the command to open this file is: subl ~/.bashrc

This will open a file where you can place all the custom settings for your shell. There are a myriad of cool customizations that you can do to make your system work for you. You do not have to write all of these yourself. You can google bash and the automated process you are looking for and you will often find the code you need.

For example, let's say, I want to create a directory and immediately go to it. In other words, I want to combine my mkdir and cd commands into a single process. Since I do this often, it makes sense to have a script for it.

Google for something like mkdir and cd together, and this should bring up a ton of shell scripts for you. Go through the comments for some of these scripts, as they also give you a detailed explanation of what the script is, and why one method is better than the other. In this sense, it's always a great learning experience.

For example, here is a script:

mkcd () {
  case "$1" in
    */..|*/../) cd -- "$1";; # that doesn't make any sense unless the directory already exists
    /*/../*) (cd "${1%/../*}/.." && mkdir -p "./${1##*/../}") && cd -- "$1";;
    /*) mkdir -p "$1" && cd "$1";;
    */../*) (cd "./${1%/../*}/.." && mkdir -p "./${1##*/../}") && cd "./$1";;
    ../*) (cd .. && mkdir -p "${1#.}") && cd "$1";;
    *) mkdir -p "./$1" && cd "./$1";;
  esac
}

Though it looks scary, I'm just going to copy and paste it at the bottom my file. Unless you want to become an expert in shell programming, I'd recommend you don't dive too deeply into understanding what this code actually is.

Essentially, this script creates a process called mkcd to combine the mkdir and cdcommands. After you have pasted this into your shell file, save and close the bashrc file.

Quit the terminal and open it again for the changes to take effect.

Now, if you type mkcd MyAutoDir, it'll create the directory and take you inside it.

So, our new command works perfectly.

The idea behind this example is to show you that the terminal can be customized however you want. The best part is you don't have to be a shell programming expert to do it.

To conclude, I'll add a cheat sheet containing the top commands and shortcuts of command line programming to the show notes folder.

Resources