Linux diff: compare two directories

Comparing Directories in Linux: Essential Commands for Efficient Management

As a Linux user, you might frequently encounter the need to compare the contents of two directories. This is a common task in various scenarios, such as synchronizing files, diagnosing changes, or simply understanding the differences between two sets of data. Linux, known for its powerful command-line tools, offers several methods to accomplish this. In my professional experience, I’ve found two commands particularly useful for comparing directories.

1. Creating a Patch Format List of Changes

When you need a detailed overview of changes between two directories, the `diff` command is your go-to tool. This command can generate a comprehensive list of differences in a patch file format, which is especially useful for developers or administrators who require a clear record of changes for version control or audit purposes.

Here’s the command:

diff -Naur dir1/ dir2/ > file.patch
- `diff`: This is the command that invokes the comparison utility.
- `-Naur`: These options stand for:
  - `N`: Treat absent files as empty, which helps in creating comprehensive patches.
  - `a`: Treat all files as text, ensuring no file is overlooked due to its format.
  - `u`: Output in unified format, which is more readable and standard for patches.
  - `r`: Recursively compare any subdirectories found.
- `dir1/ dir2/`: Replace these with the paths of your directories.
- `> file.patch`: This redirects the output to a file named `file.patch`. You can name this file as you prefer.

2. Generating a Simple List of Changed Files

In situations where you only need to know which files have been altered, without the specifics of the changes, there’s a simpler command. This generates just a list of files that differ between the two directories.

Use this command:

diff -aqr dir1/ dir2/ > changed_list.txt
- `-aqr`: The options here are slightly different:
  - `a`: As before, this treats all files as text.
  - `q`: Quick mode. This reports only when files differ, not the details of the changes.
  - `r`: Recursively compares subdirectories.
- `dir1/ dir2/`: Replace with your directory paths.
- `> changed_list.txt`: This redirects the output to a file named `changed_list.txt`, which you can rename as needed.

These two commands have served me well in various professional contexts, offering both depth and simplicity as needed. Whether you’re managing web development projects, performing system administration tasks, or simply organizing your personal files, these Linux commands are essential tools in your arsenal.

Linux: create csr and key to get SSL certificate

OpenSSL

Securing Your Website: A Simple Guide to Generating SSL Certificates in Linux

In today’s digital world, securing your website with an SSL certificate is not just a best practice; it’s a necessity. This process involves generating a private key and a Certificate Signing Request (CSR). For Linux users, this task can be accomplished effortlessly with a single command line input.

