How to use the nice command to change process priority in Linux

Introduction

In the world of Linux, managing process priorities is crucial for optimal system performance. The ‘nice’ command is a built-in utility in Linux that allows you to execute commands with altered priorities. Understanding how to use this tool effectively can significantly enhance your system management skills.

Understanding the ‘nice’ Command

The ‘nice’ command in Linux is used to run a program with modified scheduling priority. This is particularly useful when you want to allocate more or less CPU resources to a specific process.

How to Use the ‘nice’ Command

The basic syntax of the ‘nice’ command is as follows:

nice -n N command

Here, `N` represents the niceness level, ranging from -20 (highest priority) to 19 (lowest priority). By default, if you don’t specify the niceness level, the process is set to priority 10.

Example of ‘nice’ Command in Action

Let’s say you want to start a backup process with a lower priority. You could use:

nice -n 15 backup_script.sh

This command will start `backup_script.sh` with a lower CPU priority, allowing other critical tasks to take precedence.

Tips for Using ‘nice’

  • Use positive values for less critical tasks.
  • System administrators may use negative values for high-priority system processes.
  • Be cautious with setting very high or low priorities, as it might affect system stability.

Conclusion

The ‘nice’ command is a powerful tool in Linux for process priority management. By understanding its usage, you can effectively control how your system allocates resources to different processes.

Have you used the ‘nice’ command in your Linux experience? Share your thoughts and tips in the comments below!

How to Extract MP3 Audio from AVI or WebM Videos Using FFmpeg

Introduction:

What You Will Need: a computer with FFmpeg installed. I’m providing you with a link with instructions on how to install FFmpeg on Linux, Windows or MacOS just in case if it’s not already installed.

Step-by-Step Guide:

  1. Open the Terminal or Command Prompt:
    – I think there is no need to explain you on how to open Terminal on macOS/Linux or Command Prompt on Windows if you visit my website. ;) I believe in your solid technical skills.
  2. Navigate to the Video File’s Directory:
    – Use `cd` command to navigate to the folder containing the video file you need.
  3. Run the FFmpeg Command:
    – We are presenting the command:

    ffmpeg -i sample.avi -q:a 0 -map a sample.mp3
    

    – The command options description goes below:
    – `-i sample.avi`: Specifies the input file.
    – `-q:a 0`: Sets the audio quality. 0 is the highest quality.
    – `-map a`: Maps only the audio tracks.
    – `sample.mp3`: The name of the output MP3 file.

  4. Execute the Command:
    – Do not forget to replace `sample.avi` and `sample.mp3` with file name and extensions you need.
    – You should see in the Terminal or Command Prompt as the command runs the example output as it shown below:
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
...
Stream mapping:
  Stream #0:1 -> #0:0 (opus (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'sample.mp3':
  Metadata:
    COMPATIBLE_BRANDS: iso6mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    TSSE            : Lavf58.76.100
  Stream #0:0(eng): Audio: mp3, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 01:04:10.261000000
      encoder         : Lavc58.134.100 libmp3lame
size=  117368kB time=01:04:10.25 bitrate= 249.7kbits/s speed=69.9x    
video:0kB audio:117367kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000275%

What string set -Eeuo pipefail in shell script does mean?

The string `set -Eeuo pipefail` is a command used in shell scripts, particularly in Bash, to modify the behavior of the script for better error handling and debugging. Here’s what each component means:

  1. `set`: This is a shell builtin command that sets or unsets shell options and positional parameters.
  2. `-E`: This option ensures that the `ERR` trap is inherited by shell functions, command substitutions, and commands executed in a subshell environment. This enhances the error handling capabilities of the script.
  3. `-e`: The `-e` option causes the script to exit immediately if any command it runs exits with a non-zero status (i.e., it encounters an error). This prevents errors from snowballing and makes it easier to pinpoint where a script is failing.
  4. `-u`: This option treats unset variables and parameters other than the special parameters “@” and “*” as an error when performing parameter expansion. If you try to use a variable that hasn’t been set, the script will exit, which helps catch typos and other mistakes.
  5. `-o pipefail`: The `-o` option sets various shell attributes. `pipefail` is an option that affects pipelines, which are sequences of commands connected by pipes (`|`). With `pipefail` enabled, the return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status. This makes it easier to detect failures in any part of a pipeline and is particularly useful for debugging and ensuring correct script behavior.

