Filesystem

A Linux Filesystem is generally a layer of the Operating System used to handle the data management of the storage. It helps to arrange the file on the disk storage. It manages the file name, file size, creation date, and much more information about a file.

A Linux Filesystem would contain:

  • The root directory (/)
  • A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on)
  • A partition or logical volume
  • Posted on

    How to Open and Edit Files with Bash

    Bash provides several powerful commands to open and edit files directly from the command line. Whether you're working with text files, configuration files, or scripts, knowing how to open and edit files in Bash is essential for efficient terminal-based workflows.

    Here’s a guide to the most commonly used commands for opening and editing files in Bash.


    1. Viewing Files

    Before editing a file, you might want to view its contents. Here are a few ways to do that:

    cat (Concatenate)

    The cat command is used to display the contents of a file directly in the terminal.

    cat filename.txt
    

    It will output the entire content of the file.

    less and more

    Both commands allow you to scroll through large files. less is typically preferred since it allows for backward scrolling.

    less filename.txt
    
    • Use the Up and Down arrows to scroll.
    • Press q to exit.
    more filename.txt
    
    • Use the Space bar to scroll down and q to exit.

    head and tail

    • head shows the first 10 lines of a file. bash head filename.txt
    • tail shows the last 10 lines of a file. bash tail filename.txt

    To see more than the default 10 lines: - head -n 20 filename.txt (shows the first 20 lines). - tail -n 20 filename.txt (shows the last 20 lines).


    2. Editing Files in Bash

    Bash offers several text editors that allow you to edit files directly from the terminal. The most commonly used editors are nano, vim, and vi.

    nano (Beginner-Friendly Text Editor)

    nano is an easy-to-use, terminal-based text editor. It's particularly well-suited for beginners.

    To open a file with nano:

    nano filename.txt
    

    Basic commands inside nano: - Move the cursor: Use the arrow keys to navigate. - Save the file: Press Ctrl + O (then press Enter to confirm). - Exit: Press Ctrl + X. - If you've made changes without saving, nano will ask if you want to save before exiting.

    vim and vi (Advanced Text Editors)

    vim (or its predecessor vi) is a powerful text editor but has a steeper learning curve. It offers more features for advanced text editing, such as syntax highlighting, searching, and programming tools.

    To open a file with vim:

    vim filename.txt
    

    You will be in normal mode by default, where you can navigate, delete text, and use commands.

    • Switch to insert mode: Press i to start editing the file.
    • Save the file: While in normal mode, press Esc to return to normal mode and then type :w (then press Enter).
    • Exit the editor: Press Esc and type :q to quit. If you have unsaved changes, use :wq to save and quit, or :q! to quit without saving.

    vi Command Reference

    • Open a file: vi filename.txt
    • Switch to insert mode: i
    • Save changes: Press Esc and type :w (press Enter).
    • Quit without saving: Press Esc and type :q! (press Enter).
    • Save and quit: Press Esc and type :wq (press Enter).

    3. Editing Configuration Files

    Many system configuration files are text files that can be edited using Bash. Some of these files, such as /etc/hosts or /etc/apt/sources.list, may require root privileges.

    To edit such files, you can use sudo (SuperUser Do) with your text editor.

    For example, using nano to edit a configuration file:

    sudo nano /etc/hosts
    

    You’ll be prompted to enter your password before editing the file.


    4. Creating and Editing New Files

    If the file you want to edit doesn’t exist, most editors will create a new file. For example, using nano:

    nano newfile.txt
    

    If newfile.txt doesn’t exist, nano will create it when you save.

    Similarly, you can use touch to create an empty file:

    touch newfile.txt
    

    Then, you can open and edit it with any text editor (e.g., nano, vim).


    5. Editing Files with Other Editors

    While nano, vim, and vi are the most common command-line editors, there are other text editors that may be available, including:

    • emacs: Another powerful, customizable text editor.

      emacs filename.txt
      
    • gedit: A GUI-based text editor, often available on desktop environments like GNOME.

      gedit filename.txt
      

    6. Quickly Editing a File with echo or printf

    If you want to quickly add content to a file, you can use the echo or printf commands:

    • echo: Adds a simple line of text to a file.

      echo "Hello, World!" > file.txt  # Overwrites the file with this text
      echo "New line" >> file.txt  # Appends text to the file
      
    • printf: Offers more control over formatting.

      printf "Line 1\nLine 2\n" > file.txt  # Overwrites with formatted text
      printf "Appending this line\n" >> file.txt  # Appends formatted text
      

    7. Working with Multiple Files

    If you want to edit multiple files at once, you can open them in the same editor (like vim) by listing them:

    vim file1.txt file2.txt
    

    Alternatively, you can open separate terminals or use a multiplexer like tmux to edit multiple files in parallel.


    8. Searching and Replacing in Files

    If you want to find and replace text in files, you can use the search-and-replace functionality in vim or nano.

    In vim:

    • Search: Press / and type the search term, then press Enter.
    • Replace: Press Esc and type :s/old/new/g to replace all instances of "old" with "new" on the current line, or :%s/old/new/g to replace in the entire file.

    In nano:

    • Search: Press Ctrl + W and type the search term.
    • Replace: Press Ctrl + \, then type the text to find and replace.

    Conclusion

    Opening and editing files in Bash is a fundamental skill for anyone working in a Linux environment. Whether you're using a simple editor like nano, a more advanced one like vim, or creating files directly from the command line with echo, the tools provided by Bash allow you to efficiently view and manipulate text files.

    As you become more comfortable with these tools, you’ll find that working with files in Bash can be both faster and more powerful than relying solely on graphical text editors.

  • Posted on

    A Beginner's Guide to Navigating the Linux File System Using Bash

    Navigating the Linux file system is one of the first steps to becoming proficient with the command line. Bash, the default shell on most Linux distributions, provides powerful tools for managing and accessing files and directories. This guide will walk you through the basic commands and techniques for navigating the Linux file system using Bash.


    1. Understanding the Linux File System Structure

    Before you start navigating, it's important to understand how the Linux file system is organized. The structure is hierarchical, with a root directory (/) at the top.

    Here are some common directories you will encounter:

    • / (Root): The root directory is the starting point of the file system hierarchy. All files and directories stem from here.
    • /home: User-specific directories are located here. For example, /home/username/ is the home directory for a user.
    • /bin: Essential system binaries (programs) are stored here.
    • /etc: Configuration files for system-wide settings.
    • /usr: Contains user programs and data.
    • /var: Contains variable files like logs, spool files, and temporary files.

    2. Basic Commands for Navigating the File System

    pwd (Print Working Directory)

    • The pwd command shows the current directory you are in. bash $ pwd /home/username This command helps you verify your current location within the file system.

    ls (List Directory Contents)

    • The ls command lists the files and directories in the current directory. bash $ ls Desktop Documents Downloads Music Pictures Videos You can also use options with ls to get more detailed information:
      • ls -l: Displays detailed information (permissions, ownership, size, etc.)
      • ls -a: Lists all files, including hidden files (those starting with a dot).

    cd (Change Directory)

    • The cd command changes your current directory. bash $ cd /path/to/directory
      • cd ..: Goes up one level in the directory hierarchy.
      • cd ~: Takes you to your home directory.
      • cd -: Takes you to the previous directory you were in.

    ls / (List Root Directory)

    • Listing the contents of the root directory will give you an idea of the overall structure. bash $ ls / bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

    3. Navigating Between Directories

    Here are some commands and techniques for moving around the file system:

    • Absolute Path: An absolute path starts from the root directory (/).

      • Example: /home/username/Documents
    • Relative Path: A relative path is relative to your current location in the file system.

      • Example: If you're in /home/username, and want to access Documents, just use Documents.
    • Using cd with Absolute and Relative Paths:

      • Absolute path:
      cd /home/username/Documents
      
      • Relative path (from /home/username):
      cd Documents
      

    4. Using Wildcards for Navigation

    Wildcards are special characters that can be used to match multiple files or directories:

    • * (Asterisk): Matches any number of characters.

      • Example: cd /home/username/* will match all directories inside /home/username/.
    • ? (Question Mark): Matches a single character.

      • Example: ls /home/username/file?.txt will match file1.txt, file2.txt, etc., but not file10.txt.
    • [] (Square Brackets): Matches any one of the characters inside the brackets.

      • Example: ls /home/username/file[1-3].txt will match file1.txt, file2.txt, and file3.txt.

    5. Using Tab Completion for Efficiency

    Bash supports tab completion, which allows you to type a few letters of a directory or file name and press Tab to automatically complete it. If there are multiple possibilities, press Tab twice to see the options.

    For example: - Type cd /ho and press Tab to complete it as /home. - Type cd /home/username/D and press Tab to complete it as Documents (if that's the only directory starting with "D").


    6. Viewing Files and Directories

    You can view files and directories using various commands in Bash:

    • cat (Concatenate): Displays the contents of a file.

      cat filename.txt
      
    • less: Opens a file in a pager, allowing you to scroll through it.

      less filename.txt
      
    • more: Similar to less, but only allows forward navigation.

      more filename.txt
      
    • file: Determines the type of a file.

      file filename.txt
      
    • head: Displays the first 10 lines of a file (by default).

      head filename.txt
      
    • tail: Displays the last 10 lines of a file (by default).

      tail filename.txt
      

    7. Creating and Managing Files and Directories

    • mkdir (Make Directory): Creates a new directory.

      mkdir new_directory
      
    • touch: Creates a new, empty file or updates the timestamp of an existing file.

      touch newfile.txt
      
    • cp (Copy): Copies files or directories.

      cp file1.txt /path/to/destination
      
    • mv (Move): Moves or renames files or directories.

      mv oldname.txt newname.txt
      
    • rm (Remove): Deletes files or directories.

      • To remove a file:
      rm file.txt
      
      • To remove a directory (use -r for recursive removal):
      rm -r directory_name
      

    8. Checking Disk Usage and Space

    • df: Displays information about disk space usage on mounted file systems.

      df -h
      

      The -h option makes the output human-readable (e.g., in GBs).

    • du: Displays the disk usage of files and directories.

      du -sh directory_name
      

      The -s option shows only the total size, while -h makes the output human-readable.


    9. Permissions and Ownership

    • ls -l: Shows file permissions and ownership information.

      ls -l filename.txt
      

      Example output:

      -rw-r--r-- 1 user user 1234 Dec 20 12:34 filename.txt
      
    • chmod (Change Mode): Changes file permissions.

      • Example: Give the user write permission:
      chmod u+w filename.txt
      
    • chown (Change Ownership): Changes file ownership.

      • Example: Change ownership to newuser:
      chown newuser filename.txt
      

    Conclusion

    Navigating the Linux file system using Bash involves learning a few basic commands and concepts, such as using pwd to display your current location, ls to list directory contents, and cd to move between directories. As you get more familiar with Bash, you’ll also start using advanced features like wildcards, tab completion, and file manipulation commands to become more efficient. Understanding the Linux file system and mastering these commands will help you become more productive and comfortable working in a Linux environment.

  • Posted on

    Understanding File Permissions and Ownership in Bash

    File permissions and ownership are fundamental concepts in Linux (and Unix-based systems), allowing users and groups to control access to files and directories. In Bash, file permissions determine who can read, write, or execute a file, while ownership identifies the user and group associated with that file. Understanding and managing file permissions and ownership is essential for maintaining security and managing system resources.


    1. File Permissions Overview

    Every file and directory on a Linux system has three types of permissions: - Read (r): Allows the user to open and read the contents of the file. - Write (w): Allows the user to modify or delete the file. - Execute (x): Allows the user to run the file as a program or script (for directories, it allows entering the directory).

    These permissions are set for three categories of users: - Owner: The user who owns the file. - Group: Users who belong to the same group as the file. - Others: All other users who don’t fall into the above categories.

    Example:

    A typical file permission looks like this:

    -rwxr-xr--
    

    Where: - The first character - indicates it's a file (a d would indicate a directory). - The next three characters rwx represent the owner's permissions (read, write, and execute). - The next three characters r-x represent the group's permissions (read and execute). - The final three characters r-- represent the permissions for others (read only).


    2. Viewing File Permissions

    To view the permissions of a file or directory, use the ls -l command:

    ls -l filename
    

    Example output:

    -rwxr-xr-- 1 user group 12345 Dec 20 10:30 filename
    

    Explanation:

    • -rwxr-xr--: File permissions.

    • 1: Number of hard links.

    • user: Owner of the file.

    • group: Group associated with the file.

    • 12345: Size of the file in bytes.

    • Dec 20 10:30: Last modified date and time.

    • filename: The name of the file.


    3. Changing File Permissions with chmod

    To change file permissions, you use the chmod (change mode) command.

    Syntax:

    chmod [permissions] [file/directory]
    

    Permissions can be set using symbolic mode or numeric mode.

    Symbolic Mode:

    You can modify permissions by using symbolic representation (r, w, x).

    • Add permission: +
    • Remove permission: -
    • Set exact permission: =

    Examples:

    • Add execute permission for the owner:

      chmod u+x filename
      
    • Remove write permission for the group:

      chmod g-w filename
      
    • Set read and write permissions for everyone:

      chmod a=rw filename
      

    Numeric Mode:

    Permissions are also represented by numbers: - r = 4 - w = 2 - x = 1

    The numeric mode combines these numbers to represent permissions for the owner, group, and others.

    Examples: - Set permissions to rwxr-xr-- (owner: rwx, group: r-x, others: r--): bash chmod 755 filename - Set permissions to rw-r----- (owner: rw-, group: r--, others: ---): bash chmod 640 filename

    The first digit represents the owner’s permissions, the second digit represents the group’s permissions, and the third digit represents others’ permissions.


    4. Changing File Ownership with chown

    To change the ownership of a file or directory, use the chown command.

    Syntax:

    chown [owner][:group] [file/directory]
    
    • Change owner: bash chown newuser filename
    • Change owner and group: bash chown newuser:newgroup filename
    • Change only group: bash chown :newgroup filename

    Example:

    • Change the owner to alice and the group to developers: bash chown alice:developers filename

    5. Changing Group Ownership with chgrp

    If you only want to change the group ownership of a file or directory, you can use the chgrp command.

    Syntax:

    chgrp groupname filename
    

    Example:

    • Change the group ownership to admin: bash chgrp admin filename

    6. Special Permissions

    There are also special permissions that provide more control over file execution and access:

    • Setuid (s): When set on an executable file, the file is executed with the privileges of the file’s owner, rather than the user executing it.

      • Example: chmod u+s file
    • Setgid (s): When set on a directory, files created within that directory inherit the group of the directory, rather than the user’s current group.

      • Example: chmod g+s directory
    • Sticky Bit (t): When set on a directory, only the owner of a file can delete or rename the file, even if others have write permissions.

      • Example: chmod +t directory

    7. Example of Viewing, Changing, and Managing Permissions

    • View permissions: bash ls -l myfile.txt
    • Change permissions (allow read and execute for everyone): bash chmod a+rx myfile.txt
    • Change ownership (set john as owner and staff as group): bash chown john:staff myfile.txt

    Conclusion

    Understanding file permissions and ownership is crucial for managing security and accessibility in Linux. By using commands like chmod, chown, and ls -l, you can control who can access, modify, and execute files, ensuring proper security and efficient system management. Always be cautious when changing permissions, especially with system files or directories, to avoid inadvertently compromising the security of your system.

  • Posted on

    If you're suffering with server lag, ping latency, degraded system functionality or generally slow response times you may need to add extra RAM to your server.

    Of course, adding RAM is a solution - but it's not the only option you have. Why not think about utilising swap space, this enables you to let your HDD act as a virtual-backup solution to out-of-memory situations.

    These days of course using your HDD for RAM doesn't seem as bad as what it sounds, with SSD's widely used the temptation to switch to swap space is quite understandable.

    Plus, for most use-cases, you will only need as much as 2-4GB of RAM in order to compile your favourite popular software title - such as Apache, or similar. To simply run the program may require less RAM thus less overhead for cost. You could use the package manager to install Apache to avoid having to compile it but why do that when you a) want to compile and b) can simply add swap space.

    To add swap space, follow these instructions:

    sudo fallocate -l 1G /swapfile
    

    Here, we are creating a swap file with a size of 1G. If you need more swap, replace 1G with the desired size.

    A good rule of thumb is to have 3x more swap than your physical RAM. So for 2GB of RAM you would have 6GB of swap space.

    If the fallocate utility is not available on your system or you get an error message saying fallocate failed: Operation not supported, use the dd command to create the swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
    

    Next, set the swap space files permissions so that only the root user can read and write the swap file:

    sudo chmod 600 /swapfile
    

    Next, set up a Linux swap area on the file:

    sudo mkswap /swapfile
    

    Activate the swap by executing the following command:

    sudo swapon /swapfile
    

    And that should do it. Check the memory is allocated by simply using free.

    sudo free -h
    

    Finally, make the changes permanent by adding a swap entry in the /etc/fstab file:

    echo  '/swapfile swap swap defaults 0 0' >> /etc/fstab
    

    Now, when your server reboots the swap space will be reconfigured automatically.


    And that's it! If you enjoyed this post please feel free to leave a like or a comment using the messaging options below.