Command Line Basics: Difference between revisions
No edit summary |
No edit summary |
||
| (12 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
This page serves as an introduction to basic commands for managing your file system | {{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. | |||
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: | |||
<div style="background-color: #f8f9fa; border-left: 4px solid #005A9C; padding: 15px; margin-bottom: 10px;overflow: hidden;"> | |||
'''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. | |||
</div> | |||
<div style="background-color: #f8f9fa; border-left: 4px solid #198754; padding: 15px; margin-bottom: 10px;overflow: hidden;"> | |||
'''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. | |||
</div> | |||
<div style="background-color: #f8f9fa; border-left: 4px solid #FFD43B; padding: 15px; margin-bottom: 20px;overflow: hidden;"> | |||
'''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. | |||
</div> | |||
== 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. | |||
<div style="background-color: #f8f9fa; border-left: 4px solid #005A9C; padding: 15px; margin-bottom: 10px;"> | |||
'''macOS Users:''' Modern versions of macOS use <code>zsh</code> (Z shell) as the default terminal instead of Bash. For most intents and purposes, especially when running beginner commands, <code>zsh</code> functions exactly like Bash. You can run all the commands listed below natively in your terminal without any changes. | |||
</div> | |||
<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> | |||
🔗 '''[https://learn.microsoft.com/en-us/windows/wsl/install Installing WSL on Windows]''' | |||
</div> | |||
== File System Management == | |||
=== Creating a Directory === | === Creating a Directory === | ||
To create a new directory, you can use the <code>mkdir</code> command, which quite literally stands for "make directory." Simply type the command followed by the name of the directory you wish to create:<syntaxhighlight lang=" | To create a new directory, you can use the <code>mkdir</code> command, which quite literally stands for "make directory." Simply type the command followed by the name of the directory you wish to create: | ||
<syntaxhighlight lang="bash"> | |||
mkdir MyDirectory | mkdir MyDirectory | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Moving Between Directories === | === Moving Between Directories === | ||
The change directory command is called <code>cd</code> and can be used to efficiently switch between directories. To move into our newly created directory, type:<syntaxhighlight lang=" | The change directory command is called <code>cd</code> and can be used to efficiently switch between directories. To move into our newly created directory, type: | ||
<syntaxhighlight lang="bash"> | |||
cd MyDirectory | cd MyDirectory | ||
</syntaxhighlight>If we want to go backwards, we can use <code>..</code>, which always means to go back one folder from where you currently are.<syntaxhighlight lang=" | </syntaxhighlight> | ||
If we want to go backwards, we can use <code>..</code>, which always means to go back one folder from where you currently are. | |||
<syntaxhighlight lang="bash"> | |||
cd .. | cd .. | ||
</syntaxhighlight>If we want to return to our root directory, we can simply type the command with nothing after it.<syntaxhighlight lang=" | </syntaxhighlight> | ||
If we want to return to our root directory, we can simply type the command with nothing after it. | |||
<syntaxhighlight lang="bash"> | |||
cd | cd | ||
</syntaxhighlight>I can check my location using the present working directory command or <code>pwd</code>. Typing <code>pwd</code> 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. | </syntaxhighlight> | ||
I can check my location using the present working directory command or <code>pwd</code>. Typing <code>pwd</code> 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 === | === 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:<syntaxhighlight lang=" | 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: | ||
<syntaxhighlight lang="bash"> | |||
mv myfile MySubDirectory/ | mv myfile MySubDirectory/ | ||
</syntaxhighlight>The first thing after <code>mv</code> represents the file to move, and the second represents the destination. <code>mv</code> is also good for renaming files. I can change the name of myfile to mynewname as follows:<syntaxhighlight lang=" | </syntaxhighlight> | ||
The first thing after <code>mv</code> represents the file to move, and the second represents the destination. <code>mv</code> is also good for renaming files. I can change the name of myfile to mynewname as follows: | |||
<syntaxhighlight lang="bash"> | |||
mv myfile mynewname | mv myfile mynewname | ||
</syntaxhighlight>This just renames the file in place without moving it elsewhere. | </syntaxhighlight> | ||
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 <code>cp</code> command, and it works much the same way as the <code>mv</code> command. To use <code>cp</code>, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.<syntaxhighlight lang=" | Sometimes, we don't want to move files but we do want to copy them. This is the <code>cp</code> command, and it works much the same way as the <code>mv</code> command. To use <code>cp</code>, you must specify the file or folder you wish to copy, and the destination to where you want to copy it. | ||
<syntaxhighlight lang="bash"> | |||
cp myfile ../ | cp myfile ../ | ||
</syntaxhighlight>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 <code>-r</code> 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. <syntaxhighlight lang=" | </syntaxhighlight> | ||
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 <code>-r</code> 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. | |||
<syntaxhighlight lang="bash"> | |||
cp -r MyDirectory ../ | cp -r MyDirectory ../ | ||
</syntaxhighlight>This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory. | </syntaxhighlight> | ||
This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory. | |||
<div style="background-color: #e8f4f8; padding: 15px; text-align: center; margin-top: 20px; font-style: italic;"> | |||
'''Tip:''' Moving and copying large numbers of files (>100) is often much faster through the command line than through graphical file explorers! | |||
</div> | |||
=== Deleting Files === | === Deleting Files === | ||
The removal command is <code>rm</code>. Just like with <code>cp</code>, this can include the <code>-r</code> flag or not to be done recursively. <syntaxhighlight lang=" | <div style="background-color: #fff3cd; border-left: 5px solid #ffc107; padding: 15px; margin-bottom: 20px; margin-top: 15px; overflow: hidden;"> | ||
'''Important: Deletion is Permanent'''<br> | |||
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. | |||
</div> | |||
The removal command is <code>rm</code>. Just like with <code>cp</code>, this can include the <code>-r</code> flag or not to be done recursively. | |||
<syntaxhighlight lang="bash"> | |||
rm myfile | rm myfile | ||
</syntaxhighlight>This code simply deletes the file myfile.<syntaxhighlight lang=" | </syntaxhighlight> | ||
This code simply deletes the file myfile. | |||
<syntaxhighlight lang="bash"> | |||
rm -r MyDirectory | rm -r MyDirectory | ||
</syntaxhighlight>This deletes everything that is in the directory MyDirectory recursively. | </syntaxhighlight> | ||
This deletes everything that is in the directory MyDirectory recursively. | |||
=== Determining What is in a Directory === | === Determining What is in a Directory === | ||
The list command lists all of the files that are in your current working directory. <syntaxhighlight lang=" | The list command lists all of the files that are in your current working directory. | ||
<syntaxhighlight lang="bash"> | |||
ls | ls | ||
</syntaxhighlight>You can get more information about different files by using different flags. For instance, the flag <code>-l</code> 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. <syntaxhighlight lang=" | </syntaxhighlight> | ||
You can get more information about different files by using different flags. For instance, the flag <code>-l</code> 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. | |||
<syntaxhighlight lang="bash"> | |||
ls -l | ls -l | ||
</syntaxhighlight>Running just <code>ls</code> 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:<syntaxhighlight lang=" | </syntaxhighlight> | ||
Running just <code>ls</code> 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: | |||
<syntaxhighlight lang="bash"> | |||
ls ~/MyDirectory/ | ls ~/MyDirectory/ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== 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 <code>vim</code>. | |||
<syntaxhighlight lang="bash"> | |||
vim my_python_script.py | |||
</syntaxhighlight> | |||
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 <code>i</code>, then type whatever Python code you would like to execute. To stop editing the file, click the escape key and then type <code>:wq!</code> which will write your files to the file my_python_script.py. You delete entire lines by typing <code>dd</code>. These are just some of the many useful tools that <code>vim</code> offers. To edit a file once it has already been created, you need only type <code>vim my_python_script.py</code> once again. | |||
You can view the contents of a file without using <code>vim</code> via <code>cat</code>. | |||
<syntaxhighlight lang="bash"> | |||
cat my_python_script.py | |||
</syntaxhighlight> | |||
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. | |||
<div style="background-color: #e8f4f8; padding: 15px; text-align: center; margin-top: 20px; font-style: italic;"> | |||
'''Tip:''' If your terminal becomes bogged down with too much text, simply type <code>clear</code> to reset the view. | |||
</div> | |||
=== 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 <code>python</code>, others might have <code>python3/python2</code>, 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: | |||
<syntaxhighlight lang="python"> | |||
print("Hello, World!") | |||
</syntaxhighlight> | |||
I can then do the following on the terminal: | |||
<syntaxhighlight lang="bash"> | |||
python my_python_script.py | |||
</syntaxhighlight> | |||
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: | |||
<syntaxhighlight lang="bash"> | |||
python my_python_script.py > output.out | |||
</syntaxhighlight> | |||
This saves whatever is printed to a file called output.out. If I ran <code>cat output.out</code> 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 <code>nohup</code> command. This will allow the script to run in the background, while I do other things on my computer. | |||
<syntaxhighlight lang="bash"> | |||
nohup python my_python_script.py & | |||
</syntaxhighlight> | |||
Adding the <code>&</code> allows me to continue working in my current terminal without waiting for the code to finish running. | |||
== Additional Resources == | |||
* 🔗 [https://swcarpentry.github.io/shell-novice/ Software Carpentry] | |||
* 🔗 [https://www.learnshell.org/ Learn Shell] | |||
* 🔗 [https://mywiki.wooledge.org/BashGuide Bash Guide] | |||
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.