In summary, `set -Eeuo pipefail` is a common set of options used in shell scripts to make them more robust and less prone to common errors. It’s especially useful in scripts where correct error handling and script behavior are critical.

Converting Adobe Photoshop PSD Files with CMYK color scheme to RGB for best compatibility for GIMP in Linux

Recently, I encountered a challenging issue while working with graphic files: I was unable to open files created in Adobe Photoshop (with a CMYK color scheme) using the GIMP editor on Linux. This compatibility problem is not uncommon for graphic designers and developers who work across different operating systems and software.

To overcome this hurdle, I discovered a practical solution that involved converting the Photoshop PSD files to a more universally compatible format. I achieved this by utilizing the powerful ImageMagick’s convert utility. The process is straightforward and efficient. Here’s how I did it:

  1. Open your terminal in Linux.
  2. Use the following command:
convert file.psd -colorspace rgb file.png

This command effectively changes the color space from CMYK (used in Photoshop) to RGB, which is more compatible with a wide range of software, including GIMP. The conversion to PNG format ensures that the file retains its quality and is readily accessible in GIMP.

It’s a simple yet effective workaround for those who frequently work with graphic files across different platforms. This method has significantly eased my workflow, and I hope it helps others facing similar challenges.

How to Exclude Files from Git Tracking Without Using .gitignore

Git

How to Exclude Files from Git Tracking Without Using .gitignore

Introduction:

In certain scenarios, you might find yourself needing to stop Git from tracking changes in specific files without adding them to `.gitignore`. This situation often arises with local configuration files, which you do not wish to share with others but still need to keep in your repository. In this post, we’ll explore how to achieve this using the `git update-index` command.

The Command:

To tell Git to ignore changes to a specific file, you can use the `–assume-unchanged` flag with `git update-index`. This command is helpful when you want to keep a file in your repository but stop tracking any changes to it. Here’s how you do it:

git update-index --assume-unchanged path/to/file

Replace `path/to/file` with the actual path to the file you want to ignore. Once you run this command, Git will no longer track changes to the file.

Reverting Back:

If, at some point, you decide you want to start tracking changes to the file again, you can reverse the process. Use the `–no-assume-unchanged` flag to tell Git to resume tracking changes:

git update-index --no-assume-unchanged path/to/file

Again, replace `path/to/file` with the relevant file path.

Conclusion:

Using `git update-index` with the `–assume-unchanged` and `–no-assume-unchanged` flags provides a flexible way to manage file tracking in Git. It’s particularly useful for files like local configurations, where you need them in your repository but don’t want their changes to be constantly tracked. Remember, this is a local operation, and these changes are specific to your repository and do not impact other collaborators.

What is the difference between print in echo in PHP?

In PHP, both `print` and `echo` are used for outputting data to the screen, but there are some differences between them:

  1. Type:
    • `echo` is a language construct, not really a function, so you can use it without parentheses. It can take multiple arguments.
    • `print` is also a construct but behaves more like a function and always returns 1. It can take only one argument.
  2. Return Value:
    • `echo` does not return any value. It just outputs the arguments.
    • `print` always returns 1, so it can be used in expressions.
  3. Performance: `echo` is marginally faster than `print` because it doesn’t have a return value to check. However, this speed difference is extremely negligible and usually not a factor in choosing one over the other.
  4. Usage in Expressions:
    • Since `echo` does not return a value, it cannot be used in expressions. For example, you cannot use `echo` within a complex expression or inside a function expecting a value.
    • `print`, with its return value of 1, can be used in expressions.
  5. Argument Handling:
    • `echo` can take multiple parameters, separated by commas, to output. For example, `echo $str1, $str2;`.
    • `print` can only take one argument. To output multiple strings, you would need to concatenate them first.

In practical terms, the differences are quite minor, and choosing between `echo` and `print` usually comes down to personal preference or specific needs in certain situations (like needing a return value or handling multiple arguments). In most cases, they are interchangeable.

Merging MP3 Files Seamlessly with FFmpeg

It could be useful to merge a number of mp3 files into one using the following command:

ffmpeg -i "concat:file1.mp3|file2.mp3|file3.mp3|file4.mp3|file5.mp3|file6.mp3|file7.mp3|file8.mp3" -acodec copy _all_files_combined.mp3

The command above will combine mp3 files listed in -i option into one large audio file called _all_files_combined.mp3.

Check out video tutorial version located on my Youtube channel Linux Tutorials at: https://youtu.be/1YM_G7nPQo4

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.