Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Friday, December 25, 2009

Schedule a delayed shutdown

Several times I've initiated a download of a big movie file late at night before I go to bed.  Without knowing anything else, I'd normally have to leave my computer on all night just so that I could download that movie file that will probably only take up 3 hours.  Wouldn't it be nice if you could tell your computer to turn off after the time it takes to download something?

That's where the shutdown command in bash comes in handy!  Here's an example of the way I usually use it:

sudo shutdown -P 300

Firstly, you need to have root access to use shutdown, hence my use of the sudo command.  I used -P to indicate that I want to power off after shutdown (as opposed to rebooting, or 'halting').  Finally, I typed 300 to indicate that I want the computer to shutdown in 3.5 hours (300 minutes).

Another way you can use the command is by specifying the clock time at which you want your computer to shutdown in 24 hour format.  So let's say you want your computer to shut down at 4:15pm:

sudo shutdown -P 16:15

Type that in and your computer will shut down at 4:15pm.  That's it!

Tuesday, December 22, 2009

Split files into parts easily in bash

These days, USB sticks have obliterated the worry of not being be able to carry computer files from one computer to another.  However, there will be instances when you'll be tempted to email a file to someone and that file will be too big to attach to your email.  Whatever do you do!?!

This is where the split command in your Linux terminal comes in handy.  Let's say you have an .mpg file that runs for roughly 25 minutes and happens to take up 40 megabytes (e.g. 'speeches from wedding video.mpg').  Let's also say that your email server will accept attachments of maximum 10 megabytes.  So, we know that we need to split this .mpg file into 4 parts of 10 megabytes each.

Here's the command to use for the example file:

split -b 10000k 'speeches from wedding video.mpg' speeches.

Here's what the arguments mean:

  • -b tells the split command that you want to specify the 'byte' size
  • 10000k, or 10,000 kilobytes is the size of the parts we wanted (10,000 kilobytes = 10 megabytes)
  • 'speeches from wedding video.mpg' is the file we want to split into parts
  • speeches. tells the split command that all part files should begin with 'speeches.'
This will result in the creation of 4 new 10 meg files named:

  • speeches.aa, speeches.ab, speeches.ac, speeches.ad
Putting them back together again

Admittedly, this is a solution where the other person has to be a Linux user.  The person on the other end has to navigate to the directory where the part files are stored and enter in the following command:

cat speeches.?? > 'speeches from wedding video.mpg'

Voila!  Humpty dumpty has been put back together again :)



This neat Linux trick has been brought to you by an awesome book I'm currently reading called Linux All-in-One Desk Reference For Dummies.  I heartily recommend it to Linux newbies!


Monday, December 21, 2009

Finding Files in Google Desktop and bash

    Sometimes I want to find a file and I have no clue where it is.  When I was using Windows Vista, this was a simple matter of using the native file searching utility, which dutifully searched for my lost file using the index it created of my hard drive.  File indexing is an amazing thing to take advantage of.  When your computer searches a file index, it gets results FAST! 

    If you don't have a file index (remember, it's literally a simple listing of every single file you've put on your computer in one relatively small database file) then your computer will have to loop through every single file on your computer, trying to match your search argument (e.g. 'Resume*.pdf') to the file names that it processes.

Today I'll talk about three linux programs for indexing and/or finding your files:
  • Google Desktop (for easy indexing and finding using a GUI)
  • locate (for finding indexed files in bash/terminal)
  • find (for finding files in bash/terminal)



Google Desktop

Even if you are using windows, click on the link above and get this program.  It's VERY convenient to use!  Google Desktop will not only allow you to search for your files, but emails that you've sent and received as well!  Once you download it, you will want to make sure that it builds an index of the directories in which your important files will be stored.  As shown below, right click on the Google Desktop icon and click on Preferences.  On my computer I have it indexing my home user directory and my USB drive root directory.  You can of course add any extra folders you like by click on Add folder to search.


