megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

How to identify which Linux distribution is running in your system?

If you've read my earlier post, I was using Ubuntu, at that time, and I thought that was how you identify which distro is running in your system but using lsb_release -a is not always going to work as some distributions may not have it installed.

Try the following command to identify the distribution you are running in your system:

cat /etc/os-release

And you'll get the following output:

NAME="XXXX"
ID="XXXX"
DISTRIB_ID="XXXX"
PRETTY_NAME="XXXX"

Hope this helps you out!

Display list of files with their extension and file sizes

Last month, I was trying to free up some space in my company servers and I realized that there were a lot of .zip files taking up a lot of space.

So, I wrote the following command to list files by their extension:

find . -iname \*.extension -exec du -sh {} \; > file-list.txt

And later to determine which files are the largest, I executed this command to sort the list by file size:

sort -rh file-list.txt > newfile.txt

Hope this tip helps you too! 😄

View your wget progress even after closing your SSH session

Few days back, I ran a wget command to download a file and accidentally closed the SSH client.

I logged in again and checked the list of active processes and thankfully, the wget process was still running except that I wasn't able to know the current progress of it.

So, I did a little research and found a way to view the progress, so I tried the following command:

tail -f wget_log

And, I was able to view it's current download progress again.

Hope this helps you out!

How to use GitHub Personal Access Token to authenticate your git commits?

On November 2020, GitHub had announced that they would no longer accept basic username/password to authenticate git commits and it would be deprecated by Mid 2021.

Instead, they recommend you to authenticate your git commits using a Personal Access Token from your GitHub account.

Generate a personal access token

  1. Unset your credentials from your remote repository: git config --local --unset credential.helper
  2. Login to your GitHub account and go to Settings
  3. Then navigate to Developer Settings -> Personal Access Tokens
  4. Click on Generate new token
  5. Give a name to your Personal Access Token and the necessary permissions required.
  6. Once done, hit on Generate token
  7. The token will be shown once, so make sure to copy it and store it somewhere that you can remember.

Update your remote repository

Once you've generated a token, you just have to update your remote repository by following the below steps:

1. Update remote repository URL

git remote set-url origin https://<your_access_token>@github.com/<your_git_repo_url>

2. Just git pull once

Now, just perform git pull operation once and you should be good:

git pull https://<your_access_token>@github.com/<your_git_repo_url>

Hope this helps you out!

How to fetch a JSON object from an array of JSON objects by property value?

Say, for example, you have an array of JSON objects that contains the following data:

var arrOfObjects = [
    {
        name: "John Doe",
        age: 20,
        email: "john.doe@email.com"
    },
    {
        name: "Bob Smith",
        age: 56,
        email: "bob.smith@email.com"
    },
];

Hmm, that's pretty basic but how will get the information of "Bob Smith" using his email? Well, that's where the Array.find() method comes into the picture.

Try reading the Mozilla Documentation on Array.prototype.find() and implement the following:

var key = "email";
var valueToFind = "bob.smith@email.com";
var result = arrOfObjects.find(obj => {
    return obj[key] == value;
});

//This should give you the record of Bob Smith.
console.log(result) 

This works fine on all browsers except Internet Explorer (I mean, it sucks anyways!), which is okay!

Hope you found this useful!

How to update values of a column from one table to another?

Sometimes, I come across situations where I would need to update a newly added in a pivot table and this query does come in handy:

UPDATE table_a INNER JOIN table_b ON table_b.col_x = table_a.col_x SET table_a.col_y = table_b.col_y;

Hope you found this useful too!

How to switch between integrated terminal and editor in Visual Studio Code?

While, I'm trying to adjust myself to using Visual Studio Code, I found it quite annoying that there isn't a shortcut to switch focus between the editor and the integrated terminal. For a guy, like me, who makes use of the keyboard all the time, that's pretty important.

I did some research and found a way on how to do it. Just type Ctrl+Shift+P and type Open Keyboard Shortcuts and add these lines:

[
    {"key": "ctrl+`", "command":"workbench.action.terminal.focus"},
    {"key": "ctrl+`", "command":"workbench.action.focusActiveEditorGroup", "when": "terminalFocus"},
]

Save the file and now, you'll be able to switch between the two by pressing Ctrl+` keys.

Hope you found this tip useful!

Redirect from HTTP to HTTPS in Apache VirtualHosts

Here's a simple technique on how I learned to redirect a site from HTTP to HTTPS automatically using Apache's VirtualHost configuration.

Go to your configuration file or 000-default.conf and modify your configuration to something like this:

<VirtualHost *:80>
    ServerName your.domain.com
    Redirect permanent / https://your.domain.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName your.domain.com
    SSLEngine On
    # insert code here...
</VirtualHost>

Save the file and check if the configuration is correct before your restart the server:

sudo apachectl configtest

If you get the message, Syntax OK, then go ahead and restart the server:

sudo systemctl restart apache2

Now, your visitors will be redirected from HTTP to HTTPS automatically!

Hope you found this tip useful!