Advanced workshop ex1

PURPOSE

To become familiar working command line and learn some of the most basic commands.

DATA

We will not use any data for this exercise.

CONVENTIONS

Before beginning the exercise, it is important to be aware of a few conventions related to how commands are written in the exercise guides and command line.

When you in one of the exercise guides are asked to write a particular command, it could look something like this:

General command:

$ mv [old_filename.txt] [new_filename.txt]

Specific command:

$ mv file1txt file_renamed.txt

1) When you type the command, you should not include the leading “$”. It simply indicates the end of the command prompt.

2) When a command includes text in square brackets, [some text], you should substitute the text inside the square brackets AND the square brackets with, for instance, a specific file name. 

3) When I have written both a general command and a specific command, the general command is for you to understand which elements the command contains, so that you might later adapt it to your specific needs. The specific command is related to the current exercise and can be copy and pasted directly to the command prompt.

4) You should end all commands by pressing Enter. When you do that the command will be executed.

WORKFLOW

How to access the command line

Mac:

Open your Applications folder, then open the Utilities folder. Open the Terminal application. You may want to add this to your dock. Alternatively you can launch Terminal by using Spotlight search, searching for “terminal”.

You are now ready to go to the section “Listing the content of directories: ls”.

Windows:

Right click on your desktop (not on an icon or file) and select “Git Bash Here” to open a Git Bash command prompt.

At the command prompt type:

$ cd

You are now ready to go to the section “Listing the content of directories: ls”.

Listing the content of directories: ls

Nearly all commands follow a common pattern with 3 main parts. The program, the options, and the arguments, although the program can also often be called on its own in which case default options and arguments are invoked. Here’s an example:

$ ls -l ~

“ls” is the program. It will list the content of a folder.

“-l”  is the option (and does not need to be included). When you include “-l”, ls will use the long output format when listing the content of a folder.

“~” is the argument - which folder to list the content of. If you omit “~”, the content of the folder you are currently in will be listed.

Use ls to see what is in the folder you are currently in, asking first for the short output format and next for the long output format:

$ ls

$ ls -l

You might want to readjust the size of the Terminal window to make the output easier to read.

Note: On Windows, some lines will have a leading “l”. This denotes a link and is not something we will use during this workshop.

Q1: Can you find an example of a file and of a directory? Are any of the files executable? What is the size of the largest file?

Note: The size of directories as listed by ls is related to the size of space on the disk that is used to store the meta information for the directory (i.e. the table of files that belong to this directory). You cannot use ls to see the size of what is in the directory. 

Besides “-l”, some useful options for ls include:

-R, which will not only show each folder in the directory, but also all their files (it takes quite a while to show all the content. If you get tired of waiting, while the text passes over the screen, you can stop the command by pressing CTRL+C).

-a, which will show any hidden files/directories in the current directory.

Examine if you have any hidden files/directories in the folder you are standing in. Hidden files/directories have a leading “.”:

$ ls -a

If you just type the above command, you cannot see if the hidden files/directories are files or directories. For this, you should combine the command with the “-l” option:

$ls -a -l 

Note: The above command can also be written like this:

$ls -al

Q2: Do you have a hidden directory called “.ssh”?

Where are you? pwd

The directory you are currently in is called your working directory. You can see where you are using pwd:

$ pwd

Often, the working directory will also be listed as part of the command line, but sometimes the full path is not included, for instance, your home directory is sometimes substituted with “~“.

You can change your directory using the command “cd”. If you add an argument, it will change your location to that of the argument, if possible. Without an argument, it will take you to your home directory (~). Try to move to your Documents folder:

$ cd Documents

If you want to navigate “up”, that is to the directory that contains your current directory, you can use “ ..”. To go up one level (to where you came from):

$ cd ..

Note: There is a space between “cd” and “..” in the above command.

To go up X directories, just add “..” X times separated by “/“:

$ cd ../../

Move back to your home directory. Remember that calling cd without arguments will take you to your home directory:

$ cd

Where to get help: man

If you work on a Mac, most commands will have a manual in which you can read all about the command and the possible options. To access the manual, use the “man” command. Pass the name of the command you want to learn about as it’s only argument. For instance, to learn more about ls type:

$ man ls

The manual can be scrolled with the arrow keys or space bar. Press “q” to quit.

The man command is not available on Windows, but you can find information about the commands online. Once you access the AWS Linux instance (Ex. 2), man will work here too.

A few other commands: mkdir, cp, mv, rm, rmdir, less

You can use “mkdir” to make a new directory.

If you did not have a hidden directory in your home folder called .ssh, create one now (make sure you are standing in your home directory, when typing the command):

$ mkdir .ssh

Confirm that it is there with “ls -a”.

Everyone should make a directory called testdir:

$ mkdir testdir

Use “ls -l” to confirm that you now have a directory called testdir.

Move to testdir:

$ cd testdir

Q3: Are there any files/directories in testdir?

The following command, “echo”, is probably not one you will need a lot, but it will print whatever you write inside the quotes following the command. We add “> testfile.txt” to the command to direct the text into a file called testfile.txt:

$ echo “The test file just contains this line” > testfile.txt

Use ls to confirm that there is now a file named testfile.txt in testdir.

You can use the command “less” to see the content of a file (press “q” to quit):

$ less testfile.txt

The command “cp” will copy the file:

$ cp testfile.txt testfile_copy.txt

Use less to confirm that the content of testfile_copy.txt is identical to that of testfile.txt.

If you want to change the name of a file or directory, use “mv”:

$ mv testfile.txt newname.txt

Confirm that testfile.txt is now gone and that newname.txt has appeared in the directory and contains the same line of text as testfile.txt used to contain.

If you add a path to the destination file of either cp or mv, the new file will be generated in the directory that is specified by the path. Just remember that the original file will be kept, if you use cp and deleted from the original location, if you use mv. 

To remove a file entirely, use rm:

$ rm newname.txt

Now, move to your home directory (try to remember yourself how it was done).

The command “rmdir” will remove a directory, but only if it is empty:

$ rmdir testdir

Q4: Is the testdir folder gone?

You can use "rm -rf” to remove the directory even though it is not empty:

$ rm -rf testdir

WARNING: Be very careful when you use rm and rmdir. The files/directories will be totally and instantly gone and cannot be recreated!

During the next exercises the commands will not be explained in the exercise guide, but an overview of all commands can be found here