UNIX Tools – examples

Here are a few examples you’ll probably find useful at some point.

Counting the number of lines of a C project

$ wc -l `find -name "*.[ch]"|xargs`

Execute a command on a directory and all its subdirectories

$ find "path" -exec "command" {} ;

Deleting all the files not accessed for 1 day in the current directory and all its subdirectories

$ find . -atime +1 -exec rm -f {} ;

chmod on a directory and all its subdirectories

$ find "path" -type d -exec chmod "chmod args" {} ;

Removing a specific directory from a directory and all its subdirectories

$ find "path" -type d -name "directoryName" -exec rm -rf {} ;

Debian specific: Removing all unneeded *-dev packages
A quick note on what this does: it uses sudo do call dpkg to purge the packages listed by deborphan, ending in “dev”. See more on this on Debian-specific resources like: http://www.bxlug.be/rubrics/20.

$ sudo dpkg -P `deborphan -a|grep dev$|cut -b26-`

or (equivalent using xargs)

$ deborphan -a|grep dev$|cut -b26-|xargs sudo dpkg -P

Add a «<?php» line to the beginning of a set of PHP files

$ find -name "*.php" -exec sed -i '1 s/^/<?phpn/' {} ;

rsync

$~/foo: rsync -avz . host:dst/
$~/foo: rsync -avz * host:dst/

The first command will synchronize the folder ” /foo/” with the one ” /dst/foo/” on host.
The second command will synchronize the folder ” /foo/” with the one ” /dst/” on host. Options

-a–archivearchive mode
-v–verboseincrease verbosity
-z–compresscompress file data
-n–dry-runshow what would have been transferred
-u–updateupdate only (don’t overwrite newer files)
-C–cvs-excludeauto ignore files in the same way CVS does
–progressshow progress during transfer
This article was first written in July 2003 for
the BeezNest technical website (http://glasnost.beeznest.org/articles/48)

Quick Contact Form