Linux Notes
Linux Notes
0
I Linux Shell
A Linux shell is a command-line interface (CLI) program used for interacting with the operating
system (OS). It acts as a mediator between the user and the Linux kernel, allowing users to issue
commands to perform various tasks such as navigating the file system, managing files and
directories, running applications, and configuring system settings.
Popular shells for Linux include Bash (Bourne Again Shell), which is the default on many
distributions, as well as others like Zsh (Z Shell), Fish, and more. Each shell has its own features,
syntax, and capabilities, but they all serve the fundamental purpose of providing a command-line
interface to the Linux operating system.
$echo $0
1. Regular Files: These are the most common type of files in Linux. They can contain text,
programs, scripts, images, etc.
2. Directories: Directories are special files that serve as containers for other files and
directories. They provide a hierarchical structure to organize data.
3. Symbolic Links (symlinks): These are files that act as pointers to another file or
directory. They are similar to shortcuts in Windows.
4. Device Files: Found in /dev, these files represent physical and virtual devices connected
to the system (e.g., hard drives, printers, terminals).
5. Named Pipes : Special files used for inter-process communication. They allow data to
be passed between processes.
6. Sockets: Used for communication between processes on the same or different systems.
2.2 Directories
1. Root Directory: The top-level directory in Linux filesystem, denoted by /(forward slash).
All other directories and files are subdirectories or files within this hierarchy.
2. Home Directory: Each user has a designated home directory, usually located at
/home/username. This is where personal files and configurations are stored.
3. Current Directory: Represents the directory you are currently working in. It's denoted
by . (dot).
4. Parent Directory: Denoted by .. (double dot), it refers to the directory immediately
above the current directory.
1
2.3 File Paths
Absolute Path
An absolute path specifies the exact location of a file or directory from the root directory (/).
Format: /path/to/file_or_directory
Example: /home/user/Documents/file.txt
Explanation:
o / is the root directory.
o home is a directory within the root.
o user is a directory within home.
o Documents is a directory within user.
o file.txt is the file located within the Documents directory.
Relative Path
A relative path specifies the location of a file or directory relative to the current working
directory.
Format: path/to/file_or_directory
Example: Documents/file.txt
Explanation:
o If the current working directory is /home/user, then Documents/file.txt refers to
/home/user/Documents/file.txt.
Special Symbols
Files can broadly be categorized into two main types: binary files and data files.
Binary files are files that contain data in a format that's not intended to be human-readable.
Instead, they are encoded in binary format, which is a sequence of 0s and 1s, representing
machine code or compiled data. Binary files typically include executable programs, libraries, and
compiled code.
2
Characteristics:
Non-human-readable: You cannot interpret binary files directly using a text editor because they
are encoded in a format understood by computers.
Executable: Binary files include executable programs (e.g., .exe), which are applications that can
be run by the operating system.
Compiled Code: They often contain compiled code, which is the output of programming
languages after compilation with extension like .o
Data files contain information or data that is stored in a format that can be interpreted or
processed by applications or users. These files may store text, images, documents, databases, or
any other form of structured or unstructured data.
Characteristics
Human-readable: Data files are typically human-readable or can be interpreted by specific
applications.
Structured Data: They may store structured data in formats like text, XML, CSV, etc.
Content Variety: Data files can range from plain text documents to complex multimedia files like
images, audio, video, etc
Binary Files: View and edit binary files using specialized tools or hex editors capable of
interpreting binary data.
Data Files: Use text editors or dedicated applications suited for the specific data format to view
and edit.
In Linux, there are several command-line based text editors that are popular among users who
prefer working within a terminal environment. These editors are efficient, lightweight, and can
be used over SSH sessions or on systems without a graphical interface. Here are some commonly
used command-based text editors in Linux:
4.1 Nano
Nano is a simple and beginner-friendly text editor that comes pre-installed on many Linux
distributions. It's known for its straightforward interface and ease of use.
3
Open a file or create a new one: $nano filename
Basic Navigation
Editing Text
Inserting Text: Simply start typing to insert text at the current cursor position.
Deleting Text: Use the Backspace or Delete keys to remove characters.
Cut, Copy, Paste: Nano doesn't have native cut, copy, and paste shortcuts like graphical
editors. Instead:
o Ctrl + K: Cut (kill) the current line.
o Ctrl + U: Uncut (paste) the last killed text.
Saving Changes
1. Press Ctrl + O (Write Out). Nano will prompt you to confirm the filename if you haven't
specified one.
2. Press Enter to confirm the filename or modify it if needed.
3. After saving, you can continue editing the file.
Exiting Nano
Press Ctrl + X. If you have unsaved changes, Nano will prompt you to save them (Y for
yes, N for no).
If you choose to save changes, follow the steps mentioned above (Ctrl + O to save, Enter
to confirm filename).
Additional Features
Search: Use Ctrl + W to search for a specific string within the file.
4
Replace: Use Alt + R to activate the replace function, allowing you to find and replace
text within the file.
Vim (Vi IMproved) is a powerful and highly configurable text editor inspired by the classic Unix
editor Vi. It is known for its modal editing capabilities and extensive customization options.
Key Features: Multiple modes (normal, insert, visual), syntax highlighting, macros, plugins,
extensive customization.
Learning Curve: Steeper learning curve compared to Nano, but very efficient once mastered.
cd stands for "change directory." It is a command-line utility used to change the current working
directory in the command line interface (CLI).
To change the current directory, use the cd command followed by the directory path you want to
switch to.
cd [directory_path]
Examples:
Special Directories:
pwd stands for "print working directory." It is a command-line utility that outputs the absolute
pathname of the current working directory.
5
pwd
Example:
pwd /home/user
cat command is used to concatenate and display the contents of files. Despite its name, cat is
versatile and has several other uses beyond just concatenating files
You can concatenate multiple files and display them sequentially using cat
cat file1 file2 This will output the contents of file1 followed by file2
cat file1 file2 > combined_file To concatenate files and redirect the output to another file
This will combine file1 and file2 and save the output to combined_file.
5.4 ls command
ls command is used to list directory contents. It shows you the files and directories within the
specified directory, or if no directory is specified, it lists the contents of the current working
directory.
5.5 cp command
cp command is used to copy files and directories from one location to another.
6
For example, to copy a file named file1.txt from the current directory to /tmp directory
cp file1.txt /tmp
cp file1.txt file2.txt directory_name To copy multiple files into a directory
cp -r source_directory destination To copy a directory and its contents
recursively, use the -r (or -R) option
cp -i source_file destination To prompt before overwriting existing files
cp -v source_file destination To see detailed information about the files
being copied, use the -v (verbose) option:
5.6 mv command
mv command is used to move files and directories from one location to another. It can also be
used to rename files and directories within the same location.
rm command is used to remove (delete) files and directories from the file system. It's a powerful
command-line utility, but it's important to use it with caution because deleted files are typically
not recoverable without specialized tools.