Difference between revisions of "Shell"

From HPC Wiki
Jump to navigation Jump to search
m (Removed broken link.)
 
(34 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
[[Category:Basics]]
 
== General ==
 
== General ==
The Shell or commandline is program to interact with a given computer (or supercomputer). Contrary to the graphical user interface most desktop machines use, the shell is very basic and requires the user to have a certain knowledge on what commands there are and how to use them. However, even with a few basic commands, the shell can be a very powerful tool. Combining this power with the fact that the shell itself uses almost uses no resources, is the reason, why it is still in use on (nearly) every (super)computer.
+
The Shell or commandline is a program to interact with a given computer (or supercomputer). Contrary to a graphical user interface (e.g. Windows or Mac Desktop) which most desktop machines offer today, the shell seems simplistic, although daunting at the same time, because it requires the user to have a certain knowledge of the available commands and their uses. However, even with a few basic commands, the shell can be used productively and become a powerful tool in your toolbox. This power combined with the fact that the shell itself uses almost no resources is the reason, why it is still widely used on nearly every supercomputer.
  
A common shell is the bash (Bourne Again SHell), which comes with nearly every unix-based operating system. Others unix shells include the sh, csh, tcsh, zsh, ksh. All these have very similar commands/syntax, which will be described below.
+
A common shell is the bash (Bourne Again SHell), which comes with nearly every Unix-based operating system. Others Unix shells include the sh, csh, tcsh, zsh, ksh. All of these have quite similar syntax and offer a similar range of commands, which will be described below.
  
Windows has the cmd or the PowerShell (ps), which is different syntaxwise and will not be covered here, since most supercomputers run unix systems.
+
Windows has the DOS commands (available with cmd) or the PowerShell (ps), which is different syntax-wise and will not be covered in too much detail, since most supercomputers run Unix systems.
 +
 
 +
 
 +
__TOC__
  
  
 
== Usage ==
 
== Usage ==
Enter a command, hit return and read/wait for the answer :-P
+
Enter a command, hit return and wait for the answer :-P
 +
 
 +
You can abort most commands by pressing <code>Ctrl + C</code>
 +
 
 +
To make your life easier, you can also write a bunch of commands together into a text file, save it as 'name.sh' and then run that to call all those commands in sequence and repeatable. This is called an [[sh-file]] or shell script.
 +
 
 +
Furthermore you can get help for most commands by appending <code>-h</code> or <code>--help</code> to show the corresponding help message of the executed command. Additionally the so-called manual pages (or short: man pages) provide you with a detailed description for the specified command. Type <code>man ls</code> to look at the man page for the <code>ls</code> command, scroll with your arrow keys and press <code>q</code> to exit.
  
== Navigating the Unix File System ==
+
== Navigating the File System ==
 
The following commands tell you, where you are, take you somewhere else and show you what is there:
 
The following commands tell you, where you are, take you somewhere else and show you what is there:
{| class="wikitable" style="width: 40%;"
+
{| class="wikitable" style="width: 45%;"
 
| '''Linux Command''' || '''DOS Command''' || '''Description'''
 
| '''Linux Command''' || '''DOS Command''' || '''Description'''
 
|-
 
|-
 
| pwd || cd || “Print Working Directory”. Shows the current location in the directory tree.
 
| pwd || cd || “Print Working Directory”. Shows the current location in the directory tree.
 
|-
 
|-
|cd || cd, chdir || “Change Directory”. When typed all by itself, it returns you to your home directory.
+
| cd <directory> || cd <directory> || Change into the specified directory name. Example: cd /usr/src/linux
|-
 
| cd directory || cd directory || Change into the specified directory name. Example: cd /usr/src/linux
 
 
|-
 
|-
 
| cd ~ || || “~” is an alias for your home directory. It can be used as a shortcut to your “home”, or other directories relative to your home.
 
| cd ~ || || “~” is an alias for your home directory. It can be used as a shortcut to your “home”, or other directories relative to your home.
Line 29: Line 37:
 
| ls -l || dir || List files in “long” format, one file per line. This also shows you additional info about the file, such as ownership, permissions, date, and size.
 
| ls -l || dir || List files in “long” format, one file per line. This also shows you additional info about the file, such as ownership, permissions, date, and size.
 
|-
 
|-
| ls -a || dir /a || List all files, including “hidden” files. Hidden files are those files that begin with a “.”, e.g. The .bash_history file in your home directory.
+
| ls -a || dir /a || List all files, including “hidden” files. On Unix systems hidden files are those beginning with a “.”, e.g. The .bash_history file in your home directory.
 
|}
 
|}
  
 
== Doing Stuff with Files and Directories ==
 
== Doing Stuff with Files and Directories ==
The following commands tell you, where you are, take you somewhere else and show you what is there:
+
The following commands let you manipulate and interact with files. Since these commands all work on files, the syntax is usually
 +
 
 +
$ <command> <filename>
 
{| class="wikitable" style="width: 40%;"
 
{| class="wikitable" style="width: 40%;"
 
| '''Linux Command''' || '''DOS Command''' || '''Description'''
 
| '''Linux Command''' || '''DOS Command''' || '''Description'''
Line 39: Line 49:
 
| file || || Find out what kind of file it is. For example, “file /bin/ls” tells us that it is a Linux executable file.
 
| file || || Find out what kind of file it is. For example, “file /bin/ls” tells us that it is a Linux executable file.
 
|-
 
|-
| cat || type || Display the contents of a text file on the screen. For example: cat mp3files.txt would display the file we created in the previous section.
+
| vim || || Opens the [[vim]] text editor to modify files.
 +
|-
 +
| cat || type || Display the contents of a text file on the screen. For example: cat MyFile.txt would display the contents of the file.
 
|-
 
|-
 
| head || || Display the first few lines of a text file. Example: head /etc/services
 
| head || || Display the first few lines of a text file. Example: head /etc/services
Line 47: Line 59:
 
| tail -f || || Display the last few lines of a text file, and then output appended data as the file grows (very useful for following log files!). Example: tail -f /var/log/messages
 
| tail -f || || Display the last few lines of a text file, and then output appended data as the file grows (very useful for following log files!). Example: tail -f /var/log/messages
 
|-
 
|-
| cp || copy || Copies a file from one location to another. Example: cp mp3files.txt /tmp (copies the mp3files.txt file to the /tmp directory)
+
| cp || copy || Copies a file from one location to another. Example: cp MyFile.txt /tmp (copies the MyFile.txt file to the /tmp directory)
 
|-
 
|-
| mv || rename, ren, move || Moves a file to a new location, or renames it. For example: mv mp3files.txt /tmp (copy the file to /tmp, and delete it from the original location)
+
| mv || rename, ren, move || Moves a file to a new location, or renames it. For example: mv MyFile.txt /tmp (copy the file to /tmp, and delete it from the original location)
 
|-
 
|-
| rm || del || Delete a file. Example: rm /tmp/mp3files.txt
+
| rm || del || Delete a file. Example: rm /tmp/MyFile.txt
 
|-
 
|-
| mkdir || md || Make Directory. Example: mkdir /tmp/myfiles/
+
| mkdir || md || Create an empty directory. Example: mkdir /tmp/myfiles/
 
|-
 
|-
| rmdir || rd, rmdir || Remove Directory. Example: rmdir /tmp/myfiles/
+
| rmdir || rd, rmdir || Delete an empty directory. Example: rmdir /tmp/myfiles/
 
|}
 
|}
  
 +
== Getting System Info ==
 +
The following commands will give you information about the system or users:
 +
{| class="wikitable" style="width: 40%;"
 +
| '''Linux Command''' || '''Description'''
 +
|-
 +
| ps || Lists currently running process (programs).
 +
|-
 +
| w || Show who is logged on and what they are doing.
 +
|-
 +
| id || Print your user-id and group id's
 +
|-
 +
| du || Disk Usage in a particular directory. <code>du -s</code> provides a summary for the current directory.
 +
|-
 +
| top || Displays all currently running processes on your system in full-screen. A great way to see the activity on your computer in real-time. Type <code>q</code> to quit.
 +
|-
 +
| free || Displays amount of free and used memory in the system.
 +
|-
 +
| cat /proc/cpuinfo || Displays information about your CPU.
 +
|-
 +
| cat /proc/meminfo || Display lots of information about current memory usage.
 +
|-
 +
| uname -a || Prints system information to the screen (kernel version, machine type, etc.)
 +
|}
  
== FAQs ==
+
== Utilities ==
 +
Following is a list of a few useful commands:
 +
{| class="wikitable" style="width: 40%;"
 +
| '''Linux Command''' || '''Description'''
 +
|-
 +
| clear || Removes the scrollback history from your shell and moves the current prompt to the top of the screen.
 +
|-
 +
| echo || Print text to the screen. Most useful when writing shell scripts, e.g. for logging progress or printing results. For example: <code>echo “Hello World”</code>
 +
|-
 +
| less || An improved replacement for the “more” command. Allows you to scroll backwards as well as forwards.
 +
|-
 +
| grep || Search for a pattern in a file or program output and only print matching lines. For example, to find out which TCP network port is used by the “nfs” service, you can do this: <code>grep "nfs" /etc/services</code>
 +
|-
 +
| sort || Sort a file or program output. Example: sort MyFile.txt
 +
|-
 +
| su || “Switch User”. Allows you to switch to another user's account temporarily.
 +
|}
 +
 
 +
== References ==
 
[http://www.karlin.mff.cuni.cz/~hron/NMNV532/ShellIntro.pdf A short info with a good basic command reference]
 
[http://www.karlin.mff.cuni.cz/~hron/NMNV532/ShellIntro.pdf A short info with a good basic command reference]
 
[http://linux.org.mt/article/terminal/ A longer linux-shell tutorial]
 

Latest revision as of 11:28, 15 May 2020

General

The Shell or commandline is a program to interact with a given computer (or supercomputer). Contrary to a graphical user interface (e.g. Windows or Mac Desktop) which most desktop machines offer today, the shell seems simplistic, although daunting at the same time, because it requires the user to have a certain knowledge of the available commands and their uses. However, even with a few basic commands, the shell can be used productively and become a powerful tool in your toolbox. This power combined with the fact that the shell itself uses almost no resources is the reason, why it is still widely used on nearly every supercomputer.

A common shell is the bash (Bourne Again SHell), which comes with nearly every Unix-based operating system. Others Unix shells include the sh, csh, tcsh, zsh, ksh. All of these have quite similar syntax and offer a similar range of commands, which will be described below.

Windows has the DOS commands (available with cmd) or the PowerShell (ps), which is different syntax-wise and will not be covered in too much detail, since most supercomputers run Unix systems.



Usage

Enter a command, hit return and wait for the answer :-P

You can abort most commands by pressing Ctrl + C

To make your life easier, you can also write a bunch of commands together into a text file, save it as 'name.sh' and then run that to call all those commands in sequence and repeatable. This is called an sh-file or shell script.

Furthermore you can get help for most commands by appending -h or --help to show the corresponding help message of the executed command. Additionally the so-called manual pages (or short: man pages) provide you with a detailed description for the specified command. Type man ls to look at the man page for the ls command, scroll with your arrow keys and press q to exit.

Navigating the File System

The following commands tell you, where you are, take you somewhere else and show you what is there:

Linux Command DOS Command Description
pwd cd “Print Working Directory”. Shows the current location in the directory tree.
cd <directory> cd <directory> Change into the specified directory name. Example: cd /usr/src/linux
cd ~ “~” is an alias for your home directory. It can be used as a shortcut to your “home”, or other directories relative to your home.
cd .. cd.. Move up one directory. For example, if you are in /home/vic and you type “cd ..”, you will end up in /home.
ls dir /w List all files in the current directory, in column format.
ls -l dir List files in “long” format, one file per line. This also shows you additional info about the file, such as ownership, permissions, date, and size.
ls -a dir /a List all files, including “hidden” files. On Unix systems hidden files are those beginning with a “.”, e.g. The .bash_history file in your home directory.

Doing Stuff with Files and Directories

The following commands let you manipulate and interact with files. Since these commands all work on files, the syntax is usually

$ <command> <filename>
Linux Command DOS Command Description
file Find out what kind of file it is. For example, “file /bin/ls” tells us that it is a Linux executable file.
vim Opens the vim text editor to modify files.
cat type Display the contents of a text file on the screen. For example: cat MyFile.txt would display the contents of the file.
head Display the first few lines of a text file. Example: head /etc/services
tail Display the last few lines of a text file. Example: tail /etc/services
tail -f Display the last few lines of a text file, and then output appended data as the file grows (very useful for following log files!). Example: tail -f /var/log/messages
cp copy Copies a file from one location to another. Example: cp MyFile.txt /tmp (copies the MyFile.txt file to the /tmp directory)
mv rename, ren, move Moves a file to a new location, or renames it. For example: mv MyFile.txt /tmp (copy the file to /tmp, and delete it from the original location)
rm del Delete a file. Example: rm /tmp/MyFile.txt
mkdir md Create an empty directory. Example: mkdir /tmp/myfiles/
rmdir rd, rmdir Delete an empty directory. Example: rmdir /tmp/myfiles/

Getting System Info

The following commands will give you information about the system or users:

Linux Command Description
ps Lists currently running process (programs).
w Show who is logged on and what they are doing.
id Print your user-id and group id's
du Disk Usage in a particular directory. du -s provides a summary for the current directory.
top Displays all currently running processes on your system in full-screen. A great way to see the activity on your computer in real-time. Type q to quit.
free Displays amount of free and used memory in the system.
cat /proc/cpuinfo Displays information about your CPU.
cat /proc/meminfo Display lots of information about current memory usage.
uname -a Prints system information to the screen (kernel version, machine type, etc.)

Utilities

Following is a list of a few useful commands:

Linux Command Description
clear Removes the scrollback history from your shell and moves the current prompt to the top of the screen.
echo Print text to the screen. Most useful when writing shell scripts, e.g. for logging progress or printing results. For example: echo “Hello World”
less An improved replacement for the “more” command. Allows you to scroll backwards as well as forwards.
grep Search for a pattern in a file or program output and only print matching lines. For example, to find out which TCP network port is used by the “nfs” service, you can do this: grep "nfs" /etc/services
sort Sort a file or program output. Example: sort MyFile.txt
su “Switch User”. Allows you to switch to another user's account temporarily.

References

A short info with a good basic command reference