Archive for the ‘Tips and Tricks’ Category

10
Jun

Tips for Navigating Directories in Linux

Here are some basic Linux commands that will allow you to navigate easier within the terminal. These commands include dirs, pushd, popd, and cd.

cd
This is probably one of the most known commands since you really can’t get around a Linux terminal without it. There are some pretty neat tricks you can use to more quickly navigate directories. These include:

In order to talk about the next commands, you need to understand what a stack is. Imagine a spring-loaded plate holder like the ones you’d find at an all you can eat buffet (this is dirs). The employees fill this plate holder by pushing a plate onto the plate holder. Then another plate is pushed onto the top of that plate (this is pushd). This is repeated again and again and then soon, you have a full stack of plates. In order to get to any of the plates in the middle, you must pull the plate on top off, and then the next, and then the next until you get to the plate you want (this is popd). This can continue until the stack is empty. This example is known as a Last In First Out (LIFO) or First In Last Out (FILO) data structure and that’s exactly what we’re dealing with here.

Now that you have an understanding of what a stack is, let’s chat about the next three commands: dirs, pushd, popd.

dirs
This is the command you use to display the stack of currently remembered directories.

pushd
This command will push a given directory to the top of the stack of remembered directories.

popd
This command will pull the top directory off the stack of remembered directories.

9
Jun

Compare Two Directories or Filesystems Recursively

It’s so simple it doesn’t really need an entry, but there will be a time when somebody asks “how can I compare two different directories or file systems”, and I will say, “this way…”

diff -rq /dir1 /dir2

The -q option above will only tell you which files differ. It will not go into details and display each differing line of each file.

4
Jun

Example Usage of cURL Utility

Here’s a core utility that will surely be of use for Linux or Mac users: cURL. In my own description, it’s sorta like wget, but different. Description from the website:

curl is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction.

Also, it might be good to note this as well now that I brought up wget:

Curl is not a wget clone. That is a common misconception. Never, during
curl’s development, have we intended curl to replace wget or compete on its
market. Curl is targeted at single-shot file transfers.

I took some time to play around with it a bit and found that it’s quite easy to use and the documentation and man pages are nicely laid out. For example, here’s how I used curl to download an image from a password protected website:

curl http://www.thewebsite.com/image.jpg --user theusername:thepassword > image.jpg

I wanted to see if I could download multiple files at a time, so I became a little fancier and threw in a for loop with the seq command for counting. Since I knew all of my images were taken in order (100.jpg -> 110.jpg), I could use something like:

for i in $(seq 100 110); do curl http://www.thewebsite.com/$i.jpg --user theusername:thepassword > $i.jpg; done

I’m sure that this hasn’t even scratched the surface for what curl can do, so be sure to check out the man pages and have fun with it!

2
Jun

Easily Edit Binary Files with hexedit

Let’s pretend that you’ve made a mistake when creating a binary image and you didn’t realize it until after you painstakingly created this image for use in a production process. Well, instead of going back and doing it all over again, you can simply use hexedit to edit the binary file directly, saving you precious time.

hexedit -m binaryfile.dd

In my particular case, I was trying to create an SD card image for use in an SBC product and I didn’t realize until it was too late that there was a type-o in a script. I used hexedit to fix the type-o and everything worked out heavenly.

20
May

Prepping, Cleaning, and Compressing an Image

Here are some non-comprehensive tips for creating an image from a block device that runs an embedded Linux system (ie. SD Card, USB Drive, etc). Using these steps will help you prepare, clean, and compress an image to the smallest size possible. I used these recently to create a microSD card image for the company I work for (Technologic Systems) so we can upload them to an FTP server that customers have access to. It runs Debian Linux with and initrd and Busybox. I believe that BeagleBoard and other embedded system companies who produce single board computers (SBCs) also have a similar setup to this.

Steps on the Embedded System Itself:
Clear any history from within Busybox:

> .ash_profile

Boot to full Debian and clean up:

