Terminal Takeaway πŸ₯‘

watchforchange

Made a quick script that’ll keep checking a file and run it if it’s been changed.

Unlike watch it’ll only run the file once per modification instead of once per interval.

sudo nano /usr/bin/watchforchange
# Copy/paste the following:

#!/usr/bin/env sh
FILEPATH=$1
INTERVAL=$2

if [ -z "$INTERVAL" ]; then INTERVAL=1; fi
if [ ! -f "$FILEPATH" ]; then echo "Not a file"; exit; fi

while true
do
	LAST_MODIFIED=`stat -c "%Y" $FILEPATH`
	if [ "$LAST_MODIFIED" != "$LAST_MODIFIED_LAST" ]; then
		LAST_MODIFIED_LAST=$LAST_MODIFIED
		clear
		$FILEPATH
	fi
	sleep $INTERVAL
done

# Save & exit
sudo chmod +x /usr/bin/watchforchange

# Test:
watchforchange ./MY_PROJECT.sh 0.5
watchforchange ./MY_PROJECT.sh

waitforchange

1 Like

The breadth and depth of Linux is amazing. I didn’t know about watch but would have used tail -f or tail -fs to do the same thing.

tail -f file_to_watch.ext

1 Like

Show all the packages you have installed on your system but by size:

dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1n

Of course I am a Debian guy. :wink: Dpkg has a lot of interesting commands.

2 Likes

Assuming a file containing:

echo "A project"

tail would output the raw text of the file:

echo β€œA project”

watch would run the file and give you the output:

A project

1 Like

ahh! I see the difference now. Thx

Credit to Learn Linux: - YouTube

find ${PATH//:/ } -type f -executable -printf '%P\n' | sort -u | column

List every executable on your system. Great for exploration, compliments @vinylninja’s package search above.

1 Like

If you want to know your public IP you can do that comfortably from the terminal:

wget http://ipinfo.io/ip -qO -

You can also use net-tools to find what is on your local network:

arp -a

Because that was very basic, let’s go back to the Debian world with one very important command. You all probably know how to edit your sources list involving a text editor that points to /etc/apt/sources.list. But you can have it more automated, just type:

sudo apt edit-sources           

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 

It will only make sense on Debian proper and its derivatives. I would not use it on Ubuntu based distributions. But you still can have a look at it anyway.
In a default Debian install it makes sense because you can switch Debian releases (stable, unstable, experimental), do release upgrades and add non-free sources and even other repositories to your install.

1 Like

If you want to know your public IP you can do that comfortably from the terminal:

Even more comfy option with curl:
curl icanhazip.com

Oops. I thought today was Tuesday. Sorry for breaking the rules / following Rule #1 :stuck_out_tongue:

2 Likes

wow! that is a minimalistic return. Thx for bringing this to our attention.

Not so much a command line trick than a continuation of editing and choosing apt sources.

Linux Mint has a nifty feature where they choose the fastest mirror for their Mint and Ubuntu repos. You can do that by yourself in Debian with the program netselect-apt.

If you e.g. run Debian sid enabled with experimental then just run:

netselect-apt sid and later netselect-apt experimental

and it will print out the best options. Edit your sources list accordingly.

I am not sure which back-end is used by Linux Mint.

Prepend a \ to ignore aliases. Useful for one-offs or if you’re worried your install script may trigger weird aliases created by users.

alias echo='echo big'
echo 'hello'
\echo 'hello'

Output:

big hello
hello

1 Like

Another trick.

Most of us use or install lm-sensors. If you type:

sensors

it will give you the information but you can also have it in real time with:

watch sensors

4 Likes

I like -d for differences and -n to setup an interval.

… it’s also nice to user on some cisco switches

2 Likes

Nice. My DE is xfce. I have the following script that runs every 10 seconds and reports the various temps to a panel on my second monitor:

#!/bin/bash
#The following is for the Asus P8P67 MB
sensors 2>/dev/null | grep Core | cut -b1-24 | awk '{print "  " $0}' 

temps Screenshot_2021-02-28_19-25-25

One of the things I love about Linux is that you can actually run β€œwidgets” like this all of the time and not take a performance hit. Back in my days of Windoze, I would never run a weather widget or temp widget as it would make the system crawl and leak resources.

3 Likes

alacritty using Tmux as your shell is actually pretty legit

1 Like

Has some nice bash shortcuts at the end:

Ctrl + A  Go to the beginning of the line you are currently typing on
Ctrl + E  Go to the end of the line you are currently typing on
Ctrl + L  Clears the Screen, similar to the clear command
Ctrl + U  Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H  Same as backspace
Ctrl + R  Lets you search through previously used commands
Ctrl + C  Kill whatever you are running
Ctrl + D  Exit the current shell
Ctrl + Z  Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W  Delete the word before the cursor
Ctrl + K  Clear the line after the cursor
Ctrl + T  Swap the last two characters before the cursor
Esc + T   Swap the last two words before the cursor
Alt + F   Move cursor forward one word on the current line
Alt + B   Move cursor backward one word on the current line
Tab       Auto-complete files and directory names
4 Likes

Use find to delete every file in a directory that doesn’t match.

find ~/storage -type f ! -name "TerminalRocks.flac" -delete
  • find
  • ~/storage ← Search in ~/storage
  • -type f ← Files only
  • ! -name "TerminalRocks.flac" ← Find everything that doesn’t match TerminalRocks.flac
  • -delete ← Delete what’s found

Example:

fd is like find but a little more hardcore :smiley:

1 Like

problem

while read Line; do echo "$Line"; done < WhyThatsNotAProblem.txt
while read Line; do echo "$Line"; done < WhyThatsAProblem.txt

image

image