Command Line Basics: Difference between revisions
No edit summary |
No edit summary |
||
| Line 75: | Line 75: | ||
nohup python my_python_file.py & | nohup python my_python_file.py & | ||
</syntaxhighlight>Adding the & allows me to continue working in my current terminal without waiting for the code to finish running. | </syntaxhighlight>Adding the & 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] | |||
Revision as of 14:30, 11 July 2025
This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics.
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.
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.
Deleting Files
You must be extremely careful when deleting files through 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 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 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 you 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_file.pyThis line of code will show you everything that is in my_python_file.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.
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_file.py, saved in my current working directory:
print("Hello, World!")I can then do the following on the terminal:
python my_python_file.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_file.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_file.py &Adding the & allows me to continue working in my current terminal without waiting for the code to finish running.