Command line basics
This article is designed for absolute beginners at using a command line. If you’re confident using a command line and navigating the directories of your computer, this section may be straightforward for you, so only spend as much time as you think you need. Otherwise, lets get into the basics of using a command line.
What is a command line?
A command line or command line prompt is a simple interface between you and your computer that you operate by typing text commands. This is a way to give instructions directly to your computer’s operating system. You can use it to do many of the tasks you would normally use a file browser for (e.g. moving files between folders). Importantly for this course, you can also use it for running Python code. In this article we will run though all you need to know to run Python code and Python scripts using the command line, and quickly mention some of the other commands you can use in the command line you might find helpful to manage your data.
Where can I find a command line?
You may also hear a command line referred to as a terminal or terminal window. Before the development of windows-based systems a command line prompt would be the first thing you saw after starting the computer. Now however, you will generally need to open a specific program (ie a terminal window) to use a command line interface. All the major operating systems have command line programs included, such as ‘Terminal’ in Linux and Mac systems and ‘Command Prompt’ for Windows. These programs can be found and launched like any other from the start menu (or launcher on a Mac). If you have Anaconda, it will have installed its own command prompt program ‘Anaconda Prompt’, the advantage of which is it should be correctly configured to run Python on your system. Again, you can open this from the start menu or launcher depending on your system.
How do I use a command line?
Once you’ve opened your command line program you should see something like this:
symbol and a flashing cursor to invite user input.” title=”An anaconda command prompt”>
Which tells you that you are in the directory “C:Usersnathan” (obviously you will probably have a different username), and using the Anaconda environment ‘base’ (this is specific to the the Anaconda Prompt), with a flashing cursor at the prompt inviting you to type commands. After that the only trick is knowing what commands to type. Broadly speaking, most commands you will use will be in the following format:
<program or command name> -<option code> <file, directory or other object>
With the things in the angle brackets varying depending on what you want to do. So for example typing:
python -c “print(‘Hello world’)”
at the command prompt tells your machine to use the program ‘python’, with the option ‘-c’ with the object “print(‘Hello world’)”. If you try this you should see the message ‘Hello world’ displayed on the screen. This is because the option ‘-c’ tells python to expect some code in the form of text contained in the inverted commas “ “.
Depending on the context, either or both the ‘option’ and the ‘object’ parts of the command may not be needed. For example:
python -v
displays the version of python you are currently using, while:
python my_python_script.py
will look for the file called “my_python_script.py” in the current directory and try and run it using python. We’ll talk more about directories and introduce the idea of a path later, but first we’ll have a quick look at the python shell.
The Python Shell
If you just type ‘python’ on its own, without any option flag or filename you open what is known as a Python shell.
symbol, there is a triple > symbol instead, >>>”>
As you will see, you still have a flashing cursor, but the three ‘> > >’ symbols indicate you in the Python shell. This means anything you type needs to written using the Python programming language. Effectively you are now talking directly to the Python program rather than to your computer’s operating system as before.
When we learn more Python you can use a shell to try some simple commands and code, but for now to leave the Python shell and return to the operating system you can just type:
exit()
or
quit()
Absolute and relative paths
When you open a command line program you will always be placed in a particular directory, often your home directory where your personal files are stored. Your current position in the directory structure is referred to as your current directory. You can refer to any file in the current directory without having to specify the directory it is in. To refer to file in a different directory you will need to use a path.
A path is effectively just the address of a directory or folder on your computer. In the example above that path is “Usersnathan” on the drive ‘C’, and that is an example of an absolute path. An absolute path uses the full address of the directory, with all the sub-folders from the very top level of a hard-drive.
The important thing to note is that an absolute path begins with a backslash (for Mac and Linux systems this will be a forward slash /). In this case the absolute path is very short, but the deeper you go into a computer’s directory structure, the longer the absolute path gets.
To save having to write out the absolute path each time, you can also use a relative path. As the name suggests this is a path relative to the current directory. Unless the path begins with a or a /, the operating system assumes you are using a relative path. If you have a file in your Documents folder called ‘my_python_script.py’, while you could use an absolute path to run it:
python UsersnathanDocumentsmy_python_script.py
It’s quicker just to use the relative path:
python Documentsmy_python_script.py
So the absence of a leading or / indicates an relative path and tells the operating system to look from the current folder.
What if you just wanted to look one directory up the the directory structure? Then you can just use the shorthand “../” or “..” (in Windows only). For example, typing:
python ..my_python_script.py
will look one folder up the directory structure for the file, and:
python ..another_foldermy_python_script.py
will look one folder up and then in the folder another_folder for the file.
You will get the hang of this as you get more experience with coding.
Moving between directories
The best way to get the hang of paths and relative paths is to try moving around the directories (folders) on your own computer. The command to do this is ‘cd’ – for Change Directory. All the rules of relative and absolute paths apply here so:
cd /some/long/path/to/some/directory/
will try and change your current directory to that absolute path, while:
cd Documents/code/
will look for the folder ‘Documents’ in the current directory then the ‘code’ directory in the Documents folder. To move to the folder above you need the ../ notation, so:
cd ../
will take you to the directory above your current directory. You can string these together so for example:
cd ../../../
will look three directories above your current directory.
If at any point you try and move to a directory that doesn’t exist you will see an error message. You can also start typing the name of a directory and hit tab and this should offer an autocompletion of the name. If the name doesn’t exist you won’t see an autocompletion.
Showing contents of a directory
To see a list of contents of a directory you can use ls on Mac or Linux systems. If you are using Windows you may need to use dir instead, though some newer windows command line programs will accept ls. This prints out a list of the directory’s contents in the terminal window.
Managing files and directories with the command prompt
So far all we have done with the command line is move around the computer’s hard drive, look what’s in the folders and run some Python code. This is enough for many data science projects, but it is also useful to know the basics of moving, copying and deleting files and folders as this can be more convenient than using a graphical interface such File Explorer. Care should be taken however, especially when deleting files as they will not be moved to the recycling bin, they will be gone for good. These commands differ between operating systems, and we quickly mention a few of them in the table below.
Task | Windows | Mac/Linux |
---|---|---|
Making a directory | mkdir | mkdir |
Copying a file | copy | cp |
Moving (renaming) a file | move | mv |
Deleting a file | del | rm |
List files in a directory | dir | ls |
Summary
A command line is a program that gives you a way to give instructions directly to your computer’s operating system by typing commands.
There are many commands you can use, which vary depending on your computer’s operating system. For the purposes of this course, the important things to know are:
- How to use cd to move between directories using a command line, including using .. (Windows) or ../ to move up a directory
- You can use ‘ls’ (Linux/Mac) or ‘dir’ (Windows) to see a list of a directory’s contents
- Understand relative and absolute paths, so that you know how to point Python in the right place to look for your data
We often use this grammar in coding to specify where to load or save a file to.
Introduction to Image Analysis for Plant Phenotyping
Introduction to Image Analysis for Plant Phenotyping
Reach your personal and professional goals
Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.
Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free