Tuesday, January 3, 2012

Some quick tips

Find and replace text within all files within a directory

find PATH_TO_DIRECTORY -type f | xargs perl -pi -e 's/SEARCH_KEYWORD/REPLACE_KEYWORD/g'


Find a text in files in a directory and subdirectories

find PATH_TO_DIRECTORY -exec grep -l 'SEARCH_KEYWORD' {} \;


Find all modified file in last few days within a directory (exclude some sub-directory for e.g. CVS)
Create 2 files as below under DIRECTORY (e.g. find all modified files between 1st July 2011 and 31st July 2011)

touch -t 201107010000.01 FILE_1 /* 2011[year]07[month]01[day] */
touch -t 201107312359.59 FILE_2 /* 2011[year]07[month]31[day] */
find PATH_TO_DIRECTORY -type f -newer FILE_1 ! -newer FILE_2 -exec ls -ogh {} \; | grep -vw 'EXCLUDE_SUB_DIRECTORY' | cut -f4,5,6 -d' '


Batch resize image using ImageMagick

cd PATH_TO_IMAGE_DIRECTORY
mogrify -resize WIDTHxHEIGHT *
mogrify -resize WIDTHxHEIGHT -quality 80 * (to reduce quality)


Copy directory structure without the files

find * -type d -exec mkdir PATH_TO_DIRECTORY/\{\} \;