Posted by Derek@TheDailyLinux in
Tips and Tricks »
Add Comment »
The watch command allows you to periodically run a command (default, every 2 seconds but can be specified using -n or --interval option). It’s like an alias for a while 1; sleep loop and is really simple to use.
watch [options] command command_options
When I had used it, I was wanting to see the progress of the ‘dd’ command. Follow this link if you’re interested as well: http://www.commandlinefu.com/commands/view/1868/watch-the-progress-of-dd
http://linuxcommando.blogspot.com/2008/06/show-progress-during-dd-copy.html
Posted by Derek@TheDailyLinux in
Software »
Add Comment »
Check out uncrustify next time you need to stylize your c like application. Their website gives a short, concise summary of what it does. Check it out here: uncrustify on sourceforge.
It’s also highly configurable via the uncrustify.cfg config file.
Example usage:
uncrustify -c uncrustify.cfg -f source_file.c
You should be able to install it using apt-get or yum, depending on your Linux distribution.
Posted by Jim@TheDailyLinux in
Scripting,
Tips and Tricks »
1 Comment »
It is easy to use the utility netstat to list the active connections you have to your machine
netstat -t
provides the information. But often it is good to know which process/program the connection is to. Again netstat obliges
netstat -tp
However, if like me you use 80 column xterms (or even one of the linux VTs in 80 column mode), then the long lines make the output less than easy reading. So using the “cut” utility we can cut down the verbiage to what is important
netstat -tp | cut -c21-63,80-
I often like to use the “--numeric-hosts” option to netstat too.
Another use of netstat is to show those programs that are listening for connections. This can help check if services have crashed, or whether you have unwelcome “services” running on your machine.
netstat -tpl
Again “cut” can be used to trim the fat and keep the output manageable
netstat -tpl | cut -c21-36,56-63,80-
It is worth giving the man page a netstat a check, it is a very useful utility.
Posted by Derek@TheDailyLinux in
Tips and Tricks »
2 Comments »
This tip came in from a subscriber/regular contributor. It’s a script for quickly creating busybox links based on the commands included in your build. I know it will be helpful to somebody out there.
–
Hi Derek,
Dunno if you are still doing your “DailyLinux” thing, but here’s
a bit of script I knocked together today to create busybox links.
Yes I know there is an “–install” option on a lot of busybox builds,
but it doesn’t seem to play well with installing in a chroot area.
So copy the busybox executable into the “bin” directory…
cp -p /bin/busybox /data/chroot/bin
and cd into that directory
cd /data/chroot/bin
Then run this shell snippet…
./busybox --help | \
sed -e '1,/^Currently defined functions:/d' \
-e 's/[ \t]//g' -e 's/,$//' -e 's/,/\n/g' | \
while read app ; do
if [ "$app" != "" ]; then
printf "linking %-12s ...\n" "$app"
ln -sf "./busybox" "$app"
ls -ld "$app"
fi
done
and all the links for the apps built-in to the busybox version you have
get built for you.
It takes advantage of the fact that “busybox –help” gives a list of the
built apps at the end of its output, after the line “Currently defined
functions:”. The sed script, removes all line upto and including this line,
and then removes spaces, tabs and trailing commas, then it converts all
commas to newlines. This gives a list of apps, one app to a line. The while
loop reads each line and does the linking.
Dunno if this would interest anyone else.
all the best
Jim
Posted by Derek@TheDailyLinux in
Scripting »
3 Comments »
This script will recursively search for files based on listed extension names and then use the ‘ffmpegthumbnailer’ program to generate a thumbnail for each file it finds.
If you’re on a Ubuntu system, you should be able to install ffmpegthumbnailer using apt-get install ffmpegthumbnailer.
#!/usr/bin/python
import os
for root, dirnames, filenames in os.walk('/var/www/media/'):
for filename in filenames:
if filename.lower().endswith(('.m4v', '.mov', '.mpeg', 'mp4')):
ifile = os.path.join(root, filename)
ofile = os.path.splitext(ifile)[0] + ".jpg"
try:
with open(ofile) as f: pass
except IOError as e:
print "Generating thumbnail for: " + ifile
fftoptions = "-s0 -f"
command = "ffmpegthumbnailer -i %s -o %s %s" % (ifile, ofile, fftoptions)
p = os.popen(command,"r")
while 1:
line = p.readline()
if not line: break
print line
I had a specific purpose for this script, but you could easily modify it to fit your needs. When you save the file, don’t forget to mark it as executable with chmod +x scriptname.py.
Posted by Derek@TheDailyLinux in
Software »
Add Comment »
I stumbled upon pdf2swf when looking for the best way to embed a PDF into a webpage. It works really well and was easy to compile on my shared hosting account (bluehost.com). I installed using the following steps:
Download – Download version 0.9.1 (visit http://www.swftools.org/download.html for latest)
wget http://www.swftools.org/swftools-0.9.1.tar.gz
Extract – Extract the newly downloaded files
tar xzvf swftools-0.9.1.tar.gz
Configure – Make sure the environment is setup
cd swftools-0.9.1; ./configure
Compile – Getting source to binaries
make config; make
Test Run – You should get a help menu
./src/pdf2swf
Install – Copy the binaries to proper locations on the system
(You won’t be able to run this step if you’re installing on a shared host. If that’s the case, copy the binaries of interest to ~/bin and add ~/bin to PATH variable)
make install
Usage – Run the program (Run man pdf2swf for more info)
pdf2swf myfile.pdf myfile.swf
That’s it! Open the .swf using your browser to see pdf2swf’s handywork.
As a side note, I ended up ditching the .swf method for embedding my document on my website and going with Google’s PDF viewer which is done with the following line of HTML:
<iframe style="width: 700px; height: 900px; border: 0px none;" src="http://docs.google.com/gview?url=http://mydomain.com/path/to/myfile.pdf&embedded=true" width="320" height="240"></iframe>
Posted by Derek@TheDailyLinux in
Scripting »
2 Comments »
The following is a quick script that will convert minutes to hours and minutes as well as total hours in decimal form (1 hour, 30 minutes is 1.5 hours). Simply save the contents of the script below to a file, execute chmod +x filename on that file, and then run it with ./filename. Feel free to modify it to fit your needs.
#!/bin/sh
minutes=432
hrs=`echo "$minutes / 60" | bc`
min=`echo "$minutes % 60" | bc`
if [ $min -gt 0 ]; then
if [ $min -le 2 ]; then hours=`echo "$hrs + .0" | bc`; fi
if [ 3 -le $min -a $min -le 8 ]; then hours=`echo "$hrs + .1" | bc`; fi
if [ 9 -le $min -a $min -le 14 ]; then hours=`echo "$hrs + .2" | bc`; fi
if [ 15 -le $min -a $min -le 20 ]; then hours=`echo "$hrs + .3" | bc`; fi
if [ 21 -le $min -a $min -le 26 ]; then hours=`echo "$hrs + .4" | bc`; fi
if [ 27 -le $min -a $min -le 32 ]; then hours=`echo "$hrs + .5" | bc`; fi
if [ 33 -le $min -a $min -le 38 ]; then hours=`echo "$hrs + .6" | bc`; fi
if [ 39 -le $min -a $min -le 44 ]; then hours=`echo "$hrs + .7" | bc`; fi
if [ 45 -le $min -a $min -le 50 ]; then hours=`echo "$hrs + .8" | bc`; fi
if [ 51 -le $min -a $min -le 56 ]; then hours=`echo "$hrs + .9" | bc`; fi
if [ 57 -le $min -a $min -le 60 ]; then hours=`echo "$hrs + 1.0" | bc`; fi
fi
echo "Minutes Entered: $minutes"
echo "$hrs Hours, $min Minutes ($hours Hours)"
Please let us know if you think you have a better solution or have a suggestion by using the commenting system below.
Posted by Derek@TheDailyLinux in
Scripting »
3 Comments »
The following is a short and plain shell script that will start a timer when you run the program that counts up. I think you could argue that it’s not a stopwatch because it doesn’t support laps, but it’s close enough for me. You can easy get started by copying the following code block into a text editor, saving as stopwatch.sh, running chmod +x stopwatch.sh to make it executable, and finally starting it with ./stopwatch.sh. To stop it, hit Ctrl+c.
#!/bin/bash
BEGIN=$(date +%s)
echo Starting Stopwatch...
while true; do
NOW=$(date +%s)
let DIFF=$(($NOW - $BEGIN))
let MINS=$(($DIFF / 60))
let SECS=$(($DIFF % 60))
let HOURS=$(($DIFF / 3600))
let DAYS=$(($DIFF / 86400))
# \r is a "carriage return" - returns cursor to start of line
printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS
sleep 0.25
done
The previous script will allow you to track from the time you say “go” until you stop it. It’s also nice to the real-estate on your terminal and will use backspace to remove the characters printed from before to make room for the new ones.
If you’re not worried about starting it “now” or real estate in the terminal, you could always use uptime and throw it into a while loop like this:
#!/bin/sh
while true; do uptime | cut -d' ' -f2; done
Both are simple, both have their own advantages and disadvantages. Choose wisely.
Source: unix.com forums
I used the scripts to help me figure out how long it was taking my desktop to lock up so I could troubleshoot it better. Another use might be to keep track of how much time you’re spending on the computer vs the amount of time spent skiing! Have fun either way.
Edit
A subscriber, Jim, came up with a much better stopwatch script than my thrown together example. Jim sent it via email, but I’ll post it here for all to see.
It doesn’t start counting till you press the spacebar, pressing the
spacebar again pauses it counting, until the spacebar is pressed to
continue counting.
Press ‘q’ to quit
Press ‘r’ to reset to zero
#!/bin/bash
# sets stdin to no echo and give a char every tenth of a sec.
stty -echo -icanon time 1 <&0
chkspace () {
if ! read -t 0 ; then return 1 ; fi # no char pressed
read -n 1 ans
if [ "$ans" = " " ]; then return 0 ; fi
case "$ans" in
r|R) COUNT=0 ; BEGIN=$(date +%s)
printf "\r%3d Days, %02d:%02d:%02d" 0 0 0 0
;;
q|Q) stty echo icanon <&0
echo ""
exit 0
;;
[1-9]) echo " - $ans" ;;
esac
return 1
}
echo "Stopwatch: to start and stop press the SPACEBAR..."
printf "\r%3d Days, %02d:%02d:%02d" 0 0 0 0
COUNT=0
IFS=
while true ; do
while true; do
if chkspace ; then break; fi
sleep 0.1
done
BEGIN=$(date +%s)
while true; do
NOW=$(date +%s)
let DIFF=$(($NOW - $BEGIN + $COUNT))
let MINS=$(($DIFF / 60))
let SECS=$(($DIFF % 60))
let HOURS=$(($DIFF / 3600))
let DAYS=$(($DIFF / 86400))
# \r is a "carriage return" - returns cursor to start of line
printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS
if chkspace ; then break; fi
sleep 0.1
done
COUNT=$DIFF
done
Posted by Derek@TheDailyLinux in
Tutorials and Guides »
Add Comment »
This is a quick start guide, so let’s get to it! For more information, see bottom of post…
Note: I dug this one out from my post drafts from April, 2010, so I’m not sure how relevant the information is now. It looks like enough to get somebody heading in the right direction though, so I’m going to post it. I have a related guide here (Guide on TFTP Server Setup in Fedora).
Install
su -c 'yum install -y vsftpd'
Setup
gedit /etc/vsftpd/vsftpd.conf
The above steps will get you started with a FTP server in Fedora (may work in other distros such as Ubuntu as well). It will allow you to immediately start sending files via FTP as a local user. So, if my username on my FTP server was ‘derek’, I could use
ftp <em>ip_address</em> -user derek"
Here’s an example config file:
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
#local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
#xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd whith two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
Posted by Derek@TheDailyLinux in
Scripting »
4 Comments »
The following commands will extract the file extension string from a given filename. The only trick to these commands is they will give you the final extension after the last ‘.’. In other words, they will not work for extensionless files and files with two dot extension names (like file.tar.gz or similar).
for i in *; do echo $i | sed -e 's/.*[.]\(.*\)/\1/'; done
for i in *; do echo $i | awk -F. '{ print $NF }'; done
This command will grab the first dot extension even if there are two (it will return tar for file.tar.gz).
for i in *; do echo $i | cut -d'.' -f2; done
I’d love to hear your suggestions in the comments if you have a better, more optimized way of doing this.