GNU/Linux


GNU/Linux find: Get results relative to the directory searching in, instead of directory shell is in

Recently we wanted to create a list of files that could be found in a specific folder.
For that list we wanted the paths of the files to be relative to the folder we were searching in, instead of them being relative to the folder our shell was currently in.

To achieve that, we used cd to navigate into that folder and searched from there locally.
We used a sub-shell to achieve this, which was not needed, but because we did not want to change the current directory of our shell, it was needed.

The command was as follows:

(cd toThe/Path/WeAre/Interested/In && find .)

instead of:

find toThe/Path/WeAre/Interested/In

Since we were interested in getting all files, we did not put any filters on find.
Of course you can use find normally and modify it as you please.

Finally, since we wanted the list of files to be saved in a text file, we redirected the output of the above command to a file in the current working directory

(cd toThe/Path/WeAre/Interested/In && find .) > interestingFiles.txt

Copy a symbolic link without traversing it

Recently we needed to copy a Symbolic Link on a disk image we would deploy on an embedded device.
For this reason it was important for us to copy the Symbolic Link and not the file it was pointing to as that link would become valid once the machine would boot from the image.

To achieve that we used -P which instructs cp to never follow symbolic links in source. In other words it would not traverse the symbolic link and copy the symbolic link itself.

Notes:

  • --no-dereference is the same as -P.
  • -P uses a capital P.

 


Delete all files and keep the directory structure

Scenario

You have a complex folder structure and you want to remove all files and at the same time keep all folders intact.

We will present one method, using two variations of it that can achieve the above.
The method uses the GNU find command to find all files and delete them one by one.

Variation A

find . ! -type d -exec rm '{}' \;

This above command will search in the current directory and sub-directories for anything that is not a folder and then it will delete them.

  • find . – Searches in this folder, since we did not define depth, it will search in all sub-folders as well
  • ! -type dtype d instructs find to match all Directories, by adding the ! in front of the instruction it negates the result and instructs find to match anything but the Directories
  • -exec rm '{}' \; – for every result, the command after exec is executed. The filename replaces '{}' so that the results get deleted one by one.

Variation B

find . ! -type d -delete

In this example, we replaced -exec rm '{}' \; with the simpler to remember directive of -delete.


Installing Jenkins on Red Hat (CentOS 7 64bit) distributions

Following the official guides:

We tried to install Jenkins using the RPM repositories.

sudo yum install java -y;
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo;
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key;
sudo yum install jenkins -y;

Unfortunately, that resulted in an error:

warning: /var/cache/yum/x86_64/7/jenkins/packages/jenkins-2.19.2-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY

Public key for jenkins-2.19.2-1.1.noarch.rpm is not installed

Apparently, sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key; failed silently and it did not import the key.

To verify, we executed rpm -qa gpg-pubkey* to display a list of all keys installed for RPM verification. From that list we wanted to see if any of the keys was the one needed by jenkins which should end with the value d50582e6. Since none of them matched, we tried to manually re-import it which failed again.

Our Solution

Our solution, although ugly, was to disable  PGP verification in the file /etc/yum.repos.d/jenkins.repo.

[jenkins]
name=Jenkins-stable
baseurl=http://pkg.jenkins.io/redhat-stable
gpgcheck=0

That was enough to allow us to install the package using:

sudo yum install jenkins -y;

Finally, we started jenkins using sudo service jenkins start;.

Logs from failed installation


[bytefreaks@localhost ~]$ sudo yum install jenkins -y
[sudo] password for bytefreaks:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.coreix.net
* extras: mirrors.coreix.net
* updates: mirrors.coreix.net
Resolving Dependencies
--> Running transaction check
---> Package jenkins.noarch 0:2.19.2-1.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================================================================================
Package                                                Arch                                                  Version                                                     Repository                                              Size
=======================================================================================================================================================================================================================================
Installing:
jenkins                                                noarch                                                2.19.2-1.1                                                  jenkins                                                 66 M

Transaction Summary
=======================================================================================================================================================================================================================================
Install  1 Package

Total size: 66 M
Installed size: 67 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/jenkins/packages/jenkins-2.19.2-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY


Public key for jenkins-2.19.2-1.1.noarch.rpm is not installed