Jekyll2023-06-01T22:49:49+00:00https://linternux.github.io/atom.xmlLINternUXTips & Tricks for SysAdminJacques Duplessislinternux@gmail.comSearch and replace multiple files2021-04-13T09:00:00+00:002021-04-13T09:00:00+00:00https://linternux.github.io/tips_and_tricks/search-replace-multiple-files<p>At one point, you may be facing a situation where you would want to do an in place replacement of
one string by another in multiple files. In this article, we will demonstrate a couple a way to
do it, one solution using ‘perl’ and a second using ‘sed’.</p>
<!--more-->
<p>To demonstrate and visualize the replacement, we will use two text files, ‘file1.txt’ and ‘file2.txt’.
Let’s see the content of these two files before we begin our example.
We will use the ‘paste’ command to show the file1.txt and file2.txt side by side.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
ABC Robin
Batman robin
Robin batcave
Joker joker
Batcave abc
</code></pre></div></div>
<p><br /></p>
<h2 id="string-replacement-using-perl">String replacement using ‘perl’</h2>
<p class="text-justify">Let’s begin with our first string replacement in multiple example using ‘perl’. In the example
below we are replacing every occurrence of ‘Robin’ with ‘Boy Wonder’ in all “*.txt” files in
current path. You may want to do a backup of your files before running this command, because
the string replacement will be done directly within each files (Or use our second example that
do a backup of files changed). Please note, that this search and replace is case sensitive
and that the string ‘robin’ was not replaced.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
ABC Robin
Batman robin
Robin batcave
Joker joker
Batcave abc
<span class="nv">$ </span>perl <span class="nt">-pi</span> <span class="nt">-e</span> <span class="s2">"s/Robin/Boy Wonder/g;"</span> <span class="k">*</span>.txt
<span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
ABC Boy Wonder
Batman robin
Boy Wonder batcave
Joker joker
Batcave abc
</code></pre></div></div>
<p><br /></p>
<h2 id="string-replacement-using-perl-with-preserving-original">String replacement using ‘perl’ with preserving original</h2>
<p>In this example, we replace the string “Robin” with “BatGirl” in all text files (*.txt), but
this time the original file are preserve as a ‘.bak’ files.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
ABC Robin
Batman robin
Robin batcave
Joker joker
Batcave abc
<span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-l</span>
total 8
<span class="nt">-rw-rw-r--</span> 1 jacques sadmin 41 Apr 12 11:38 file1.txt
<span class="nt">-rw-rw-r--</span> 1 jacques sadmin 43 Apr 12 11:38 file2.txt
<span class="nv">$ </span>perl <span class="nt">-p</span> <span class="nt">-i</span><span class="s1">'.bak'</span> <span class="nt">-e</span> <span class="s2">"s/Robin/BatGirl/g;"</span> <span class="k">*</span>.txt
<span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
ABC BatGirl
Batman robin
BatGirl batcave
Joker joker
Batcave abc
<span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-l</span>
total 16
<span class="nt">-rw-rw-r--</span> 1 jacques sadmin 38 Apr 12 10:30 file1.txt
<span class="nt">-rw-rw-r--</span> 1 jacques sadmin 36 Apr 12 10:26 file1.txt.bak
<span class="nt">-rw-rw-r--</span> 1 jacques sadmin 40 Apr 12 10:30 file2.txt
<span class="nt">-rw-rw-r--</span> 1 jacques sadmin 38 Apr 12 10:26 file2.txt.bak
</code></pre></div></div>
<p><br /></p>
<h2 id="string-replacement-in-multiple-file-using-sed">String replacement in multiple file using ‘sed’</h2>
<p>In this example, we demonstrate hot to do an in place string replacement using the ‘sed’ command.
The example shown below replace the string ‘ABC’ with ‘XYZ’. This replacement is case sensitive,
notice the first line of file1.txt was not replace (See next example for case-insensitive
replacement).</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
ABC Robin
Batman robin
Robin batcave
Joker joker
Batcave abc
<span class="nv">$ </span><span class="nb">sed</span> <span class="nt">-i</span> <span class="s1">'s/ABC/XYZ/g'</span> file<span class="k">*</span>
<span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
abc Batman
XYZ Robin
Batman robin
Robin batcave
Joker joker
Batcave abc
</code></pre></div></div>
<p>If we take the same command and add a ‘i’ at the end of the ‘sed’ command, this make the search
and replace case insensitive.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">sed</span> <span class="nt">-i</span> <span class="s1">'s/ABC/XYZ/gi'</span> file<span class="k">*</span>
<span class="nv">$ </span><span class="nb">paste </span>file1.txt file2.txt
XYZ Batman
XYZ Robin
Batman robin
Robin batcave
Joker joker
Batcave XYZ
</code></pre></div></div>Jacques Duplessislinternux@gmail.comAt one point, you may be facing a situation where you would want to do an in place replacement of one string by another in multiple files. In this article, we will demonstrate a couple a way to do it, one solution using ‘perl’ and a second using ‘sed’.Identify physical network port name2021-03-30T10:30:02+00:002021-03-30T10:30:02+00:00https://linternux.github.io/network/identify-physical-network-port-name<p>The ‘ethtool’ is a command line utility used to query and control network device driver and
hardware settings, particularly for wired Ethernet devices. In our example, we will use it to flash
the light on the back of a card, this will be useful to identify the device name that belong to
what port on the back of you Linux computer.</p>
<!--more-->
<p><br />
<img src="/assets/img/sadm_network_light.jpg" class="align-center" alt="" /></p>
<center>
Image by <a href="https://pixabay.com/users/martinelle-495565/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=1572617">Martinelle</a> from <a href="https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=1572617">Pixabay</a>
</center>
<p><br /></p>
<p>Let’s say you have two network cables plug in on the back of a
server and you want to know which one is eth0. Use the following command and the interface light
will start blinking (for 10 seconds) on the chosen adapter.
<br /></p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># ethtool -p eth0 10 </span>
</code></pre></div></div>
<p>This command can be executed without disturbing the traffic on the interface.</p>Jacques Duplessislinternux@gmail.comThe ‘ethtool’ is a command line utility used to query and control network device driver and hardware settings, particularly for wired Ethernet devices. In our example, we will use it to flash the light on the back of a card, this will be useful to identify the device name that belong to what port on the back of you Linux computer.Resetting file permission with rpm2021-03-29T10:34:00+00:002021-03-29T10:34:00+00:00https://linternux.github.io/sysadmin/resetting-file-permission-with-rpm<p><br />
Did you know that you can reset file permission to what they were when you installed the package. This may become helpful if somebody made a mistake and change the permission on a number of files (This never happen right).</p>
<!--more-->
<p>This is where the command “rpm –setperms <package>” become handy.</package></p>
<p>In our example we will change the permission on the “find” command.<br />
Let’s find out what package contains the “/usr/bin/find” command.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># rpm -qf /usr/bin/find</span>
findutils-4.2.27-5.el5
</code></pre></div></div>
<p>Let’s see what are the actual permission of the find program (755).</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># ls -l /usr/bin/find</span>
<span class="nt">-rwxr-xr-x</span> 1 root root 151244 Jul 7 2008 /usr/bin/find
</code></pre></div></div>
<p>Let’s change them to 777 with the chmod command</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># chmod 777 /usr/bin/find</span>
</code></pre></div></div>
<p>Now the program “find” have this permission.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># ls -l /usr/bin/find</span>
<span class="nt">-rwxrwxrwx</span> 1 root root 151244 Jul 7 2008 /usr/bin/find
</code></pre></div></div>
<p>Let’s run our command to reset all files included in the package “findutils”.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># rpm --setperms findutils</span>
</code></pre></div></div>
<p>You can see now that the permission of the ‘find’ are back to their initial state.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># ls -l /usr/bin/find</span>
<span class="nt">-rwxr-xr-x</span> 1 root root 151244 Jul 7 2008 /usr/bin/find
</code></pre></div></div>
<p>Be aware that this command only reset the permission and not the owner/group of the file, as you can see from the example below.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># ls -l /usr/bin/find</span>
<span class="nt">-rwxr-xr-x</span> 1 root root 151244 Jul 7 2008 /usr/bin/find
<span class="c"># chmod 777 /usr/bin/find</span>
<span class="c"># chown jacques.jacques /usr/bin/find</span>
<span class="c"># ls -l /usr/bin/find</span>
<span class="nt">-rwxrwxrwx</span> 1 jacques jacques 151244 Jul 7 2008 /usr/bin/find
<span class="c"># rpm --setperms findutils</span>
<span class="c"># ls -l /usr/bin/find</span>
<span class="nt">-rwxr-xr-x</span> 1 jacques jacques 151244 Jul 7 2008 /usr/bin/find
</code></pre></div></div>Jacques Duplessislinternux@gmail.comDid you know that you can reset file permission to what they were when you installed the package. This may become helpful if somebody made a mistake and change the permission on a number of files (This never happen right).Doing more with less2021-03-29T10:03:03+00:002021-03-29T10:03:03+00:00https://linternux.github.io/general/doing-more-with-less<p>We usually use the “less” command to browse the content of a text file. We use the
[Page-Up], [Page-Down], [Down-Arrow], [Up-Arrow] keys to move within the file, but did you know
that you can do more with the less command ?</p>
<!--more-->
<p><img src="/assets/img/sadm_more_less.jpg" class="align-center" alt="" /></p>
<center>
Image by <a href="https://pixabay.com/users/kaboompics-1013994/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=791109">Karolina Grabowska</a> from <a href="https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=791109">Pixabay</a>
</center>
<p><br /></p>
<h2 id="you-can-see-the-content-of-a-rpm-file">You can see the content of a rpm file</h2>
<h3 id="-less-unzip-60-21el7x86_64rpm"># less ./unzip-6.0-21.el7.x86_64.rpm</h3>
<pre>
Name : unzip
Version : 6.0
Release : 21.el7
Architecture: x86_64
Install Date: (not installed)
Group : Applications/Archiving
Size : 374042
License : BSD
Signature : RSA/SHA256, Fri 03 Apr 2020 05:09:58 PM EDT, Key ID 24c6a8a7f4a80eb5
Source RPM : unzip-6.0-21.el7.src.rpm
Build Date : Wed 01 Apr 2020 12:49:28 AM EDT
Build Host : x86-02.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.info-zip.org/UnZip.html
Summary : A utility for unpacking zip files
Description :
The unzip utility is used to list, test, or extract files from a zip
archive. Zip archives are commonly found on MS-DOS systems. The zip
utility, included in the zip package, creates zip archives. Zip and
unzip are both compatible with archives created by PKWARE(R)'s PKZIP
for MS-DOS, but the programs' options and default behaviors do differ
in some respects.
Install the unzip package if you need to list, test or extract files from
a zip archive.
* Mon Nov 04 2019 Jakub Martisko <jamartis@redhat.com> - 6.0-21
- Fix CVE-2019-13232
- Resolves: CVE-2019-13232
-rwxr-xr-x 1 root root 32136 Apr 1 2020 /usr/bin/funzip
-rwxr-xr-x 2 root root 185512 Apr 1 2020 /usr/bin/unzip
-rwxr-xr-x 1 root root 90760 Apr 1 2020 /usr/bin/unzipsfx
-rwxr-xr-x 1 root root 2953 Oct 10 2008 /usr/bin/zipgrep
-rwxr-xr-x 2 root root 185512 Apr 1 2020 /usr/bin/zipinfo
drwxr-xr-x 2 root root 0 Apr 1 2020 /usr/share/doc/unzip-6.0
-rw-r--r-- 1 root root 4633 Jan 2 2009 /usr/share/doc/unzip-6.0/BUGS
-rw-r--r-- 1 root root 3606 Jan 3 2009 /usr/share/doc/unzip-6.0/LICENSE
-rw-r--r-- 1 root root 18337 Apr 19 2009 /usr/share/doc/unzip-6.0/README
-rw-r--r-- 1 root root 1984 Apr 19 2009 /usr/share/man/man1/funzip.1.gz
-rw-r--r-- 1 root root 18132 Apr 1 2020 /usr/share/man/man1/unzip.1.gz
-rw-r--r-- 1 root root 5512 Apr 19 2009 /usr/share/man/man1/unzipsfx.1.gz
-rw-r--r-- 1 root root 1543 Apr 19 2009 /usr/share/man/man1/zipgrep.1.gz
-rw-r--r-- 1 root root 8934 Apr 1 2020 /usr/share/man/man1/zipinfo.1.gz
</pre>
<p><br /></p>
<h2 id="view-the-content-of-zip-file">View the content of zip file</h2>
<h3 id="-less-studiopresszip">$ less StudioPress.zip</h3>
<pre>
Archive: StudioPress.zip 196759 bytes 42 files
drwxr-xr-x 2.3 unx 0 bx stor 17-Sep-08 09:05 StudioPress/
-rw-r--r-- 2.3 unx 3848 tx defN 14-Jan-08 09:05 StudioPress/comments.php
-rw-r--r-- 2.3 unx 231 tx defN 14-Jan-08 09:05 StudioPress/searchform.php
-rw-r--r-- 2.3 unx 169 tx defN 4-Feb-08 09:44 StudioPress/adsense_top.php
-rw-r--r-- 2.3 unx 1393 tx defN 5-Feb-08 06:50 StudioPress/home.php
-rw-r--r-- 2.3 unx 1411 tx defN 14-Jan-08 09:05 StudioPress/functions.php
</pre>
<p><br /></p>
<h2 id="view-the-content-of-tar-or-tgz-file"><strong>View the content of tar or tgz file</strong></h2>
<h3 id="-less-update_invtar">$ less update_inv.tar</h3>
<pre>
drwxr-xr-x jacques/jacques 0 2008-01-03 20:48:50 update_inv/
drwxr-xr-x jacques/jacques 0 2008-03-22 13:52:07 update_inv/log/
-rw-rw-r-- jacques/jacques 0 2008-03-22 13:52:07 update_inv/log/update_inv.log
drwxr-xr-x jacques/jacques 0 2008-03-22 13:52:33 update_inv/html/
-rw-rw-r-- jacques/jacques 941 2008-03-26 19:24:51 update_inv/html/gumby.html
drwxr-xr-x jacques/jacques 0 2008-03-22 13:52:07 update_inv/tmp/
-rwxrwxr-x jacques/jacques 19059 2008-03-26 19:00:43 update_inv/update_inventory.py
update_inv.tar
</pre>Jacques Duplessislinternux@gmail.comWe usually use the “less” command to browse the content of a text file. We use the [Page-Up], [Page-Down], [Down-Arrow], [Up-Arrow] keys to move within the file, but did you know that you can do more with the less command ?Making Date Calculation2021-03-12T10:03:03+00:002021-03-12T10:03:03+00:00https://linternux.github.io/scripting/making-date-calculation<p>You’re doing a script and you need to know what was the date 35 days ago or what will the date be in 21 days. This is where the “date” command can help you doing that kind of calculation. In this little article we will present some examples to help us making some data calculation. I would like to point out, that all of these date calculation take in consideration the leap year.</p>
<!--more-->
<p><br />
<img src="/assets/img/sadm_calendar.jpg" class="align-left" alt="" /></p>
<center>
Image by <a href="https://pixabay.com/users/amber_avalona-1512238/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=2290045">Amber Avalona</a> from <a href="https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=2290045">Pixabay</a>
</center>
<p>Revision 1.1 <br />
2 March 2021 - <a href="/assets/pdf/making-date-calculation.pdf">Download a pdf version</a></p>
<div id="entry-table-of-contents" class="toc-wrapper">
<h2 id="toc-toggle" class="no_toc">
Table of Contents <i class="toc-toggle-icon fas fa-chevron-down"></i>
</h2>
<ol id="markdown-toc">
<li><a href="#current-date-and-time" id="markdown-toc-current-date-and-time">Current date and time</a></li>
<li><a href="#epoch-time" id="markdown-toc-epoch-time">Epoch time</a></li>
<li><a href="#date-in-the-future" id="markdown-toc-date-in-the-future">Date in the future</a></li>
<li><a href="#date-in-the-past" id="markdown-toc-date-in-the-past">Date in the past</a></li>
</ol>
</div>
<h2 id="current-date-and-time">Current date and time</h2>
<table>
<thead>
<tr>
<th style="text-align: left">Command</th>
<th style="text-align: left">Description</th>
<th style="text-align: left">Result</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">date</td>
<td style="text-align: left">Get/Show current date & time</td>
<td style="text-align: left">Sun 28 Feb 2021 17:05:41 EST</td>
</tr>
<tr>
<td style="text-align: left">date “+%C%y.%m.%d %H:%M:%S”</td>
<td style="text-align: left">Current Date/Time YYYY.MM.DD HH:MM:SS</td>
<td style="text-align: left">2021.02.28 21:09:17</td>
</tr>
<tr>
<td style="text-align: left">date “+%C%y.%m.%d %T”</td>
<td style="text-align: left">Current Date/Time YYYY.MM.DD HH:MM:SS</td>
<td style="text-align: left">2021.02.28 21:09:17</td>
</tr>
<tr>
<td style="text-align: left">date ‘+%C%y.%m.%d’</td>
<td style="text-align: left">Current Date/Time Format YYYY.MM.DD</td>
<td style="text-align: left">2021.02.28</td>
</tr>
<tr>
<td style="text-align: left">date “+%j”</td>
<td style="text-align: left">Day of year (001..366)</td>
<td style="text-align: left">059</td>
</tr>
<tr>
<td style="text-align: left">date +%q</td>
<td style="text-align: left">Quarter of year (1..4)</td>
<td style="text-align: left">1</td>
</tr>
<tr>
<td style="text-align: left">date +%u</td>
<td style="text-align: left">Day of week (1..7); 1 is Monday</td>
<td style="text-align: left">7</td>
</tr>
<tr>
<td style="text-align: left">date +%w</td>
<td style="text-align: left">Day of week (0..6); 0 is Sunday</td>
<td style="text-align: left">0</td>
</tr>
<tr>
<td style="text-align: left">date +%U</td>
<td style="text-align: left">week number of year (00..53)</td>
<td style="text-align: left">09</td>
</tr>
<tr>
<td style="text-align: left">date +%d</td>
<td style="text-align: left">Day of month (e.g., 01)</td>
<td style="text-align: left">28</td>
</tr>
<tr>
<td style="text-align: left">date +%m</td>
<td style="text-align: left">Month number (01..12)</td>
<td style="text-align: left">02</td>
</tr>
<tr>
<td style="text-align: left">date +%y</td>
<td style="text-align: left">Last two digits of year (00..99)</td>
<td style="text-align: left">21</td>
</tr>
<tr>
<td style="text-align: left">date +%Y</td>
<td style="text-align: left">Current Year with century</td>
<td style="text-align: left">2021</td>
</tr>
<tr>
<td style="text-align: left">date +%H</td>
<td style="text-align: left">Current Hour (00..23)</td>
<td style="text-align: left">21</td>
</tr>
<tr>
<td style="text-align: left">date +%M</td>
<td style="text-align: left">Current Minute (00..59)</td>
<td style="text-align: left">09</td>
</tr>
<tr>
<td style="text-align: left">date +%S</td>
<td style="text-align: left">Current second (00..60)</td>
<td style="text-align: left">41</td>
</tr>
</tbody>
</table>
<p><br /></p>
<h2 id="epoch-time">Epoch time</h2>
<table>
<thead>
<tr>
<th style="text-align: left">Command</th>
<th style="text-align: left">Description</th>
<th style="text-align: left">Result</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">date</td>
<td style="text-align: left">Get current date & time</td>
<td style="text-align: left">Tue 02 Mar 2021 10:42:09 AM EST</td>
</tr>
<tr>
<td style="text-align: left">date +%s</td>
<td style="text-align: left">Get current epoch time</td>
<td style="text-align: left">1614699732</td>
</tr>
<tr>
<td style="text-align: left">date -d @1614699732 “+%Y/%m/%d %T”</td>
<td style="text-align: left">Convert Epoch Time to a Date format</td>
<td style="text-align: left">2021/03/02 10:42:12</td>
</tr>
<tr>
<td style="text-align: left">date –date=’03/02/2021 10:42:12’ +%s</td>
<td style="text-align: left">Convert a Date to an Epoch Time</td>
<td style="text-align: left">1614699732</td>
</tr>
</tbody>
</table>
<p><br /></p>
<h2 id="date-in-the-future">Date in the future</h2>
<table>
<thead>
<tr>
<th style="text-align: left">Command</th>
<th style="text-align: left">Description</th>
<th style="text-align: left">Result</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">date –date=”35 days”</td>
<td style="text-align: left">Date/Time in 35 days</td>
<td style="text-align: left">Sun Apr 4 18:12:51 EDT 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=”1 month”</td>
<td style="text-align: left">Date/Time in one month</td>
<td style="text-align: left">Sun Mar 28 18:58:26 EDT 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=”this friday”</td>
<td style="text-align: left">Date/Time Next Friday</td>
<td style="text-align: left">Fri Mar 5 00:00:00 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=”next day”</td>
<td style="text-align: left">Date/Time Tomorrow</td>
<td style="text-align: left">Mon Mar 1 18:01:42 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=”tomorrow”</td>
<td style="text-align: left">Date/Time Tomorrow</td>
<td style="text-align: left">Mon Mar 1 18:01:42 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’fortnight’</td>
<td style="text-align: left">Date/Time 15 days ahead</td>
<td style="text-align: left">Sun Mar 14 21:30:30 EDT 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’5 fortnight’</td>
<td style="text-align: left">Date/Time 5 times 15 days ahead</td>
<td style="text-align: left">Sun May 9 21:38:33 EDT 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’2 hour’</td>
<td style="text-align: left">Date/Time 2 hours from now</td>
<td style="text-align: left">Sun Feb 28 22:43:30 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’20 minute’</td>
<td style="text-align: left">Date/Time 20 minutes from now</td>
<td style="text-align: left">Sun Feb 28 21:04:49 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date -dmonday +%Y/%m/%d</td>
<td style="text-align: left">Date of next Monday</td>
<td style="text-align: left">2021/03/01</td>
</tr>
<tr>
<td style="text-align: left">date -d’monday-fortnight ago’ +%Y%m%d</td>
<td style="text-align: left">Last Monday - 2 weeks from now</td>
<td style="text-align: left">20210215</td>
</tr>
<tr>
<td style="text-align: left">date –date “next month - $(date +%d) day”</td>
<td style="text-align: left">Last Day of Month</td>
<td style="text-align: left">Wed 31 Mar 2021 10:30:21 AM EDT</td>
</tr>
<tr>
<td style="text-align: left">date -dnext-monday +”%Y/%m/%d”</td>
<td style="text-align: left">Date/Time of next Monday</td>
<td style="text-align: left">2021/03/01</td>
</tr>
<tr>
<td style="text-align: left">date -d’monday+fortnight’ +”%Y/%m/%d”</td>
<td style="text-align: left">Next Monday + 2 weeks from now</td>
<td style="text-align: left">2021/02/15</td>
</tr>
<tr>
<td style="text-align: left">date -d’52+monday’ +”%Y%m%d”</td>
<td style="text-align: left">Date of 52th Monday from now</td>
<td style="text-align: left">20220221</td>
</tr>
<tr>
<td style="text-align: left">date -d’monday+14 days’ +”%Y%m%d”</td>
<td style="text-align: left">Date of next Monday + 14 Days</td>
<td style="text-align: left">20210315</td>
</tr>
</tbody>
</table>
<p><br /></p>
<h2 id="date-in-the-past">Date in the past</h2>
<table>
<thead>
<tr>
<th style="text-align: left">Command</th>
<th style="text-align: left">Description</th>
<th style="text-align: left">Result</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left">date –date=’20 minutes ago’</td>
<td style="text-align: left">Date/Time 20 minutes ago</td>
<td style="text-align: left">Sun Feb 28 20:25:25 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’2 hours ago’</td>
<td style="text-align: left">Date/Time 2 hours ago</td>
<td style="text-align: left">Sun Feb 28 18:44:20 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’10 days ago’</td>
<td style="text-align: left">Date/Time 10 days ago</td>
<td style="text-align: left">Thu Feb 18 20:51:01 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’10 weeks ago’</td>
<td style="text-align: left">Date/Time 10 weeks ago</td>
<td style="text-align: left">Sun Dec 20 20:51:28 EST 2020</td>
</tr>
<tr>
<td style="text-align: left">date –date=’10 months ago’</td>
<td style="text-align: left">Date/Time 10 months</td>
<td style="text-align: left">Tue Apr 28 21:52:27 EDT 2020</td>
</tr>
<tr>
<td style="text-align: left">date –date=’10 year ago’</td>
<td style="text-align: left">Date/Time 10 years ago</td>
<td style="text-align: left">Mon Feb 28 20:52:54 EST 2011</td>
</tr>
<tr>
<td style="text-align: left">date –date=”yesterday” +”%Y/%m/%d %T”</td>
<td style="text-align: left">Yesterday Date</td>
<td style="text-align: left">2021/02/27 20:49:45</td>
</tr>
<tr>
<td style="text-align: left">date –date=’yesterday’</td>
<td style="text-align: left">Yesterday Date/Time</td>
<td style="text-align: left">Sat Feb 27 20:49:49 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date –date=’fortnight ago’</td>
<td style="text-align: left">Date/Time 15 days ago</td>
<td style="text-align: left">Sun Feb 14 20:40:06 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date -dlast-monday</td>
<td style="text-align: left">Date/Time of Last Monday</td>
<td style="text-align: left">Mon Feb 22 00:00:00 EST 2021</td>
</tr>
<tr>
<td style="text-align: left">date -dlast-monday +”%Y/%m/%d”</td>
<td style="text-align: left">Date Last Monday</td>
<td style="text-align: left">2021/02/22</td>
</tr>
<tr>
<td style="text-align: left">date -d’monday-14 days’ +”%Y/%m/%d %T”</td>
<td style="text-align: left">Two Mondays ago</td>
<td style="text-align: left">20210215</td>
</tr>
</tbody>
</table>Jacques Duplessislinternux@gmail.comYou’re doing a script and you need to know what was the date 35 days ago or what will the date be in 21 days. This is where the “date” command can help you doing that kind of calculation. In this little article we will present some examples to help us making some data calculation. I would like to point out, that all of these date calculation take in consideration the leap year.Force user to change password2021-03-11T09:28:10+00:002021-03-11T09:28:10+00:00https://linternux.github.io/sysadmin/force-user-to-change-password-at-next-login<p>User is calling and say that he forgot is password. So you need to change his password and for security reason you want to force him to change it upon his first login. The command “chage -d 0” (change aging) will force user to change his password upon first login.</p>
<!--more-->
<p><br />
<img src="/assets/img/sadm_password.jpg" class="align-left" alt="Password Image" /></p>
<center>
Image by <a href="https://pixabay.com/users/absolutvision-6158753/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=2781614">Gino Crescoli</a> from <a href="https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=2781614">Pixabay</a>
</center>
<p><br /></p>
<p>Change the user password, because he forgot it.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c"># passwd josblow</span></code></pre></figure>
<p><br /></p>
<p>Then we set expiration date to 0, so when user login with his new password, he will have to change it before continuing.</p>
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c"># chage -d 0 josblow</span></code></pre></figure>
<p><br />
So when the user log on the server, he will need to change his password before going forward.</p>
<pre>
$ ssh josblow@raspi1
josblow@raspi1's password:
You are required to change your password immediately (administrator enforced)
Linux raspi1.maison.ca 5.10.17+ #1403 Mon Feb 22 11:26:13 GMT 2021 armv6l
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for josblow.
Current password:
New password:
Retype new password:
passwd: password updated successfully
Connection to raspi1 closed.
$ ssh josblow@raspi1
josblow@raspi1's password:
Linux raspi1.maison.ca 5.10.17+ #1403 Mon Feb 22 11:26:13 GMT 2021 armv6l
josblow@raspi1:~ $
</pre>
<p><br /></p>Jacques Duplessislinternux@gmail.comUser is calling and say that he forgot is password. So you need to change his password and for security reason you want to force him to change it upon his first login. The command “chage -d 0” (change aging) will force user to change his password upon first login.Linux Administration Books for Free2021-03-02T19:40:52+00:002021-03-02T19:40:52+00:00https://linternux.github.io/general/linux-administration-books-for-free<p>A short post to list some free books for Linux users and administrators, take a look you might find one that you would like to have.</p>
<!--more-->
<p><br />
<img src="/assets/img/sadm_books.jpg" class="align-left" alt="" /></p>
<center>
Image by <a href="https://pixabay.com/users/blende12-201217/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=1659717">Gerhard G.</a> from <a href="https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=1659717">Pixabay</a>
</center>
<p>Revision 1.1 - 2 March 2021 -</p>
<ul>
<li><strong>The Linux Command Line</strong>
<ul>
<li>Have a brief look at the book from this <a href="http://linuxcommand.org/tlcl.php">page</a> and <a href="https://sourceforge.net/projects/linuxcommand/files/TLCL/19.01/TLCL-19.01.pdf/download">download the book</a>.
<br /></li>
</ul>
</li>
<li>The <strong>Pro Git book</strong>, written by Scott Chacon and Ben Straub and published by Apress, is available <a href="https://git-scm.com/book/en/v2">here</a> and <a href="https://github.com/progit/progit2/releases/download/2.1.295/progit.pdf">download the book</a>.<br />
<br /></li>
<li><strong>The Debian Administrator’s Handbook</strong>
<ul>
<li>Written by two Debian developers — Raphaël Hertzog and Roland Mas — the Debian Administrator’s Handbook</li>
<li>Take a look at the home <a href="https://debian-handbook.info/">page</a> and <a href="http://debian-handbook.info/download/stable/debian-handbook.pdf">download the book</a>.</li>
</ul>
</li>
</ul>
<p><br /><br />
See you soon !
<br /></p>Jacques Duplessislinternux@gmail.comA short post to list some free books for Linux users and administrators, take a look you might find one that you would like to have.