Archive for the ‘Tips and Tricks’ Category
Posted by Derek@TheDailyLinux »
Add Comment »
find /path/to/directory -type d -empty -exec echo {} ;
I think it’s easy to get the idea based on the example above, but just in case you need inspiration, you could automatically remove these empty directories with the following (Careful! This uses ‘-rf’ flag which shows no warnings or mercy!):
find /path/to/directory -type d -empty -exec rm -rf {} ;
Check out some additional methods here:
http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/
Posted by Derek@TheDailyLinux »
Add Comment »
In an embedded system without a battery backed real time clock (RTC), a time-based ‘fsck’ check can be quite annoying. To disable the time-based check, use the tune2fs command on the drive containing the root partition like so:
tune2fs -i 0 /dev/<device node>
There’s more information here at this link for those of you interested:
http://serverfault.com/questions/28343/will-my-system-fsck-when-i-reboot
Posted by Derek@TheDailyLinux »
Add Comment »
When using Linux serial ports, the tty has a default 5 to 10ms latency time built-in. To drop this, use the low_latency parameter of setserial. This is useful for getting a more real-time response in Linux serial ports. Be sure to check out the man pages for more information.
Minimize the receive latency of the serial device at the cost of greater CPU utilization. (Normally there is an average of 5-10ms latency before characters are handed off to the line discpline to minimize overhead.) This is off by default, but certain real-time applications may find this useful.
Posted by Derek@TheDailyLinux »
2 Comments »
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:
cd /path/to/dir
- Change current working directory (
pwd) to /path/to/dir
cd ..
- Change back one directory level
cd ~
- Change to your home directory at /home/username/
cd -
- Change to the directory you were in last
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.
dirs -v
- View all directories on the stack with numbers
dirs +N OR dirs -N
- Display Nth directory counting left to right, OR right to left
cd `dirs +N`
- Change directory to dirs stack number N
pushd
This command will push a given directory to the top of the stack of remembered directories.
pushd .
- Push the current directory onto the stack
pushd /path/to/dir
- Push the /path/to/dir onto the stack
popd
This command will pull the top directory off the stack of remembered directories.
popd
- Pull the top direcotry (0) off of the stack, resulting in changing directory to (1)
Posted by Derek@TheDailyLinux »
Add Comment »
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.
Posted by Derek@TheDailyLinux »
Add Comment »
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!
Posted by Derek@TheDailyLinux »
Add Comment »
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.
Posted by Derek@TheDailyLinux »
Add Comment »
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
Posted by Derek@TheDailyLinux »
Add Comment »
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
Posted by Derek@TheDailyLinux »
Add Comment »
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!