Table of Contents

    Introduction

    In this blog, we’ll walk you through the essential commands and techniques for managing files and directories in Linux. From basic navigation and file creation to advanced permission handling and file compression, this guide will equip you with the knowledge to master your Linux environment. Let’s dive in!

    Basics of File and Directory Management

    In Linux, everything is considered a file, including directories. A file is simply a collection of data, while a directory is a special type of file that contains other files and directories. Understanding this basic concept is crucial for effective file and directory management in Linux.

    Navigating the Filesystem

    Navigating the Linux filesystem efficiently is key to managing files and directories. Here are some essential commands:

    The pwd command

    The command ‘pwd‘ (Print Working Directory) displays the full path of the current working directory. It’s useful to know where you are in the filesystem at any given time.

    $ pwd
    /home/techlensfocus

    The cd command

    The command ‘cd‘ (Change Directory) changes your current directory. For example, to move to the /home/techlensfocus directory:

    $ cd /home/techlensfocus

    You can use the relative path. For example:

    $ cd Documents

    Or you can you absolute path. For instance:

    $ cd /home/techlensfocus/Documents

    You can go here to learn more about ‘cd’ and ‘pwd’ command.

    The ls command

    The command ‘ls‘ (List) lists the contents of a directory. It’s one of the most frequently used commands.

    $ ls

    You can go here to learn more about the ‘ls’ command.

    Creating and Deleting Files and Directories

    Creating Files

    Creating files in Linux can be done using several commands and methods:

    The touch command

    The ‘touch‘ command creates an empty file if it does not already exist. For example, to create a file named example.txt. If the file already exists, the ‘touch’ command updates its timestamp.

    $ touch example.txt

    Creating multiple files at once: You can create multiple files in one command by specifying multiple file names. For example, the command below creates three empty files named: file1.txt, file2.txt, file3.txt:

    $ touch file1.txt file2.txt file3.txt

    Changing to a specific Timestamp: You can specify a different timestamp for the file using the -t option, followed by the timestamp in the format [[CC]YY]MMDDhhmm[.ss]

    $ touch -t 202401011230.45 file.txt

    This command sets the access and modification times of file.txt file to January 1, 2024, 12:30:45.

    Using Reference files: The -r option allows you to set the timestamps of one file to match those of another file. See example below:

    $ touch -r referencefile.txt targetfile.txt

    This command updates the timestamps of target.txt to match those of referencefile.txt

    Example in a script: A script might use ‘touch‘ to create a file and then write to it

    #!/bin/bash
    
    # Create a log file
    $ touch logfile.txt
    
    # Write current date and time to the log file
    $ date >> logfile.txt
    
    # Display the contents of the log file
    $ cat logfile.txt

    Text Editors (nano, vi, vim)

    These editors can be used to create and edit files. For example, using ‘nano‘ to create and edit a file:

    $ nano example.txt

    Once you’ve finished editing, you can save the file and exit the editor.

    Creating Directories

    The mkdir command

    The ‘mkdir‘ command creates a new directory. For example, to create a directory named directory_example:

    $ mkdir directory_example

    You can also create nested directories using the -p option. For example, to create a directory structure parent/child/grandchild:

    $ mkdir -p parent/child/grandchild

    This command creates all the specified directories in the path, if they do not already exist.

    Deleting Files and Directories

    Deleting files and directories is straightforward with the following commands

    The rm command

    The command ‘rm‘ (Remove) is used to delete files. For example, to delete a file named Example1.txt

    $ rm Example1.txt

    You should you the -i option, it will prompt you to make sure that you really want to delete the file. For instance:

    $ rm -i Example2.txt

    The rmdir command

    This ‘rmdir‘ command is used to deletes empty directories. For example, to delete an empty directory named old_directory

    $ rmdir old_directory

    To delete a directory and all its contents, use the -r option. For example:

    $ rm -r old_directory

    Viewing File Contents

    To manage files effectively, you need to be able to view their contents. Linux provides several commands for this purpose

    The cat command

    The command ‘cat‘ will display the entire content of a file. It’s useful for quickly viewing small files. For example:

    $ cat example1.txt

    Learn more about cat command here.

    The more and less command

    These commands allow you to view large files one page at a time. ‘more‘ displays the file content and allows you to scroll down, while ‘less‘ provides more control, including backward navigation. For example, to view log.txt with less:

    $ less log.txt

    You can navigate using the arrow keys, and press ‘q‘ to quit.

    The head and tail command

    These commands are used to view the beginning and end of files, respectively. By default, ‘head‘ displays the first 10 lines, and ‘tail‘ displays the last 10 lines. Below is an example to view the first 5 lines of myfile.txt

    $ head -n 5 myfile.txt

    Similarly, to view the last 5 lines:

    $ tail-n 5 myfile.txt

    Copying, Moving, and Renaming Files and Directories

    Copying

    Copying files and directories is a common task in Linux, and the ‘cp ‘command is used for this purpose.

    The cp command

    The command ‘cp‘ copies files from one location to another. For instance, to copy a file name source.txt to destination.txt:

    $ cp source.txt destination.txt

    To copy a file into a different directory:

    $ cp source.txt /path/to/destination/

    To copy directories and their contents, use the -r (recursive) option. For example,

    $ cp -r source_dir destination_dir

    To copy file with prompt before overwriting, use the -i option:

    $ cp -i data.csv /home/user/backup/data.csv

    To copy multiple files to a directory:

    $ cp file1.txt file2.txt /path/to/destination/

    This command copies file1.txt and file2.txt to the specified directory.

    To copy all files in a Directory to another Directory:

    $ cp /home/user/docs/* /home/user/backup/docs/

    Moving and Renaming

    The mv command

    The command ‘mv‘ moves files and directories from one location to another. It’s also used to rename files and directories. For example, to move myfile.txt to a different directory:

    $ mv myfile.txt /path/to/destination/

    To rename a file from oldname.txt to newname.txt:

    $ mv oldname.txt newname.txt

    You can move entire directories in the same way as files. For example, to move a directory named source_dir to destinatino_dir:

    $ mv source_dir /path/to/destination/

    Note that if you move or rename file within the same filesystem, the inode number will not change.

    To move all files to another Directory:

    $ mv /home/user/downloads/* /home/user/backup/

    This command moves all files from the “downloads” directory to the “backup” directory.

    The wildcard “*” can be used to match patterns in filenames. For example, you want to move all files named “script” to folder named “backup”. Assuming the files and the “backup” directory are in the current working directory:

    $ mv -v *script* backup/

    The -v (verbose mode) option allow you to see what is being moved.

    Changing Permissions and Ownership

    File Permissions

    Understanding Permissions: Permissions are represented by a combination of letters and dashes. The first character indicates the file type. ‘‘ for regular files and ‘d‘ for directory.

    The next nine characters are grouped into sets of three, representing the permissions for the owner, group, and others.

    • r‘ stands for read permission.
    • w‘ stands for write permission.
    • x‘ stands for execute permission.
    • ‘ indicates no permission.

    There are three types of user who receive the permission to file or directory:

    • User (owner) permissions.
    • Group (group owner) permissions.
    • Other (everyone else on the Linux system) permissions.

    Viewing Current Username: to view your current username, you can use the ‘whoami‘ command:

    $ whoami

    View Group Membership: To view all the groups you belong to, use the ‘groups‘ command:

    $ groups

    The cmod command

    This command is used to change file or directory permissions. There are two methods to use’ chmod‘: symbolic and numeric.

    Symbolic mode

    In symbolic mode, for instance, to add execute permission for the user on myfile.txt:

    $ chmod u+x myfile.txt

    To remove write permission for others:

    $ chmod o-w example1.txt

    To set read and write permissions for the group and remove all permissions for others:

    $ chmod g+rw,o= example2.txt

    Notes:

    • + : Adds a permission.
    • : Remove a permission.
    • = : Make a permission equal to.

    You can also use ‘a‘ character to refer to all categories (user, group, other) to set permission. In the below example, the command changes the permissions of file1 to make it executable by all users:

    $ chmod a+x file1

    Recursive Mode: Use -R option to change permissions for all files in a directory and its subdirectories:

    $ chmod -R a+x /path/to/directory

    Numeric mode

    In numeric mode, we use a three-digit number to set permissions. Each digit represents the sum of permissions for the user, group, and others, respectively (read = 4, write = 2, execute = 1).

    For instance, to set read, write, and execute permissions for the user, read and execute for the group, and read and execute for others:

    $ chmod 755 example.txt

    Here is the table showing the permissions in a mode:

    PermissionCorresponding Number
    rwx7 (4 + 2 + 1)
    rw-6 (4 + 2 + 0)
    r-x5 (4 + 0 + 1)
    r4 (4 + 0 + 0)
    -wx3 (0 + 2 + 1)
    w2 (0 + 2 + 0)
    x1 (0 + 0 + 1)
    0 (0 + 0 + 0)

    Default Permission

    The default permissions for newly created files and directories in Linux are influenced by the “umask” (user mask). Umask is a special variable on the system that sets the default permissions by “masking” out specific bits, effectively subtracting permissions from the maximum possible default. For example, if the umask is ‘022’, it means:

    • 0 for owner (no permissions are subtracted)
    • 2 for group (write permission is subtracted)
    • 2 for others (write permission is subtracted)

    Let’s say a file with permission ‘666’ (read and write for owner, group, and others), with umask, the result will be: 666 022 = 644 (read and write for owner, read-only for group and others).

    To check for the umask value:

    $ umask

    To change the umask value to ‘027’:

    $ umask 027

    File Ownership

    In Linux, files and directories in Linux are owned by a user and a group. Changing ownership can be necessary for various administrative tasks.

    The chown command

    The command ‘chown‘ (Change Owner) changes the owner of a file or directory. For example, to change the owner of example3.txt to newuser:

    $ chown newuser example3.txt

    To change both the owner and the group:

    $ chown newuser:newgroup example4.txt

    Here is another example of changing the ownership of file1 to both username and groupname:

    $ chown root.root file1
    • root (before the dot): This is the username of the new owner of the file.
    • root (after the dot): This is the group name to which the file will belong.
    • file1: This is the name of the file or directory for which the ownership is being changed.

    The dot ‘.‘is used to separate the username from the group name. In some systems, a colon ‘:‘ can be used

    The chgrp command

    The command ‘chgrp‘ (Change Group) changes the group ownership of a file or directory. For example, to change the group of example.txt to newgroup:

    $ chgrp newgroup example.txt

    Searching for Files and Directories

    Efficiently searching for files and directories is a crucial skill in Linux, especially when dealing with large filesystems. Linux provides some powerful tools to help you locate files and directories quickly. Let’s have a look!

    The find command

    The command ‘find‘ is a versatile and powerful tool for searching files and directories based on various criteria. For instance, to search for files and directories by name, start with the current directory:

    $ find . -name "myfile.txt"

    To find directories or other specific file types, use the -type option. For example, to find all directories named backup:

    $ find . -type d -name "backup"

    To find all regular files (not directories or special files):

    $ find /path/to/search -type f

    To find files based on size. For example, to find files larger than 100MB:

    $ find /path/to/search -size +100M

    You can also combine different criteria to narrow down your search. For example, to find all .log files in the /var/ log directory modified in the last 7 days:

    $ find /var/log -name "*.log" -mtime -7

    Below are common criteria used with the ‘find‘ command:

    FunctionalityCriteriaExample
    Find files based on modification time (more than / less than x days ago)-mtime +x
    -mtime -x
    find . -mtime +7 (searches for files modified more than 7 days ago)
    Search for specific filenames (can contain wildcards)-name xfind . -name "*.txt" (finds all .txt files)
    Locate files by size-size xfind . -size 10M (finds files exactly 10 Mb in size)
    Identify files of a specific type:
    b for block files
    c for character files
    d for directory files
    p for named pipes
    f for regular files
    l for symbolic links (shortcuts)
    s for sockets
    -type xfind . -type d (finds all directories)
    Discover files owned by a particular user-user xfind . -user root (finds files owned by the root user)
    Search for files accessed within a timeframe (minutes)-mmin +x
    -mmin -x
    find . -mmin -10 (finds files modified less than 10 mins ago)
    Locate empty files or directories-emptyfind . -empty (finds all empty files and folders)
    Identify files based on filesystem-fstype xfind . -fstype ext4 (searches for files on the ext4 filesystem)
    Search for filenames using regular expressions-regex xfind . -regex ".+\.log$" (finds all files ending with .log)
    Find files based on access time (days)-atime -xfind . -atime -1 (searches for files accessed less than 1 day ago)
    Locate files by group ownership-group xfind . -group developers (finds files owned by the “developers” group)
    Discover files based on inode number-inum xfind . -inum 12345 (searches a file with inode number 12345)

    The grep command

    The command ‘grep‘ is used to search for text within files. It’s particularly useful for finding specific information inside files.

    To search for a string of text within a file. For example, to search for the word “error” in logfile.txt:

    $ grep "error" logfile.txt

    To search within all files in a directory and its subdirectories, use the -r option. For example, to search for the word “error” in all files within the /var/log directory:

    $ grep -r "error" /var/log

    To ignore case sensitivity, use the -i option. For instance, to search for “Error” or “error”:

    $ grep -i "error" logfile.txt

    To display the line numbers where the search term is found, use the -n option. For example:

    $ grep -n "error" logfile.txt

    Managing Links

    Linux allows you to create links to files and directories, which can be either hard links or soft (symbolic) links. Understanding how to create and manage these links is essential for efficient file management.

    Hard Links

    A hard link is an additional name for an existing file. Both the original file and the hard link point to the same inode (data on disk), making them indistinguishable from each other. Hard link cannot span different filesystems.

    Benefits of using hard link:

    • Data Redundancy: Hard links provide a way to maintain multiple references to the same file data, which can be useful for backup purposes. If one link is deleted, the data is still accessible via the other link.
    • Space Efficiency: Since hard links point to the same inode (physical data on disk), they do not consume additional disk space. This makes them efficient for creating multiple references to large files.
    • Consistency: Changes made to the file content via any hard link are immediately reflected across all other hard links. This ensures data consistency.

    Creating Hard Link

    Use the ‘ln’ command to create a hard link. For example, to create a hard link named hardlink.txt for a file named original.txt:

    $ ln original.txt hardlink.txt
    
    # Verify the hard link (both files will have the same inode number)
    $ ls -li original.txt hardlink.txt

    Now, original.txt and hardlink.txt refer to the same data on the disk. Changes made to one will reflect in the other.

    Deleting Hard Link

    Deleting a hard link does not delete the actual data on disk as long as there is at least one remaining hard link pointing to that data. The inode reference count is decremented by one. Other hard links to the same file remain fully functional, and the data is still accessible through them.

    If the original file is deleted but there are hard links pointing to it, the data remains intact, and the hard links continue to provide access to the data. The data is only deleted from the disk when the last hard link is removed, reducing the inode reference count to zero.

    From the previous example, you created a hard link named hardlink.txt. Now to delete the hardlink.txt

    # Delete the hard link
    $ rm hardlink.txt
    
    # Verify that the original file still exists and contains the data
    $ ls -li original.txt
    $ cat original.txt

    Soft Link

    A soft link, or symbolic link, is a pointer to another file or directory. Unlike hard links, symbolic links can point to files and directories across different filesystems.

    Benefits of using soft link (symbolic link):

    • Flexibility: Soft links can point to files and directories across different filesystems and partitions, providing greater flexibility compared to hard links.
    • Convenience: Soft links are useful for creating shortcuts to frequently accessed files or directories, simplifying navigation and file management.
    • Version Management: Soft links can be used to point to different versions of files or directories, making it easy to switch between versions by updating the link target.

    Creating Soft Link

    To create a symbolic link, the original file must be pre-exist. Use the ‘ls -s‘ command to create a symbolic link. For example, to create a symbolic link named softlink.txt for a file named original.txt:

    $ ln -s original.txt softlink.txt

    The softlink.txt file points to original.txt If original.txt is deleted, the softlink.txt becomes a broken link (it points to a non-existent file).

    Symbolic links are also particularly useful for linking directories. For example, to create a symbolic link named link_to_dir for a directory named original_dir:

    $ ln -s /path/to/original_dir /path/to/link_to_dir

    Symbolic links do not share the same inode and data blocks with their target file. To distinguish between linked files and physical files, you can view the inode number. These number is unique identification number. Use the -i option to the ‘ls‘ command:

    $ ls -i *log_file
    1234567 log_file  296781 sl_log_file

    From the output, we see the indo number is different. Thus, they are different files.

    Deleting Soft Link

    Deleting a soft link only removes the link itself. The target file or directory remains unaffected. If the target file or directory is deleted, the soft link becomes a broken link, pointing to a non-existent location. Accessing a broken link results in an error.

    To delete the soft link named softlink.txt:

    # Delete the soft link
    $ rm softlink.txt
    
    # Verify that the original file still exists and contains the data
    $ ls -l original.txt
    $ cat original.txt

    Archiving and Compressing Files

    Managing disk space efficiently and organizing multiple files into a single package are essential tasks in Linux. This is where archiving and compressing files come into play. Linux provides several powerful tools for this purpose, such as ‘tar‘, ‘gzip‘, and ‘bzip2‘.

    Archiving Files with tar

    The ‘tar‘ command is used to combine multiple files into a single archive file, often referred to as a tarball.

    Creating an Archive: to create an archive file, use the -cvf options (create, verbose, file). For example, to archive a directory named mydir into a file named archive.tar:

    $ tar -cvf archive.tar mydir/

    This command will create an archive named archive.tar containing all the files and subdirectories within mydir.

    Extracting an Archive: To extract the contents of an archive, use the -xvf options (extract, verbose, file). For example, to extract archive.tar:

    $ tar -xvf archive.tar

    Viewing Contents of an Archive: To list the contents of an archive without extracting them, use the -tvf options (list, verbose, file). For example:

    $ tar -tvf archive.tar

    Compressing Files with gzip and bzip2

    Compression reduces the size of files, saving disk space and making file transfer faster.

    Compressing with gzip To compress a file using gzip, simply use the gzip command. For example, to compress archive.tar:

    $ gzip archive.tar

    This will create a compressed file named archive.tar.gz. To decompress the file:

    $ gunzip archive.tar.gz

    Compressing with bzip2: bzip2 provides higher compression ratios than gzip, but it is slower. To compress a file using bzip2:

    $ bzip2 archive.tar

    This will create a compressed file named archive.tar.bz2. To decompress the file:

    $ bunzip2 archive.tar.bz2

    Combining Archiving and Compression

    It is common to combine archiving and compression to create compressed archive files.

    Creating a Compressed Archive with tar and gzip: To create a compressed archive in one step, use the -z option with tar. For example:

    $ tar -cvzf archive.tar.gz mydir/

    This command creates a compressed tarball named archive.tar.gz

    Extracting a Compressed Archive with tar and gzip:

    $ tar -xvzf archive.tar.gz

    Creating a Compressed Archive with tar and bzip2: Use the -j option with tar . For example:

    $ tar -cvjf archive.tar.bz2 mydir/

    This command creates a compressed tarball named archive.tar.bz2

    Extracting a Compressed Archive with tar and bzip2:

    $ tar -xvjf archive.tar.bz2

    Conclusion

    Mastering file and directory management in Linux enhances your efficiency and productivity. This guide covered essential tools and techniques, from basic navigation and manipulation to advanced practices in permissions, ownership, and symbolic links.

    Key areas included:

    • Basic Management: Commands to list, create, and navigate files and directories.
    • File and Directory Operations: Using ‘touch‘, ‘mkdir‘, ‘rm‘, ‘cp‘, ‘mv‘ to manage your filesystem.
    • File Viewing and Editing: Tools like ‘cat‘, ‘less‘, ‘nano‘ and ‘vim‘.
    • Permissions and Ownership: Understanding and using ‘chmod‘, ‘chown‘, and ‘chgrp‘.
    • Searching and Linking: Efficiently finding files with ‘find‘ and ‘grep‘ and managing hard and soft links.
    • Archiving and Compressing: Using ‘tar‘, ‘gzip‘ and ‘bzip2‘ for efficient file storage and transfer.

    By mastering these commands and concepts, you can ensure your Linux system is well-organized, secure, and efficient.

    Leave a Reply

    Your email address will not be published. Required fields are marked *