Tag Archives: command-line

Linux – Find Command Examples and Tutorial

The find command is a powerful and versatile tool that can do much more than search for files. There are many ‘tests’ that can be added to make the search as specific as needed. Complex searches can then be combined with ‘actions’ to be performed on files that match. Here are a few examples:

***  ALWAYS perform searches first with NO COMMANDS on the end (like -delete or -exec). There is NO UNDOING a bad command so make sure you know what files will be affected.  ***

  • Find files by name (such as copies). As with many commands, the argument right after find tells it where to start looking. Using . says to start looking from the terminal’s current directory. The -name test uses unix-style wildcards and must match a complete file or directory name. Placing a * on each side of your query will find it in the middle of a name. The last part is a test for file type. The most common flags for -type are f for file, d for directory, and l for symbolic link.
  • Find empty folders and delete them. Here were are replacing the -name test with a simple check for -empty because we are looking for empty directories. We can then specify -type d to find only directories. The last part is a call to the action -delete. Use this carefully and remember, always search first and only use actions after you know what will be affected.
  • Find iPhone screenshots and move them to a screenshots folder. This example uses the -exec action flag, which can be used to perform almost any command on the found files. This flag really shows off the incredible usefulness of the find command. This time, were are not searching from current directory, but telling find to start from the “~/iPhonePics” folder. For the -name test, we are looking for any files ending in “.png”; so in this case we need only one wildcard to fill out the start of each filename: “*.png”. The -exec action has extra requirements for a complete command. Brackets {} are where each found file path will be inserted into the command. Also, the command must be finished off with \; to let find know that’s the end. In this case, the mv command will be performed on each file and told to move them from anywhere in “iPhonePics” to my “~/Screenshots” folder.

I hope these three varied examples have given you inspiration on how to add the Linux find command into your tools. I am using this on Ubuntu 14.04 and version 4.4.2 of GNU findutils. Whatever version or Linux system you have it should work about the same. Please ask in the comments below if you have any questions! Have a great day!

Rainbow Terminal Text With Lolcat

Like seeing rainbows all over the place? Does it make you especially happy to have a colorful terminal? Yeah, me too. There’s a little Ruby program called lolcat and it will help you out (in the happiness department).


Steps to a super colorful terminal:

  1. Install lolcat in one of two ways.
    1. As a Ruby gem using the standard Ruby utility.  gem install lolcat
    2. Through your system’s package manager.  sudo apt-get install lolcat
  2. Pipe your program through lolcat.
    • cat cool_file.txt | lolcat

That’s really it. The only problem I’ve come across is when piping through a program that is still in control of the terminal output.  My main example is with Pianobar, the Pandora CLI app. It doesn’t actually break anything, you just can’t see the last line of the terminal. This means the song progress can’t be seen, and it can be a problem if the user is expected to enter information. I’ve gotten used to it though, it’s really worth it for the colors!

Have fun with rainbows all up in your face!



Guake Terminal – Dual Monitor Edits

For a long time my favorite Terminal program has been Guake. It is “a drop-down terminal for GNOME Desktop Environment.” It is always available and hidden when not in use. Simply press a button and it pops out from top of the screen, press again and it goes away.

To install Guake simply  sudo apt-get install guake

The only problem I’ve had with it is on my desktop machine when two monitors are plugged in. Guake always defaults to the left monitor. It does a great job of determining the size of ‘monitor 1’, which in my case is a much smaller extra one to the side. To get Guake on my right-side monitor I had to tweak the source code. Here’s how you can do the same:

  1. Make a copy of theGuake program and put it in yourbin folder. I renamed mine toguake-dualmon but you can call it whatever you want.
  2. Edit the copy in your preferred editor.
  3. Find the method definition  def get_final_window_rect(self):
  4. First we will correctly position the terminal on the right monitor. Add one line at the end, between window_rect.y = 0  and  return window_rect . The window_rect.x and window_rect.y variables tell theGuake window where to be located. Set window_rect.x to be the width of your left monitor and window_rect.y will depend on how offset the monitors are. I had to play with the ‘y’ setting to get it just right or the text starts off the top of the screen.
  5.  Now Guake will be positioned on your right monitor, but it will still be the size of the left one. In my case it was sized at 1280, and I needed it to be 1920. Divide your right monitor’s width by the left monitor’s width (ie. 1920/1280 = 150). Still within  get_final_window_rect(self): you will find the line width = 100 . This setting is a percentage of your left monitor, so set it to the answer you got by dividing one width into the other. In my case it was:
  6. That’s it! Just make sure to always run your copy of the program, or better yet add it to your autostart so it runs automatically!

