Linux introduction: Difference between revisions
(Marked this version for translation) |
|||
(15 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
This article is aimed at Windows and Mac users | <languages /> | ||
<translate> | |||
<!--T:1--> | |||
This article is aimed at Windows and Mac users who do not have or have very little experience in UNIX environments. It should give you the necessary basics to access the compute servers and being quickly able to use them. | |||
Connections to the servers use the SSH protocol, in text mode. You do not use a graphical interface (GUI) but a | <!--T:2--> | ||
Connections to the servers use the [[SSH]] protocol, in text mode. You do not use a graphical interface (GUI) but a <b>console</b>. Note that Windows executables do not run on our servers without using an emulator. | |||
== Obtaining help == | <!--T:27--> | ||
There is a self-paced course available on this topic from SHARCNET: [https://training.sharcnet.ca/courses/enrol/index.php?id=182 Introduction to the Shell] | |||
== Obtaining help == <!--T:3--> | |||
Generally, UNIX commands are documented in the reference manuals that are available on the servers. To access those from a terminal: | Generally, UNIX commands are documented in the reference manuals that are available on the servers. To access those from a terminal: | ||
{{Command|man command}} | {{Command|man command}} | ||
< | <code>man</code> uses <code>less</code> (see the section [[#Viewing and editing files|Viewing and editing files]]), and you must press <code>q</code> to exit this program. | ||
<!--T:4--> | |||
By convention, the executables themselves contain some help on how to use them. | By convention, the executables themselves contain some help on how to use them. | ||
Generally, you invoke this help using the command line argument <code>-h</code> or <code>--help</code>, or in certain cases, <code>-help</code>. | |||
For example, | |||
{{Command|ls --help}} | |||
== Orienting yourself on a system == <!--T:6--> | |||
Following your connection, you are directed to your <code>$HOME</code> directory (the UNIX word for <i>folder</i>) for your user account. | |||
== Orienting yourself on a system == | When your account is created, your <code>$HOME</code> only contains a few hidden configuration files that start with a period (.), and nothing else. | ||
Following your connection, you are directed to your < | |||
When your account is created, your < | |||
<!--T:7--> | |||
On a Linux system, you are strongly discouraged to create files or directories that contain names with spaces or special characters, including accents. | On a Linux system, you are strongly discouraged to create files or directories that contain names with spaces or special characters, including accents. | ||
=== Listing directory contents === | === Listing directory contents === <!--T:8--> | ||
To list all files in a directory in a terminal, use the < | To list all files in a directory in a terminal, use the <code>ls</code> (list) command: | ||
{{Command|ls}} | {{Command|ls}} | ||
To include hidden files: | To include hidden files: | ||
Line 29: | Line 37: | ||
And, to obtain detailed information on all files (permissions, owner, group, size and last modification date): | And, to obtain detailed information on all files (permissions, owner, group, size and last modification date): | ||
{{Command|ls -l}} | {{Command|ls -l}} | ||
The option < | The option <code>-h</code> gives the file sizes in human readable format. | ||
<!--T:9--> | |||
You can combine options, for example: | You can combine options, for example: | ||
{{Command|ls -alth}} | {{Command|ls -alth}} | ||
=== Navigating the | === Navigating the filesystem === <!--T:10--> | ||
To move about in the | To move about in the filesystem, use the <code>cd</code> command (change directory). | ||
So, to change to < | <!--T:11--> | ||
So, to change to <code>my_directory</code>, type: | |||
{{Command|cd my_directory}} | {{Command|cd my_directory}} | ||
To change to the parent folder | To change to the parent folder, type: | ||
{{Command|cd ..}} | {{Command|cd ..}} | ||
And, to move back to your home directory (< | And, to move back to your home directory (<code>$HOME</code>): | ||
{{Command|cd}} | {{Command|cd}} | ||
=== Creating and removing directories === | === Creating and removing directories === <!--T:12--> | ||
To create (make) a directory, use the < | To create (make) a directory, use the <code>mkdir</code> command: | ||
{{Command|mkdir my_directory}} | {{Command|mkdir my_directory}} | ||
To remove a directory, use the < | To remove a directory, use the <code>rmdir</code> command: | ||
{{Command|rmdir my_directory}} | {{Command|rmdir my_directory}} | ||
Deleting a directory like this only works if it is empty. | Deleting a directory like this only works if it is empty. | ||
=== Deleting files === | === Deleting files === <!--T:13--> | ||
You can remove files using the < | You can remove files using the <code>rm</code> command: | ||
{{Command|rm my_file}} | {{Command|rm my_file}} | ||
You can also recursively remove a directory: | You can also recursively remove a directory: | ||
{{Command|rm -r my_directory}} | {{Command|rm -r my_directory}} | ||
The (potentially dangerous!) < | The (potentially dangerous!) <code>-f</code> option can be useful to bypass confirmation prompts and to continue the operation after an error. | ||
=== Copying and renaming files or directories === | === Copying and renaming files or directories === <!--T:14--> | ||
To copy a file use the < | To copy a file use the <code>cp</code> command: | ||
{{Command|cp source_file destination_file}} | {{Command|cp source_file destination_file}} | ||
To recursively copy a directory: | To recursively copy a directory: | ||
{{Command|cp -R source_directory destination_directory}} | {{Command|cp -R source_directory destination_directory}} | ||
To rename a file or a folder (directory), use the < | To rename a file or a folder (directory), use the <code>mv</code> command (move): | ||
{{Command|mv source_file destination_file}} | {{Command|mv source_file destination_file}} | ||
This command also applies to directories. You should then replace < | This command also applies to directories. You should then replace <code>source_file</code> with <code>source_directory</code> and <code>destination_file</code> with <code>destionation_directory</code>. | ||
== File permissions == | == File permissions == <!--T:15--> | ||
UNIX systems support 3 types of permissions : read (< | UNIX systems support 3 types of permissions : read (<code>r</code>), write (<code>w</code>) and execute (<code>x</code>). For files, a file should be readable to be read, writable to be modified, and executable to be run (if it's a binary executable or a script). For a directory, read permissions are necessary to list its contents, write permissions enable modification (adding or removing a file) and execute permissions enable changing to it. | ||
Permissions apply to 3 different classes of users, the owner (< | <!--T:16--> | ||
Permissions apply to 3 different classes of users, the owner (<code>u</code>), the group (<code>g</code>), and all others or <i>the world</i> (<code>o</code>). To know which permissions are associated to files and subdirectories of the current directory, use the following command: | |||
{{Command|ls -la}} | {{Command|ls -la}} | ||
<!--T:17--> | |||
The 10 characters at the beginning of each line show the permissions. | The 10 characters at the beginning of each line show the permissions. | ||
The first character indicates the file type : | The first character indicates the file type : | ||
* < | * <code>-</code>: a normal file | ||
* < | * <code>d</code>: a directory | ||
* < | * <code>l</code>: a symbolic link | ||
<!--T:18--> | |||
Then, from left to right, this command shows read, write and execute permissions of the owner, the group and other users. Here are some examples : | Then, from left to right, this command shows read, write and execute permissions of the owner, the group and other users. Here are some examples : | ||
* < | * <code>drwxrwxrwx</code>: a world-readable and world-writable directory | ||
* < | * <code>drwxr-xr-x</code>: a directory that can be listed by everybody, but only the owner can add or remove files | ||
* < | * <code>-rwxr-xr-x</code>: a world-readable and world-executable file that can only be changed by its owner | ||
* < | * <code>-rw-r--r--</code>: a world-readable file that can only be changed by its owner. | ||
* < | * <code>-rw-rw----</code>: a file that can be read and changed by its owner and by its group | ||
* < | * <code>-rw-------</code>: a file that can only be read and changed by its owner | ||
* < | * <code>drwx--x--x</code>: a directory that can only be listed or modified by its owner, but all others can still pass it on their way to a deeper subdirectory | ||
* < | * <code>drwx-wx-wx</code>: a directory that everybody can enter and modify but where only the owner can list its contents | ||
Important note: to be able to read or write in a directory, you need to have execute permissions (< | <!--T:19--> | ||
Important note: to be able to read or write in a directory, you need to have execute permissions (<code>x</code>) set in all parent directories, all the way up to the filesystem's root (<b><code>/</code></b>). So if your home directory has <code>drwx------</code> permissions and contains a subdirectory with <code>drwxr-xr-x</code> permissions, other users cannot read the contents of this subdirectory because they do not have access (by the executable bit) to its parent directory. | |||
After listing the permissions, < | <!--T:20--> | ||
After listing the permissions, <code>ls -la</code> command gives a number, followed by the file owner's name, the file group's name, its size, last modification date, and name. | |||
The < | <!--T:21--> | ||
The <code>chmod</code> command allows you to change file permissions. The simple way to use it is to specify which permissions you wish to add or remove to which type of user. To do this, you specify the list of users (<code>u</code> for the owner, <code>g</code> for the group, <code>o</code> for other users, <code>a</code> for all three), followed by a <code>+</code> to add permissions or <code>-</code> to remove permissions, which is then followed by a list of permissions to modify (<code>r</code> for read, <code>w</code> for write, <code>x</code> for execute). Non-specified permissions are not affected. Here are a few examples: | |||
* Prevent group members and all others to read or modify the file < | <!--T:22--> | ||
* Allow everybody to read the file < | * Prevent group members and all others to read or modify the file <code>secret.txt</code>: {{Command|chmod go-rwx secret.txt}} | ||
* Make the file < | * Allow everybody to read the file <code>public.txt</code>: {{Command|chmod a+r public.txt}} | ||
* Allow group members to read and write in the directory < | * Make the file <code>script.sh</code> executable: {{Command|chmod a+x script.sh}} | ||
* Prevent other users | * Allow group members to read and write in the directory <code>shared</code>: {{Command|chmod g+rwx shared}} | ||
* Prevent other users from reading or modifying your home directory: {{Command|chmod go-rw ~}} | |||
== Viewing and editing files == | == Viewing and editing files == <!--T:23--> | ||
=== Viewing a file === | === Viewing a file === | ||
To view a file read-only, use the < | To view a file read-only, use the <code>less</code> command: | ||
{{Command|less file_to_view}} | {{Command|less file_to_view}} | ||
You can then use the arrow keys or the mouse wheel to navigate the document. | You can then use the arrow keys or the mouse wheel to navigate the document. | ||
You can search for something in the document by typing < | You can search for something in the document by typing <code>/what_to_search_for</code>. | ||
You can quit < | You can quit <code>less</code> by pressing the <code>q</code> key. | ||
=== Comparing two files === | === Comparing two files === <!--T:24--> | ||
The < | The <code>diff</code> command allows you to compare two files: | ||
{{Command|diff file1 file2}} | {{Command|diff file1 file2}} | ||
The < | The <code>-y</code> option shows both files side by side. | ||
=== Searching within a file === | === Searching within a file === <!--T:25--> | ||
The < | The <code>grep</code> command allows you to look for a given expression in one file: | ||
{{Command|grep 'tata' file1}} | {{Command|grep 'tata' file1}} | ||
or multiple files | ... or in multiple files: | ||
{{Command|grep 'tata' fil*}} | {{Command|grep 'tata' fil*}} | ||
Note that, in Linux, the | Note that, in Linux, the <code>*</code> wildcard matches zero or more characters. The <code>?</code> wildcard matches exactly one character. | ||
<!--T:26--> | |||
The text to be searched for can also be variable. For example, to look for | The text to be searched for can also be variable. For example, to look for | ||
the text | the text <i>number 10</i> or <i>number 11</i>, etc. with any number between 10 and 29, | ||
the following command can be used: | the following command can be used: | ||
{{Command|grep 'number [1-2][0-9]' file | {{Command|grep 'number [1-2][0-9]' file | ||
}} | }} | ||
A regular expression must be used for the search text. To learn more | A regular expression must be used for the search text. To learn more, [http://www.cyberciti.biz/faq/grep-regular-expressions/ see this guide to regular expressions]. | ||
[http://www.cyberciti.biz/faq/grep-regular-expressions/ | </translate> |
Latest revision as of 16:58, 16 October 2024
This article is aimed at Windows and Mac users who do not have or have very little experience in UNIX environments. It should give you the necessary basics to access the compute servers and being quickly able to use them.
Connections to the servers use the SSH protocol, in text mode. You do not use a graphical interface (GUI) but a console. Note that Windows executables do not run on our servers without using an emulator.
There is a self-paced course available on this topic from SHARCNET: Introduction to the Shell
Obtaining help
Generally, UNIX commands are documented in the reference manuals that are available on the servers. To access those from a terminal:
[name@server ~]$ man command
man
uses less
(see the section Viewing and editing files), and you must press q
to exit this program.
By convention, the executables themselves contain some help on how to use them.
Generally, you invoke this help using the command line argument -h
or --help
, or in certain cases, -help
.
For example,
[name@server ~]$ ls --help
Orienting yourself on a system
Following your connection, you are directed to your $HOME
directory (the UNIX word for folder) for your user account.
When your account is created, your $HOME
only contains a few hidden configuration files that start with a period (.), and nothing else.
On a Linux system, you are strongly discouraged to create files or directories that contain names with spaces or special characters, including accents.
Listing directory contents
To list all files in a directory in a terminal, use the ls
(list) command:
[name@server ~]$ ls
To include hidden files:
[name@server ~]$ ls -a
To sort results by date (from newest to oldest) instead of alphabetically:
[name@server ~]$ ls -t
And, to obtain detailed information on all files (permissions, owner, group, size and last modification date):
[name@server ~]$ ls -l
The option -h
gives the file sizes in human readable format.
You can combine options, for example:
[name@server ~]$ ls -alth
To move about in the filesystem, use the cd
command (change directory).
So, to change to my_directory
, type:
[name@server ~]$ cd my_directory
To change to the parent folder, type:
[name@server ~]$ cd ..
And, to move back to your home directory ($HOME
):
[name@server ~]$ cd
Creating and removing directories
To create (make) a directory, use the mkdir
command:
[name@server ~]$ mkdir my_directory
To remove a directory, use the rmdir
command:
[name@server ~]$ rmdir my_directory
Deleting a directory like this only works if it is empty.
Deleting files
You can remove files using the rm
command:
[name@server ~]$ rm my_file
You can also recursively remove a directory:
[name@server ~]$ rm -r my_directory
The (potentially dangerous!) -f
option can be useful to bypass confirmation prompts and to continue the operation after an error.
Copying and renaming files or directories
To copy a file use the cp
command:
[name@server ~]$ cp source_file destination_file
To recursively copy a directory:
[name@server ~]$ cp -R source_directory destination_directory
To rename a file or a folder (directory), use the mv
command (move):
[name@server ~]$ mv source_file destination_file
This command also applies to directories. You should then replace source_file
with source_directory
and destination_file
with destionation_directory
.
File permissions
UNIX systems support 3 types of permissions : read (r
), write (w
) and execute (x
). For files, a file should be readable to be read, writable to be modified, and executable to be run (if it's a binary executable or a script). For a directory, read permissions are necessary to list its contents, write permissions enable modification (adding or removing a file) and execute permissions enable changing to it.
Permissions apply to 3 different classes of users, the owner (u
), the group (g
), and all others or the world (o
). To know which permissions are associated to files and subdirectories of the current directory, use the following command:
[name@server ~]$ ls -la
The 10 characters at the beginning of each line show the permissions. The first character indicates the file type :
-
: a normal filed
: a directoryl
: a symbolic link
Then, from left to right, this command shows read, write and execute permissions of the owner, the group and other users. Here are some examples :
drwxrwxrwx
: a world-readable and world-writable directorydrwxr-xr-x
: a directory that can be listed by everybody, but only the owner can add or remove files-rwxr-xr-x
: a world-readable and world-executable file that can only be changed by its owner-rw-r--r--
: a world-readable file that can only be changed by its owner.-rw-rw----
: a file that can be read and changed by its owner and by its group-rw-------
: a file that can only be read and changed by its ownerdrwx--x--x
: a directory that can only be listed or modified by its owner, but all others can still pass it on their way to a deeper subdirectorydrwx-wx-wx
: a directory that everybody can enter and modify but where only the owner can list its contents
Important note: to be able to read or write in a directory, you need to have execute permissions (x
) set in all parent directories, all the way up to the filesystem's root (/
). So if your home directory has drwx------
permissions and contains a subdirectory with drwxr-xr-x
permissions, other users cannot read the contents of this subdirectory because they do not have access (by the executable bit) to its parent directory.
After listing the permissions, ls -la
command gives a number, followed by the file owner's name, the file group's name, its size, last modification date, and name.
The chmod
command allows you to change file permissions. The simple way to use it is to specify which permissions you wish to add or remove to which type of user. To do this, you specify the list of users (u
for the owner, g
for the group, o
for other users, a
for all three), followed by a +
to add permissions or -
to remove permissions, which is then followed by a list of permissions to modify (r
for read, w
for write, x
for execute). Non-specified permissions are not affected. Here are a few examples:
- Prevent group members and all others to read or modify the file
secret.txt
:[name@server ~]$ chmod go-rwx secret.txt
- Allow everybody to read the file
public.txt
:[name@server ~]$ chmod a+r public.txt
- Make the file
script.sh
executable:[name@server ~]$ chmod a+x script.sh
- Allow group members to read and write in the directory
shared
:[name@server ~]$ chmod g+rwx shared
- Prevent other users from reading or modifying your home directory:
[name@server ~]$ chmod go-rw ~
Viewing and editing files
Viewing a file
To view a file read-only, use the less
command:
[name@server ~]$ less file_to_view
You can then use the arrow keys or the mouse wheel to navigate the document.
You can search for something in the document by typing /what_to_search_for
.
You can quit less
by pressing the q
key.
Comparing two files
The diff
command allows you to compare two files:
[name@server ~]$ diff file1 file2
The -y
option shows both files side by side.
Searching within a file
The grep
command allows you to look for a given expression in one file:
[name@server ~]$ grep 'tata' file1
... or in multiple files:
[name@server ~]$ grep 'tata' fil*
Note that, in Linux, the *
wildcard matches zero or more characters. The ?
wildcard matches exactly one character.
The text to be searched for can also be variable. For example, to look for the text number 10 or number 11, etc. with any number between 10 and 29, the following command can be used:
[name@server ~]$ grep 'number [1-2][0-9]' file
A regular expression must be used for the search text. To learn more, see this guide to regular expressions.