Intro to Command Line and Bash

If you are going to use the bfb_delivery command-line interface (CLI), you will need to know how to use the command line and Bash in a couple of very simple ways. This document gives you an introduction to using the command line and Bash, specifically the builtin tools and commands you will use to navigate your file system. It also gives you extra tutorials that you don’t necessarily need to use the CLI, but that you may find useful.

To use the bfb_delivery CLI, you will at least need to know how to understand the basic command prompt and how to navigate your file system. This is covered in the first two sections, The “command prompt” or “terminal” and Navigating your file system, but there are additional sections on manipulating files and directories, reading files, writing to files, and using wildcards.

Note

This document is not a comprehensive guide to the command line or Bash. It is a brief introduction to the tools you will use in the bfb_delivery CLI. If you want to learn more about the command line and Bash, there are many resources available online.

The “command prompt” or “terminal”

There are a number of applications you can download to interact with the command line. On Windows, you can use Command Prompt or PowerShell. On macOS, you can use Terminal. On Linux, you can use the terminal that comes with your distribution. You can also download other terminal applications like Git Bash or Anaconda Command Prompt.

For our purposes, I recommend installing Anaconda Command Prompt and Git Bash, since it comes Anaconda with conda (for working with Python packages like bfb_delivery, not necessary for this tutorial) and since Git Bash comes with Unix-style commands, and since they are both Windows compatible. You can download Anaconda at https://www.anaconda.com. You can download Git at https://git-scm.com.

When you open the terminal (Git Bash or Anaconda Prompt), you will see a prompt that looks something like this:

(base) MacBook-Pro:bfb_delivery me$

The first part of the prompt in parens is the conda environment you have activated. It may also be absent if you have no env activated. The second part of the prompt is the name of the computer, followed by a colon and the current directory you are in. Then you see your username followed by a dollar sign. The dollar sign indicates that the terminal is ready to accept commands. So, in this example we have the base env activated on a MacBook-Pro, we are in the bfb_delivery directory, and our username is “me”.

This format may very depending on your setup. For instance, you may not see a machine name, or the full path to your present working directory may appear instead of just the directory name.

Manipulating files and directories

You can use a number of commands to manipulate files and directories. Here are some basic commands you may use:

  • mkdir: Make directory. This command will create a new directory.

  • touch: Create file. This command will create a new file.

  • cp: Copy. This command will copy a file or directory.

  • mv: Move. This command will move a file or directory. You can also use it to rename a file or directory, by basically moving it to the same location with a different name.

  • rm: Remove. This command will delete a file or directory. Be careful with this command, as it will not ask you to confirm the deletion.

Making a directory

You can use the mkdir command to create a new directory. Here is an example:

$ mkdir new_directory

This command will create a new directory called new_directory in the current directory. You can use an absolute path to create a directory in a different location:

$ mkdir /path/to/new_directory

Creating a file

You can use the touch command to create a new file. Here is an example:

$ touch new_file.txt

This command will create a new file called new_file.txt in the current directory.

Copying files and directories

You can use the cp command to copy a file. Here is an example:

$ cp file.txt copy_of_file.txt

This command will create a copy of file.txt called copy_of_file.txt in the current directory.

To copy a whole directory, you can use the -r option:

$ cp -r directory copy_of_directory

Moving files and directories

You can use the mv command to move a file. Here is an example:

$ mv file.txt new_location/file.txt

This command will move file.txt to the directory new_location.

To rename a file, you can move it to the same location with a different name:

$ mv file.txt new_file.txt

This command will rename file.txt to new_file.txt.

To mv a whole directory, you can use the -r option:

$ mv -r directory new_location/directory

Removing files and directories

You can use the rm command to remove a file. Here is an example:

$ rm file.txt

This command will delete file.txt from the current directory.

To remove a whole directory, you can use the -r option:

$ rm -r directory

This command will delete the directory directory and all its contents.

Wildcards

You can use wildcards to match multiple files or directories. The most common wildcard is the asterisk (*), which matches any number of characters. Here are some examples:

$ ls *.txt

This command will list all files that end in .txt.

$ mv *.txt text_files/

This command will move all files that end in .txt to the directory text_files.

Reading files

You can use a number of commands to read files. Here are some basic commands you may use:

  • cat: Concatenate. This command will display the contents of a file.

  • less: This command will display the contents of a file one page at a time.

  • head: This command will display the first few lines of a file.

  • tail: This command will display the last few lines of a file.

  • grep: This command will search for a string in a file.

Displaying the contents of a file

You can use the cat command to display the contents of a file. Here is an example:

$ cat file.txt

This command will display the contents of file.txt in the terminal.

Writing to files

You can use a number of commands to write to files. Here are some basic commands you may use:

  • echo: This command will display a string in the terminal.

  • >>: This command will append a string to a file.

  • >: This command will overwrite a file with a string.

You can also use graphical text editors like Notepad or TextEdit to write to files, but we will focus on the command line here. There are some text editors you can use in the terminal, like nano or vim.

Displaying a string in the terminal

You can use the echo command to display a string in the terminal. Here is an example:

$ echo "Hello, world!"

This command will display Hello, world! in the terminal.

Appending a string to a file

You can use the >> command to append a string to a file. Here is an example:

$ echo "Hello, world!" >> file.txt

This command will append Hello, world! to file.txt. (You can use cat to see the contents of file.txt.)

Overwriting a file with a string

You can use the > command to overwrite a file with a string. Here is an example:

$ echo "Hello, world!" > file.txt

Using nano

You can use the nano text editor to write to files. Here is an example:

$ nano file.txt

This command will open the file.txt in the nano text editor. If the file doesn’t exist, it will create it, and if it does exist you will see the contents and find your cursor at the top. You can write to the file, save it, and exit the editor. To save and exit, press Ctrl + O to save, then press Enter to confirm the filename, then press Ctrl + X to exit. There are additional commands at the bottom of the nano editor window when it is open.