The Archives

  • 21.Jul.11
    Printing sequential numbers in BSD shell | rafacas | (0)
    In Linux, the seq command is pretty useful in some scripts, because it prints a sequence of numbers: $ seq 1 5 1 2 3 4 5 It is usually used in for loops: for i in `seq 1 5`; do ... done But this command is not included in BSD-like OSes. It is contained in the sh-utils, so one option is downloading and compiling it. But I prefer using the commands that come by default with the OS, for portability. In the BSD case, I found the jot command that prints sequential or random data. The following example shows the seq behaviour with jot. $ jot 5 1 2 3 4 5 Other example ...
  • 30.Nov.09
    Changing file extension shell | rafacas | (0)
    I usually change the annoying JPG extension to jpg (I do not like uppercase file names or extensions). For this I use a function I found in shell-fu: rename_ext() { local filename for filename in *."$1"; do mv "$filename" "${filename%.*}"."$2" done } I copied it into my .bashrc file, so that I use it as follows: $ rename_ext JPG jpg
  • 10.Mar.09
    Converting filenames to lowercase cmd | rafacas | (0)
    $ for i in *; do mv "$i" "$(echo $i|tr [:upper:] [:lower:])"; done Turns all uppercase characters in the present directory filenames into lowercase. There is no collision detection, so if some name gets repeated, the destination file will be overwritten and the first file will be lost. Use the above if you know that there will be no collisions.
  • 10.Feb.09
    Some xargs scripts, shell | pfortuny | (1)
    Rafacas has already mentioned it, but xargs is sometimes much more useful than what it looks like. Two examples come to mind: Way too many files for rm or ls. It may well happen that a script has generated more than 10000 files in the same directory (it was your friend, not you, I know). If you try and rm * in there, you will be in trouble. Ditto if you simply want to count them with ls | wc -l. However, the following works: $ find . -type f -print0 | xargs -0 ls | wc -l (or rm instead of ...
  • 23.Jan.09
    Space for shell | pfortuny | (5)
    So your friend sent you a batch of tiffs to be converted into jpg's (using ImageMagick's convert command). However your friend (who is not so geeky as you) uses spaces in filenames (which is a deadly sin), like in First photo of birthday.tiff Second photo.tiff You wanted to use a shell for loop, but the obvious $ for i in `find . -name *tiff` ; do convert "$i" "${i%tiff}jpg" ; done produces garbage (try it). This is just one of the consequences of your friend's carelessness. But there is a solution. IFS (Internal Field Separator) is a shell variable controlling what character(s) delimit a 'field' in ...
  • 05.Jan.09
    Thumbnails cmd | fernape | (2)
    Easy and fast way for creating thumbnails: $ for fich in *.jpg;do convert "$fich" -resize 32x32 thumb_"$fich";done
  • 02.Dec.08
    Quiet grep automated, shell | pfortuny | (0)
    Usually during an cron job, one needs to check for an expression in a log file, or in the output of a command, without generating any cumbersone new output (such as a standard grep would do): this is exactly the use of the -q option of grep. For example: for i in `grep -v '^#' /etc/passwd | cut -f1 -d:` grep $i /var/log/samba/log.smbd | grep -q 'failed to authenticate' if [ $? == 0 ] ; then mail -s 'Error in samba' $i <<EOM ...
  • 17.Sep.08
    Time cmd | pfortuny | (0)
    $ time for i in `ls` ; do cat $i ; done Shows the 'real', 'user' and 'sys' time invested in the task.