apt-get autoremove
apt-get clean
rm -rf /var/log/*/*

Now, clear any history in Debian and shutdown:

> .bash_history; history -c; shutdown -h now

Steps on the Linux PC:
Mount SD card on another Linux box (assumes /dev/sdb) and “zero out” all available partitions. In this example, there are four, one is raw kernel image so it is left alone:

mount /dev/sdb1 /mnt/tmp; cd /mnt/tmp
dd if=/dev/zero of=zerofill; rm -rf zerofill
umount /dev/sdb1

mount /dev/sdb3 /mnt/tmp; cd /mnt/tmp
dd if=/dev/zero of=zerofill; rm -rf zerofill
umount /dev/sdb3

mount /dev/sdb4 /mnt/tmp; cd /mnt/tmp
dd if=/dev/zero of=zerofill; rm -rf zerofill
umount /dev/sdb4

Now, create the .dd image and compress it:

dd if=/dev/sdb of=theImage.dd
bzip2 --best theImage.dd

Using the steps above, I was able to compress a 2GB SD image down to ~800MB (zeros compress very well)! And, since I took the time to clear out the history and logs, the user who downloads and uses the image will have a nice, clean image to work from. You could take it another step further and reduce the size of your kernel modules using my other guide:
Command to Reduce File Size of Installed Kernel Modules

11
May

Fixing ‘rm: cannot remove’ or ‘rm: cannot lstat’ Error Messages

If you happen to come across an error message such as the following, chances are that your filesystem has become corrupted somehow and all it really needs is some good ‘ol T.L.C. with fsck -f.

rm: cannot lstat `filename': Permission denied
rm: cannot remove `filename': Stale NFS file handle

6
May

Set an “Alarm Clock” for a Program to Run at a Certain Time

If you ever have the need to setup a program to run at a certain time, then the at is the command for you. Here’s the standard blurb from the man pages explaining what it is:

       at and batch read commands from standard  input	or  a  specified  file
       which are to be executed at a later time, using /bin/sh.

       at      executes commands at a specified time.

       atq     lists  the  user's  pending  jobs, unless the user is the supe-
	       ruser; in that case, everybody's jobs are listed.   The	format
	       of  the	output	lines (one for each job) is: Job number, date,
	       hour, queue, and username.

       atrm    deletes jobs, identified by their job number.

Now, I won’t reinvent the wheel on how to utilize the command. Instead, I’ll simply point you to this reference over at nixcraft (cyberciti.biz). Have fun!

26
Apr

Delete Lines Matching a Pattern or Line Number with ‘sed’

I had a unique situation where I wanted to remove a line that contained “PRINT” in a batch of HTML pages. To do it quickly, I used the sed command:

sed -i '/PRINT/d' *.htm

The -i option tells sed to edit the file in place so I wouldn’t have to play pipe games. The '/PRINT/d' argument tells sed to look for the text PRINT and then delete the line that contains it. Keep in mind that this will work on the entire document, not only the first item it finds. So, be sure this is what you want. Test the waters first, especially if you’re working with a batch job where this is going to edit many files at once.

Another possibility was to delete only the particular line number that this pattern occurred on:

# First, use grep to find the line it occurs on
grep -n "PRINT" *.html
# Then, use sed to delete that particular line (ie. line 16)
sed -i '16 d'

There are a lot more sed tricks here, so be sure to check it out.

21
Apr

Set Linux Terminal Console Column Width

A quick trick I learned today was how to set the column width for a terminal console with stty. This way, the shell acts a bit more normal.

stty cols 80

A snippit from the stty man page reveals some more information and tricks:

* cols N
	      tell the kernel that the terminal has N columns
* rows N
	      tell the kernel that the terminal has N rows
* size print the number of rows and columns according to the kernel

I didn’t realize how useful this program was! Certainly worth taking a deeper look into if your always consoled into your embedded Linux system.

14
Apr

Easy Way to Bring Binaires Down to Size

In an embedded environment, the amount of space your binaries (kernel modules, programs, etc) take up is important to consider. A very easy way to remove a lot of bulk from them is to use the objcopy command:

objcopy --strip-unneeded filename

« Previous PageNext Page »

Switch to our mobile site