Archives for January, 2010
Posted by Derek@TheDailyLinux »
Add Comment »
A really quick way to wrap text to a defined number of columns in ‘vi’ is to use
:%!fmt
The “%” selects the entire document, the “!” replaces the paragraph with output from the ‘fmt’ program. The ‘fmt’ program will, by default, wrap lines to 80 columns. If you’d like something different, you can simply type the number of columns like so
:%!fmt 50
Take a look at the ‘fmt’ man page for more options. Also, this :%! trick doesn’t just work with fmt, it works with programs like awk too. Have fun!
Apparently, “{!}fmt” will only wordwrap the paragraph which can be incredibly handy… I haven’t been able to reproduce the success from Mac OSX with VIM. Thoughts?
Posted by Derek@TheDailyLinux »
2 Comments »
This is a no-frills Linux command line guide/cheat sheet that will help you extract or unarchive or uncompress just about any file that you’re bound to come across. If you’d like to have more options, read the man pages! Also, the opposite to this guide about creating/compressing/archiving files in Linux can be found here.
[Read more →]
Posted by Derek@TheDailyLinux »
Add Comment »
Here’s a tip that I used just about everyday at work. We usually compress our .dd images down using bzip2 and it can be a hassle having to uncompress and then dd the image to the SD card or other media. Instead, why not do it all in one swift command with bz2cat?
bz2cat path/to/the/filename.dd.bz2 | dd of=/dev/sdb
Posted by Derek@TheDailyLinux »
1 Comment »
Say you want to automatically start programs when starting up Linux. A quick and easy way to make a script or program start when Linux does is through the init scripts located in the /etc/rc2.d/ directory. These scripts are called in order of priority when Linux enters runlevel 2. Your default runlevel may be different than 2, so be sure to check with the ‘runlevel’ command or in the /etc/inittab. In order for the scripts here to start, they must follow a three-part naming schema. For example: /etc/rc2.d/S10thisscript. This script will Start with priority of 10 when the system enters runlevel 2 and is described as thisscript. The description doesn’t matter, but it is useful to give it a meaningful name. For more information, please see this page.
Here’s a very quick and dirty walkthrough that will change the IP address whenever the system is booted:
echo "ifconfig eth0 192.168.1.102" > /etc/init.d/changeip
chmod +x /etc/init.d/changeip
ln -s /etc/init.d/changeip /etc/rc2.d/S10changeip
So, you can see that we create a script called changeip in the /etc/init.d/ directory, gave it executable permissions, and then created a symbolic link to /etc/rc2.d/S10changeip so that it will run when the system is started. As a general note, the script can be located anywhere as long as the symbolic link is targeted at /etc/rc2.d/S10changeip.
If you are going to be calling a binary that needs to run in the background, you’ll want to use the nohup and sleep commands like below to avoid having your application be terminated with the init process (the sleep command will prevent the nohup command from being killed before it’s initialized — ironic):
nohup myprog &
sleep 1
Along the same lines as the above trick, a script can be made to run whenever a user logs in by placing the name and location of your script into the ~/.profile directory. For example:
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n
echo "Now running user script..."
/home/username/changeip
Happy scripting!
Note: I’ve just learned that on Debian systems, all you may need to do is create the script, make it executable, and then call the “update-rc.d” command like such:
chmod +x the_script_name
update-rc.d the_script_name defaults
Source: http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/
Posted by Derek@TheDailyLinux »
Add Comment »
A handy trick for always making sure that a particular device is assigned a particular node in /dev is to use local udev rules. This way, you’ll always know that the device you plug in will always be at the same spot (ie. your USB thumb drive will always mount to /dev/thumb instead of /dev/sdxx). Some folks may refer to this as static assignment or forcing a /dev node. I took advantage of udev local.rules so that my 160GB external HDD with media will always mount to /dev/media and my 500GB external HDD with video will always mount to /dev/video. This way, I can edit my fstab rules to always mount /dev/media to /mnt/media and /dev/video to /mnt/video and I don’t have to guess which HDD belongs to /dev/sdb or /dev/sdc. Here are the steps I took:
1.) Plug in your device and find out which /dev node it was assigned by using “dmesg” output or “fdisk -l”. Then, get a unique piece of information about it. As long as it is unique, it should work. In my case, each HDD has a different model number, so I used the ATTRS{vendor} attribute as a unique identifier. Use the following command to view device information:
udevinfo -a -p $(udevinfo -q path -n /dev/sdb)
2.) You’ll need to create a local.rules file and fill it with the unique information from the above command (see my example below).
touch /etc/udev/rules.d/10-local.rules
My example:
This tells udev to create a symlink to /dev/video when a device with a vendor attribute of ST350032 is detected and the same applies for line two.
ATTRS{vendor}=="ST350032", SYMLINK+="video"
ATTRS{vendor}=="ST316002", SYMLINK+="media"
3.) Use the following command to restart udev and then unplug and re-plugin the device to verify that you indeed have a new /dev/mydevice node.
/etc/init.d/udev restart
You can then further take advantage of this by editing your /etc/fstab file to automatically mount the drive. Like mine for example:
/dev/video /mnt/video ntfs-3g defaults,locale=en_US.utf8 0 0
/dev/media /mnt/media ntfs-3g defaults,locale=en_US.utf8 0 0
Additional Information:
http://reactivated.net/writing_udev_rules.html
Additional Guides:
http://ubuntu-tutorials.com
Posted by Derek@TheDailyLinux »
Add Comment »
1.) Become root:
su
2.) Plug in your SD card, HDD, or other block device and then use the following command to see which /dev/diskN node it’s located on:
diskutil list
3.) Unmount the disk where “N” is the number of the disk taken from the above command:
diskutil unmountDisk /dev/diskN
If the above command was successful, you will see:
Unmount of all volumes on diskN was successful
4.) Use the ‘dd’ command to copy the image file (.dd or .img generally) to the entire disk:
dd if=myImage.dd of=/dev/diskN
Also, you can write the image to particular partitions of the disk with (N is the disk number and P is the partition number):
dd if=myPartitionImage.dd of=/dev/diskNsP
The process to do this under Linux is very similar except that it’s not required to un-mount the drive before using the ‘dd’ command and the commands are a little different. For example, you would use “fdisk -l” instead of “diskutil list”, your device node would be located at “/dev/sda” instead of “/dev/disk” and the un-mount command is “umount” instead of “diskutil unmountDisk”.
Posted by Derek@TheDailyLinux »
Add Comment »
The stty and setserial programs are useful for setting up serial ports on a Linux box. For example, today I needed to set the IRQ and baud rate of two serial ports in order to get them to play nicely. So, I used setserial to setup the IRQ and then stty to setup the baud rate, like this:
root@ts7000:root# setserial /dev/tts/2
/dev/tts/2, UART: 16550A, Port: 0x89c003e8, IRQ: 33
root@ts7000:root# setserial /dev/tts/3
/dev/tts/3, UART: 16550A, Port: 0x89c002e8, IRQ: 40
root@ts7000:root# stty -F /dev/tts/2 115200
root@ts7000:root# stty -F /dev/tts/3 115200
Now that I look at it, I could have set the baudrate with setserial /dev/tts/2 baud_base 115200, but then I wouldn’t have been introduced to the good little program stty!
Of course, I’d encourage you to take a look at the man pages that I linked to in the first sentence so you can learn all about the neat features of both programs.
Posted by Derek@TheDailyLinux »
Add Comment »
A neat little trick for getting around the terminal history is by using the “reverse-i-search” feature of the terminal. To use this, simply hit [ctrl+r] in the terminal. What you’ll see is
(reverse-i-search)`':
Simply start typing and it will find previously used commands. You can hit [ctrl+r] again to search through the history if there is more than one match. For example, I typed in “hi” and it found the text pattern in my history of the command “which ifconfig”. At this point, I can hit [enter] and it will re-run the command or I can modify the command if I choose with the arrow keys like normal.
(reverse-i-search)`hi': which ifconfig
Happy terminaling!
Posted by Derek@TheDailyLinux »
Add Comment »
A situation presented itself today where I needed to monitor the ‘dmesg’ output continually. This is normally easy for regular files because you can simply use the command tail -f thefile and it will always present you with the latest information. Well, not so with the dmesg output. If you try, you’ll be greeted with some sort of error message. One way around this is to run the dmesg -c command in a while loop like so…
while true;do dmesg -c;done
Understand that running dmesg -c will clear the dmesg ring buffer contents after printing, so you will lose the contents of dmesg.
Now, I’m not 100% sure if that while loop is a totally elegant or acceptable solution, so I offer another solution — the /proc/sys/kernel/printk file. According to IBM.com…
This holds four numeric values that define where logging messages are sent, depending on their importance. For more information on different log levels, read the manpage for syslog(2). The four values of the file are:
1. Console Log Level: messages with a higher priority than this value will be printed to the console
2. Default Message Log Level: messages without a priority will be printed with this priority
3. Minimum Console Log Level: minimum (highest priority) value that the Console Log Level can be set to
4. Default Console Log Level: default value for Console Log Level
Default setting: 6 4 1 7
By experimenting with these values, you can send printk messages directly to the terminal window instead of only to dmesg. You can set these values using
echo "9 9 9 9" > /proc/sys/kernel/printk
Happy debugging!
Source: http://www.linuxforums.org/forum
Additional Resource: http://www.de-brauwer.be/wiki
Posted by Derek@TheDailyLinux »
Add Comment »
A Makefile provides an easy, fast way to compile an application that requires more than just a simple gcc myprogram.c -o myprogram command. For example, to compile a GTK+ app, the common, basic compile command is gcc -g -Wall myguiapp.c -o myguiapp -export-dynamic `pkg-config –cflags –libs gtk+-2.0`. It’s not terrible, but it would be a pain to have to retype that every time you need to recompile. The alternative is to create a Makefile and type make. This is my very first Makefile, and I believe it makes a great example:
NAME=myguiapp
CFLAGS=-g -Wall -o $(NAME)
GTKFLAGS=-export-dynamic `pkg-config --cflags --libs gtk+-2.0`
SRCS=main.c
CC=gcc
# top-level rule to create the program.
all: main
# compiling the source file.
main: $(SRCS)
$(CC) $(CFLAGS) $(SRCS) $(GTKFLAGS)
# cleaning everything that can be automatically recreated with "make".
clean:
/bin/rm -f $(NAME)
As you can see, when make is called, it will compile the source with all my options including the program name. When make clean is called, it will delete the compiled program.
One important thing to remember is that these Makefiles are very picky about spacing and tabs. You MUST use a TAB at the beginning of commands and a TAB must not be at the beginning of blank lines. The first error causes the commands not to run. The second causes the “make” utility to complain that there is a “blank” command.
Obviously, this is an incredibly simple Makefile in comparison to a lot of them out there, but this is a good starting point. You can customize the Makefile to match your compiling needs. If you’d like to learn more, Google is your friend. I can point you in a couple of good directions with these links:
http://mrbook.org/tutorials/make/
http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html
Happy coding!