Here’s the new get_final_window_rect() method looks like with edited lines highlighted:

Have any questions? Feel free to ask in the comments below. Thanks for reading!

Python Snippet: Get Terminal Width

There is a wonderful command on UNIX systems to return the width of the current terminal. Enter  tput cols and see for yourself! Tput can actually provide a lot of different info by querying the terminfo database. Really all I’ve needed though is ‘cols’ for columns.

I was writing a Python CLI app, and wanted to format and print output in a nice table. To do that (without downloading a table printing module) I had to know how much space was available for printing. Can’t make a nice-looking set of data if every other line is being wrapped!

Here is a simple function that returns the width of whatever terminal is running the script. Subprocess is the most current built-in Python module for running system commands. It contains a selection of different functions each with many optional parameters, depending on the complexity of your needs. Using subprocess.check_output() is the way to go if you are looking to get back the output of a command. The  check_output() function has a whole slew of accepted arguments, but in this simple instance we need only one: a list containing command to run and the arguments to pass to it.

The first exception will be raised if the system returns an error when trying to execute the tput command. The second exception will be raised if the command (in our case tput) has a problem with the given argument ( cols). If nothing goes wrong, the output will be a nice whole number that you can use to determine exactly how many characters can be printed per line!

Have you ever needed to know the width of your terminal? If you have any questions please feel free to ask in the comments below!

Here’s a link to this code snippet on Github Gists: terminal_width.py

Finally, Netflix on Ubuntu

In my house we watch a lot of instant Netflix. So, as you can imagine, it was pretty sad finding out it wouldn’t work when I changed my laptop to Ubuntu Linux. The Netflix technical blog has talked about their eventual switch to all HTML5 video. Hopefully they won’t continue blocking Linux users after they finalize the change, as the new encoding should be platform independent.

Last time I checked, the only workaround I could find was Netflix-desktop. It runs Wine with a windowless Firefox browser inside. Many of the reviews said it was stable but with choppy playback, especially for older machines. My Acer laptop certainly wouldn’t handle that (and my tower is dual-boot so not worth it).

Enter Pipelight! This great program allows you to install Silverlight onto your Linux browser. Included are a few other normally unavailable plugins like Flash and Shockwave.  Installing Pipelight does not activate any plugins on its own. You manually decide what to activate and whether it’s system-wide or current user only. So, we add two repositories, and install the software. Make sure to quit any web browser before installing.

Now you activate Silverlight. This will be for your current user only, so just add  sudo to apply Silverlight to all users.

Now open your browser of choice and Silverlight will begin installing. There’s one more step left. Netflix will still see you as a Linux user and redirect you away from watching videos. This is where a browser plugin comes in. There are two options for Firefox and one for Chrome. I’m a fan of Chrome and went with User Agent Switcher. If you’re using Firefox, you’ll need to go with UAControl or User Agent Overrider. Whichever browser you’re on, the advice I’ve seen says to select the newest version of Firefox available in the browser plugin.

The plugin for Chrome, User Agent Switcher, had some reviews about it not deactivating properly for certain sites. Because of this, I created a new Chrome user to install it in (which can be done in Settings). Separate users have a different set of extensions, allowing your Netflix viewing to have no impact on your normal browsing.

Now you’ll be having smooth playback without a full Wine instance. Pipelight does use parts of the Wine code base, but it’s not resource heavy as far as I’ve seen. Netflix played beautifully with this setup on my four-year old Acer laptop. If you can’t live without instant streaming on your Ubuntu machine (or other Linux distro) then this seems to be the best option.

If you give it a go, let me know if you have any questions or run into any problems!

Source: WEB UPD8 Pipelight: Silverlight In Your Linux Browser | Ask Ubuntu Netflix Streaming Question