UNIX/Linux Tutorial: A Comprehensive Guide
1. Steps to Start Using UNIX
- Upon turning on the computer, the user is informed of the installed UNIX version.
- The system prompts for a login or username.
- The system prompts for a password.
- The prompt, also known as the command prompt or shell prompt, appears.
- The .profile file is executed.
2. What is a Shell?
Several shell variations exist, including the standard Bourne shell, Korn shell, C shell, and Bash. A shell script is a text file containing commands, executable programs, and control structures. It’s also an executable file.
If the shell script is named program, you can run it by:
- Calling the shell (e.g., sh or bash program)
- Using the dot command: . program
- Making it executable with chmod +x program and then running it with ./program
3. Metacharacters Explained
- ?: Replaces a single character.
- *: Matches zero or more characters.
- >: Redirects output.
- <: Redirects input.
- >>: Appends output.
- <<: Redirects input from a here document.
- !: Negation. If A is true, !A is false.
- […]: Represents a character class. For example, [abc] matches either a, b, or c.
- &: Runs a command in the background.
- |: Pipes the output of one command to the input of another.
- ;: Separates commands. Multiple commands can be entered on one line if separated by semicolons.
- #: Indicates a comment. Everything after # on a line is ignored.
4. Background Processes and Command Separators
A background process continues running while you work on other tasks in the foreground. UNIX allows multiple commands on a single line, separated by semicolons (;).
Example: ls -l b[AIOU]rra displays a detailed listing of files in the current directory starting with “b”, having “a”, “i”, “o”, or “u” as the second character, and ending with “rra”.
5. Utilities of the cat Command
The cat command has three main utilities:
- Displaying file contents: cat texto.txt displays the contents of texto.txt.
- Writing to a file: cat > new.txt creates new.txt with the same content as texto.txt. If new.txt exists, it’s overwritten.
- Appending to a file: cat >> new.txt appends the content of texto.txt to new.txt without modifying the original content.
Files can be concatenated using cat. For example:
cat file1 file2 file3 > new
creates new with the combined content of file1, file2, and file3.
6. Common UNIX Utilities
- cp: Copies files.
- rm: Deletes files.
- mv: Moves or renames files.
- grep: Searches for strings within data streams (e.g., text files).
- find: Locates files based on various criteria (e.g., type, modification time, name).
- wc: Counts characters, words, and lines in a file.
- sort: Sorts data streams.
- diff: Compares two text files and displays the differences.
- tar: Archives multiple files into a single package.
7. vi Editor Modes
vi has two modes: command mode and insert mode.
You can enter vi by typing vi or vi texto.txt to edit the file texto.txt.
vi starts in command mode. To exit:
- :q: Exits if no changes were made.
- :q! or :quit!: Exits without saving changes.
- :wq or ZZ: Saves changes and exits.
Typing q in command mode will exit vi only if no changes were made to the file. Otherwise, it will display a warning.
8. Standard Streams and Redirection
UNIX has three standard streams:
- Standard input (stdin): 0
- Standard output (stdout): 1
- Standard error (stderr): 2
To redirect error messages to a file, use 2> filename. For example:
cp texto.txt /tmp 2> errors.log
copies texto.txt to the /tmp directory and logs any error messages to errors.log instead of displaying them on the screen.
9. More UNIX Commands
- df: Displays disk space usage for mounted filesystems.
- du: Shows disk space used by a specified file or directory.
- head: Displays the first 10 lines of a file.
- passwd: Changes the user’s password.
- sed: A stream editor for modifying text.
- tail: Displays the last 10 lines of a file.
- ls: Lists directory contents.
- find: Locates files based on various criteria.
- lp: Sends a file to a printer.
10. Differences Between UNIX and DOS
- UNIX is multi-user and multi-tasking, while DOS is single-user and single-tasking.
- UNIX is security-conscious, requiring logins and passwords, while DOS lacks built-in security features.
- UNIX is designed for communication and networking, while DOS has limited networking capabilities.
- UNIX is case-sensitive, while DOS is not.
11. Command Line Standards in UNIX
- UNIX commands are case-sensitive.
- Options must be preceded by a hyphen (-) and separated from the command and other options by spaces.
- Arguments are separated from options and each other by spaces.
- Multiple commands can be entered on a single line, separated by semicolons (;).
12. Important Directories
- /: The root directory.
- /bin: Contains essential executable files for system booting and operation.
- /dev: Contains device files.
- /etc: Contains system configuration files.
- /lib: Contains libraries needed for compiling and running programs.
- /tmp: Contains temporary files.
- /home: Contains user home directories.
13. More Command Explanations
- pwd: Displays the current working directory’s absolute path.
- cd: Changes the current working directory.
- cd (with no arguments): Changes to the user’s home directory.
- cd –: Changes to the previous working directory.
- mkdir: Creates a new directory.
- rmdir: Deletes an empty directory.
- ls: Lists directory contents.
- ls -a: Lists all files, including hidden files.
- ls -l: Displays a detailed listing.
14. Common vi Commands
Action | Command |
---|---|
Delete a line | cc |
Delete 7 lines | 7cc |
Undo last action | u |
Insert a blank line | o |
Delete a character | x |
Insert text at cursor | i |
Insert text before cursor | a |
Search forward for “a” | /a |
Repeat forward search | / |
Search backward for “a” | ?a |
Repeat backward search | ? |
Delete to end of word | dw |
Delete to beginning of word | db |
Delete to end of line | d$ |
Delete to a string | d/string/ |
Exit insert mode | ESC |
Save file | :w |
Save and exit | :wq or 😡 |
Exit without saving | :q! |
15. History, .bash_history, and !
- history: Displays the command history.
- .bash_history: A file storing the command history. The history command displays its contents.
- ! followed by a number (e.g., !234): Executes the command with that number in the history list.
16. More Shell Utilities
- alias: Creates shortcuts for commands.
- break: Exits a loop.
- read: Reads input from the keyboard and assigns it to variables.
- set: Displays or sets shell variables.
- shift: Shifts positional parameters.
- unalias: Removes an alias.
17. Password Files and Profiles
The /etc/passwd file contains user information, including username, user ID, group ID, home directory, and shell. The encrypted password field is replaced with “x”, and the actual encrypted password is stored in /etc/shadow for security.
There are two types of profile files: global (/etc/profile) and personal (.profile in the user’s home directory). The global profile is executed first for all users, followed by the personal profile for the specific user when they log in.
18. Important Shell Variables
- PS1: Primary prompt string.
- PS2: Secondary prompt string (used for multi-line commands).
- PATH: A list of directories where the shell searches for executable files.
- HOME: The user’s home directory.
- PWD: The current working directory.
- HISTFILE: The path to the history file.
- HISTSIZE: The maximum number of commands stored in the history file.
To use a variable’s value in a command, prefix it with a dollar sign ($).
19. UNIX System Components
- Hardware: The physical components of the system.
- Kernel: The core of the operating system, responsible for low-level tasks like resource management and process scheduling.
- Shell: The user interface that allows interaction with the kernel.
- User Area: The space where user programs and applications run.
20. Shell Functions and Types
The shell has two main functions:
- Command interpreter: Analyzes, interprets, and executes user commands.
- Programming language: Allows for writing shell scripts using control structures, variables, and instructions.
Different shell types include Bourne shell, Korn shell, C shell, and Bash.
21. Basic Commands
- date: Displays the current date and time.
- who: Shows users logged into the system.
- whoami: Displays the current user’s username.
- tty: Displays the terminal’s name.
- echo: Prints text to the screen.
- banner: Displays a large text banner.
- id: Shows user and group IDs.
- cal: Displays a calendar.
22. Inodes
An inode is a data structure that stores information about a file, including its type, permissions, owner, group, size, and timestamps. Each file has a unique inode number. Directories contain a list of inode numbers and filenames, allowing access to files.
23. File Types in UNIX
- Regular files: Contain data or program code.
- Directories: Contain lists of files and their inode numbers.
- Special files: Represent devices and provide a way to interact with them.
24. ls -l Output
The ls -l command displays a detailed listing of files, with each line containing the following fields:
- File type (e.g., -, d, l for regular file, directory, link)
- Owner permissions (read, write, execute)
- Group permissions (read, write, execute)
- Other permissions (read, write, execute)
- Number of hard links
- Owner username
- Group name
- File size in bytes
- Modification date and time
- Filename
25. Access Permissions
UNIX files and directories have three types of permissions: read (r), write (w), and execute (x). For directories, execute permission allows entering the directory. Permissions are granted to three user classes: owner (u), group (g), and others (o).
26. umask and chmod
- umask: Sets the default permissions for newly created files and directories.
- chmod: Changes the permissions of existing files and directories.
27. Redirection, Pipes, and Filters
Redirection allows changing the standard input or output of a command. Operators like <, >, <<, and >> are used for redirection.
Pipes (|) connect the output of one command to the input of another.
Filters are commands that process data streams, modifying or transforming the output of another command.
28. User Variables
To create a user variable, assign a value to it:
variable_name=value
To display the variable’s name, use:
echo variable_name
To display the variable’s value, use:
echo $variable_name
29. The Shell’s Importance
The shell is a command interpreter and a programming language. It’s the user’s interface to the UNIX operating system, mediating between the user and the kernel. When a user logs in, the shell initializes and executes commands from the .profile file. It then displays the prompt ($) and waits for user input.
30. The .profile File
The .profile file contains customizations that are executed when a user logs in. There’s a global .profile in /etc and a personal .profile in each user’s home directory. Users can customize their personal .profile to suit their needs.
31. UNIX Operating System Layers
The UNIX operating system is structured in layers:
- Hardware: The physical components.
- Kernel: The core of the OS, managing resources and processes.
- Shell: The user interface, interacting with the kernel.
- User Area: Where user programs and applications run.
The shell acts as an intermediary between the kernel and the user area, allowing users to interact with the system.
32. The Kernel
The kernel is the core of the UNIX operating system. It resides in memory and handles low-level tasks like resource management, process scheduling, memory management, and device drivers.
33. File Permissions Examples
- -rwx—r–: Regular file, owner has read, write, and execute permissions, group has read permission, others have no permissions.
- dr-xr-xr-x: Directory, owner has read, write, and execute permissions, group and others have read and execute permissions.
- -rwxr-xr-: Regular file, owner has read, write, and execute permissions, group and others have read and execute permissions.
- drwxrwxrwx: Directory, owner, group, and others have read, write, and execute permissions.
34. Command Results (ls, rm, mv, mkdir, grep)
This section describes the results of various commands, primarily related to file and directory manipulation and searching. It’s best to experiment with these commands in a terminal to understand their behavior fully.
35. Command Results (chmod)
This section explains the effects of different chmod commands on file permissions. Again, it’s recommended to try these commands to see how they modify permissions.
36. Redirection Examples
- cat letter: Displays the contents of the file letter.
- cat map > letter2: Redirects the output of cat map to the file letter2, overwriting its contents if it exists.
- cat map > letter2 2> error2: Redirects the standard output to letter2 and the standard error to error2.
37. More Command Explanations (ln, write, ftp, telnet, ps, kill)
- ln: Creates hard or symbolic links to files.
- write: Sends messages to other users logged into the system.
- ftp: Enables file transfer between computers over a network. Supports anonymous and authenticated connections. Commands like get, mget, put, and mput are used for downloading and uploading files.
- telnet: Opens a remote terminal session on another computer.
- ps: Lists running processes.
- kill: Terminates a process.
38. Displaying File Contents (cat, more, less)
- cat: Displays the entire file content at once.
- more: Displays the file content one screen at a time, pausing after each screen.
- less: Similar to more, but allows scrolling backward and forward through the file.
39. Links in Linux
A link is a way to refer to a file by a different name. Linux has two types of links:
- Hard links: Multiple names for the same inode. Deleting one hard link doesn’t affect the others unless it’s the last one.
- Symbolic links (soft links): A special file that points to another file. Deleting a symbolic link doesn’t affect the original file, but if the original file is deleted, the symbolic link becomes broken.
The ln command creates hard links, and ln -s creates symbolic links.