Cheats

A cheat sheet containing scripts that I normally use (shell, sql, python .etc)

rename files to indexed filenames
cc=0; for i in `ls`; do let cc=$cc+1;  echo $i; echo $cc ; cp $i $cc.png; done

extract all images:
for i in `find . -name images.zip`;do d=`echo $i | sed ‘s/\/images.zip//g’| sed ‘s/\.\///g’`; unzip -o -d $d $i ; done

remove extra .wav extention from .WAV.wav files:
for i in `find . -name ‘*.wav’`; do echo $i; d=`echo $i | sed s/\.wav//g`; echo $d; cp $i $d; done

change .html files to .jsp:
for i in *;do d=`echo $i | sed s/’.html’//g` ; echo $d ; cp $d{.html,.jsp}; done

mysql start
/etc/rc.d/init.d/mysqld start

copy images for other resellers
go to mcm directory: cd mcm
for i in ` ls -d */`;do echo $i’images’; cp mixonic/images/case-jacket.gif $i’images’; done

zip all images
cd 680digital
for i in ` ls .. `;do echo $i’images’;  cd ../$i; 7z.exe a -tzip images.zip images/* ; done

list unix users
cat /etc/passwd

find process using a port:
netstat -o | grep 1098

dump table from production
pg_dump –host *** -d resellers -U *** –table {_table_name} –create -f {_table_name}.sql –format p
multiple tables:
export table_names=( base_prices override_prices )
for i in ${table_names[@]};do echo $i; pg_dump –host [host] -d resellers -U *** –table $i –create -f $i.sql –format p ;done

and load table dump into localhost:
for i in `ls *.txt`; do psql -U*** -h localhost resellers -f $i > $i.log; done

postgres number of tcp connections:
netstat -an|grep 5432

postgres connect to db with psql
psql -h localhost -U *** -W -d resellers

postgres psql run query and write to file
$ psql -h [host] -U *** -W -d resellers -c “select …” > wrapQuery.txt

restart postgres
/etc/init.d/postgresql [start,stop,restart]

postgres config
/var/lib/pgsql/data/postgresql.conf

surround email addresses with !!
sed -e ‘s/\([A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,4\}\)/!!\1!!/g’  < email1.txt > new.txt
then extract them:
cat new.txt| grep @ |  sed -e ‘s/.*!!\(.*\)!!.*/\1/g’  | less

import database:
“C:\Program Files\PostgreSQL\8.1\bin\pg_dump” -U *** -h *** shipping > DFS-Shipping-Localhost.sql
“C:\Program Files\PostgreSQL\8.1\bin\psql” -U *** -h localhost -d resellers -f DFS-resellers-localhost.sql

searches for package names for pattern
apt-cache search <pattern>

iterating over file names and concating  to each:
for i in `ls`; do echo sepans$i; done > filenames.txt

removing blank from filenames:
for i in *; do mv “$i” `echo “$i” | tr ‘ ‘ ‘_’`; done

change jars to jar.zip
for i in *.jar; do mv “$i”{,.zip}; done

replace breaklines enters returns with ;
tr ‘\r\n’ ‘;’ < filenames.txt > filename2.txt

files created / modified after a certain time

touch -t `date +%m%d0000` /tmp/$$
or touch -t 200701310846.26 /tmp/$$ (yyyyMMddhhmm.ss)
find /tmefndr/oravl01 -type f -newer /tmp/$$
rm /tmp/$$

remove files older than a year – purge:
find . -type f -mtime +365 -exec rm {} \;

find string in files newer than a time ordered by modified time:
find . -name ‘…Logger.log*’  -newer /tmp/$$ | xargs ls -rt | xargs grep WaitingForContent > messages.txt

find the order number replace it with # in the first line of messages.txt
sed -n ’1p;1q’ messages.txt | sed ‘s/\(SO00[0-9]*\)/#/g’
or more precise: sed -n ’1p;1q’ messages.txt | sed ‘s/\(SO00\([0-9]\)\{6\}\)/#/g’

apache restarting
/etc/init.d/httpd [start/stop/restart]

apache error logs:
/var/log/httpd/error_log

config:
/etc/httpd/conf/httpd.conf

extract tar.gz archive
tar -xzf archive.tar

compress tar
tar cf – target | gzip -c > target.tgz

rmove non empty folders
rm -rf  (use with care!)

remove svn data (disconnects source from svn server)
find . -name ‘.svn’ -type d | xargs rm -rf  (use with extra care!!!!!!!!!!!)

files larger than 1Mb:
du . | grep “[0-9]\{7\}”

list only directory folder directories
ls -dl */

export project names,

get all the files
cd Projects/[random_project_id]

for i in ${projectIds[@]}; do  cd ../$i; ls –format=single-column $PWD/*; done

write all h files with path in a file for classpath setting:
for i in ‘*.jar’; do echo ${PWD}/$i | sed s/’jar ‘/’jar;’/g ; done > alljars.txt

invalid non iso characters:
’ ” – “

← Back