Preferences are opened in your default browser

You may want to modify settings in the other tabs shown above.  Go to the Gmail tab and follow the instructions there if you would like Google Desktop to index your gmail account.  Also, go to Display and click on the Change Hotkey button lower down on the page in order to set up a key combination that will bring up a quick search box (shown below).


My key combo:    Shift+Control+?


When you're sure that everything is set, click on Save Preferences and Google Desktop will start indexing your files!  Bear in mind that the process is slow, so you'll have to give it several hours until it's done indexing everything.  You can check on Google Desktop's progress in indexing by right-clicking on the Google Desktop icon, then on Index, then on Index Status.  You'll then see its progress in indexing all of your files and emails.

What I really like about Google Desktop is the simplicity in finding and opening anything on your computer.  Type your key combo, quick search box comes up, type in your search argument, click on the result matching the file you were looking for, then your file opens!



locate

As I understand it, Ubuntu 9.10 (and i'm sure basically every other distro of Linux) indexes all my files for me on some periodic basis.  This is what enables the locate program to work in the terminal.  locate searches through the generated file index database for a term of your choosing.  So let's say I want to search for the manual I recently downloaded for my Hammer Drill, but I have no clue where it went!  Here's how I would use the command to find my file:

inkhorn@inkhorn-laptop:/$ locate -i hammer

Here's what the arguments I used mean:

  • -i tells the locate program to ignore the case of your search term
  • hammer is simply the search term

Here is the output from the locate command:

/home/inkhorn/Documents/Hammer Drill Manual.pdf
/usr/share/pixmaps/pidgin/emotes/default/hammer.png
/usr/src/linux-headers-2.6.31-15/arch/avr32/boards/hammerhead
/usr/src/linux-headers-2.6.31-15/arch/avr32/boards/hammerhead/Kconfig
/usr/src/linux-headers-2.6.31-15/arch/avr32/boards/hammerhead/Makefile
/usr/src/linux-headers-2.6.31-16/arch/avr32/boards/hammerhead
/usr/src/linux-headers-2.6.31-16/arch/avr32/boards/hammerhead/Kconfig
/usr/src/linux-headers-2.6.31-16/arch/avr32/boards/hammerhead/Makefile

The first result is the one I'm looking for!  Now I know where to look to open my hammer drill manual.



find

The find command has to loop through each and every file in the directory you specificy in your search argument.  If you told it to search the root directory '/' for a file with the word 'Hammer' in it, your computer would probably take an intolerably long time finding it.  Happily enough, you don't need to do that and probably have an idea generally where it is.  To use my hammer drill example:

inkhorn@inkhorn-laptop:/$ find /home/inkhorn -iname 'Hammer*.*'

Here's what the arguments I used mean:
  • -iname tells find to ignore case when comparing your search argument string to the file names that it loops through
  • 'Hammer*.*' tells it to look for a file of any extension (.*) starting with the word 'Hammer' and continuing with any other words after that (*)

Here is the output from the find command:

/home/inkhorn/Documents/Hammer Drill Manual.pdf

Cool thing is that the find command only outputs file results while the locate command outputs directory results!  In this instance, the results came on screen really quickly.

Now for something even cooler:  I can enter in a command that looks for a file that I'm interested in and opens it in a program that reads it!  Here is what I would enter in if I wanted to look for the hammer drill menu in terminal and automatically open it up in an excellent PDF reader named Okular:

inkhorn@inkhorn-laptop:/$ find /home/inkhorn -iname 'Hammer*.*' -type f -exec okular '{}' \;

You've already seen the first part of the find command in action.  The second part tells the find command to execute a program called 'okular' that should then take the find results as an argument.  In other words, okular opens up the file listed in the find output.

You can use this to open up other file types that you want to find.  If you're looking to open a word document, replace 'okular' with 'oowriter', a spreadsheet, replace 'okular' with 'oocalc'.

For more info on using find and locate, click here.