Linux Terminal Tutorial - Miscellaneous Commands

Introduction

This section will demonstrate the usage of miscellaneous commands in the Linux Terminal.

Handling archives archives with tar

Files, directories and software are often distributed in compressed form. Under Linux the most popular compression format is tar.gz (also referred to as tar-balls) which is a combination of the tar and zip (gz) formats.

Creating an archive using tar

Creating an archive requires using the correct combination of command switches to set the mode for the tar program.

To compress a directory using tar use the following combination of command switches:

  • c - create an archive,
  • v - print out additional information about the process (verbose)
  • z - pass the tar archive through gzip
  • f - use an archive file

Now, compress each of the main directories in Tutorial:

$ tar -cvzf nvt_expr_1.tar.gz nvt_expr_1
$ tar -cvzf npt_expr_1.tar.gz npt_expr_1
$ tar -cvzf md_expr_1.tar.gz md_expr_1

Extracting an archive using tar

To extract archives the following mode needs to be used:

  • x - extract an archive,
  • v - print out additional information about the process (verbose)
  • f - use an archive file

To demonstrate this, first create the directory tar_example under Tutorial.

Next, move the tar.gz files created in the previous steps to this directory and finally cd into this directory (Your current working directory should be Tutorial/tar_example/)

Finally, extract nvt_expr_1.tar.gz in the current working directory:

$ tar -xvzf nvt_expr_1.tar.gz

Sometimes it will be necessary to extract an archive to a specific directory. To achieve this, use the -C option.

In the terminal, commands can also be linked. Thus to demonstrate two concepts above, create a new directory npt_extract and extract npt_expr_1.tar.gz:

$ mkdir npt_extract && tar -xvf npt_expr_1.tar.gz -C npt_extract

Note

In the command above the mkdir and tar commands are chained. The && means that the mkdir will be executed, and if the mkdir command is successful, the tar command will execute

With only one & the tar command will execute regardless of whether the mkdir command was successful.

The above command will create the directory npt_expr_1 inside the npt_extract directory. This is because the archive maintains the original directory structure. To only extract the contents of main directory into the new directory, add the --strip-components=1 option to the original command.

Lets try this with md_expr_1.tar.gz:

$ mkdir md_extract & tar -xvf md_expr_1.tar.gz -C md_extract --strip-components=1

Confirm the correct execution of the command by using ls on md_extract

Keep terminal sessions active using screen

Many operations in the HPC environment require many days or months to execute and progress can be loss when a loss of connectivity to a remote terminal session occurs. To combat this, you may use the command screen to keep terminal sessions active.

Create a screen session

To demonstrate the functionality of screen, first create a screen session called demo_screen_1:

$ screen -S demo_screen_1

Now run the command ping www.google.com. This command will continue to run until we stop it (by pressing ctrl + c).

Detach a screen session

Now detach your session (while ping continues to run):

$ *press Ctrl+A, release, then press D*

The running ping command should disappear and you should see a message in the terminal like this:

[detached from 36750.demo_screen_1]

Note that the number may differ on your session.

List available screen sessions

You may have multiple screen sessions. To see a list of active screen sessions, issue the following command:

$ screen -ls

You should spot your detached screen session in the list.

Now logout and and log back into the cluster (skip this step if you are not following this tutorial on a remote server).

Reattaching a screen session

Now reattach the detached session demo_screen_1:

$ screen -r demo_screen_1

The ping command should still be running and be producing output on the screen again. You may end ping by pressing ctrl+c.

Deleting/quiting a screen session

To exit and delete the screen session, first obtain a list of screen sessions and then issue the following command with the name of the session by screen -ls

To demonstrate this, delete demo_screen_1:

$ screen -X -S 36750.demo_screen_1 quit

Confirm the deletion of the session with screen -ls