Skip to content

Command Line

Nikoleta Glynatsi edited this page Aug 11, 2024 · 7 revisions

An introduction to command line. Firstly, you need to identify and open your command line interface.

We recommend the following:

  • Windows: we will choose to use Git bash (which was installed on your machine when you installed Git).
  • nix (another way to describe Mac OS and/or Linux machines): we will use the system terminal.

Finding your computer's name.

Let us first let's find out the name of your computer by running:

$ whoami

Finding your current location

Now let's find out which directory (folder) we are currently in:

$ pwd

This stands for "present working directory".

Type the command in and press enter. It should list where you are currently located in your command line interface.

Seeing what is in your current location

To view the contents of the current directory:

$ ls

This stands for "list".

Type the command in and press enter. You should see a list of the various files and directory in your current directory. Open your current directory in a graphical user interface and compare.

Moving to another location

If you want to enter a directory that is in your current directory type:

$ cd <directory>

Try moving to your Desktop. It should be something like:

$ cd Desktop

Creating a directory

To create a directory:

$ mkdir <directory_name>

Experiment with creating a directory for this workshop:

$ mkdir euroscipy2024

If your directory structure looked like this:

|--- home/
|--- Desktop/
     |--- research
     |--- photos

It will now look something like:

|--- home/
|--- Desktop/
     |--- research
     |--- photos
     |--- euroscipy2024

Move into the directory we just created:

$ cd euroscipy2024

Let's create one further directory src. You can navigate in the new folder with:

$ cd src

If you now wanted to go back to the parent directory:

$ cd ..

Where .. is short hand for a previous directory.

Creating a file

To create a file:

$ touch <file_name>

Experiment with creating a file named README.md in the directory euroscipy2024.

$ touch README.md

If you type ls you will see that the file has been created.

Copying files

To copy a file:

$ cp <file> <new_file_directory_and_name>

Let's copy README.md to src:

$ cp README.md src

Moving/renaming files

To move a file:

$ mv <file> <new_file_directory_and_name>

Having two README.md files could cause some confusion. So let's rename the copy we created by using the mv command.

$ mv src/README.md src/index.md

Note that if you want to rename a file you can do this by passing the new name in the same directory.

WARNING When using the command line interface you will not be prompted for confirmation if move/mv were to overwrite another file. Be careful.

Deleting files

To delete a file:

$ rm <file>

Copying and removing directories

To copy a directory:

$ cp -r <dir> <target>

To remove a directory:

$ rm -r <dir>