cs:bash_shell
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_shell?do=diff&rev2%5B0%5D=1551188037&rev2%5B1%5D=1570639262&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | cs:bash_shell [2024/04/08 22:34] (current) โ created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Bash shell ====== | ||
+ | Guide on the usage of the most common commands of the bash shell | ||
+ | < | ||
+ | <iframe width=" | ||
+ | </ | ||
+ | |||
+ | ===== Generic Linux Command Syntax ===== | ||
+ | <code bash> | ||
+ | command [-options] [arguments] | ||
+ | </ | ||
+ | * If a command is to long to stay on a single row, it may be continued on the next row using the character "'' | ||
+ | * More than one command can be executed on the same line, separating the commands with ";" | ||
+ | <code bash> | ||
+ | command1; command2; | ||
+ | </ | ||
+ | * In this case commands will be executed sequentially | ||
+ | |||
+ | ===== Help ===== | ||
+ | All linux commands are documented, and their documentation can be accessed using: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== The Unix file system ===== | ||
+ | The Unix file system is a hierarchical file system organized in directory: | ||
+ | * ''/'' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | A file that starts with "'' | ||
+ | |||
+ | Files within a directory can be accessed using: | ||
+ | * an //absolute path//: ''/ | ||
+ | * a //relative path//: '' | ||
+ | |||
+ | ===== Files Commands ===== | ||
+ | <code bash> | ||
+ | cp [-fir] src1 src2 ... dest | ||
+ | </ | ||
+ | * copy one or more files, possibly in a directory | ||
+ | <code bash> | ||
+ | rm [-fir] file1 file2 ... | ||
+ | </ | ||
+ | * delete '' | ||
+ | * files can be directories if the '' | ||
+ | <code bash> | ||
+ | mv [-fi] file1 file2 ... dest | ||
+ | </ | ||
+ | * move one or more file, possibly in a directory | ||
+ | |||
+ | | ||
+ | File commands options are: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== Directories Commands ===== | ||
+ | <code bash> | ||
+ | cd dir | ||
+ | </ | ||
+ | * goes into the '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | <code bash> | ||
+ | pwd | ||
+ | </ | ||
+ | * prints the name of the current directory | ||
+ | |||
+ | <code bash> | ||
+ | mkdir dir | ||
+ | </ | ||
+ | * makes the '' | ||
+ | |||
+ | <code bash> | ||
+ | rmdir dir | ||
+ | </ | ||
+ | * removes the '' | ||
+ | * '' | ||
+ | |||
+ | ===== Link Commands ===== | ||
+ | A //Symbolic link// is a file that contains a reference to another file or directory | ||
+ | * **Hard link**: more than one file name point to the same file | ||
+ | * **Soft link**: is a special file that contains the file that must be pointed | ||
+ | |||
+ | <code bash> | ||
+ | ln src alias | ||
+ | </ | ||
+ | * makes an //hard link// between '' | ||
+ | * //hard links// may not normally point to directories and they can not link paths on different volumes, for this reason usually //soft link// are preferred | ||
+ | |||
+ | <code bash> | ||
+ | ln -s src alias | ||
+ | </ | ||
+ | * makes a symbolic //soft link// between '' | ||
+ | |||
+ | ===== Files and Directories protection ===== | ||
+ | In Linux, every file or directory has special protections that allow differential access to the different users of the system | ||
+ | |||
+ | The system users are grouped into three sets: | ||
+ | * **u**: //user//, the owner of the file | ||
+ | * **g**: //group// users | ||
+ | * **o**: //other// users | ||
+ | * **a**: //all// users | ||
+ | |||
+ | Files protections are: | ||
+ | * **r**: //read// permission | ||
+ | * **w**: //write// permission | ||
+ | * **x**: // | ||
+ | |||
+ | Directories protections are: | ||
+ | * **x**: directory crossing | ||
+ | * **r**: files list | ||
+ | * **w**: files creation and/or deletion | ||
+ | |||
+ | ===== Files and Directories protection Commands ===== | ||
+ | Change the group of the files: | ||
+ | <code bash> | ||
+ | chgrp [-R] group file1 file2 ... | ||
+ | </ | ||
+ | Change the owner (and potentially the group) of the files: | ||
+ | <code bash> | ||
+ | chown [-R] user[: | ||
+ | </ | ||
+ | Change the protection of the files: | ||
+ | <code bash> | ||
+ | chmod [-R] protection file1 file2 ... | ||
+ | </ | ||
+ | * protection is the sum of: 4 for '' | ||
+ | |||
+ | Examples: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Options: | ||
+ | * '' | ||
+ | |||
+ | ===== The ls Command ===== | ||
+ | <code bash> | ||
+ | ls [-options] [files] | ||
+ | </ | ||
+ | * list directory contents | ||
+ | * Options are: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== ls command example ===== | ||
+ | <code bash> | ||
+ | user@pc:~$ ls -al ~/tmp | ||
+ | |||
+ | drwxr-xr-x | ||
+ | drwxr-xr-x 64 user user 12288 2009-10-13 15:10 ../ | ||
+ | drwxr-xr-x | ||
+ | -rw-r--r-- | ||
+ | lrwxrwxrwx | ||
+ | </ | ||
+ | |||
+ | This command lists the '' | ||
+ | * the first character is the file type ('' | ||
+ | * '' | ||
+ | * the number of the second column is 1 for files and the number of file contained in the directory for directories | ||
+ | * the first '' | ||
+ | * the second number is the dimension in byte | ||
+ | * date and hour are the creation time | ||
+ | * the last is the // | ||
+ | |||
+ | ===== Viewing a textual file ===== | ||
+ | Using visual editor like '' | ||
+ | |||
+ | With '' | ||
+ | <code bash> | ||
+ | cat file1 file2 ... | ||
+ | </ | ||
+ | * print in order file1 and file2 content | ||
+ | |||
+ | With '' | ||
+ | <code bash> | ||
+ | tail [-n number] file1 file2 ... | ||
+ | </ | ||
+ | * print the last number rows of '' | ||
+ | * with '' | ||
+ | |||
+ | With '' | ||
+ | <code bash> | ||
+ | head [-n number] file1 file2 ...} | ||
+ | </ | ||
+ | * like '' | ||
+ | |||
+ | With '' | ||
+ | <code bash> | ||
+ | more file | ||
+ | </ | ||
+ | * prints a file page by page. Use space to go to the next page | ||
+ | |||
+ | With '' | ||
+ | <code bash> | ||
+ | diff file1 file2 | ||
+ | </ | ||
+ | * displays differences between '' | ||
+ | |||
+ | ===== The find command ===== | ||
+ | |||
+ | '' | ||
+ | |||
+ | |||
+ | Options are: | ||
+ | * '' | ||
+ | * examples of '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== The grep command ===== | ||
+ | <code bash> | ||
+ | grep [-opt] pattern file1 file2 ... | ||
+ | </ | ||
+ | The '' | ||
+ | |||
+ | Its options are: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== The tar command ===== | ||
+ | <code bash> | ||
+ | tar [options] tarfile [file1 file2 ...] [dir1 dir2 ...] | ||
+ | </ | ||
+ | The '' | ||
+ | |||
+ | **Compression**: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Example: | ||
+ | <code bash> | ||
+ | tar cvzf / | ||
+ | </ | ||
+ | it //creates// a // | ||
+ | |||
+ | **Decompression**: | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Example 1: | ||
+ | <code bash> | ||
+ | tar tvzf / | ||
+ | </ | ||
+ | it //tests// a //verbose// //zipped// file, i.e., it tests the archive previously compressed | ||
+ | |||
+ | Example 2: | ||
+ | <code bash> | ||
+ | tar xvzf / | ||
+ | </ | ||
+ | it // | ||
+ | |||
+ | ===== The sort command ===== | ||
+ | <code bash> | ||
+ | sort [-opt] file1 file2 ... | ||
+ | </ | ||
+ | it sorts '' | ||
+ | |||
+ | Options are: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== The bash shell ===== | ||
+ | * A //shell// is a textual interface between the user and the operating system | ||
+ | * The //shell// is not part of the Linux kernel, it is a normal userspace program | ||
+ | * Since the //shell// is a normal process, different shells has been developed. The most commons are Bourne shell ('' | ||
+ | |||
+ | The focus of this document is on '' | ||
+ | |||
+ | Each shell, when opened, search a configuration script in the home directory and run it | ||
+ | * it can be used to customize the shell behavior or to initialize environment variables | ||
+ | * in '' | ||
+ | |||
+ | ===== Completion and History ===== | ||
+ | * Using TAB the shell automatically complete file names (using the file present in the actual directory or in the directories listed in the environment variable '' | ||
+ | * โ or โ: use arrows to scroll commands previously performed | ||
+ | * use the '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== Pipe ===== | ||
+ | <code bash> | ||
+ | command1 | command2 | ||
+ | </ | ||
+ | A '' | ||
+ | |||
+ | This linking is performed by the operator '' | ||
+ | |||
+ | Examples: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== Process Management ===== | ||
+ | * **Batch execution**: | ||
+ | * **Concurrent execution**: | ||
+ | * '' | ||
+ | * it returns immediately the control to the shell that executes '' | ||
+ | |||
+ | Summarizing: | ||
+ | * normally a command is run foreground (//fg//) | ||
+ | * with the symbol ''&'' | ||
+ | * A program that run //fg// can be suspended pressing //CTRL-Z// | ||
+ | * A suspended program can be restarted foreground using the command '' | ||
+ | |||
+ | In the following figure the process states and the transitions between them are illustrated: | ||
+ | {{: | ||
+ | |||
+ | ===== Scripts - Shell commands in a File ===== | ||
+ | A sequence of commands can be written in a file and they can be executed directly calling it | ||
+ | |||
+ | It can be executed indirectly: | ||
+ | <code bash> | ||
+ | source < | ||
+ | </ | ||
+ | |||
+ | It can be executed directly: | ||
+ | * the script file must have the // | ||
+ | * the first row must begin with ''# | ||
+ | * to run the script type '' | ||
+ | |||
+ | ===== Shell Variables ===== | ||
+ | <code bash> | ||
+ | variableName=value | ||
+ | </ | ||
+ | * instantiates a variable with the name '' | ||
+ | * e.g., '' | ||
+ | |||
+ | <code bash> | ||
+ | $variableName | ||
+ | </ | ||
+ | * used to access the variable | ||
+ | * to print a variable the command '' | ||
+ | * to print the value of variable '' | ||
+ | <code bash> | ||
+ | user@pc:~$ echo $b | ||
+ | pippo | ||
+ | </ | ||
+ | |||
+ | ===== Shell Environment Variables ===== | ||
+ | * Environment variables are written in upper-case and are used for the shell configuration | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== Useful Environment Variables ===== | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Example: add to the environment variable '' | ||
+ | <code bash> | ||
+ | export PATH=\$PATH:/ | ||
+ | </ | ||
+ | |||
+ | ====== References ====== | ||
+ | - Slides form which this page is derived: {{: | ||
+ | - Some examples regarding the //bash// shell scripting language: [[cs:Bash language]] | ||
+ | - List of resources regarding the Linux operating system: [[cs:linux operating system]] |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_shell?do=diff&rev2%5B0%5D=1551188037&rev2%5B1%5D=1570639262&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/bash_shell.txt ยท Last modified: 2024/04/08 22:34 by 127.0.0.1