A Quick Guide to Using Command Line (Terminal)

Being able to use command line is necessary for high-level coding. Here are the essentials you need to know.

Jerry Wei
Towards Data Science

--

Opening Command Line. If you’ve just started coding, you may have no idea what command line even is. No worries! It’s just a simple way to tell your computer what to do. There are varying ways of accessing command line, depending on what operating system you use.

  • MacOS: Open spotlight search (default way to do this is by hitting command and the space bar) and type in “terminal”. Select the application called terminal and press the return key. This should open up an app with a black background. When you see your username followed by a dollar sign, you’re ready to start using command line.
  • Linux: You can open Terminal by directly pressing [ctrl+alt+T] or you can search it up by clicking the “Dash” icon, typing in “terminal” in the search box, and opening the Terminal application. Again, this should open up an app with a black background. When you see your username followed by a dollar sign, you’re ready to start using command line.
  • Windows: On Windows 10, open the start menu and go to the shortcuts folder called “Windows System”. Pressing the dropdown menu should reveal a shortcut to open the Command Prompt application. Right click on the shortcut, press “More”, and press “Run as Administrator”. For Windows 8, go to the start screen, press “All Apps”, and scroll right until the “Windows System” folder shows up. You can find Command Prompt there. For Windows 7, open the start menu and click on “All Programs”. Click on “Accessories” and you’ll find the Command Prompt shortcut. Right click on the shortcut and press “Run as Administrator”.

Accessing Servers. If you run code on a server (e.g. a GPU cluster or something like Amazon Web Services), you need to be able to access the server. The best method to do this through Secure Shell (ssh), which lets you securely control and modify your server using the Internet. You can do this by typing in the following:

ssh username@host_server

Where username is the account name of your account on the server, and host_server is the host (e.g. GPU cluster’s name). If you have a password to access your account on the server, command line will prompt you to enter that in. If it’s your first time accessing that particular server, your computer may also ask you if it can remember the authenticity key — type in ‘yes’ or the corresponding phrase so that your computer doesn’t ask you this every time. When you’re linked up to the server, you should see that the command line starts with a green header with some kind of username@host format. Use this as an indicator that you’re really connected to the server.

Moving files. This one’s pretty simple. Type in the following:

mv target destination

Where mv is move, target is the file you want to move, and destination is where you want to move it to.

Copying files/folders. If you’re trying to copy files around, the easiest way to do this is using the cp command. Use the following script to copy around files.

cp -r target destination

In this script, cp represents copying and -r represents doing it recursively. The target is the target folder/file you’re trying to copy, and destination is the target folder you’re trying to copy it to. Important notes:

  • Let’s say you type in “cp -r home/target/ home/destination/”. If “home” is a folder and “target” is a folder, but “destination” is not a folder in home, then “target” will be copied into home but will be named “destination”.
  • Let’s say you type in “cp -r home/target/ home/destination/”. If all of these are valid folder, “target” will be copied as a folder inside of “destination” inside of “home” — you’ll end up with a folder that’s “home/destination/target/”.
  • Let’s say you type in “cp -r home/target/ users/”. This will just copy the “target” folder into the “users” folder under the same name (target).
  • Use the * character to represent all. For example, to copy all files in a folder that start with the letter a, your target should be “/folder/a*”. To copy all files of a certain extension (say something like .png), your target should be “/folder/*.png”.
  • Use the . character to represent the current folder.
  • If the file/folder already exists in destination, it WILL BE OVERRIDDEN. So make sure you don’t make this rookie mistake.

Deleting files/folders. Deleting files is pretty essential, so here’s how you should do it. Use this script:

rm -r target

Where the rm represents remove and -r represents recursively doing it. Target is the file/folder you’re trying to delete. More pro tips if you didn’t read the notes under “Copying files/folders”:

  • This command will not let you delete a file that’s not there to begin with (it’ll say something like “no such file or directory)
  • Use the * character to represent all. For example, to delete all files in a folder that start with the letter a, your target should be “/folder/a*”. To delete all files of a certain extension (say something like .png), your target should be “/folder/*.png”.
  • Use the . character to represent the current folder.
  • This command will delete files VERY QUICKLY. When a file is deleted, you can’t get it back. There’s no control-Z on this. Always make sure you type in the right directory/file.
  • Alternatively, you can enter “-i” after -r (e.g. rm -r -i target), which will prompt the computer to ask for permission to delete every single file.

Listing files. When you’re in a folder, it’s probably pretty useful to be able to see the files/folders in there. Use this:

ls target (options)

Target is the folder whose contents you’re trying to list (if you’re trying to list the contents of the folder you’re currently in, leave target blank). You can also use the following options:

  • “-F”: adds a character for the type of file (e.g. a “*” for an exectuable script or a “/” for a directory).
  • “-f”: stops the computer from sorting the contents. Useful when there are huge numbers of files and it’s not useful to sort the files.
  • “-a”: lists all files, including hidden files that would normally be hidden.
  • “-h”: will let you also get the sizes of the files.
  • “-t”: sorts the files by when it was last modified.

Changing the directory you’re in. Use this simple command to change your current directory:

cd target

Where cd is change directory and target is the folder you want to enter. More tips:

  • Use the . character to represent the current folder. Doing “cd .” will refresh what the command line says your current directory is (useful if you’ve changed a folder name in the path to your folder).
  • Use “../” to represent the enclosing folder. For example, if you’re currently in “home/target/”, then you can do “cd ../” to get to the “home” folder. The “../” is also stackable, so two of them would get you 2 folders back.

Making a new folder. This is also a pretty simple command. Use the following script:

mkdir target

Where mkdir is make directory and target is the name of the folder you want to make. For example, “mkdir target” would make a folder called “target” inside of the directory command line is currently in.

Clearing out the clutter. When you run too many commands, it’s easy to get your command line screen cluttered up with a bunch of green, blue, and white lines. Use this one word command to clear it up:

clear

Self-explanatory: clear represents clearing the screen.

Running scripts. If you’re coding, you’ll likely want to be able to run your code using command line. Lucky for you, there’s a great way to do this! Now, I’m a Python user, so here’s Python first:

python target

Target is the file you want to execute (it should end in “.py”).

For Java, use this:

java target

javac target

Doing “java” will run the file, and “javac” will compile it. Target is the file you want to execute (it should end in “.java”).

Other languages like C and JavaScript require special packages to run in command line, which I’ve hyperlinked instructions for.

Photo by Erwan Hesry on Unsplash

Congratulations! You’re now capable of using command line (to a certain extent). These are just the basic commands, there are more specialized commands to use for specific tasks, but these are the ones that almost anyone using command line should know.

--

--

Large language models, AI for health. Research Engineer at Google DeepMind.