Quickstart/Linux Command Line

From HPC
Jump to: navigation , search

The Command Line

The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style, there are a few different ones to choose from. However, the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

Below is an example of Viper's command-line interface using the terminal program MobaXterm.

Commandline.jpg

Directories

As with other operating systems such as Microsoft Windows, the filesystem is based around files and directories. Linux is no exception to this and uses several commands for the user to navigate around its filesystem.

File Paths

Although an important concept to understand the rule here is very simple. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point.

Here are two examples, firstly of the absolute path:

username@viper:~$ pwd
/home/user

user@viper:~$ cd /var
user@viper:~$ pwd
/var

And relative path

username@viper:~$ pwd
/home/user

username@viper:~$ cd myfiles
username@viper:~$ pwd
/home/user/myfiles


pwd

On the command line pwd (or print working directory) displays the current directory you are in. This would appear as:


username@viper:~$ pwd
/home/username

cd

On the command line cd (or change directory) change your current directory to the one specified:


username@viper:~$ cd /var
username@viper:~$ pwd
/var
username@viper:~$ cd /home/user
username@viper:~$ pwd
/home/username

There is also a shortcut back to your home directory by typing the character ~ (tilda) which has the same effect as typing (for example) /home/user.


username@viper:~$ cd ~
username@viper:~$ pwd
/home/username

To go to the directory above (or the parent directory), we use the characters ..


username@viper:~$ pwd
/home/username

username@viper:~$ cd ..
username@viper:~$ pwd
/home

ls

This command lists the contents of a directory:


username@viper:~$ ls
myfile1.txt myfile2.txt mydirectory1

ls also has several options:

  • ls -l shows a long listing with more information
  • ls -a shows all files including those that are hidden
  • ls -la shows a combination of the options above
  • ls *(file type) shows all files with that file type (eg: ls *py will show all python files)


mkdir

This command makes a directory from the specified directory:


username@viper:~$ mkdir mydirectory1
username@viper:~$ cd mydirectory1
username@viper:~$ pwd
/home/user/mydirectory1

rmdir

This command removes the specified directory, note the directory must be empty and must not be the directory you are currently in:


username@viper:~$ rmdir mydirectory1

Files

Files on Linux are case-sensitive. This means that FILE1 is different from file1, and /etc/hosts are different from /etc/Hosts (the latter one does not exist on a typical Linux computer).

Everything on Linux is a file. A directory is a special kind of file, but it is still a (case-sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1), and any process are all represented somewhere in the file system as a file.

touch

This creates an empty file, which can be useful for various uses.


username@viper:~$ touch newfile.c
username@viper:~$ ls
newfile.c

cp

Copy files or directories from a source to a destination:


username@viper:~$ cp parallel.c mybackup.c  (copies parallel.c to mybackup.c)
username@viper:~$ cp parallel.c mydirectory1   (copies parallel.c to mydirectory1)
username@viper:~$ cp *c backupdirectory/  (copies all *.c files to backupdirectory)
username@viper:~$ cp –r mydirectory1 mydirectory2  (copies one directory to another, note the option –r for recursive copying)

mv

Move files from a source to a destination. A versatile command that can rename a file too:


username@viper:~$ mv file1.c testfile.c  (rename file1.c to testfile.c)
username@viper:~$ mv directory1 directory2  (rename directory)
username@viper:~$ mv file1.c /home/user/myrepo  (mv file1.c to /home/user/myrepo/file1.c)

cat

The cat command (short for concatenate) is one of the most universal tools, yet all it does is copy standard input to standard output.


username@viper:~$ cat /etc/resolv.conf

domain example.com
search example.com
nameserver 192.168.1.42

rm

Icon warning.png REMOVING A FILE OR DIRECTORY IS PERMANENT, there is no recycling bin - this file/directory will be gone forever.

Remove a file, as always be very careful with this command and without a backup, this file will be lost forever.


username@viper:~$ ls
newfile.c backup.c
username@viper:~$ rm newfile.c
username@viper:~$ ls
backup.c

You can use rm -r directory_name to remove directories that are not empty.

Further Commands

Additional (but non-essential) Commands

File permissions in the command line



Back / Next (Using Modules)