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.
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 to run a command).
1 2 |
|
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 |
|
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 |
|
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 |
|
The root directory has many subdirectories including your home directory. Let's go to the 'bin' directory.
1 2 3 |
|
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 |
|
To go up to the previous directory, use -
with cd
such as
1 2 3 |
|
You can use ..
more than once if you have to move up multiple levels
of parent directories.
1 2 |
|
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 |
|
Creating and removing directories
To make a directory, use mkdir
(short for 'make directory') such as
1 2 3 |
|
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 |
|
To make a subdirectory, use with -p
option such as
1 2 3 |
|
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 |
|
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 |
|
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 |
|
Copying and renaming directories and files
To copy files or directories, use cp
such as
1 2 3 |
|
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 |
|
Listing directories and files
We already used ls. To list directories and files, us ls (short for 'list').
1 2 |
|
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 |
|
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
tolinux_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
intonew_dir
- Remove
garbage
dir
This is an answer (not including the login process).
1 2 3 4 5 6 7 8 9 |
|