Every so often something really useful appears on Reddit, and this is such a case. You may encounter a situation where you want to execute the contents of a bash script, but not more frequently than every few seconds. A Reddit user wanted to know How to check if a command in .bashrc has been executed within last 10 seconds if yes don’t execute the command again. The response by Reddit user mdaffin is brilliant in its simplicity, and can be used in any bash script where you don’t want the contents executed too often:
Write a time stamp to some file, check said file before you run the command if now – timestamp > 10s run the command and update the timestamp.
EDIT: Like this (with modification times instead):
if [[ ! -f "$TS_FILE" ]] || [[ "$(expr "$(date +%s)" - "$(stat -c %Y "$TS_FILE")")" -gt 10 ]]; then
You’d replace the
echo "running" line with the part of the bash script you want to run only if it’s been 10 seconds since the last time the script was run, or whatever number of seconds you specify after the
-gt. If the bash script actually outputs a file as part of its normal operation then you could specify that file in the
TS_FILE= line; there would be no need to create a separate timestamp file (unless some other process could also modify that same file).
This doesn’t actually stop the bash script from running; it just prevents it from executing the part of the script that you don’t want executed too frequently. This could be very useful in a situation where without such protection, the too-frequent execution of the script might cause something undesirable to happen (such as getting locked out of an online site for hammering it with requests). Depending on the situation there may be other, perhaps even better ways to avoid this possibility, but in other cases this may indeed be the best approach.
Grepping is awesome, as long as you don’t glob it up! This article covers some grep and regex basics.
Source: Globbing and Regex: So Similar, So Different | Linux Journal
If you’re trying to remove a Time Machine backup from a drive and find that it’s stuck in the Mac Trash with a specific error message stating the trash can’t be emptied because “Some items in the Trash cannot be deleted because of System Integrity Protection”, then read on to learn how to resolve this particular Time Machine backup removal problem.
Source: How to Remove Stuck Time Machine Backups from Mac Trash Due to System Integrity Protection Error
This brief tutorial will show you how to quickly and easily convert Mac OS X Icon files (.icns) to .png files.
Source: How to convert .icns files to .png files in OS X (Simple Help)
Sed is a powerful command akin to the find and replace feature in most text editors. Find out what sed is and how you can use it to improve your efficiency.
Source: What Is SED and How Do You Use It? – Make Tech Easier
Grep is a small Unix program for finding matching patterns. Begun as a Unix program, it can be found on Linux as well as Mac and BSD. It can read just about any text, meaning it can read input from another commands, or it can open and look through files directly. Grep is insanely useful, especially for looking through directories from the command line.
Source: What Is GREP and How Do You Use It? – Make Tech Easier
What is Secure Copy?
scp allows files to be copied to, from, or between different hosts. It uses ssh for data transfer and provides the same authentication and same level of security as ssh.
Source: Example syntax for Secure Copy (scp) (hypexr.org)
Linux is a multiuser operating system. In a multiuser environment, it is necessary to ensure that a user cannot access or modify files or directories that they arent supposed to. File permissions provide a protection mechanism for controlling access to files and directories.
Linux’s file security model is based on that of Unix. Each file or directory can be accessed or modified by the user who created it, or a group of users who have been given permission to do so. Permissions can also be defined for other users that do not belong to either of these two categories.
In this guide, we will go over how file permissions work in Linux for beginners. We’ll cover how you can view the permissions associated with files and directories and also how you can change them.
Source: Understanding Linux Permissions (Linux Academy)
Previously we covered KeePassXC password manager which is also free application. Here comes another password manager called Enpass, it is free and cross-platform available for Linux, Windows, Mac, Android, Blackberry, Windows Phone and iOS.
Source: Enpass Is The Free Cross-Platform Password Manager (NoobsLab)
You can write a bash script such that it receives arguments specified when the script is called from the command line. This method is used when a script has to perform a slightly different function depending on the values of input parameters (the arguments).
For example, you may have a script called “stats.sh” that performs a particular operation on a file, such as counting its words. If you want to be able to use that script on many files, it is best to pass the file name as an argument, so that you can use the same script for all the files to be processed.
Source: How to Pass Arguments to a Bash-Script (Lifewire)