Skip to content

Linux Command Line Interface for Beginners I

The operating system of the MSU HPC is CentOS, which is a distribution of Linux. So if you want to use our system, it is essential to equip some basic knowledge of Linux. Even though Linux supports a GUI, most works are done on a terminal via text. 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.

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 to run a command).

1
2
$ 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 prints out the shell's current working directory. You can change the working directory using the cd command, an abbreviation for 'change directory'.

1
2
3
$ 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.

1
2
3
$ 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 -

1
2
3
$ cd -
$ pwd
/

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

1
2
3
$ 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

1
2
3
$ cd ..
$ pwd
/

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

1
2
3
$ cd -
$ pwd
/bin

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

1
2
$ cd ../..
$ pwd

Relative and absolute Paths

A path is an address of a directory. Most of the examples we've looked at so far use relative paths. So your final location is decided based on your current working directory. However, sometimes you want to use an absolute path than a 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 path.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ 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

1
2
3
$ 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.

1
2
3
$ mkdir temp02 temp03 temp04
$ ls
temp01 temp02 temp03 temp04

To make a subdirectory, use with -p option such as

1
2
3
$ 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.

1
2
3
$ 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).

1
2
3
4
5
$ 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

1
2
3
$ rm list.txt
$ ls
temp01 temp02 temp03

Copying and renaming directories and files

To copy files or directories, use cp such as

1
2
3
$ cp list.txt list2.txt
$ ls
list.txt list2.txt temp01 temp02 temp03

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

The mv command moves files/directories or renames them.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ 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'). 

1
2
$ 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ 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 ..

Exercises

Now let's do some exercises. 

  • Log in your MSU HPC account and go to any dev-node. 
  • Create a 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 (not including the login process).
1
2
3
4
5
6
7
8
9
$ 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