Tag Archives: Terminal

Ubuntu Tech Snippet #13 – Get your public IP in terminal

Sometimes when you’re working on a project or are SSHed into a remote server, you need to find out the public IP of the device, without using a GUI web-browser.

I had to do this while working on setting up a script on my server, and came across this neat little command that will grab your public IP and print it nicely out for you to use!

All you have to do is run echo $(curl -s https://api.ipify.org) and you’re good to go!

Output from echo $(curl -c https://api.ipify.org)

Output from echo $(curl -c https://api.ipify.org)

You can also do this programatically. In python you can run this code to get the result as a JSON object:

import requests
session = requests.Session()
ret = session.get('https://api.ipify.org',
                   params={"format":"json"})
print ("Public IP Address:",ret.json()['ip'])

You’ll need to install the Python Requests module. This can be done either via pip. Just run sudo pip install requests. You can technically install it using easy_install, but please, please don’t. Just use pip.

And that’s it!

Thanks for reading! If you have any tips of your own, leave them as a comment down below and I’ll be sure to take a look at them!

Tagged , , , ,

Ubuntu Tech Snippet #8 – Copy and Paste in Terminal with the keyboard

What’s the big deal?

One of the many things that power users use to be more efficient on their PC is using the mouse as little as possible. The less clicking and movement, the better. At least, that’s how I prefer it. If I’m working on coding or writing (like this blog post, for example!) and I can keep both hands on the keyboard and not have to mess around, clicking on stuff, then I’m far faster.

Thus, this shortcut/tech tip was awesome when I found out about it! Normally, when using the Ubuntu terminal, one would have to right-click and then choose copy or paste. While this may not seem like that much to your average user, being able to lose that extra three/five seconds when doing lots of research, debugging, or simply following instructions on a tutorial, can be a big deal.

How doth this worketh?

This tech tip is quite simple, comprising of two different keystroke combos.

First up, is the terminal version of Ctrl+C. You simply tack on the Shift key in that sequence, so it is now: Ctrl+Shift+C. Basically, it’s the normal keyboard shortcut for copying, with the Shift key added on.

That’s right, it’s that simple. Normally, pressing Ctrl+C in a terminal window would terminate whatever program was being used. Adding that shift key makes it so that the terminal ignores the normal useage of Ctrl+C, and uses it in the GUI fashion.

And the paste shortcut is exactly the same. Simply take the normal paste shortcut Ctrl+V, and add the shift key after the Ctrl key: Ctrl+Shift+V.

Conclusion:

That’s all! While this may not benefit everyone, and it most definitely won’t, I know it will be useful to someone, if even just me.

Make sure to comment down below with any tips of your own you want to submit!

Tagged , , ,

Ubuntu Tech Snippet #7 – Find the remaining disk space in all mounted partitions

Many people have several partitions on their systems for different OSes or even different partitions within one OS. This tech snippet is an easy way to find out how many more videos, applications, songs, and documents you have space for.

First off you need to start up a terminal window, you can do this by pressing Ctrl+Alt+T.

terminal

Empty Terminal Window – Open via Ctrl+Alt+T

Next, enter the command:

df
df terminal

Terminal Window running the df command

This brings up the results you see in the image above. However, seeing space available in 1K blocks can be relatively useless for the average user. So, here’s a flag you can add to make the output slightly more understandable:

Clear the terminal screen (clear) and run the df command again, except this time, include the -h flag:

df -h

 

df -h terminal

Terminal window running the df -h command

This flag, which stands for Human readable, takes the 1k blocks from regular df and converts the to slightly easier-to-understand units like Megabytes and Gigabytes. All of the columns remain the same, just the units have changed. As you can see in the screenshot above, I have a 36GB partition with 34GB used and 565MB available.

While not particularly useful for the average user, it is a very handy tool for more advanced Ubunt-ites and for systems (like servers) without GUIs, this becomes a must-have tool.

Tagged , , , , ,

Warning! You WILL want to turn your volume down if you are wearing headphones. You run the following commands at your own risk, I cannot confirm the safety of your ears.

Another Ubuntu Tech Snippet inbound! This one is a bit bizarre, I must say, and rather geeky.

The actual idea is pretty simple, you just run a command in terminal and pipe it through aplay. You can take this and try any commands you want, but the commands I list are the ones that I have heard actually play something more than a two-second electronic squelch.

I originally found out about this on Unix/Linux Stack Exchange. First thing to do is run the command

dmesg | aplay

which runs the dmesg (which, from the man page,  examines or controls the kernel ring buffer) command and then pipes the output into aplay, the command-line music player. In this case there are no flags on the dmesg command, so it just reads all the messages from the kernel ring buffer.

By piping the output from dmesg into aplay you will be getting the audio interpretation of whatever data dmesg returns. It will vary between computers, so don’t blame me if your speakers, earbuds, headphones, or eardrums explode because of the interpretation of raw std data. I didn’t do anything horrendous to my system, so you should be safe. Emphasis being on should – I’ve not tested or researched any chances of damage.

Another fun one to play is

ls -l | aplay

in the home directory, especially if you installed lots of software that has config folders/files in the home directory.

This is a Tech Tip that really has no real application, it’s just fun.


One final thing to try if you suddenly become addicted to raw data musically represented

Run

ls -R | aplay

in your Documents directory and have fun listening to the ear-splitting screeches of your computer’s innards. Because the -R flag on ls recursively lists subdirectories within the currently directory, this command can go on for quite a while. Just a warning.

Ubuntu Tech Snippet #5 – Hear what terminal commands “sound” like

Tagged , , , ,

I use my Raspberry Pi as a headless server, and thus SSH in all the time. While I can shut down the Pi and remove the SD card to load files on, or use a USB stick, those require accessing the Pi (and in the case of the USB stick – running wires to a USB hub.) This can be a pain, so if I need to download a file I just use wget.

It comes installed with Raspbian by default as far as I know. It makes sense that it would, as so many core software rely on it. Thusly, I use it download files from the internet or, if I need to put a file on it from my Ubuntu laptop, I just pop the file into my local server and download it onto my Pi. Works great!

However, there is one issue that I have run across, and that is that just running a simple wget command like

wget soandso.com/thisZIP.tar.gz

results in an error:

Resolving dl.dropboxusercontent.com (dl.dropboxusercontent.com)... failed: Name or service not known.
wget: unable to resolve host address `dl.dropboxusercontent.com'

Kind of issue, you might say. It took me ages to figure this one out, but finally I discovered that if you add the -4 flag to the command, it forces wget to use IPv4 addresses, thus resolving the issue and allowing downloads to happen!


Bonus Tip!
If you are downloading a large file and the download gets interrupted, tack on the -c flag on the wget command and it will continue from where you left off. Pretty nifty, eh?

Raspberry Pi Tech Tip #1 – Fix Unable to Resolve Host Address error for wget on Raspbian

Tagged , , , , , ,

How to create an alias in Terminal that sticks around

If you use the Terminal in Linux/Mac a lot, then you know how much trouble it can be entering the same exact command over and over again. For example, I use Python a lot and I have a directory (folder for Windows users) that I store my Python Scripts in, but its a few levels into my folders, so I had to type this big, long command

cd Documents/Python/Scripts

and it got tiresome. The fix? Create an alias.
While you can create temporary ones that only work until you close the terminal window, if you plan to use it over and over, you need a little more permanent solution and here’s how:

NOTICE: This has only been tested on Linux, Mac does not work by following this tutorial exactly, though there may be a work-around avaliable

Open up terminal and enter this command

sudo gedit ~/.bash_aliases

This will, after you enter your password, open up gedit and display the bash_aliases file. It should look something like this:

gedit_005

You can see where I have added my own aliases for my python scripts and programs I wrote and wish to quickly access (the lines that say #Quick alias for …)

`Ok already` you’re saying. Enough with all the jibber-jabber  and get on with the tutorial. Okedoke, I will.

For this tutorial I will make a command that makes sure you want to remove a file instead of just deleting it willy-nilly.

How to is as follows:

  1. Run the sudo gedit ~/.bash_aliases command to load the bash_aliases file into gedit so you can edit it
  2. Basically choose anywhere in the file to type, but towards the top makes more sense and enter this # This is the beginning of the comment that will tell what this alias is for
  3. Next, type in This alias is a command that makes sure I want to remove (rm) something. This is the comment about your alias
  4. Press enter and type this alias. This is the beginning of the alias programming.
  5. Type in rm. This text is the command (alias) we are creating. This command will replace the current rm command.
  6. Enter ="rm -i" after the rm text. This text we just entered is what the command (alias) will do when we enter it. It will run the real rm command, but with the -i (interactive) option attached. This will make you confirm the deletion (interactively) instead of just deleting it, perhaps accidentally deleting a file you really needed.
  7. Save the file and then exit gedit and then restart the BASH Terminal. Once you start it again, our new command will work and you will have successfully made your first permanent alias!

As you can see with my custom aliases, I added a comment explaining what each one does by placing a # before the comment text. While you don’t have to do it, it’s a good idea to, as it explains to you (and anyone else who might be using your system), what it does. Once you have (or haven’t) added the comment you can get into the programming for the alias.

alias [alias-name]="[command]"
[alias-name] is the command you want to create (eg. scripts)
[command] is the BASH command that your alias will call

Once you do this, save the file and exit. Don’t try to use your new command yet, you have to restart the shell first, so close the Terminal window and open it again. Once the Terminal has been restarted you can now use your command

Tagged , ,
%d bloggers like this: