Difference between revisions of "Training/Linux - command line"

From HPC
Jump to: navigation , search
m
m
Line 36: Line 36:
 
'''dbird@viper:~$ pwd'''
 
'''dbird@viper:~$ pwd'''
  
/home/dbird
+
''/home/dbird''
  
 
=== cd ===
 
=== cd ===
Line 46: Line 46:
 
'''user@viper:~$ pwd'''
 
'''user@viper:~$ pwd'''
  
/var
+
''/var''
 
'''user@viper:~$ cd /home/user'''
 
'''user@viper:~$ cd /home/user'''
  
 
'''user@viper:~$ pwd'''
 
'''user@viper:~$ pwd'''
  
/home/user
+
''/home/user''
  
 
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.
 
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.
Line 59: Line 59:
 
'''user@viper:~$ pwd'''
 
'''user@viper:~$ pwd'''
  
/home/user
+
''/home/user''
  
 
To go to the directory above (or parent directory), we use the characters ..
 
To go to the directory above (or parent directory), we use the characters ..
Line 65: Line 65:
 
'''user@viper:~$ pwd'''
 
'''user@viper:~$ pwd'''
  
/home/user
+
"/home/user"
  
 
'''user@viper:~$ cd ..'''
 
'''user@viper:~$ cd ..'''
Line 71: Line 71:
 
'''user@viper:~$ pwd'''
 
'''user@viper:~$ pwd'''
  
/home
+
"/home"
  
 
There is also the character . which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.
 
There is also the character . which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.
  
 
=== Absolute and relative paths ===
 
=== Absolute and relative 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 absolute path
 +
 +
'''user@viper:~$ pwd'''
 +
 +
"/home/user"
 +
 +
'''user@viper:~$ cd /var'''
 +
 +
'''user@viper:~$ pwd'''
 +
 +
"/var"
 +
 +
And relative path
 +
 +
"'user@viper:~$ pwd'"
 +
 +
"/home/user"
 +
 +
"'user@viper:~$ cd myfiles'"
 +
 +
'''user@viper:~$ pwd'''
 +
 +
"/home/user/myfiles"
 +
 +
The command line will help you in typing a path without errors. If you type a partial command such as:
 +
 +
'''user@viper:~$ cd /home/user/my'''
 +
 +
Pressing the '''TAB''' key will fill in the rest of the directory name, if that directory exists and it is unique). So the following would appear
 +
 +
'''user@viper:~$ cd /home/user/myfiles'''
 +
 +
=== ls ===
 +
 +
This command lists the contents of a directory:
 +
 +
'''user@viper:~$ ls'''
 +
 +
''myfile1.txt myfile2.txt mydirectory1''
 +
 +
'''ls''' has a number of useful different options
 +
 +
'''user@viper:~$ ls –l'''  (show a long listing with more information)
 +
'''user@viper:~$ ls –a'''  (show all files including those that are hidden)
 +
'''user@viper:~$ ls –la'''  (combines both of the options above)
 +
 +
=== mkdir ===
 +
 +
This commands makes a directory from the specified directory:
 +
 +
* user@viper:~$ mkdir mydirectory1
 +
* user@viper:~$ cd mydirectory1
 +
* user@viper:~$ pwd
 +
/home/user/mydirectory1
 +
 +
 +
 +
 +
  
  

Revision as of 14:15, 27 February 2017

Introduction

This represents a series of teaching pages to allow you to learn more about Viper's Linux command line interface

Command Line

Like other operating systems Linux does have a window’s type environment too called X and hopefully in the future its successor Wayland. (Linux refers to X and Wayland with the term Display Server). However, with VIPER the vast majority of work will be carried out on 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.

Man pages

This will explain the use of man pages (also called manual pages) on Linux. Most Linux files and commands have pretty good man pages to explain their use. Type man followed by a command (for which you want help) and start reading. Press q to quit the man page. See below:

user@viper:~$ man whois (shows manual page for the command whois)

user@viper:~$ man syslog.config (shows the manual page for a configuration file)

user@viper:~$ man syslogd (show the manual for a daemon (background program))

user@viper:~$ man –k syslog (an apropos which shows a list of available man pages with this string contained within it)

Working with 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 a number of commands for the user to navigate around its own filesystem.

This module is a brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux system.

This section will also discuss absolute and relative paths and path completion in the bash shell.

pwd

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

dbird@viper:~$ pwd

/home/dbird

cd

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

user@viper:~$ cd /var

user@viper:~$ pwd

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

user@viper:~$ pwd

/home/user

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.

user@viper:~$ cd ~

user@viper:~$ pwd

/home/user

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

user@viper:~$ pwd

"/home/user"

user@viper:~$ cd ..

user@viper:~$ pwd

"/home"

There is also the character . which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.

Absolute and relative 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 absolute path

user@viper:~$ pwd

"/home/user"

user@viper:~$ cd /var

user@viper:~$ pwd

"/var"

And relative path

"'user@viper:~$ pwd'"

"/home/user"

"'user@viper:~$ cd myfiles'"

user@viper:~$ pwd

"/home/user/myfiles"

The command line will help you in typing a path without errors. If you type a partial command such as:

user@viper:~$ cd /home/user/my

Pressing the TAB key will fill in the rest of the directory name, if that directory exists and it is unique). So the following would appear

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

ls

This command lists the contents of a directory:

user@viper:~$ ls

myfile1.txt myfile2.txt mydirectory1

ls has a number of useful different options

user@viper:~$ ls –l (show a long listing with more information) user@viper:~$ ls –a (show all files including those that are hidden) user@viper:~$ ls –la (combines both of the options above)

mkdir

This commands makes a directory from the specified directory:

  • user@viper:~$ mkdir mydirectory1
  • user@viper:~$ cd mydirectory1
  • user@viper:~$ pwd

/home/user/mydirectory1






Further Information