Hello, and welcome to part two of our Linux command line series. You will learn some more interesting tips that you can use to master your system, so hold on to your seats, because here we go.
The tasks, part two
Setting date and time
I must confess, this was a task that I had to do a long time ago in front of a terminal and had no idea how to do it. That is because I was used to the Gnome way of doing that but at the time I had no Gnome. So what to do?
man date
, of course. Depending on the country you live in, the date format differs from other parts of the world. In the United States, the date/time format is of the form mm/dd/yy or mm/dd/yyyy, where m is month, d is day and y is year, either in two-digit format (e.g. 86 for 1986). Where I’m getting at is the fact that the way that you set your date with the date command may differ from the format you’re used to (or what is used in your country). This paragraph will not be a manual page replacement, but it will help you set your system’s date/time quickly, provided you have root privileges. If you simply type
date
with no other arguments/flags, it will show you the current date. To set the date, you should type something like
date [MMDDhhmm[[CC]YY][.ss]]
M is month, D is day, h is hour, m is minute, C is century (the first two digits of year, like 20 for 2012), Y is year and s stands for seconds. Therefore to set your date for example to “Fri Jul 6 13:45:50 2012” you would do:
# date 070613452012.50
On GNU/Linux systems, Ubuntu included, you can set the date (day, month) and time (hour, minute) without the year, second or century part. Remember that other *nix systems work a little bit differently, so this is Linux-only.
Update your Ubuntu/Debian system
Usually your system is configured to look for updates all by itself and if there are any just ask you if you want to apply the changes. What if you mistakenly altered the settings of your system so it doesn’t do that? There’s nothing to be ashamed about, it’s a good way to learn, and we all have been there. So, if for whatever reason you have to update manually, here’s how. Ubuntu, just like its’ “father”, Debian, and the derivatives, use dpkg as the package manager. The front-ends for dpkg, which is somewhat of a low level package tool, are apt-* and aptitude. On a default Ubuntu system only the apt-* tools will be installed, and the tool we will talk about is apt-get
. In Ubuntu, one updates in two steps: the first contacts some remote server to “find out” if there are updates to the installed software, and the second actually performs the upgrade.
# apt-get update # apt-get upgrade
This is really simple, and that’s all there is to it. You will maybe see a message in your notification area that you have to reboot before the changes take effect, like when the kernel or important libraries are updated, and I recommend you do that, to avoid unpleasant surprises and to use the latest updates that usually close bugs and add new features. Also, if you update every two or three days or so, the better.
Mounting USB and optical media
If you run a desktop, usually media is automounted for you. If you run a server, it’s a big security risk to use unchecked media on it. These are the two extremes of USB or CD/DVD media on Linux. But as always, life isn’t made of black and white. So here’s how to mount these media on Ubuntu, provided you don’t have automounting and you glanced a bit at the mount manual. On my system (and probably on yours too), the first CD-ROM device is /dev/sr0
. So the second will be sr1 and so on. Now, as root, we will have to create a mount point and go on with it. Usually the static mount points (e.g. the ones that aren’t automounted) are created in /mnt.
# mkdir /mnt/cdrom # mount /dev/sr0 /mnt/cdrom
You will get an error and the drive will not be mounted. Why? You need to tell mount what filesystem type to use. So, if it’s a CD, use -t iso9660
. where -t stands for type. If it’s a DVD, use -t udf
. And if all else fails, my colleague wrote an excellent article that treats the subject in depth.
When it comes to mounting USB devices, the problem is not the filesystem type, which is usually vfat, but finding the drive itself. Depending on your configuration, the moment you insert the drive it will be asigned a drive letter. Not in the way Windows systems do, but in the form sdx, where x is a letter. One way that usually works for me (if you have problems with this method try switching the port) is good ol’ fdisk. Here’s the command and what it yields on my laptop with a single hard disk and a 1 GB USB drive inserted:
# fdisk -ul Disk /dev/sda: 250.1 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00061326 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 32819199 15360000 83 Linux /dev/sda3 32819200 37013503 2097152 82 Linux swap / Solaris /dev/sda4 37015550 488396799 225690625 5 Extended /dev/sda5 37015552 53399551 8192000 83 Linux /dev/sda6 53401600 61790207 4194304 83 Linux /dev/sda7 61792256 488396799 213302272 83 Linux Disk /dev/sdb: 1007 MB, 1007419392 bytes 255 heads, 63 sectors/track, 122 cylinders, total 1967616 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x91f72d24 Device Boot Start End Blocks Id System /dev/sdb1 * 63 1967615 983776+ 6 FAT16
I purposely left the whole output for comparison, so here’s what I found out. I ignored /dev/sda, because that’s my internal hard disk, and noticed that the USB drive is /dev/sdb, which has a single partition of type FAT16. This translates into
# mkdir /mnt/stick # mount -t /dev/sdb1 /mnt/stick
Burning CDs
If we’re on the matter of optical media, let’s see in short how you can burn your own CDs or DVDs without having to use graphical applications. After all, CD/DVD burning applications with a GUI are nothing more than front-ends to the ones I’m just about to show you. First of all, you must know what kind of data you want to burn. Is it audio that you want to burn as data, like mp3? Is it audio and you want to create an audio CD? Is it simply data, like backups? Or maybe it is a ISO image that you created yourself or it was created by others? Let’s see…
First, here’s what you need to do so you have all the tools at hand:
# apt-get install wodim cdrkit genisoimage cdrdao
Let’s see first what is the name of your recording device. Typing wodim -checkdrive
will print something like that:
$ wodim -checkdrive Device was not specified. Trying to find an appropriate drive... Detected CD-R drive: /dev/cdrw Using /dev/cdrom of unknown capabilities Device type : Removable CD-ROM Version : 5 Response Format: 2 Capabilities : Vendor_info : 'TSSTcorp' Identification : 'CDDVDW TS-L632N ' Revision : '0503' Device seems to be: Generic mmc2 DVD-R/DVD-RW. wodim: Cannot load media with this drive! wodim: Try to load media by hand. Using generic SCSI-3/mmc CD-R/CD-RW driver (mmc_cdr). Driver flags : MMC-3 SWABAUDIO BURNFREE Supported modes: TAO PACKET SAO SAO/R96P SAO/R96R RAW/R16 RAW/R96P RAW/R96R
For the rest of this tutorial, what matters is that the device is /dev/cdrw
. Now let us use it for something practical, bearing in mind that there will have to be a blank disk inside. Let’s burn the latest Ubuntu image with wodim:
$ cd iso/ $ wget -c ftp://ftp.heanet.ie/mirrors/ubuntu-cdimage/releases/oneiric/release/ubuntu-11.10-dvd-amd64.iso $ wodim -v dev=/dev/cdrw ubuntu-11.10-dvd-amd64.iso
Now, if we are to presume that you already have the wav files at hand for burning an audio CD (if you don’t, the software you’re interested in is called lame), let’s see how to burn the disc.
$ cd directory_with_wavs $ wodim -v -pad speed=1 dev=/dev/cdrw -dao -swab *.wav
We spoke earlier of ISO images. What if you want to create a ISO image yourself? After you have the files you need in one directory, use mkisofs, then write the resulting ISO image as above:
$ mkisofs -V name_of_volume -J -r -o name_of_iso.iso folder/
In the end, since I said I’m gonna talk about creating wavs from mp3s, here it is:
$ for i in *.mp3; do lame --decode "$i" "`basename "$i" .mp3`".wav; done
Conclusion
We would definitely like your feedback on this article, folks. Well, not only on this one, but especially. Do you consider using the command line is cooler?