folders

All posts tagged folders by Linux Bash
  • Posted on

    Using Advanced File Search Techniques with find and grep

    The find and grep commands are incredibly powerful on their own, but when combined, they can perform advanced file search operations that allow you to filter and locate files based on specific content and attributes. Below is a guide to advanced techniques using find and grep for efficient file searches in Linux.


    1. Combining find with grep for Content Search

    While find is used to locate files based on various attributes like name, size, and type, grep is used to search the contents of files. By combining both, you can locate files and then search within them for specific text patterns.

    Search for Files and Grep Content Within Them

    You can use find to locate files, and then pipe the results to grep to search for specific content inside those files.

    • Example 1: Search for Files with Specific Content

      find /path/to/search -type f -exec grep -l "search_term" {} \;
      
      • This command searches for all files in the specified directory (/path/to/search) and looks inside each file for search_term. The -l option with grep ensures that only filenames are listed, not the content itself.
    • Example 2: Search for Content in .txt Files

      find /path/to/search -type f -name "*.txt" -exec grep -H "search_term" {} \;
      
      • This command looks for search_term in all .txt files within the specified directory and its subdirectories. The -H option in grep includes the filename in the output.

    2. Using grep with find for Case-Insensitive Search

    If you want to search for content regardless of case (case-insensitive search), you can use the -i option with grep. This makes your search more flexible, especially when you don’t know the exact case of the text you're searching for.

    • Example 1: Case-Insensitive Search for Content bash find /path/to/search -type f -exec grep -il "search_term" {} \;
      • This command searches for the term search_term in all files and returns only those that contain the term, regardless of whether it's upper or lower case. The -i option makes the search case-insensitive.

    3. Search for Files Containing Multiple Patterns

    You can combine multiple search patterns with grep using regular expressions or multiple grep commands.

    • Example 1: Search for Files Containing Multiple Words Using grep

      find /path/to/search -type f -exec grep -l "word1" {} \; -exec grep -l "word2" {} \;
      
      • This command searches for files that contain both word1 and word2. Each grep command adds an additional filter.
    • Example 2: Using Extended Regular Expressions

      find /path/to/search -type f -exec grep -E -l "word1|word2" {} \;
      
      • The -E option tells grep to use extended regular expressions, allowing you to search for either word1 or word2 (or both) in the files.

    4. Search for Files Modified Within a Specific Time Frame

    You can combine find and grep to search for files modified within a specific time frame and then search the contents of those files.

    • Example 1: Search for Files Modified in the Last 7 Days and Contain Specific Content

      find /path/to/search -type f -mtime -7 -exec grep -l "search_term" {} \;
      
      • This command finds files modified in the last 7 days and then searches within those files for search_term.
    • Example 2: Search for Files Modified More Than 30 Days Ago

      find /path/to/search -type f -mtime +30 -exec grep -l "search_term" {} \;
      
      • This finds files modified more than 30 days ago and searches them for search_term.

    5. Limit Search Depth with find and Search Content

    You can combine find's -maxdepth option with grep to limit the depth of your search for both files and content.

    • Example 1: Search Only in the Top-Level Directory for Specific Content

      find /path/to/search -maxdepth 1 -type f -exec grep -l "search_term" {} \;
      
      • This searches for files containing search_term only in the top-level directory (not in subdirectories).
    • Example 2: Search Within Subdirectories of a Specific Depth

      find /path/to/search -maxdepth 3 -type f -exec grep -l "search_term" {} \;
      
      • This searches for files containing search_term within the top 3 levels of directories.

    6. Using xargs with find and grep for Efficiency

    When working with large numbers of files, using xargs with find and grep can be more efficient than using -exec. xargs groups the output from find into manageable batches and then executes the command on those files, reducing the number of times the command is executed.

    • Example 1: Using xargs with grep

      find /path/to/search -type f -print0 | xargs -0 grep -l "search_term"
      
      • This command finds all files and searches them for search_term. The -print0 and -0 options ensure that filenames containing spaces or special characters are correctly handled.
    • Example 2: Using xargs to Search for Multiple Patterns

      find /path/to/search -type f -print0 | xargs -0 grep -lE "word1|word2"
      
      • This command searches for files that contain either word1 or word2, using grep with extended regular expressions.

    7. Search for Empty Files

    Empty files can be difficult to track, but find can be used to locate them. You can then use grep to search for any specific content or verify that the files are indeed empty.

    • Example 1: Find Empty Files

      find /path/to/search -type f -empty
      
      • This command finds files that have zero bytes of content.
    • Example 2: Find Empty Files and Search for a Pattern

      find /path/to/search -type f -empty -exec grep -l "search_term" {} \;
      
      • This command searches for empty files and looks inside them for search_term.

    8. Search for Files Based on Permissions and Content

    You can search for files based on their permissions and contents by combining find's permission filters with grep.

    • Example 1: Find Files with Specific Permissions and Search for Content bash find /path/to/search -type f -perm 644 -exec grep -l "search_term" {} \;
      • This command searches for files with 644 permissions and then looks for search_term inside them.

    9. Advanced Regular Expressions with grep

    grep allows the use of regular expressions to match complex patterns in file contents. You can use basic or extended regular expressions (with the -E option).

    • Example 1: Search for Lines Starting with a Specific Pattern

      find /path/to/search -type f -exec grep -l "^start" {} \;
      
      • This searches for lines in files that start with the word start.
    • Example 2: Search for Lines Containing Either of Two Words

      find /path/to/search -type f -exec grep -E -l "word1|word2" {} \;
      
      • This searches for lines containing either word1 or word2 in the files.

    10. Using find and grep with -exec vs xargs

    While -exec is useful for running commands on files found by find, xargs is often more efficient, especially when dealing with a large number of files. For example:

    • Using -exec:

      find /path/to/search -type f -exec grep -l "search_term" {} \;
      
    • Using xargs:

      find /path/to/search -type f -print0 | xargs -0 grep -l "search_term"
      

    The xargs version is typically faster because it processes files in batches, reducing the overhead of repeatedly calling grep.


    Conclusion

    By combining the power of find and grep, you can create advanced search techniques for locating files based on both attributes (like name, size, and permissions) and content. These tools are highly flexible and allow you to fine-tune searches with complex filters and conditions, making them indispensable for system administrators and advanced users working with large datasets or file systems.

  • 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

    Introduction

    After reading this document you should be able to identify why Linux defines its filesystem hierarchy in one big tree and explain the role of the filesystem hierarchy standard, explain what is available at boot in the root directory (/), explain each subdirectory purpose and typical contents. The aim here is to be able to create a working bash script which knows where to put its different data stores including lockfiles, database(s) or temporary files; including the script itself.

    One Big Filesystem

    As with all Linux installations there is a set protocol to follow which could be looked at as one big tree starting from its root, /. This often contains different access points not just typical file or folder components, but in fact mounted drives, USB or CD/DVD media volumes and so on. Even more adapt, these can span many partitions; as one filesystem. Regardless, the end result is one big filesystem, meaning applications generally do not care what volume or partition the data resides upon. The only drawback you may encounter is different naming conventions however there are now standards to follow in the Linux-ecosystem for cross-platform conformity.

    Defining The Data

    There has to be one method for defining all data in order to satisfy clear distinctions. Firstly, you may examine data and identify whether it is shareable or not. For instance, /home data may be shared across many hosts however .pid lock files will not. Another angle you may look at is are the files variable or static, meaning if no administrator input is given they will remain the same, ie. static - or else the data changes when the filesystem is in operation without human interaction hence it is called variable. With this in mind, you must identify which trees and sub-trees are accessible by your application or command prompt to identify if they can be manipulated at runtime and where they should reside if you are creating these filetypes.

    To summarise: - Shared Data is Common To All Systems - Non-Shareable Data is Local To One System - Static Data Never Changes When Left Alone - Variable Data Will Change During Application Processing

    The Filesystem Hierarchy Standard aims to help achieve unity across all platforms however different distributions can often invent new methodologies that will generally become standard over time. While the FHS (Filesystem Hierarchy Standard) publishes its standard new conventions are currently in play despite this document, see here: linuxfoundation.org...

    DIRECTORY FHS Approved PURPOSE
    / Yes Primary directory of the entire filesystem hierarchy.
    /bin Yes Essential executable programs that must be available in single user mode.
    /boot Yes Files needed to boot the system, such as the kernel, initrd or initramfs |images, and boot configuration files and bootloader programs.
    /dev Yes Device Nodes, used to interact with hardware and software devices.
    /etc Yes System-wide configuration files.
    /home Yes User home directories, including personal settings, files, etc.
    /lib Yes Libraries required by executable binaries in /bin and /sbin.
    /lib64 No 64-bit libraries required by executable binaries in /bin and /sbin, for systems which can run both 32-bit and 64-bit programs.
    /media Yes Mount points for removable media such as CDs, DVDs, USB sticks, etc.
    /mnt Yes Temporarily mounted filesystems.
    /opt Yes Optional application software packages.
    /proc Yes Virtual pseudo-filesystem giving information about the system and processes running on it. Can be used to alter system parameters.
    /sys No Virtual pseudo-filesystem giving information about the system and processes running on it. Can be used to alter system parameters. Similar to a device tree and is part of the Unified Device Model.
    /root Yes Home directory for the root user.
    /sbin Yes Essential system binaries.
    /srv Yes Site-specific data served up by the system. Seldom used.
    /tmp Yes Temporary files; on many distributions lost across a reboot and may be a ramdisk in memory.
    /usr Yes Multi-user applications, utilities and data; theoretically read-only.
    /var Yes Variable data that changes during system operation.

    Run du --max-depth=1 -hx / to see the output of your root filesystem hierarchy.

    The Root Directory (/)

    Starting with our first directory, the root directory (/) this is often the access point mounted across multiple (or single) partitions with other locations such as /home, /var and /opt mounted later. This root partition must contain all root directories and files at boot in order to serve the system. Therefore it needs boot loader information and configuration files plus other essential startup data, which must be adequate to perform the following operations: - Boot the system - Restore the system on external devices such as USB, CD/DVD or NAS - Recover and/or repair the system (ie. in rescue mode)

    The root directory / should never have folders created directly within it; period.

    Binary Files (/bin)

    • The /bin directory must be present for a system to function containing scripts which are used indirectly by other scripts. It's important because non-privileged users and system administrators all have access to this directory plus it contains scripts needed to be served before the filesystem is even mounted. It is common place to store non-essential scripts which do not merit going in /bin to be served from /usr/bin, however /bin is becoming more acceptable to be used in common-place operation, in fact in RHEL they are the same directory. Often symbolic links are used from/bin to other folder locations in order to preserve two-way folder listings.

    They are as follows: cat, chgrp, chmod, chown, cp, date, dd, df, dmesg, echo, false, hostname, kill, ln, login, ls, mkdir, mknod, more, mount, mv, ps, pwd, rm, rmdir, sed, sh, stty, su, sync, true, umount and uname

    Other binaries that may be present during boot up and in normal operation are: test, csh, ed, tar, cpio, gunzip, zcat, netstat, ping

    The Boot Directory (/boot)

    This folder contains the vmlinuz and intramfs (also known as initrd) files which are put there in order to serve the boot operation, the first is a compressed kernel and the second is the initial RAM filesystem. Other files include config and system.map.

    Device Files (/dev)

    Device files are often used to store device nodes; commonly serving various hardware references including nodes - network cards however are more likely to be named eth0 or wlan0 meaning they are referenced by name. The directory /dev/ will automatically create nodes using udev when system hardware is found. Quite aptly, ls /dev | grep std will show you some useful output references which can be used to process data either to the terminal or into the obis.

    Configuration Files (/etc)

    Used to contain config directives (or contained folders with config directives) for system-wide programs and more importantly system services.

    Some examples are: csh.login, exports, fstab, ftpusers, gateways, gettydefs, group, host.conf, hosts.allow, hosts.deny, hosts,equiv, hosts.lpd, inetd.conf, inittab, issue, ld.so.conf, motd, mtab, mtools.conf, networks, passwd, printcap, profile, protocols, resolv.conf, rpc, securetty, services, shells, syslog.conf

    More crucially, the following helps keep the system correctly configured: - /etc/skel This folder contains the skeleton of any new users /home directory - /etc/systemd - Points to or contains configuration scripts for system services, called by service - /etc/init.d - Contains startup and shutdown scripts used by System V initialisation

    System Users (/home)

    On Linux, users working directories are given in the /home/{username} format and are typically named in a naming convention such as /home/admin /home/projects/home/stagingor/home/production. Typically, this could be their name or nickname or purpose, eg./home/steve,/home/steve-work` and so on.

    With Linux, this folder can be accessed via the ~ symbol which is given to system users in order to direct the user to the currently logged in users home directory, eg. ls ~/new-folder etc; which is also accessible by using $home. The only caveat to this is that the root user is placed in /root - all other users reside in /home, typically mirroring /etc/skel as previously outlined. (see “Configuration Files (/etc)”)

    System Libraries (/lib and /lib64)

    These folders are for libraries serving binary files found in /bin or other locations where scripts are found. These libraries are important because they maintain the upkeep of essential system programs (binaries) which help boot the system and then are used by the system once booted, fundamentally. Kernel modules (device or system drivers) are stored in /lib/modules and PAM (Pluggable Authentication Modules) are stored in /lib/security.

    For systems running 32bit and 64bit the /lib64 is usually present. More common place is to use the one folder with symbolic links to the actual destination of the library, similar to how /bin has reformed back into one folder, providing the same structure with separation of differing program importance (using symbolic links) while maintaining the single source for all scripts of that type.

    External Devices (/media)

    The /media folder is often found to be a single source for all removable media. USB, CD, DVD even the ancient Floppy Disk Drive. Linux mounts these automatically using “udev” which in turn creates references inside /media making for simple access to external devices, autonomously. As the device is removed, the name of the file in this directory is removed also.

    Temporary Mounts (/mnt)

    This folder is used for mount points, usually temporary ones. During the development of the FHS this would typically contain removable devices however /media is favoured on modern systems.

    Typical use scenarios are: - NFS - Samba - CIFS - AFS

    Generically, this should not be used by applications, instead mounted disks should be located elsewhere on the system.

    Software Packages (/opt)

    This location is where you would put system-wide software packages with everything included in one place. This is for services that want to provide everything in one place, so you would have /opt/project/bin etc. all in this folder. The directories /opt/bin, /opt/doc, /opt/include, /opt/info, /opt/lib, and /opt/man are saved for administrator usage.

    System Processes (/proc)

    These are special files which are mounted like with /dev and are constantly changing. They only contain data at the point you make the request, so typically a file may be 0kb but if you look, may contain many lines of data; this is accessed only when you run the cat or vi operation, it does indeed remain empty. Important pseudo files are /proc/interrupts, /proc/meminfo, /proc/mounts, and /proc/partitions.

    System Filesystems (/sys)

    This directory is the mount point for the sysfs pseudo-filesystem where all information resides only in memory, not on disk. This is again very much like /dev/ and /proc in that it contains mounted volumes which are created on system boot. Containing information about devices and drivers, kernel modules and so on.

    Root (/root)

    This is generally called “slash-route” pronounced phonetically and is simply the primary system administrators home folder. Other user accounts are encouraged with specific access details for better security.

    System Binaries (/sbin)

    This is very similar to /bin and as mentioned may very well have symbolic link references inside /bin with the actual program residing here. This allows for a one-solution-fits-all because /bin will display all programs whereas /sbin would be designated to the programs listed there. Some of these programs include: fdisk, fsck, getty, halt, ifconfig, init, mkfs, mkswap, reboot, route, swapon, swapoff and update.

    System Services (/srv)

    Popular for some administrators this is designed to provide system service functionality. You can be fairly lax with naming conventions here you may want to group applications into folders such as ftp, rsync, www, and cvs etc. Popular by those that use it and may be overlooked by those what don’t.

    Temporary Files (/tmp)

    Used by programs that do not want to keep the data between system boots and it may be periodically refreshed by the system. Use at your own discretion. Be aware as this is truly temporary data any large files may cause issues as often the information is stored in memory.

    System User (/usr)

    This should be thought of as a second system hierarchy. Containing non-local data it is best practice to serve administrator applications here and is often used for files and packages or software that is not needed for booting.

    DIRECTORY PURPOSE
    /usr/bin Non-essential command binaries
    /usr/etc Non-essential configuration files (usually empty)
    /usr/games Game data
    /usr/include Header files used to compile applications
    /usr/lib Library files
    /usr/lib64 Library files for 64-bit
    /usr/local Third-level hierarchy (for machine local files)
    /usr/sbin Non-essential system binaries
    /usr/share Read-only architecture-independent files
    /usr/src Source code and headers for the Linux kernel
    /usr/tmp Secondary temporary directory

    Other common destinations are /usr/share/man and /usr/local, the former is for manual pages and the latter is for predominantly read-only binaries.

    Variable Data (/var)

    This directory is intended for variable (volatile) data and as such is often updated quite frequently. Contains log files, spool directories and files administrator files and transient files such as cache data.

    SUBDIRECTORY PURPOSE
    /var/ftp Used for ftp server base
    /var/lib Persistent data modified by programs as they run
    /var/lock Lock files used to control simultaneous access to resources
    /var/log Log files
    /var/mail User mailboxes
    /var/run Information about the running system since the last boot
    /var/spool Tasks spooled or waiting to be processed, such as print queues
    /var/tmp Temporary files to be preserved across system reboot. Sometimes linked to /tmp
    /var/www Root for website hierarchies

    Transient Files (/run)

    These are meant to be files that are updated quite regularly and are not often maintained during reboots, useful for containing temporary files and runtime information. The use of run is quite new and you may find /var/run and /var/lock symbolic link references. The use of this is more common place in modern systems.