Command Line Basics

From Stat DS Computing Wiki
Revision as of 04:05, 26 June 2026 by Ajshen (talk | contribs)


Implementation Checklist
Back to Beginner's Guide

This page serves as an introduction to basic commands for managing your file system using Bash. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics.

While graphical user interfaces (GUIs) are intuitive, mastering the command line is an essential milestone for research. Transitioning to the terminal unlocks capabilities that are either impossible or heavily bottlenecked in a GUI:

High-Performance Computing (HPC): If your research scales up to training complex machine learning models on very large datasets, you will inevitably need to use a computing cluster. Most HPC environments and job schedulers (like Slurm) are headless and operate exclusively via the command line.

Automation & Reproducibility: The terminal allows you to string together commands into executable scripts. Instead of manually clicking through a GUI to rename large trees of experiment configuration folders, you can automate structural reorganizations with a single command, saving hours and ensuring zero human error.

Speed & Resource Efficiency: Navigating file systems, filtering text across massive datasets, and monitoring system processes is fundamentally faster in the terminal. It also avoids the heavy memory and processing overhead of a graphical interface, reserving your machine's computing power for actual analysis.

Using Bash on Windows and macOS

While Bash is the standard command-line interface for Linux, these commands can be used on almost any operating system with the right setup. To get started, navigate to your terminal, or log in to any of the department servers.

macOS Users: Modern versions of macOS use zsh (Z shell) as the default terminal instead of Bash. For most intents and purposes, especially when running beginner commands, zsh functions exactly like Bash. You can run all the commands listed below natively in your terminal without any changes.

Windows Users: Windows does not come with a native Bash terminal by default. To run these commands locally on your machine, you will need to install the Windows Subsystem for Linux (WSL).

🔗 Installing WSL on Windows

File System Management

Creating a Directory

To create a new directory, you can use the mkdir command, which quite literally stands for "make directory." Simply type the command followed by the name of the directory you wish to create:

mkdir MyDirectory

Moving Between Directories

The change directory command is called cd and can be used to efficiently switch between directories. To move into our newly created directory, type:

cd MyDirectory

If we want to go backwards, we can use .., which always means to go back one folder from where you currently are.

cd ..

If we want to return to our root directory, we can simply type the command with nothing after it.

cd

I can check my location using the present working directory command or pwd. Typing pwd into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.

Moving and Copying Files

Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:

mv myfile MySubDirectory/

The first thing after mv represents the file to move, and the second represents the destination. mv is also good for renaming files. I can change the name of myfile to mynewname as follows:

mv myfile mynewname

This just renames the file in place without moving it elsewhere.

Sometimes, we don't want to move files but we do want to copy them. This is the cp command, and it works much the same way as the mv command. To use cp, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.

cp myfile ../

This creates a new copy of myfile in the previous directory on the path to my current working directory. If I have a directory, I might want to copy its entire contents to a new directory. This requires the -r flag, meaning recursive. This essentially means that for every element in the directory, we recursively copy those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use.

cp -r MyDirectory ../

This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory.

Tip: Moving and copying large numbers of files (>100) is often much faster through the command line than through graphical file explorers!

Deleting Files

Important: Deletion is Permanent
You must be extremely careful when deleting files through the terminal. Unlike when you are using your general user interface and click and drag something to the trash, files cannot easily be restored. Never remove something without being absolutely certain it is what you intend to do and that there are no unintended consequences of its deletion.

The removal command is rm. Just like with cp, this can include the -r flag or not to be done recursively.

rm myfile

This code simply deletes the file myfile.

rm -r MyDirectory

This deletes everything that is in the directory MyDirectory recursively.

Determining What is in a Directory

The list command lists all of the files that are in your current working directory.

ls

You can get more information about different files by using different flags. For instance, the flag -l gives additional information about your read/write access for a file, when it was last created/manipulated, who has access to it, the owner, etc.

ls -l

Running just ls tells you what is in your current working directory, but you can provide a path to determine what is in another directory. The following lists everything that is in MyDirectory, where ~ just indicates the root:

ls ~/MyDirectory/

Creating, Editing, and Running Scripts

Creating and Editing a New File

You can create and edit new files directly in the terminal. There are many commands for doing this: this author's personal favorite is vim.

vim my_python_script.py

This creates a file called my_python_script.py. When you run this line in the terminal, an editor will appear. To insert information into the editor, click i, then type whatever Python code you would like to execute. To stop editing the file, click the escape key and then type :wq! which will write your files to the file my_python_script.py. You delete entire lines by typing dd. These are just some of the many useful tools that vim offers. To edit a file once it has already been created, you need only type vim my_python_script.py once again.

You can view the contents of a file without using vim via cat.

cat my_python_script.py

This line of code will show you everything that is in my_python_script.py without taking you to a terminal editor. This is great for viewing logs and output files from running chunks of code on the servers.

Tip: If your terminal becomes bogged down with too much text, simply type clear to reset the view.

Running a Python Script

This next part depends on if you have Python installed on your computer and what you have it installed as. Some might have it saved just as python, others might have python3/python2, depending on if you have multiple versions.

Say I have the following Python code in my_python_script.py, saved in my current working directory:

print("Hello, World!")

I can then do the following on the terminal:

python my_python_script.py

The terminal will then show "Hello, World!"

I can also redirect the output of the script to another file if I wish to save it. This can be done via:

python my_python_script.py > output.out

This saves whatever is printed to a file called output.out. If I ran cat output.out I would see "Hello, World!" printed.

If I want to run a Python script that may take some time, and I wish to walk away from my computer or do something else in the terminal while it is running, I can use the nohup command. This will allow the script to run in the background, while I do other things on my computer.

nohup python my_python_script.py &

Adding the & allows me to continue working in my current terminal without waiting for the code to finish running.

Additional Resources