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 –archive archive mode
-v –verbose increase verbosity
-z –compress compress file data
-n –dry-run show what would have been transferred
-u –update update only (don’t overwrite newer files)
-C –cvs-exclude auto ignore files in the same way CVS does
–progress show progress during transfer
This article was first written in July 2003 for
the BeezNest technical website (http://glasnost.beeznest.org/articles/48)
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *