ubuntu dual boot illustrated site ubuntu dual boot windows 7 maverick meerkat
Command Line Page 

Edited Wednesday January 4th 2012  Document made with KompoZer
PAGE INDEX
Beginners Start Here

Fun With Linux Commands - including: Using your computer for an alarm clock - how to edit /etc/crontab

General Purpose Commands

File Ownership and Permissions

File Compression

System Administration

Networking

Putting commands together -  |  >  >>

apt

Hardware Detection and Testing - hardware and software testing and diagnostics

Internet and Downloads  -  btdownloadcurses, wget, youtube-dl

Text Editors - gnu nano, gedit, kate, mousepad, vim


For Beginnners
These days you don't have to learn Linux commands to be able to use Linux. Most Linux based operating nowadays have all the GUI programs you need to do almost anything.
Learning how to use Linux commands is fun though, and there are still a lot of useful and time saving things we can do with Linux commands.
The main thing to know is that you don't need to force yourself to learn anything and everything right away. Give yourself plenty of time. You'll be surprised how much you can learn without even trying.

One hint that everyone should be aware of is that if someone (from a web forum maybe), recommends you run a Linux command, you should look up the 'man' page for the command to see what it will do before you apply it. The word 'man' in Linux is short for 'manual'.  That's where we look up commands we want to learn how to use them better.

To do that you open your terminal and type: man <name-of-command>

The man pages are little like a dictionary or an encyclopedia, but for Linux commands. It's a good idea to learn how to read the man pages. They contain lots of really interesting information and can help you get the most out of Linux.

Linux commands are a bit like ordering a hamburger, just a plain, bare hamburger is okay, but a hamburger with your own favorite combination of ingredients is a lot better. You might order your hamburger with onions, salad or coleslaw, ketchup or mustard,  cheese, an egg, and so on.
Linux commands are a lot like ordering a hamburger, they have lots of 'options', and these options make a big difference to what happens when the command is run. You can run Linux commands with no options, but it's a bit like ordering a plain hamburger - boring. If you want to get the most out of Linux commands, you need to be able to specify the right options, just like when you specify what you want in your hamburger when you order it.

The man pages are for telling us what options are available for each command and what the options are supposed to do. 
The problem for new users is, the 'man pages' were written mainly by the programmers themselves, and they're fairly brief and concise. 
Most new users have a hard time understanding what the man page is trying to tell them. This creates a kind of recursive problem. The new user is the very person who probably needs to be able to access the information in the man page most urgently. Unfortunately, the man pages are full of strange words and concepts. For example, if the new user doesn't know what 'recursive' means, they will have no idea what I'm talking about right now.

Most of the time we still need to try out the command and see for ourselves what happens with different options before we feel comfortable using that command.
It takes time and practice to feel confident running Linux commands.

The best place to start learning how to use Linux at the command line is from one or two of these great Linux tutorials,

 'Terminal For Beginners' , by kryal of Ubuntu Web Forums would be a great help. It's a very informative thread, it's a Ubuntu Web Forum thread. Some Linux tutorials are more specific to other distros and some of the commands work a little differently, so it's nice to see an good all-Ubuntu tutorial, thanks Kryal!

UNIX Tutorial for Beginners
This one's about Unix, which is the language that Linux is based on. Linux uses unix commands, plus some extra ones that Unix might not have. This is the first one I began learning from, and I still refer to it often. There are a few things here that might not directly apply to Ubuntu, but it's a great tutorial.

Linux Command.org

Linux Online - Linux courses

The Linux Tutorial

A Hands on Guide  by Machtelt Garrels

Rute User's Tutorial and Exposition by Paul Sheer

TLUG CS Student Linux Users Guide

HowTO Ubuntu System by ajmorris

These are just a few, there are plenty more you can google if you like.

Terminal
Most people will already know what a terminal is. In Ubuntu, you can find the 'terminal' by going 'Applications'-->'Accessories'-->'Terminal', and click on 'Terminal'.

I find it handy to right-click on the accessories menu link for 'Terminal', and click 'Add this launcher to panel. That will add an icon for it on our top panel (or tool bar if you are an ex-windows user) so it will be quicker to find it from now on. Terminal is something we all use a lot.

When your terminal is open you can type Linux commands after the prompt.
A Linux terminal prompt looks some thing like this,
Code:
herman@bookpc:~$
'herman' is my username,
               '@bookpc' means I'm at a computer named 'bookpc',
                              '~', which is a symbol called a 'tilde', is a shorthand way of saying I'm working in my /home/username directory right now.
                                  '$', is the prompt, and it's a dollar sign to indicate that I have a BASH shell

We can type commands after the prompt: $      and when we press our 'enter' key we expect our computer to do something, (run the command).

Opening a 'tty'

With Ubuntu booted up, try pressing 'Ctrl' + 'Alt' + 'F1', (or any other F key from 2 to 6), and that will give you a 'tty'.
Not so many new users will know how to get a 'tty' and it can be quite useful. A 'tty' is something like a terminal.
You can log in with your username and password then, and practice Linux commands there.
If your gnome desktop (Graphical User Interface) doesn't load, or if it ever freezes, sometimes you can't open a terminal, but you might be able to open a 'tty'.
Most of the time, I use a terminal for my Linux commands but it's good to know that a'tty' is there for an emergency.
bookpc login: _
Here you are supposed to log in with your username and password before you can do anything.
Press F7 to go back to your Desktop.

Some Fun With Linux Commands
W3M, the terminal based web browser

bc the stone age calculator (he-he, only joking,  actually it's quite modern and very useful)

cal is short for calendar

espeak - make your computer talk to you

Alarm clock (set your computer to wake you up in the morning) (now it makes coffee too!)

Camera Tricks With GNU/Linux - Time lapse photography and geotagged





W3M, the terminal based web browser

This is kind of neat open a tty or a terminal and type the following,
Code:
w3m www.google.com
That gives you a command-line type of web browser, try it! You can't use your mouse, but you can move the cursor around with keys on your keyboard and type. It might take some experimenting to become skilled at using it.
Type 'q' for quit, to exit w3m, then 'y' for yes.
When you are finished using the tty, type 'exit' to log out of the tty.,just press 'Ctrl' + 'Alt' + 'F7' to return to the normal G.U.I.

There are tty's for F2, F3, F4, F5, and F6. (When those keys are pressed simultaneously with 'Ctrl' + 'Alt'
'Ctrl' + 'Alt' + 'F7' keys return you to GUI mode.



'bc' the terminal based calculator

You can also type 'bc' for a terrific text-mode calculator that can handle a lot of digits. One of the great features of this calculator is you can press your 'up arrow' key to scroll back to previously entered operations. Very handy and convenient. You will discover more about bc when you start practicing with it. When done, type 'quit' to get back to your command prompt.
bc



cal is short for calendar

Another little trick is to type 'cal' for a calendar. Today's date will be highlighted.
Code:
cal

cal -y will give us a calendar for the whole year,
Code:
cal -y

If we type the whole word 'calendar', (as a command), we will be given a list of historical events for today's date,
Code:
calendar

If we type 'date' as a command, we will be given the current date and time.
date

TIP: You can change the color of your terminal by right-clicking on an open terminal window and then click 'edit current profile', and in the 'Colors' tab, remove the check mark from the square for 'Use colors from system theme'. Then you can choose your own text color and background color for your terminal. If that isn't enough for you, you can click the 'effects' tab, and choose a background image, or select the radio button for 'transparent background', and even set the opacity with the slider under there.



espeak - make your computer talk to you

Code:
espeak "hello Joe"

Try it!
You can type any words or sentences after the 'espeak' command and your computer will repeat the words you typed through the speakers.



Use your computer for an alarm clock

1. Learn how to play a sound file from the command line

(i) totem media player can be started from the command line.
totem Music/<tab>
TIP: Use 'tab completion'.

(ii) Use totem to play a sound file from your terminal, 
totem Music/track01.ogg
Where 'Music/track01.ogg' is a sound file in my computer.

(iii) be ready to copy the command you used to play the sound file, 
totem Music/track01.ogg



2. Edit /etc/crontab
The following command will open my text file called /etc/crontab with my text editor 'gedit'.
Code:
gksudo gedit /etc/crontab

my /etc/crontab file before editing:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user    command
17 *    * * *    root    run-parts --report /etc/cron.hourly
25 6    * * *    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily
47 6    * * 7    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.weekly
52 6    1 * *    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.monthly
#
Here's what my /etc/crontab file looked like before I added any of my own commands to it.
There are four commands in here already. These are for running anacron scripts in the directories cron.d cron.daily, cron.weekly and cron.monthly. Those control regular operating system functions. It would be best to leave those alone unless you know what you are doing.

Now I'm going to edit my /etc/crontab file by adding a new line of my own, but first I need to explain the meaning of the figures are in each of the columns. We really can't see any columns there at all. If it was a spreadsheet we would be able to see the columns. Since it's only a plain text file, they're not so easy to distinguish. We need to know they're there.
mhdommondowusercommand
m the 'm'   column is for minutes, you can type any number here from 0 to 59, or an * means 'every' minute

hthe 'h'   column is for hours, you can type any number here from 0 to 23, or * for 'every' hour


domthe 'dom' column is for 'day of the month' from 1 to 31, or * for 'every' day



monthe 'mon' column is for 'month of the year' use a number 1 to 12 or type 'march', or * for 'every'




dowthe 'dow' column is for 'day of the week', mon=1, tues=2, wed=3, thu=4, fri=5, sat=6, sun=7





userthe user column is for username, herman in my case, or possibly root






command: the 'command' column is for a command, (to play the music)

So if I want to play an .ogg sound file early every morning to wake me up in time to get ready for work, I will add a line like this,
mhdommondowusercommand
154***hermanexport DISPLAY=:0 && /usr/bin/totem /home/herman/Music/track01.ogg

15 4 * * *  herman export DISPLAY=:0 && /usr/bin/totem /home/herman/Music/track01.ogg

That line means at 15 minutes past 4 every day of (the) month, every month, every day of (the) week, my computer will run the program called /usr/bin/totem, which is to open the file named /home/herman/Music/track01.ogg

The 'export DISPLAY=:0 &&' part is special.
That part of the command is only needed because totem is really a GUI application, and therefore we need to tell crontab which X-server to use. T
hanks to henriquemaia for this how-to: crontab: How to run GUI programs with cron.
I originally made this how-to with the program called 'play', which was a terminal based sound player, so I didn't need the 'export DISPLAY=:0 &&' part, but the 'play' program doesn't seem to exist anymore. I tried mplayer which is another terminal based media player which is supposed to be good. Mplayer works from terminal but unfortunaltely I couldn't get mplayer to work from crontab.

my /etc/crontab file after editing:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user    command
17 *    * * *    root    run-parts --report /etc/cron.hourly
25 6    * * *    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily
47 6    * * 7    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.weekly
52 6    1 * *    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.monthly
#

15 4 * * *  herman export DISPLAY=:0 && /usr/bin/totem /home/herman/Music/track01.ogg 
This is what the file looks like with the command typed, (or copied and pasted) in place. You have to click 'save' too, for it to work.
(Yours won't be in color of course, and mine isn't either, the highlight colors shown here are only to make it easier to show you what I did).

NOTES:
1. To get the command right, it's best to run it from a terminal first, and then copy your command from there. Sometimes there are some strange syntax peculiarities in some of the filenames, especially if they're not native to Linux.
2. The program named 'totem' is located in /usr/bin. I recommend telling crontab the exact, complete file path for the program just to make sure, but it will probably still work okay if you just type 'totem' without the whole filepath.
3. You should test run your new crontab by setting the time for a few minutes from now and waiting to see if the file will play to be sure it will work as expected.
Your cron job needs to be set for at least two minutes ahead of time to give the cron daemon time to register the new cron job. The cron daemon runs every minute to scan the files for changes.
4. To check and see if the cron daemon is running, try this command,
Code:
ps aux | grep cron
5. If crontab is running and you still can't get crontab to work, check your command once or twice again for errors.

Once you get the knack of using crontab,
it is possible to do much more with crontab than this. You can get crontab to do almost anything, automatic backups, send or receive email...
As a matter of fact I bet I can even get my computer to turn my kettle on for me five minutes before it wakes me up in the morning!   :)

Take a look at this,
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user    command
17 *    * * *    root    run-parts --report /etc/cron.hourly
25 6    * * *    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily
47 6    * * 7    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.weekly
52 6    1 * *    root    test -x /usr/sbin/anacron || run-parts --report /etc/cron.monthly
#
10 4 * * * herman /usr/bin/eject ; /usr/bin/eject -t
15 4 * * *  herman export DISPLAY=:0 && /usr/bin/totem /home/herman/Music/track01.ogg
The eject ; eject -t command will cause the CD-ROM drawer to open and close at 04:10 every morning.
Now if I somehow place a switch for the kettle where the CD-ROM drawer is likely to strike it on its way open... : ) 

Actually, there is a website already devoted to having Linux make coffee, it's quite a sophisticated website too, 'Linux DOES make coffee and it tastes good as well!', Coffee Making.
And not only that, there's even 'Life After Coffee' too!
I really like that Coffee Making tutorial, but I have a problem.
Where I live it would be hard to get the right electronic parts to make a relay switch or a triac for my computer's parallel port.

I wanted to see if I could get my computer to make coffee for me using low cost, common everyday household items that anyone, anywhere would have lying around or could easily get ahold of.
  • My computer happens to be situated next to a wall, so I glued an old icecream stick to the wall with blu-tack and some scotch tape.
  • The icecream stick supports a marble and holds it right in the path of the CD-ROM drawer when it opens.
  • The CD-ROM drawer knocks the marble off the stick and it falls on a mouse trap.
  • The falling marble sets off the mouse trap.
  • The mouse trap hits the switch for a five socket power board and turns the power on for my electric kettle.
Well, at least it boils the kettle for me, if I had a coffee percolator it would make coffee. The principle is in place.
If you want to see photos of my set-up, I have uploaded them to my gmail picasa site, click this link to take a look, http://picasaweb.google.com/herman543/HowMyComputerMakesCoffee

Now back to business, here's a link to the Official Ubuntu Wiki's How-to for cron, CronHowto
It says there that it's better to use the other method for setting up a cron job, the crontab command. I wanted to start with this /etc/crontab method just explained though, because it's a little simpler for beginners.
To use cron, you need to learn only a little something more, how to use nano text editor. It's not hard to use nano, but I wanted to take things one step at a time and not hit new users with too many things at once.
Here's a link to my 'Back up and Restore Page', where I have an example of the use of crontab to make regular backups for email and other stuff. Back Up and Restore.
Now that I have shown you how you can make a cron job by editing /etc/crontab, it's only two more steps to learn to use the crontab command and nano instead, and do things the recommended way.

Another way to set alarms in Ubuntu is by configuring reminders in Evolution's Calendar to run a program, see Memos, Tasks and Calendar Appointments.


Camera Tricks With GNU/Linux

Did you know that some digital cameras and probably most webcams can be controlled by Linux?
That means we can use our Ubuntu computer to tell  the camera when to take pictures or video.
In other words, it's possible to use Ubuntu for time lapse photography.
Time lapse photography is really cool. Just try a google search for a few examples of time lapse photography and I'm sure you'll be impressed.

I first read about this at the following website,  A Webcam Server with Linux, and I think that most people would find that entire website very interesting and helpful, especially if you enjoy having fun with Linux using the command line.

After  reading Professor  Marzolla's excellent website,  (linked above), I was inspired to try something like that myself, but with a little bit of a difference. For my purposes I wanted to be able to take pictures and at the same time use my USB GPS receiver and have each photo geotagged in real time, (immediately after each photo is taken). Here is a link to a thread in Ubuntu Web Forums where I posted details about that, geotag webcam images.

In the course of my research into time lapse photography, I also learned that it's possible to 'pan' the camera, (have it turn a little in between shots), and when this is done well, it becomes a special effect which adds to the quality of the finished product.  To acheive this, I read that it is possible to use a program in called  pyrocket to
control a toy rocket launcher with a camera on it instead of the cannon or missile launcher.





Links that explain all about specific commands

The umount Command - The Linux Information Project

The head Command - The Linux Information Project

The useradd Command - The Linux Information Project

Checking disks for errors using the badblocks command - Ubuntu Magnet

Learn The DD Command Revised - Linux Questions.org - by awesome machine

Genisoimage – Creates ISO-9660 CD-ROM filesystem images - Debian Admin


General Purpose Commands

awk '!a[$0]++' ./all_roads.csv > ./all_roadsII.csv
copy data containing duplicate lines from a file and creat a new file without any duplicate lines, even if the duplicated lines are non-consequtive
lslist of the files and directories inside a directory, in alphabetic order running across the page (horizontal)
ls -ashow a list of the all (including invisible) files and directories inside a directory, in alphabetic order running across the page (horizontal)
ls -lshow files and directories in a long list,  vertically listed (columns), in alphabetic order, including ownership and other details of each file
ls -lXshow files and directories in a long list, (columns), sorted by filename eXtension, including ownership details of each file
ls -lSshow files and directories in a long list, (columns), sorted by file Size, including ownership details of each file
ls -ltshow files and directories in a long list, (columns), sorted by last modification time, including ownership details of each file 
ls -lhshow files and directories in a long list, (columns), with humanly readable file sizes in alphabetical order, including ownership details of each file 
cdchange directory
cd /usr/local/apachego to /usr/local/apache/ directory
cd~go to your home directory
cd-go to the last directory you were in
cd..go up a directory
catshort for 'catalog' - print file contents to the screen
cat /boot/grub/menu.lstcat the contents of /boot/grub/menu.lst to your screen
taillike cat, but only reads the end of the file
tail /var/log/messagessee the last 10 (by default) lines of /var/log/messages
tail -f /var/log/messageswatch the file continuously, while it's being updated
tail -200 /var/log/messagesprint the last 200 lines of the file to the screen
morelike cat, but opens the file one screen at a time rather than all at once
more /usr/sbin/update-grubbrowse through the usr/sbin/update-grub file. hit Space to go to the next page, q to quit
cpthe copy command - make another copy of a file
cp filename filename.backupcopies filename to filename.backup
cp -a /media/oldinst/home/* /media/newinst/home/copies all files, retaining permissions from one directory to another
cp -Rav *. ./newdircopies all files and directories recurrsively in the current directory INTO newdir
mvthe move command - move or rename a file
mv oldfilename newfilenamemove a file or directory from oldfilename to newfilename
rmdelete (remove) a file
rm filename.txtdeletes filename.txt, will more than likely ask if you really want to delete it
rm -f filename.txtdeletes filename.txt, will not ask for confirmation before deleting
rm -rf tmp/ recursively deletes the directory tmp, and all files in it, including subdirectories.
BE VERY CAREFULL WITH THIS COMMAND!!!
echo 'hello world'The echo Command - linfo.org
prints messages in the terminal screen, useful for bash scripting for informing users
UNIX / Linux echo command examples - nixCraft Linux Forum
findYou can use this command to search for files if you know the name of the file you're looking for
sudo find / -name oo-welcome.odtfinds files with the name 'oo-welcome.odt'
find documents/textfiles | grep diannelooks for all the files in /home/herman/documents containing the word 'dianne'
find /media/Windows -iname "*.jpg" -print finds all .jpg images in Windows and shows you the list of thier names
find /media/Windows -iname "*.jpg" -exec cp {} rescue \;
finds all .jpg files in /media/Windows and copies them to a directory named rescue
find /Pictures -iname "*.jpg" -exec mv {} Pictures/jpg \; for sorting your files, rounds up all .jpg files in pictures and moves them into one directory
locateIt's better to use 'locate' instead of 'find' , because 'find' thrashes your hard drive.
sudo updatedbCreate or update the database used by the locate command
A cron job is run by default in Ubuntu to update the locate database every night
locate oo-welcome.odtfinds files with the name 'oo-welcome.odt'
grep looks for patterns in files
grep root /etc/passwd shows all matches of 'root' in /etc/passwd
grep -v root /etc/passwd
grep --color -i -n -o -R  -e grep .searches for files containing the word 'grep', in entire home directory.
grep --color -i -n -e ffmpeg .bash_history search your .bash_history file for the word 'ffmpeg', and print the lines.
TIP: You can use this to remind yourself of all ffmpeg commands you used in the past.
grep --color -i -n  -C3 -e grep ./file.txt searches the file 'file.txt' for the word 'grep', prints 3 lines of context
grep -A 100 'End Default Options' /boot/grub/menu.lst
wcword count
wc -l filename.txt tells how many lines are in filename.txt
which grubwill return the results of where a file named 'grub' is found
file /usr/sbin/grub the file command tries to find out what kind of file a file is by looking at it's content.
file /usr/sbin/* prints out a list of all files/directories in a directory, in this example, /sbin directory
stat filenameprints out a lot details about a file such as size, last access time, mod time, changed time, etc.
cmp file1 file2The cmp command compares two files byte-by-byte and displays the first occurrence where the files differ.
cmp file1 file2 -lcommand compares two files byte-by-byte and displays a long list of occurrence where the files differ
diff file1 file2compares two files, reporting all differences, similar to the cmp command, but the output is more verbose
comm filename1 filename2The comm command compares two sorted files and displays the instances that are common.
The display is separated into 3 columns.
column 1 displays what occurs in first files but not in the second
column 2 displays what occurs in second file but not in first
column 3 displays what is common in both files
lncreate's "links" between files and directories
ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf
Now you can edit /etc/httpd.conf rather than the original. changes will affect the orginal, however you can delete the link and it will not delete the original.
touchcreate an empty file


File Ownership and Permissions

chmod
















changes file access permissions.
The set of 3 go in this order from left to right: USER - GROUP - ANYONE
Here's a table for quick reference, the numbers 1 to 7 is this table are to tell you what numbers to use in your chmod command.
The first number is the file owner's permission, and the second and third numbers set the group and other's permissions.
ownergroupanyone
No permission - - - 0
Execute only - - x1
Write only - w -2
Write, execute - w x3
Read only r - -4
Read, execute r - x5
Read, write r w -6
Read, write, execute r w x7
No permission - - - 0
Execute only - - x1
Write only - w -2
Write, execute - w x3
Read only r - -4
Read, execute r - x5
Read, write r w -6
Read, write, execute r w x7
No permission - - - 0
Execute only - - x1
Write only - w -2
Write, execute - w x3
Read only r - -4
Read, execute r - x5
Read, write r w -6
Read, write, execute r w x7
sudo chmod 644 webpage.htmlchmod 644 : the owner can read and write to this file but everyone else can only read it
sudo chmod 700 private_filechmod 700 : the owner can do anything with this file, but no-one else is allowed to see it
sudo chmod 755 myscript.shchmod 755 : to make scripts work they need to have execute permissions
sudo chmod 777 public_filechmod 777 : anyone is allowed to do anything with this file
chownchanges file ownership permissions. The set of 2 go in this order from left to right: USER - GROUP
chown herman myfile.txt changes the owner of the file to herman
chown herman.herman myfile.txtchanges the owner to herman and the group of the file to herman


File Compression




tar -cvvf stuff.tar dir/

tar -xvvf stuff.tar
tar Used for creating and extracting .tar and .tar.gz files, 't.a.r.' is short for 'TapeARchive', (was originally used for cassette storage media).

 create a tarball of all files in the named directory called stuff.tar
(quick - no file compression, just an archive)
 extract the named directory from a tarball called stuff.tar
tar -czvvf stuff.tar.gz dirname

tar -xzvvf stuff.tar.gz (or .tgz)
 create a tarball of all files in the 'dirname' directory and call it stuff.tgz (with file compression)
(.gz is faster than bz2 but gives less compression).
extracts the files inside stuff.tar.gz
tar -cjvvf stuff.tar.bz2 dirname

tar -xjvvf stuff.tar.bz2 (or .tbz2)
 create a tarball of all files in the 'dirname' directory called stuff.tbz2 (with extra file compression)
(.bz2 is slow and gives more compression than .gz).
extracts the files inside stuff.tar.bz2
zip files.zip file1 file2 file3

unzip filename.zip
compress the three files, display the rate of compression of each file, and store them in files.zip

decompress a (*.zip) file zipped with a DOS compatible compression utility.
gzip filename

gzip -d filename.gz 
compress filename and turn it into a *.gz file

Decompress the file, extract it
zip filename

 gunzip filename.gz

bunzip2 filename.bz2

compress filename and turn it into a *.z file

decompress a *.z or a *.gz file

decompress a *.bz2 file

links: File Compression - Quick explanation, and File Compression Tools For Linux - Another explanation with just a few more details.



System Administration

lsb_release -afinds out what version of Ubuntu you're using
lastshows who logged in and when
last -20shows only the last 20 logins
last -20 -ashows last 20 logins, with the hostname in the last field
wshows who is currently logged in and where they are logged in from
whoThis also shows who is on the server in an shell


top
top shows system processes, memory information, uptime and other useful info in a live table

then type Shift + M to sort by memory usage or Shift + P to sort by CPU usage
To find and 'kill' a runaway process


du -sh

du -sh *
du shows disk usage.

shows a summary, in human-readble form, of total disk space used in the current directory, including subdirectories.

same thing, but for each file and directory. helpful when finding large files taking up space
ps




ps U username 

ps aux

ps aux --forest
ps is short for process status, which is similar to the top command. It's used to show currently running processes and their PID.

A process ID is a unique number that identifies a process, with that you can kill or terminate a running program in your computer (also see kill command).

shows processes for a certain user

shows all system processes

shows all system processes like the above but organizes in a hierarchy that's very useful!
kill

kill 10550

kill -9 431
terminate a system process

kill process number 10550

kill process number 431, (process number from ps command)

Use top or ps ux to get system PIDs (Process IDs)

EG:
PID     TTY     TIME     COMMAND
10550     pts/3     0:01     /bin/csh
10574     pts/4     0:02     /bin/csh
10590     pts/4     0:09     APP

Each line represents one process, with a process being loosely defined as a running instance of a program. The column headed PID (process ID) shows the assigned process numbers of the processes. The heading COMMAND shows the location of the executed process.
sudo reboot

sudo shutdown -h now

sudo shutdown -h +3

sudo shutdown -h 20:00
reboot the system

shut down the system (halt) right now! You can us -h for 'halt' or -r for 'reboot'.

shut down the system (halt) after three minutes, warning messages are given to allow users time to close programs

shut down the system (halt) at 8 pm, warning messages are given to allow users time to close programs


Networking
sudo lshw -C networkfor information about network hardware


ssh -p 223 herman@192.168.0.160
ssh stands for secure shell, you use it for remote control of a computer with ssh server installed.

connect to a computer running an ssh server at port 223, username is 'herman', IP address is ' 192.168.0.160'



scp -P223 commands herman@192.168.0.160:commands


scp -P223 herman@192.168.0.160:/home/herman/test_results . 


 
scp stands of secure cp (copy), it's the name of a protocol/program for copying files across a network

this copies the file 'commands' from silver laptop (remote machine) to bookpc.


(note the dot .) this copies the file 'test_results' from bookpc to a remote computer (silver laptop).
Where: silver laptop is the computer we're sitting in front of, port 223 is the port in bookpc that's configured for SSH, and the bookpc'sIP address is 192.168.0.160

wget -q -O - http://whatismyip.com/automation/n09230945.asp && echo -en '\n'display your external IP address for the router (internet)
ifconfig | grep -Po "inet addr:.+Bcast" | grep -Po '(?:\d{1,3}\.){3}\d{1,3}'display your internal IP address for the computer (in the LAN)
ping -c1 -a -b -v 192.168.0.18finds out if there's a computer listening at IP address 192.168.0.18
sudo tcpdump -c5 -vvv -nlisten on network and see who's pinging you
ifconfig

ifconfig | grep 'inet addr:'
display network information for the host computer (including IP address)

display IP address only
netstat

netstat -an

netstat -rn
shows all current network connections.

shows all connections to the server, the source and destination ips and ports.

shows routing table for all ips bound to the server.
netstat -an | grep 21

nc -zv <server-ip> 21
You may run this command in the server to check whether it's listening at port 21.

You might use the 'nc' command from another Gnu/Linux computer, (replace <server-ip> with the server's IP address), to check the server to see if 21 port is reachable.
nmap -T Aggressive -A -v 10.1.1.*

scan LAN in address range 10.1.1.* to see what machines are in the network and gather information
Warning: may be misinterpreted as an attack by some firewalls, do not use in corporate environments without permission from the network administrator.
How to keep a computer from answering to ping?(#18)
a simple "echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all" will do the
trick... to turn it back on, simply
"echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all"
Firewall - iptables commands
iptables -I INPUT -s IPADDRESSHERE -j DROP : This command stops any connections from the IP address
iptables -L : List all rules in iptables
iptables -F : Flushes all iptables rules (clears the firewall)
iptables --save : Saves the currenty ruleset in memory to disk
service iptables restart : Restarts iptables
sendEmail -f user@bigpond.net.au -t user@bigpond.net.au -o message-file=hello.txt -v -s mail-hub.bigpond.net.auEmail a text file from the command line
(useful for scripting purposes)
Where: 'hello.txt' is the name of a text file
Where: user@bigpond.net.au is the email address of the sender
Where: user@bigpond.net.au is the email address of the recipient
Where: mail-hub.bigpond.net.au is the name of the mail server



Putting commands together

command1 ; command2 run command 1 and then run command 2
command1 && command2 run command1 and if successful, run command2
command1 | command2
read output from command1 and write it to command2 as input
file < command read from a file and write the words to a command as input
command1 > file
read output from command1 and write it to a file
if a file with the same name exists, overwrite it
command 1 >> file
read output from command1 and write it to a file,
if the file already exists, preserve the existing data and append to it,
if the file does not already exist then create a new file
command1 || command2run command2 if command1 fails



apt
When you have an installed operating system with all of your favorite software added, you might want a list of the programs you installed so you can easily install the same ones again in case you ever need to re-install for some reason.
This is only good for installing in the same version, not for upgrading to a newer version of Ubuntu.
http://www.math-linux.com/spip.php?article80

http://www.cyberciti.biz/tips/linux-get-list-installed-software-reinstallation-restore.html
Before you re-install, if you want a list of added software so you can install the same packages again in the new system.
sudo dpkg --list >> installedI.txt   You might run this command on a fresh installation and then keep a copy of the output file for the future. 

sudo dpkg --list >> installedII.txt 
This command will output another list of all installed packages to a text file and call it installedII.txt.
diff installedI.txt   installedII.txt >> addedsoftware.txtNow you can run the diff command on the two files to get a list of all added software
sudo dpkg --set-selections < addedsoftware.txt Then, run this command
sudo apt-get dselect-upgrade... and finally, this one.
Installed .deb packages are stored in /var/cache/apt/archives, and you can make a backup of all the packages in /var/cache/apt/archives and paste it into another /var/cache/apt/archives in another installation of the same version of Ubuntu so you won't have to re-download the same packages again from the internet.
sudo apt-cache showpkg hdparm | less






Hardware Detection
and Testing
dmesg > boot.messagesthe dmesg program helps users to print out their bootup messages
 - possibly useful to identify a hardware issue which may be causing problems during boot-up
cat /var/log/syslog | less
if your computer shuts down suddenly and unexpectedly
- you should check your logs to see if  a hardware problem was the cause
cat /var/log/messages | lessif your computer shuts down suddenly and unexpectedly
 - you should check your logs to see if it's a hardware problem was the cause
cat /var/log/kern.log | lessif your computer shuts down suddenly and unexpectedly
 - you should check your logs to see if it's a hardware problem was the cause
cat /var/log/Xorg.0.log  | less
for diagnosing xserver problems for your video display
sensorsdisplays temperatures, voltages, fan rpm - HOW TO: Install and configure lm-sensors
sudo hddtemp /dev/sd*1displays temperatures of all hard drives containing a partition number1
sudo dmidecode
motherboard and BIOS information - very good! :)
sudo dmidecode -t baseboardjust the motherboard info
sudo dmidecode -t memoryjust the memory info
cat /proc/cpuinfo
gives information about what kind of CPU your PC has
cpufreq-infoif installed, this program provides info about your CPU frequency
lspcihardware information - PCI bus
sudo lspci |grep [Aa]udio
identify the audio controller
sudo lshw | lesslists all hardware
sudo lshw -C disk
Lists all hard disks and CD/DVD drives
sudo lshw -C networkgives information on your network card(s)
lsscsi if lsscsi is installed, lists scsi hard disks
lsusbLists all usb devices
hdparm -tT /dev/sdameasure the read speed performance of the first hard disk
iostat -p /dev/sdadisplay information about disk activity (useful for flash memory)
iostat -kdx /dev/sdadisplay information about disk activity (useful for flash memory)
pidstat -d 2watch which programs are writing to disc (useful for flash memory)
cdrecord -checkdrive dev=/dev/scd0gives details about the CD-DVD drive
sudo smartctl -a /dev/sda >> harddisk.report  checks on the condition of a hard disk drive and prints the output to a file
sudo badblocks -sv /dev/sda2 >> badblocks.reportcheck a hard disk partition for bad blocks and write information to a report
sudo badblocks -sv /dev/sda2 10000000check just the first 10000000 blocks of a partition, (just a sample), to save time
sudo badblocks /dev/sda -w -s -p 5!!! securely erase all data from a hard drive while checking for bad blocks !!!
sudo lshw | sed -n '/*-memory/,/*-pci/p' | head -n -1gives a description of the computer's memory modules
iwconfigfind information about wireless networking card
iwlist scan If your wireless network card is working, this command should find your wireless network and tell you all about it
aplay -l
lists all PLAYBACK Hardware Devices (sound cards)
xdpyinfoX-Server information
sudo apt-get install xresprobe

sudo ddcprobe
to install xresprobe, (so we can run ddcprobe)

displays X-Server info 
lspci -x | grep -i "vga\| display"more X-Server info (video card)
lsdevif procinfo is installed - advanced
lshal advanced , verbose



Internet and Downloads

btdownloadcurses karmic-netbook-remix-i386.iso.torrentcommandline bittorrent
- you may need to install bittornado for this to work
wget http://www.examplefile
download stuff with wget
especially useful for large downloads (without any file corruption, even if the connection is interupted),
or for writing scripts to automatically download a whole list of items (while you sleep).
sudo apt-get install youtube-dlYou can install youtube-dl to download youtube video's.
youtube-dl <url>Then after this you can download most youtube videos by using this format in terminal.






Text Editors

gksudo gedit /boot/grub/menu.lstgedit is the default text editor for gnome in Ubuntu - Tips for using Gedit Text Editor
kdesu kate  /boot/grub/menu.lstkate and kwrite are the default text editors for KDE in Kubuntu
sudo mousepad /boot/grub/menu.lstmousepad is the default text editor for XFCE in Xubuntu
sudo nano /boot/grub/menu.lst


nano
nano is the excellent command line text editor!
You may someday need to use a command line text editor to rescue a broken system with no GUI.  See Getting Started with GNU nano Text Editor, or just open nano and press ^g  for the help menu, ('ctrl'+'g'). NOTE: the '^' (caret symbol) stands for the 'ctrl' key.
vim /boot/grub/menu.lst

vim
:help
:q
vim (vi improved), is another editor with lots of features, Vim Documentation - sourceforge
and/or, to learn how to use vim:
open vim
type :help
:q to exit





Getting Started with GNU nano Text Editor

The GNU nano text editor can be used to edit vital operating system files from a command line interface.
Knowing how to use the GNU nano text editor might be important to you some day, it can get you out of a tight situation.
If someday you can't boot normally because of a wrongly edited system configuration file, you may be able to boot into 'rescue mode' instead, (from GRUB). Once in 'Rescue Mode', you can open a root shell. From there you should be able to open the file with nano and edit your mistake to easily fix your problem.

Don't wait for an emergency before starting to learn to use nano, start practicing with it now, it's easy. Then you'll know how to use it when you really need it. Knowing how to use GNU nano may save you the need for re-installing your operating system someday.

GNU nano is also the text editor we use for configuring crontab to run a program automatically at a certain time and date, see  Configure 'crontab'.

To open Nano and create a new file,
Code:
sudo nano 
(You can name the file later, when you type 'Ctrl' + 'o' to write the file to disc).

More often, you will be opening a file that already exists. For example, I can open my /etc/fstab file in nano and edit it,
Code:
sudo nano /etc/fstab



 GNU nano 2.0.7                                               File: /etc/fstab                                                 

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0

# /dev/sda1
UUID=6178d387-9ab5-4f23-a56d-8e0cba0addc3 /               ext3    relatime,errors=remount-ro 0       1

# /dev/sdc6
UUID=4e282580-9d44-408c-1a55-84937b04c643 none            swap    sw              0       0
/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto,exec,utf8 0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0

#/dev/sdb1
# UUID=3c0ea3df-9bbc-4365-84ff-c29b1ad50afa   /media/BACKUPS   ext3   defaults    0       2

#/dev/sdd1
UUID=1a206651-0db0-4b89-b468-608dbe379026   /media/DATA   ext3   defaults    0       2




##############################################################################################
#/dev/sda1: UUID="6178d387-9ab5-4f23-a56d-8e0cba0addc3" TYPE="ext3" SEC_TYPE="ext2" LABEL="HARDY"
#/dev/sda5: UUID="4d4939f2-de30-438a-896f-af6a77406eea" SEC_TYPE="ext2" TYPE="ext3" LABEL="INTREPID"
##/dev/sda6: TYPE="swap" UUID="efab5213-b3d7-4019-80ae-c322519626b8"
#/dev/sdb1: UUID="3c0ea3df-9bbc-4365-84ff-c29b1ad50afa" SEC_TYPE="ext2" TYPE="ext3" ##############
#/dev/sdc1: UUID="79a5a0ce-499e-4738-9d9b-60590f1b83fe" SEC_TYPE="ext2" TYPE="ext3" LABEL="GUTSY"
#/dev/sdc2: UUID="9e9e5184-127f-45d2-bf4a-e6f0a112f180" SEC_TYPE="ext2" TYPE="ext3" LABEL="FEISTY"
#/dev/sdc4: UUID="5b326dec-325e-416c-8bd3-5e8523bf259c" SEC_TYPE="ext2" TYPE="ext3" LABEL="OPENSUSE"
#/dev/sdc5: UUID="6e9774c7-6881-4e7d-8bbd-38087fc76dfb" TYPE="reiserfs" LABEL="DEBIAN"
##/dev/sdc6: TYPE="swap" 4e282500-809d-4440-8c1a-5584937b04c6
#/dev/sdd1: UUID="1a206651-0db0-4b89-b468-608dbe379026" SEC_TYPE="ext2" TYPE="ext3" LABEL="DATA"


^G Get Help     ^O WriteOut         ^R Read File         ^Y Prev Page         ^K Cut Text        ^C Cur Pos
^X Exit         ^J Justify          ^W Where Is          ^V Next Page         ^U UnCut Text      ^T To Spell

The instructions for how to use nano are already built into it, all you need to do is look at the help menu across the bottom of the screen.
  1. You can use your right arrow key or your left arrow key to move the cursor.
  2. Your up and down arrow keys will move the cursor too.
  3. Just type in the file as you normally type to edit the file.
  4. When you are ready, press 'Ctrl' + 'o' to write the changes to disc (save)
  5. Press 'Ctrl' + 'x' to exit from nano

TIP:

The 'carat' symbol looks like a Chinese hat: ^
The carat symbol is shorthand for the 'Ctrl' key on your keyboard.
For example, if you want to see the help file for nano, press the 'Ctrl'+ 'G' keys on your keyboard.
When you are finished reading, press 'Ctrl' + 'X', (to exit).

Links:
GNU nano Home Page
Nano Basics Guide - Gentoo Linux


Tips for using Gedit Text Editor

Gedit text editor is extremely simple to use, but it's more than just another boring text editor, most people overlook a few it's wonderful features.

Next time you have gedit text editor open, take a look in 'Edit'-->'Preferences'.
  • In the 'View' tab, there are some useful options you can enable, such as 'show line numbers', and 'highlight the current line'.
  • The other tabs are worth exploring too, 'Editor' and 'Font and Colors' tabs contain features that may interest you.
  • Especially look in the 'Plugins' tab. There you will find all sorts of useful plugins you can enable, like 'sort' and others. 
Some of the can be extremely useful and if you haven't tried them out you may be missing out on getting the best out of your Ubuntu Linux experience.

The  'Insert Date and Time function from the 'Edit' menu is one I use a lot.  This makes it easy to use Gedit for keeping a diary or log book.

================================================================================
COOL IDEA:
Did you know that you can use the 'grep' command to pull information out of a diary or log book into a .csv file which can then be opened with Open Office.org spreadsheet?

To make use of this cool idea, you need to plan ahead and make the diary entries that you will later want in your spreadsheet in '.csv' format.
'csv' stands for 'comma separated values', and when a plain text .csv file is opened with a spreadsheet program, each column in the spreadsheet will be determined by where you place commas in the .csv file.
Another thing you should keep in mind is that non-numerical values should be enclosed in "double quotation marks".
More info: Comma Separated Values -Wikipedia

Another trick is to use keywords like 'expenses' for a main heading and a second keyword like 'petrol' as a subheading to designate the  type of expense. Later, you can use the grep command on either of those two keywords or some other keyword to bring up all the lines that contain the information you're looking for.

The rest of my diary can be filled up with all sorts of irrelevant information and stories.

For example, I will make all records of petrol (gasoline) purchases under the headings of 'expenses' and the subheading 'petrol' in my diary.
I will make the entries in .csv format, using copy and paste to keep the columns all the same.
example: '2006_log.txt',
 <snip>
Fri 15 Dec 2006
"expenses: motor car repair:",$95.00,"for",1,"tire",,,"Western Tire Service"," Hughenden",15/12/06,,
=========

Fri 22 Dec 2006
"expenses: petrol:",$60.45,"for",45.53,"litres","@",132.77," V-8 Service Station"," Hughenden",22/12/06,14:25:00,"for tomorrow's journey"
motor car repair: Changed engine oil, topped up automatic transmission fluid, power steering oil, greased steering joints and cleaned the engine and underside of the transmission and under car.
===========

Sat 23 Dec 2006
Left Hughenden about 05:00 in the Ford ute.
"expenses: petrol:",$67.82,"for",56.66,"litres","@",119.7," Shell Garbutt"," Townsville",22/12/06,09:28:00,"no comment"
Met J Brosso in Kenworth Aerodyne unloading at Townsville depot at around 09:30.
After reloading with empty crates we fueled up in Bohle and headed to Malanda.
"expenses: entertainment:",$70.00,"for",1,"carton",,,"Carrington Drive-Thru","Atherton",24/12/06,,"Southern Comfort and Dry"
===========

 <snip>

Some day when I want to do some accounting, I can use the grep command to pull the relevant information from between all the other lines in my diary,
grep 'petrol' 2006_log.txt >> petrol_purchases.csv
This command will write all the output to a .csv file and name it 'petrol purchases.csv'.

Example of .csv file's contents,
"expenses: petrol:",$60.45,"for",45.53,"litres","@",132.77," V-8 Service Station"," Hughenden",22/12/06,14:25:00,"for tomorrow's journey"
"expenses: petrol:",$67.82,"for",56.66,"litres","@",119.7," Shell Garbutt"," Townsville",22/12/06,09:28:00,"no comment"

The .csv file can be opened with any spreadsheet program for further processing.

I could instead use the grep command with the keyword 'Expenses', to make a .csv file for all of my expenses recorded in my diary, or I could use some other keyword such as 'Entertainment' to gather up all of my entertainment expenses.

TIP:
If you're not sure how to make lines in .csv format, try working in reverse, and make an example spreadsheet first with the columns you will want to see later. Then use 'Save As', from the 'File' menu, and choose to save your spreadsheet as a .csv file. Then you can open the .csv file with Gedit and copy an example line to your diary. Once you get started, keep to the same columns by copying and pasting an earlier entry and then editing that each time you want to make a new entry.

TIP:
Gnumeric Spreadsheet can be installed in Ubuntu  and is very capable when it comes to working with .csv files, I prefer Gnumeric Spreadsheet for this purpose.