Skip to content

Linux Command Line Interface for Beginners I

The operating system of the MSU HPC is Ubuntu, 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).

input
1
pwd
output
1
/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'.

input
1
2
cd /
pwd
output
1
/

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.

input
1
2
cd 
pwd
output
1
/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 -

input
1
2
cd -
pwd
output
1
/

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

input
1
2
cd bin
pwd
output
1
/bin

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

input
1
2
cd ..
pwd
output
1
/

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

input
1
2
cd -
pwd
output
1
/bin

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

input
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.

input
1
2
cd /
pwd
output
1
/
input
1
2
cd -
pwd
output
1
/mnt/home/iamsam
input
1
2
3
cd /
cd /mnt/home/iamsam
pwd
output
1
/mnt/home/iamsam

Creating and removing directories

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

input
1
2
mkdir temp01
ls
output
1
temp01

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

input
1
2
mkdir temp02 temp03 temp04
ls
output
1
temp01 temp02 temp03 temp04

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

input
1
2
mkdir -p temp04/temp05/temp06
ls temp04
output
1
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.

input
1
rm temp04
output
1
rm: cannot remove 'temp04': Is a directory
input
1
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).

input
1
ls
output
1
temp01 temp02 temp03
input
1
2
ls > list.txt
ls
output
1
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

input
1
2
rm list.txt
ls
output
1
temp01 temp02 temp03

Copying and renaming directories and files

To copy files or directories, use cp such as

input
1
2
cp list.txt list2.txt
ls
output
1
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.

input
1
2
mv list2.txt temp01
ls
output
1
list.txt temp01 temp02 temp03
input
1
ls temp01
output
1
list2.txt
input
1
2
3
cd temp01
mv list2.txt list3.txt
ls
output
1
list3.txt

Listing directories and files

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

input
1
ls
output
1
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.

input
1
ls -l -a -t
output
1
2
3
4
5
6
7
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 ..
input
1
ls -lat
output
1
2
3
4
5
6
7
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).
input
1
2
3
4
5
6
7
8
9
mkdir linux_tutorial
cp -r /mnt/research/common-data/workshops/intro2Linux_iamsam linux_tutorial
cd linux_tutorial
ls -a
mv .hidden not_hidden
ls not_hidden
mkdir new_dir
cp not_hidden/youfoundit.txt new_dir
rm -r garbage