Linux introduction: Difference between revisions

Jump to navigation Jump to search
no edit summary
(use example that has results if tested, copyedits)
No edit summary
Line 10: Line 10:
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}}
<tt>man</tt> uses <tt>less</tt> (see the section [[#Viewing and editing files|Viewing and editing files]]), and you must press <tt>q</tt> to exit this program.
<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-->
<!--T:4-->
Line 18: Line 18:


<!--T:5-->
<!--T:5-->
Generally, you invoke this help using the command line argument <tt>-h</tt> or <tt>--help</tt>, or in certain cases,  <tt>-help</tt>.
Generally, you invoke this help using the command line argument <code>-h</code> or <code>--help</code>, or in certain cases,  <code>-help</code>.


== Orienting yourself on a system == <!--T:6-->
== Orienting yourself on a system == <!--T:6-->
Following your connection, you are directed to your <tt>$HOME</tt> directory (the UNIX word for "folder") for your user account.
Following your connection, you are directed to your <code>$HOME</code> directory (the UNIX word for <i>folder</i>) for your user account.
When your account is created, your <tt>$HOME</tt> only contains a few hidden configuration files that start with a ".", and nothing else.
When your account is created, your <code>$HOME</code> only contains a few hidden configuration files that start with a period (.), and nothing else.


<!--T:7-->
<!--T:7-->
Line 28: Line 28:


=== Listing directory contents === <!--T:8-->
=== Listing directory contents === <!--T:8-->
To list all files in a directory in a terminal, use the <tt>ls</tt> (list) command:
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 43: Line 43:


=== Navigating the filesystem === <!--T:10-->
=== Navigating the filesystem === <!--T:10-->
To move about in the filesystem, use the <tt>cd</tt> command (change directory).
To move about in the filesystem, use the <code>cd</code> command (change directory).


<!--T:11-->
<!--T:11-->
So, to change to <tt>my_directory</tt> you type:
So, to change to <code>my_directory</code> you type:
{{Command|cd my_directory}}
{{Command|cd my_directory}}
To change to the parent folder you type:
To change to the parent folder you type:
{{Command|cd ..}}
{{Command|cd ..}}
And, to move back to your home directory (<tt>$HOME</tt>):
And, to move back to your home directory (<code>$HOME</code>):
{{Command|cd}}
{{Command|cd}}


=== Creating and removing directories === <!--T:12-->
=== Creating and removing directories === <!--T:12-->
To create (make) a directory, use the <tt>mkdir</tt> command:
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 <tt>rmdir</tt> command:
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 === <!--T:13-->
=== Deleting files === <!--T:13-->
You can remove files using the <tt>rm</tt> command:
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!) <tt>-f</tt> option can be useful to bypass confirmation prompts and to continue the operation after an error.
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 === <!--T:14-->
=== Copying and renaming files or directories === <!--T:14-->
To copy a file use the <tt>cp</tt> command:
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:
Line 77: Line 77:


== File permissions == <!--T:15-->
== File permissions == <!--T:15-->
UNIX systems support 3 types of permissions : read (<tt>r</tt>), write (<tt>w</tt>) and execute (<tt>x</tt>). 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.
UNIX systems support 3 types of permissions : read (<code>r</code>), write (<tt>w</tt>) and execute (<tt>x</tt>). 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.


<!--T:16-->
<!--T:16-->
Line 92: Line 92:
<!--T:18-->
<!--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 :
* <tt>drwxrwxrwx</tt>: a world-readable and world-writable directory
* <code>drwxrwxrwx</code>: a world-readable and world-writable directory
* <tt>drwxr-xr-x</tt>: a directory that can be listed by everybody, but only the owner can add or remove files
* <code>drwxr-xr-x</code>: a directory that can be listed by everybody, but only the owner can add or remove files
* <tt>-rwxr-xr-x</tt>: a world-readable and world-executable file that can only be changed by its owner
* <code>-rwxr-xr-x</code>: a world-readable and world-executable file that can only be changed by its owner
* <tt>-rw-r--r--</tt>: a world-readable 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.
* <tt>-rw-rw----</tt>: a file that can be read and changed by its owner and by its group
* <code>-rw-rw----</code>: a file that can be read and changed by its owner and by its group
* <tt>-rw-------</tt>: a file that can only be read and changed by its owner
* <code>-rw-------</code>: a file that can only be read and changed by its owner
* <tt>drwx--x--x</tt>: 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--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
* <tt>drwx-wx-wx</tt>: a directory that everybody can enter and modify but where only the owner can list its contents
* <code>drwx-wx-wx</code>: a directory that everybody can enter and modify but where only the owner can list its contents


<!--T:19-->
<!--T:19-->
Important note: to be able to read or write in a directory, you need to have execute permissions (<tt>x</tt>) set in all parent directories, all the way up to the filesystem's root ('''<tt>/</tt>'''). So if your home directory has <tt>drwx------</tt> permissions and contains a subdirectory with <tt>drwxr-xr-x</tt> permissions, other users cannot read the contents of this subdirectory because they do not have access (by the executable bit) to its parent directory.
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.


<!--T:20-->
<!--T:20-->
After listing the permissions, <tt>ls -la</tt> command gives a number, followed by the file owner's name, the file group's name, its size, last modification date, and name.
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.


<!--T:21-->
<!--T:21-->
The <tt>chmod</tt> 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 (<tt>u</tt> for the owner, <tt>g</tt> for the group, <tt>o</tt> for other users, <tt>a</tt> for all), followed by a <tt>+</tt> to add permissions or <tt>-</tt> to remove permissions, which is then followed by a list of permissions to modify (<tt>r</tt> for read, <tt>w</tt> for write, <tt>x</tt> for execute). Non-specified permissions are not affected. Here are a few examples:
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), 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:


<!--T:22-->
<!--T:22-->
* Prevent group members and all others to read or modify the file <tt>secret.txt</tt>: {{Command|chmod go-rwx secret.txt}}
* Prevent group members and all others to read or modify the file <code>secret.txt</code>: {{Command|chmod go-rwx secret.txt}}
* Allow everybody to read the file <tt>public.txt</tt>: {{Command|chmod a+r public.txt}}
* Allow everybody to read the file <code>public.txt</code>: {{Command|chmod a+r public.txt}}
* Make the file <tt>script.sh</tt> executable: {{Command|chmod a+x script.sh}}
* Make the file <code>script.sh</code> executable: {{Command|chmod a+x script.sh}}
* Allow group members to read and write in the directory <tt>shared</tt>: {{Command|chmod g+rwx shared}}
* 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 ~}}
* Prevent other users from reading or modifying your home directory: {{Command|chmod go-rw ~}}


== Viewing and editing files == <!--T:23-->
== Viewing and editing files == <!--T:23-->
=== Viewing a file ===
=== Viewing a file ===
To view a file read-only, use the <tt>less</tt> command:
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 <tt>/what_to_search_for</tt>.
You can search for something in the document by typing <code>/what_to_search_for</code>.
You can quit <tt>less</tt> by pressing the <tt>q</tt> key.
You can quit <code>less</code> by pressing the <code>q</code> key.


=== Comparing two files === <!--T:24-->
=== Comparing two files === <!--T:24-->
The <tt>diff</tt> command allows you to compare two files:
The <code>diff</code> command allows you to compare two files:
{{Command|diff file1 file2}}
{{Command|diff file1 file2}}
The <tt>-y</tt> option shows both files side by side.
The <code>-y</code> option shows both files side by side.


=== Searching within a file === <!--T:25-->
=== Searching within a file === <!--T:25-->
The <tt>grep</tt> command allows you to look for a given expression in one file:
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 in multiple files:
... or in multiple files:
{{Command|grep 'tata' fil*}}
{{Command|grep 'tata' fil*}}
Note that, in Linux, the "<tt>*</tt>" wildcard matches zero or more characters. The "<tt>?</tt>" wilcard matches exactly one character.
Note that, in Linux, the "<code>*</code>" wildcard matches zero or more characters. The "<code>?</code>" wilcard matches exactly one character.


<!--T:26-->
<!--T:26-->
rsnt_translations
53,770

edits

Navigation menu