<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>BASH on Much Ado About IT</title><link>https://it.knightnet.org.uk/tags/bash/</link><description>
Recent content about BASH from Much Ado About IT |
Ramblings and rantings from IT Architect &amp; Designer, Julian Knight</description><generator>Hugo | gohugo.io | Theme twenty-sixteen</generator><language>en-gb</language><copyright>This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.</copyright><lastBuildDate>Mon, 24 Apr 2023 21:27:28 +0000</lastBuildDate><atom:link href="https://it.knightnet.org.uk/tags/bash/feed.xml" rel="self" type="application/rss+xml"/><item><title>Checking user access to files in Linux</title><link>https://it.knightnet.org.uk/blog/checking-user-access-to-files-in-linux/</link><guid isPermaLink="true">https://it.knightnet.org.uk/blog/checking-user-access-to-files-in-linux/</guid><pubDate>Fri, 18 Jan 2019 17:33:26 +0000</pubDate><guid>https://it.knightnet.org.uk/blog/checking-user-access-to-files-in-linux/</guid><description><div>How can I know if a user has access to a file or a folder in Linux? Also, what groups is a user a member of? A few things I can never remember how to do.</div><div>&lt;h2 id="add-a-user-to-a-group">Add a user to a group&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">sudo usermod -a -G &lt;span class="o">[&lt;/span>userName&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>groupName&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="check-what-groups-a-user-is-a-member-of">Check what groups a user is a member of&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">groups &lt;span class="o">[&lt;/span>userName&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="check-if-a-user-has-access-to-a-file-or-folder">Check if a user has access to a file or folder&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">sudo -u &lt;span class="o">[&lt;/span>userName&lt;span class="o">]&lt;/span> ls &lt;span class="o">[&lt;/span>path&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># or perhaps&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sudo -u &lt;span class="o">[&lt;/span>userName&lt;span class="o">]&lt;/span> namei &lt;span class="o">[&lt;/span>path&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Note that it isn&amp;rsquo;t always obvious where a blockage may lie in the path.
You may need to walk back up the path to find out.&lt;/p>
&lt;h2 id="give-a-group-access-to-a-folder-or-file">Give a group access to a folder or file&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">sudo chgrp &lt;span class="o">[&lt;/span>grpName&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>fileOrFolderPath&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># or&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sudo chown &lt;span class="o">[&lt;/span>userName&lt;span class="o">]&lt;/span>:&lt;span class="o">[&lt;/span>grpName&lt;span class="o">]&lt;/span> &lt;span class="o">[&lt;/span>fileOrFolderPath&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="change-the-level-of-group-access-for-a-folder-or-file">Change the level of group access for a folder or file&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">sudo chmod -R g+rwx &lt;span class="o">[&lt;/span>fileOrFolderPath&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="references">References&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://unix.stackexchange.com/questions/82347/how-to-check-if-a-user-can-access-a-given-file">StackExchange: How to check if a user can access a given file?&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://serverfault.com/questions/136826/add-group-rwx-permissions-to-a-folder">ServerFault: Add group rwx permissions to a folder&lt;/a>&lt;/li>
&lt;/ul></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/linux">Linux</category><category domain="https://it.knightnet.org.uk/tags/linux">Linux</category><category domain="https://it.knightnet.org.uk/tags/bash">BASH</category></item><item><title>Cygwin BASH function to open the latest version of a document</title><link>https://it.knightnet.org.uk/2009/07/cygwin-bash-function-to-open-the-latest-version-of-a-document.html</link><guid isPermaLink="true">https://it.knightnet.org.uk/2009/07/cygwin-bash-function-to-open-the-latest-version-of-a-document.html</guid><pubDate>Wed, 15 Jul 2009 21:43:50 +0000</pubDate><guid>https://it.knightnet.org.uk/2009/07/cygwin-bash-function-to-open-the-latest-version-of-a-document.html</guid><description><div/><div>&lt;p>One handy function I’ve added to .bashrc (so it is always available) under Cygwin (the LINUX command environment for Windows) works out the current working version of a document. It assumes that you keep copies that have a version number or date in the file name that will sort correctly.&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;!-- raw HTML omitted -->
&lt;p>You can use it with an alias like this:&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;!-- raw HTML omitted -->
&lt;p>If you name your documents sensibly such as “a-document-2009-07-20.doc” or “a-document-v01.01.doc”, then the latest version of the file will be opened in the default application&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;p>&lt;!-- raw HTML omitted -->&lt;!-- raw HTML omitted -->Technorati&lt;!-- raw HTML omitted --> : &lt;!-- raw HTML omitted -->BASH&lt;!-- raw HTML omitted -->&lt;!-- raw HTML omitted -->&lt;!-- raw HTML omitted --> &lt;!-- raw HTML omitted -->&lt;!-- raw HTML omitted -->Diigo Tag Search&lt;!-- raw HTML omitted --> : &lt;!-- raw HTML omitted -->BASH&lt;!-- raw HTML omitted -->&lt;!-- raw HTML omitted -->&lt;/p>
&lt;!-- raw HTML omitted --></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/categories/windows">Windows</category><category domain="https://it.knightnet.org.uk/tags/bash">BASH</category><category domain="https://it.knightnet.org.uk/tags/batch">Batch</category><category domain="https://it.knightnet.org.uk/tags/cygwin">Cygwin</category><category domain="https://it.knightnet.org.uk/tags/scripting">Scripting</category></item><item><title>Shell script to Back up critical files (using RSYNC)</title><link>https://it.knightnet.org.uk/2008/08/shell-script-to-back-up-critical-files-using-rsync.html</link><guid isPermaLink="true">https://it.knightnet.org.uk/2008/08/shell-script-to-back-up-critical-files-using-rsync.html</guid><pubDate>Thu, 14 Aug 2008 01:57:00 +0000</pubDate><guid>https://it.knightnet.org.uk/2008/08/shell-script-to-back-up-critical-files-using-rsync.html</guid><description><div/><div>&lt;p>Following up from my article on backing up USB drives, this recipe backs up the critical files on my desktop to remote storage (a NAS device on my network). Note that PC2 is the desktop to be backed up, SLUG1 (192.168.1.2) is the NAS device and USER1 is the user id doing the backup.&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;!-- raw HTML omitted -->
&lt;p>I have a similar script that runs on the NAS device which backs key files on that to a remote hosting service on a different continent! That way, I don’t have to worry about the house burning down or being burgled.&lt;/p></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/categories/linux">Linux</category><category domain="https://it.knightnet.org.uk/tags/backup">Backup</category><category domain="https://it.knightnet.org.uk/tags/bash">BASH</category><category domain="https://it.knightnet.org.uk/tags/batch">Batch</category><category domain="https://it.knightnet.org.uk/tags/scripting">Scripting</category></item><item><title>Automatically Backing up a USB Drive with RSYNC (KDE)</title><link>https://it.knightnet.org.uk/2008/08/automatically-backing-up-a-usb-drive-with-rsync-kde.html</link><guid isPermaLink="true">https://it.knightnet.org.uk/2008/08/automatically-backing-up-a-usb-drive-with-rsync-kde.html</guid><pubDate>Thu, 14 Aug 2008 01:22:00 +0000</pubDate><guid>https://it.knightnet.org.uk/2008/08/automatically-backing-up-a-usb-drive-with-rsync-kde.html</guid><description><div/><div>&lt;p>USB Drives of all kinds need to be backed up and the best backup is an automatic one (it’s the only way to make sure that it gets done!).&lt;/p>
&lt;p>So here is one recipe for doing just that using RSYNC and some BASH scripting magic.&lt;/p>
&lt;p>I’ve split this into two files. You don’t have to do this of course and one may well be better for you. I used two because I can run the second one manually as well. Put everything in autorun.sh if you want to backup each drive individually, however, note that KDE produces an annoying extra dialog (a security warning) asking if you really want to run the autorun.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>autorun.sh&lt;/p>
&lt;p>This resides in the root of the USB drive and is executed automatically by KDE when the drive is detected (though not if the drive is attached when booting)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>usb-backup-manual.sh&lt;/p>
&lt;p>This is a bit of a nasty hack, I have manually configured a list of drives that might be attached so that I can back them all up together. Not elegant but it works for me.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h4 id="autorunsh">autorun.sh&lt;/h4>
&lt;!-- raw HTML omitted -->
&lt;!-- raw HTML omitted -->
&lt;p>Note the use of KDialog to provide a minimal GUI. In the second file, KDialog produces a progress bar.&lt;/p>
&lt;p>Also note the RSYNC parameters. These are always painful to get to grips with so it is nice to have an example to work from. In this case I am backing up so I am making sure that the backup is an exact copy of the original (as opposed to synchronising which would allow changes to happen on either side).&lt;/p></div></description><author>Julian Knight</author><category domain="https://it.knightnet.org.uk/categories/development">Development</category><category domain="https://it.knightnet.org.uk/categories/linux">Linux</category><category domain="https://it.knightnet.org.uk/tags/backup">Backup</category><category domain="https://it.knightnet.org.uk/tags/bash">BASH</category><category domain="https://it.knightnet.org.uk/tags/batch">Batch</category><category domain="https://it.knightnet.org.uk/tags/scripting">Scripting</category><category domain="https://it.knightnet.org.uk/tags/usb">USB</category></item></channel></rss>