Command Line Basics: Difference between revisions
No edit summary |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{ImplementationSidebar}} | {{ImplementationSidebar}} | ||
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. | 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. | ||
| Line 28: | Line 25: | ||
<div style="background-color: #f8f9fa; border-left: 4px solid #FFD43B; padding: 15px; margin-bottom: 10px;"> | <div style="background-color: #f8f9fa; border-left: 4px solid #FFD43B; padding: 15px; margin-bottom: 10px;"> | ||
'''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).<br><br> | '''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).<br><br> | ||
🔗 '''[https://learn.microsoft.com/en-us/windows/wsl/install | 🔗 '''[https://learn.microsoft.com/en-us/windows/wsl/install Installing WSL on Windows]''' | ||
</div> | </div> | ||
Latest revision as of 17:20, 20 August 2026
| 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:
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 MyDirectoryMoving 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 MyDirectoryIf 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.
cdI 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 mynewnameThis 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
The removal command is rm. Just like with cp, this can include the -r flag or not to be done recursively.
rm myfileThis code simply deletes the file myfile.
rm -r MyDirectoryThis 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.
lsYou 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 -lRunning 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.pyThis 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.pyThis 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.pyThe 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.outThis 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.