Page tree
Skip to end of metadata
Go to start of metadata

The OS of MSU HPC is CentOS which is a brach of Linux. So if you want to use our system, it is essential to equip some basic knowledge on Linux. Even though Linux supports GUI, most works are done on a dummy terminal via texts. The Linux command line is a text interface to Linux. 

We will walk through some practical exercises to become familiar with a few basic commands and concept.

Navigation

Let's run the first command. Type pwd and pressing the Enter or Return key to run it (From now, I'll not mention pressing Enter/Return key part to run command).

$ pwd
/mnt/home/iamsam/

You will see a path such as /mnt/home/user/your_id

pwd is an abbreviation of 'print working directory'. It print out the shell's current working directory. You can change the working directory using the cd command, an abbereviation for 'change directory'.


$ cd /
$ pwd
/

Now your working directory is '/' which is the root directory. There is nothing much you can do on the root directory, so let's go to your 'home' directory.

$ cd 
$ pwd
/mnt/home/iamsam/

Regardless of your location, when you just type 'cd', you will be home. You can also type 'cd ~' instead of 'cd' to be back your home.

To go the previous directory, type 'cd -'

$ cd -
$ pwd
/

The root directory has many subdirectories including your home directory. Let's go to 'bin' directory.

$ cd bin
$ pwd
/bin

To go up to the parent directory (it is / for us now), use the special syntax of two dots with cd such as

$ cd ..
$ pwd
/

To go up to the previous directory, use - with cd such as

$ cd -
$ pwd
/bin

You can use .. more than once if you have to move up multiple levels of parent directories.

$ cd ../..
$ pwd

Relative and absolute Paths

A path is an address of a directory. Most of the examples we've looked at so far uses relative paths. So you final location is decides based on your current working directory. However, sometimes you want to use an absolute path than relative one. Your home's absolute path at HPC is /mnt/home/your_id. See the example to find how to use a relative and absolute paths.

$ cd /
$ pwd
/
$ cd -
$ pwd
/mnt/home/iamsam
$ cd /
$ cd /mnt/home/iamsam
$ pwd
/mnt/home/iamsam

Creating and removing directories

To make a directory, use mkdir (short for 'make directory') such as

$ mkdir temp01
$ls
temp01

ls is a command to list files and folder. We will learn it in a minute. You can create multiple directories as well.

$ mkdir temp02 temp03 temp04
$ ls
temp01 temp02 temp03 temp04

To make a subdirectory, use with p option such as

$ mkdir -p temp04/temp05/temp06
$ ls temp04
temp05

To remove directory use rm command with r. Without r option, rm will not delete directories (but you can delete files). r means recursive.

$ rm temp04
rm: cannot remove 'temp04': Is a directory
$ rm -r temp04

Creating and removing files

You can use editors to create files, but it is out of scope of this tutorial. Let's use ls and a pipe > (we will explain pipes later).

$ ls
temp01 temp02 temp03
$ ls > list.txt
$ ls
list.txt temp01 temp02 temp03

Now we have three directories (temp01, temp02, temp03) and one file (list.txt). We already know how to delete directories. To delete files, we use command rm such as

$ rm list.txt
$ ls
temp01 temp02 temp03

Copying moving/renaming directories and files

To copy files or directories, use cp such as

$ cp list.txt list2.txt
$ ls
list.txt list2.txt temp01 temp02 temp03

With r option, you can copy files and directories recursively. i.e., copy subdirectories and files.

mv command mv files/directories or rename them.

$ mv list2.txt temp01
$ ls
list.txt temp01 temp02 temp03

$ ls temp01
list2.txt

$ cd temp01
$ mv list2.txt list3.txt
$ ls
list3.txt

Listing directories and files

We already used ls. To list directories and files, us ls (short for 'list'). 

$ ls
temp01 temp02 temp03

There are many options for ls. Most frequently used options are

  • a: list all files and directories including hidden contents
  • h: print sizes in human readable format (e.g.: 1K, 2.4M, 3.1G)
  • l: list with a long listing format
  • t: sort my modification time

You can use options separately like ls -l -a -t or together like ls -lat.

$ ls -l -a -t
total 8
-rw-r--r--    1 iamsam  staff    30 Mar 23 15:37 list.txt
drwxr-xr-x    6 iamsam  staff   192 Mar 23 15:37 .
drwxr-xr-x    2 iamsam  staff    64 Mar 23 15:37 temp03
drwxr-xr-x    2 iamsam  staff    64 Mar 23 15:37 temp02
drwxr-xr-x    2 iamsam  staff    64 Mar 23 15:37 temp01
drwxr-xr-x+ 113 iamsam  staff  3616 Mar 23 15:21 ..

$ ls -lat
total 8
-rw-r--r--    1 iamsam  staff    30 Mar 23 15:37 list.txt
drwxr-xr-x    6 iamsam  staff   192 Mar 23 15:37 .
drwxr-xr-x    2 iamsam  staff    64 Mar 23 15:37 temp03
drwxr-xr-x    2 iamsam  staff    64 Mar 23 15:37 temp02
drwxr-xr-x    2 iamsam  staff    64 Mar 23 15:37 temp01
drwxr-xr-x+ 113 iamsam  staff  3616 Mar 23 15:21 ..

Exercise

Now let's do some exercise. 

  • Log in your MSU HPC account and go to any dev-node. 
  • Create linux_tutorial dir on your home.
  • Copy a folder and contents for this tutorial from 
  • /mnt/research/common-data/workshops/intro2Linux_iamsam to linux_tutorial dir on your home
  • Go to linux_tutorial
  • Find a hidden directory and rename it to not_hidden
  • Check the contents of not_hidden
  • Create a new directory called new_dir
  • Copy the file youfoundit.txt into new_dir
  • remove garbage dir
This is an answer (I do not include the login process).
$ mkdir linux_workshop
$ cp -r /mnt/research/common-data/workshops/intro2Linux_iamsam linux_tutorial
$ cp linux_tutorial
$ ls -a
$ mv .hidden not_hidden
$ ls not_hidden
$mkdir new_dir
$ cp not_hidden/youfoundit.txt new_dir
$ rm -r garbage
  • No labels