All Distros, Bash

Remove all except specific files

This is one way to remove all files but a few of them you want to keep.

First of all, enable bash’s extended globbing features by running:

$ shopt -s extglob

Then you should be able to perform the following commands, for example:

*** Remove all files but a few in a directory ***

$ rm !(file1|file2|fileN)

For directories:

$ rmdir !(file1|fileN)  <-- if dirs are empty, of course

or

$ rm -rf !(file1|fileN)

Examples:

rm_but_a_few

 

Advertisements
Fedora/RHEL, Ubuntu

Solve VirtualBox Guest Additions Autorun issue on Fedora 26 and Debian 9

Install VirtualBox Guest Additions on Fedora 26

NOTE: If your user does not have root rights, add it to the root group (probably ‘wheel’).

$ dnf update kernel*

$ sudo reboot

After it boots up again, the guest additions should be up and running, and you should be able to see your desktop on full screen.

Install VirtualBox Guest Additions on Debian Stretch (9)

Insert the VBoxGuestAdditions CD image from the ‘Devices’ menu. (I had a lot of mount issues with this). It should automatically mount and automatically run.

If it does not autorun, you have to manually run the VBoxLinuxAdditions.run script. First solve any root permissions you may have (adding $USER to the visudo file should solve it: insert a line right after ‘root’ with your user name). Then go to /media/cdrom0 or wherever your VBoxGuestAddition.iso is mounted and issue:

# bash VBoxLinuxAdditions.run

It should run seamlessly if everything is OK.

You are welcome to share your comments.

Cheers

Atom

Hide .gitignore files in Atom Tree view

I have been using Atom as my coding editor for almost a year now. As a Python developer, the *.pyc files often bugged me whenever I ran my code.

Adding those files in the .gitgnore file did not prevent them to show up in the tree view and hiding them from the Atom menus was not as much intuitive as I would guess at first, so I had to dig a little further.

To hide all files included in the .gitignore file:

atom_hide_3
Image1: find the ‘tree-view’ package and press Settings to enter the preferences of the package.
atom_hide_1
Image2: Scroll down to see the preference options.
atom_hide_2
Image3: Check the “Hide VCS ignored files” checkbox.

You may now see your tree view free of unwanted files.

I hope it helps.

Cheers!

Git

Git basics: create and delete branches remotely

To create a remote branch:

$ git checkout -b <branch-name>
$ git push -u origin <branch-name>

To delete a remote branch:

$ git push origin --delete <branch-name> 
$ git branch -d <branch-name>

If there are unmerged changes which you are confident of deleting:

$ git branch -D <branch-name>

And that is all.

Cheers!

Git

Git diff between branches

There are several ways of displaying the differences between your git branches. The simplest ones being a little fuzzy and, if the diff is big enough, not very helpful. But thre are some tweaks we can make. The one I’ve been using mostly in the past weeks is:

$ git log --oneline --graph --all --decorate

Well, like always, when I see too big a command like this, I create an alias for it.
Anyway, this will print the current layout of all branches of your project, including the remote ones.

For example:

git_tree

It produces a cool graph layout with the commit title only, and it’s a very useful way to understand where things are in your project. However, if it’s really a big project, it might be too overwhelming.

I created some new branches with new commits and got a little more elaborated tree, like this:

git_tree_2.png

For the graph above, I created a new branch “bug883” from “develop” and “sample_branch” from “master”. None of them was pushed remotely, as we can tell by the output.

Well, give it a try and share what you think.

Cheers!

UPDATE: Many other ways of getting the diffs are discussed in this Stack Overflow post.

Python

Merge two dictionaries in Python (2.7)

Disclaimer:
This matter is extent. There are a number of different ways to do the same thing. My goal is to have an easy-to-do shortcut at hand to use it when I need it. See a link below for the source article with more information and discussion on the topic.

Dictionaries to be merged into a third one, z:

>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}

To merge them:

>>> z = x.copy()
>>> z.update(y)

which returns:

>>> z
{'a': 1, 'b': 3, 'c': 4}

Note that the value of 'b' was the last one updated, coming from the y dictionary.

For further details on this topic, please visit the source article at Stack Overflow.

Thanks!

Fedora/RHEL, Packages, Ubuntu

List gpg keys in your system

1.1. RPM based distros (RHEL, CentOS, Fedora)

Import:

$ sudo rpm --import repomd.xml.key

List:

$ rpm -q gpg-pubkey|sort

Getting info:

rpm -qi  | grep Summary   

(or without the ‘grep’ part to view full info of the key package)

To remove:

sudo rpm -e --allmatches 

Example:

[root chip ~]# rpm -q gpg-pubkey|sort
gpg-pubkey-1ac70ce6-41bebeef
gpg-pubkey-30c9ecf8-3f9da3f7
gpg-pubkey-4f2a6fd2-3f9d9d3b
gpg-pubkey-8df56d05-3e828977
gpg-pubkey-8df56d05-3e828977
gpg-pubkey-a109b1ec-3f6e28d5
gpg-pubkey-db42a60e-37ea5438
gpg-pubkey-e42d547b-3960bdf1
[root chip ~]# rpm -e --allmatches gpg-pubkey-8df56d05-3e828977
[root chip ~]# rpm -q gpg-pubkey|sort
gpg-pubkey-1ac70ce6-41bebeef
gpg-pubkey-30c9ecf8-3f9da3f7
gpg-pubkey-4f2a6fd2-3f9d9d3b
gpg-pubkey-a109b1ec-3f6e28d5
gpg-pubkey-db42a60e-37ea5438
gpg-pubkey-e42d547b-3960bdf1

1.2 Debian / Ubuntu

$ gpg --list-keys user_ID

Example:

$ gpg --list-keys
/home/iplsdk/.gnupg/pubring.gpg
-------------------------------
pub   2048R/E5****A4 2015-01-12
uid                  IBM Software Development Kit for Linux on Power
sub   2048R/F2*****9 2015-01-12

pub   4096R/78C*****4 2016-02-11
uid                  IBM SDK for Linux on Power (The IBM Software Development Kit for Linux on Power is a free Eclipse-based Integrated Development Environment) <iplsdk@linux.vnet.ibm.com>
sub   4096R/B3*****0 2016-02-1

Add a gpg-key in your environment:

After you get the .gpg file, you need to 'add-apt-key' it into your keyring. Here:
$ sudo apt-key add .gpg

Don’t forget:
$ sudo apt-get update

Cheers!