Here’s how you can generate your SSL certificate:

  1. Open your Linux terminal. Begin by launching your command line interface.
  2. Enter the OpenSSL command. Use the following syntax to initiate the creation of your private key and CSR:
    openssl req -nodes -newkey rsa:2048 -keyout www.shkodenko.com.key -out www.shkodenko.com.csr
    
  3. Provide necessary details. You’ll be prompted to fill in various fields. These include:
       - Country Name (2 letter code) [XX]
       - State or Province Name (full name) []
       - Locality Name (e.g., city) [Default City]
       - Organization Name (e.g., company) [Default Company Ltd]
       - Organization Unit Name (e.g., section) []
       - Common Name (e.g., your name or your server's hostname)
       - Email Address []
    

    These details are critical as they form the backbone of your SSL certificate, ensuring its validity and credibility.

  4. Check the generated files. After running the command, two important files will be created in the folder:
    – `www.shkodenko.com.key`: This is your private key, utilizing a robust RSA algorithm with 2048 bits encryption.
    – `www.shkodenko.com.csr`: The CSR file, essential for obtaining your SSL certificate from a Certificate Authority (CA).

By following these steps, you can secure your website with a vital layer of encryption, safeguarding both your data and your users’ trust. Remember, SSL certificates not only protect sensitive information but also boost your website’s credibility and search engine ranking.

Stay secure and happy coding!

Efficiently Displaying Configuration Files in Linux: Excluding Empty Lines and Comments

Efficiently Displaying Configuration Files in Linux: Excluding Empty Lines and Comments

When working with Linux, there’s a frequent need to view the contents of configuration files, such as `/etc/my.cnf`, without the clutter of empty lines or comments. This task can be efficiently accomplished using a combination of command-line tools.

Here’s a simple yet effective command sequence:

cat /etc/my.cnf | sed '/^$/d' | grep -v "#" | more

This command pipeline works as follows:

  1. `cat /etc/my.cnf` – Displays the contents of the file `/etc/my.cnf`.
  2. `sed ‘/^$/d’` – Removes empty lines. The `sed` command searches for lines that start (`^`) and end (`$`) without any characters in between, indicating an empty line, and deletes (`d`) them.
  3. `grep -v “#”` – Excludes lines containing the ‘#’ character, commonly used for comments in config files. The `-v` flag inverts the match, showing only lines that do not contain the specified character.
  4. `more` – Paginates the output, making it easier to read through.

Customizing for Different Comment Styles

Different configuration files might use various characters for comments, such as semicolons (`;`). You can easily adapt the command to suit these differences. Simply replace the `#` in the `grep -v “#” ` section with the relevant comment character. For instance, to exclude lines with semicolons, you would use `grep -v “;”`.

This method offers a quick and customizable way to view the essential contents of configuration files, free from the usual distractions of comments and blank spaces.

Efficiently Logging Your Linux Console Activities

Efficiently Logging Your Linux Console Activities

Working with the Linux console regularly demands a systematic approach to logging your activities. This practice isn’t just about keeping a record; it’s crucial for analyzing past actions and generating detailed reports. Here’s how you can achieve this with ease.

1. Understanding the `history` Command

Firstly, the Linux environment offers the `history` command, a handy tool to view your command history. However, it has a limitation: it only displays the commands, not their outputs. This is where the `script` command comes into play.

2. Harnessing the `script` Command for Comprehensive Logging

For a more robust logging solution, I prefer using:

script -a /path/to/file.log

As soon as you execute this command, it initiates a logging session, saving all console activities to the specified file. You’ll see a confirmation message:

Script started, file is /path/to/file.log

Now, every action in the console, including outputs, is being logged. This is invaluable for later review and reporting.

3. Finalizing Your Logging Session

To conclude your logging session, simply exit the console as you normally would. Upon exiting, you’ll notice a message:

Script done, file is /path/to/file.log

This message signifies that your logging is complete, and all your activities are safely stored in the specified log file.

Conclusion

With this method, you now have a comprehensive log of your console session, a resource that can prove invaluable for future reference and analysis. Happy logging! 😉

How to clear the history of a Git repository from a PHP file?

In order to completely remove a PHP file from Git including its commit history, you need to follow this sequence of actions:

Make sure that you have created a working backup of the file you are going to delete and checked that you can restore from it.
Delete the file from the repository. You can do this with the following command:

git rm --cached path/to/file.php

This will save the file to a local copy of your file system and remove it from the Git repository.
Instead of path/to/file.php, specify the actual path to the PHP file you want to delete.

Commit the file deletion with the command:

git commit -m "Removed path/to/file.php from Git history"

Rewrite the Git history and remove all references to the file with the command:

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path/to/file.php' --prune-empty --tag-name-filter cat -- --all

Instead of path/to/file.php, specify the actual path to the PHP file you want to delete.

Perform the cleanup with the command:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

Perform a push with the force flag with the following command:

git push origin --force --all

This command overwrites a remote Git repository with an updated history, so make sure you have a backup and don’t overwrite your important changes with it.

Please be careful when overwriting the history of a Git repository, especially if you are collaborating with other developers. Also, inform all members of your team about the upcoming changes and coordinate with them.

PHP composer show view installed libraries

Composer logo

To effectively manage and view the list of all PHP libraries installed in your project using Composer, you can utilize the following command:

composer show | more

Adding the | more part to the command is particularly helpful when dealing with extensive lists. This suffix enables you to paginate the output, making it easier to read and navigate, especially when the list extends beyond the size of one screen.

For more targeted searches, especially when you need to find a specific library, you can filter the output using this command:

composer show | grep -i "packageName"

Here, | grep -i “packageName” serves as a powerful tool. It performs a case-insensitive search within your library list, allowing you to quickly locate any library fragment by name. This feature is incredibly useful for swiftly pinpointing the exact library you’re interested in, amidst a potentially large collection of installed libraries.

Dealing with Binary Files in the Console: A Quick Fix for Common Issues – using reset command

Viewing binary files through the console can occasionally lead to unexpected and disruptive issues. You might have experienced situations where the console seems to ‘break’— displaying strange characters, replacing normal text with incomprehensible hieroglyphs, and even triggering unexpected sound alerts or bizarre color blends. These anomalies can render the console practically unusable, disrupting your workflow.

Fortunately, there’s a simple solution to this problem. Whenever you encounter these disruptions, just enter the command:

reset

This command effectively resets your console, clearing any irregularities and restoring its normal functionality. It’s a quick and efficient way to tackle the chaos caused by binary file interactions and ensures you can continue your work without major interruptions.

Remember, while the reset command is powerful, it’s always good practice to understand the nature of the files you’re dealing with. Use appropriate tools and commands for viewing or editing binary files to minimize these occurrences.

Exploring Array Syntax in Shell Scripting: A C-Like Approach

Shell scripting provides a versatile way to automate tasks in Unix-like systems. For programmers familiar with C or languages with similar syntax, the following pattern can be particularly intuitive:

#!/usr/bin/env sh

arr=('foo' 'bar' 'baz')

for ((i=0; i < ${#arr[@]}; i++)); do
    echo "arr[${i}]: ${arr[i]}"
done

In this script, we define an array named arr with three string elements: ‘foo’, ‘bar’, and ‘baz’. We then iterate over the array using a for loop that closely resembles the syntax in C.

The loop utilizes a C-style for-loop syntax to iterate over the array indices. With i as the index, the loop runs as long as i is less than the length of the array, which we obtain with ${#arr[@]}. Within the loop, each element of the array is accessed via ${arr[i]} and printed out alongside its index.

This syntax provides a familiar structure for those accustomed to C-like languages, offering a seamless transition to writing shell scripts.

Linux find search for files changed in date range

Introduction

In this quick guide, we’ll explore some useful bash commands to find files modified within a specific time frame. This is especially useful for system administrators and developers.

Finding Files Modified Today

To find all files in a directory that were modified today, you can use the following command:

find /dir -type f -mtime -1

This command searches for files (`-type f`) in the `/dir` directory that have been modified in the last 1 day (`-mtime -1`).

Finding Files Modified Exactly N Days Ago

If you need to find files changed exactly N days ago, use:

find /dir2 -type f -mtime n

Replace `n` with the number of days ago you are interested in.

Deleting Files Modified More Than M Days Ago

To find and delete files modified more than M days ago, proceed with caution and use:

find /dir3 -type f -mtime +m -exec rm -fv {} \;

Warning: This command will delete files. Use it carefully.

Conclusion

These are just a few basic but powerful ways you can manage your file system more effectively using bash commands.

Some useful composer options we are use for production Docker setup in Laravel apps

Composer logo

As a part of our Docker production setup of Laravel web application we are using the following composer install with some options described below:

  • –optimize-autoloader or -o: This flag tells Composer to optimize the autoloader for better performance. Autoloading is a mechanism in PHP that automatically loads classes when they are needed. Optimizing the autoloader can make your application load faster by reducing the number of file operations required to load classes.
  • –no-dev: This flag tells Composer not to install the development dependencies specified in the `composer.json` file. Development dependencies typically include things like testing libraries, code analysis tools, and other packages that are not needed in a production environment.
  • –prefer-dist: This flag tells Composer to prefer installing package distributions (i.e., ZIP or TAR archives) over the source code from version control repositories. Using distributions can be faster and more efficient because it avoids the need to compile or build the packages from source.

So, when you run

composer install --optimize-autoloader --no-dev

, you are installing the project’s production dependencies while optimizing the autoloading process for better performance and excluding any development-specific dependencies. This is commonly used when preparing a PHP project for deployment to a production server.

We hope this information will help you to prepare your Laravel application for production environment.

The full list of all possible options you could check at composer documentation page.