Octal

Shell tricks

86 posts in this topic

Wow... I didn't know about the Alias command. I have already used it multiple times since I read this thread today. Very useful command.

1

Share this post


Link to post
Share on other sites

This generates a list of resolved Linux virtual hosts on the fly:

#!/bin/sh
for i in `/sbin/ifconfig -a|grep 'inet addr'|awk -F":" '{print $2}'|awk '{print $1}'`;do host $i;done 2>/dev/null

Can anybody come up with anything that uses a different approach or is more concise?

1

Share this post


Link to post
Share on other sites
You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?

Because I was working with a directory structure that has about 500 email accounts. There are quite a few things left out of this script to avoid posting SQL scripts for a propriatery software company on a public forum.

The directory structure is like this: /var/mail/<customerid>/Maildir/cur

Where the customer id is pulled from a SQL database and inputted into my bash script that deletes the contents of the person(s) inbox. What decided the customer id in my case was inactive customers that had email disabled due to negligence of some type: spam, porn, large attachments, you name it.

Well with the interesting parts gutted, this script does essentially nothing. I'm just wondering why you posted it.

Actually, although Im replying to prehistory, rm -rf , or rather the linux kernel has a hard limit to how many files it can remove. It spews back "/bin/rm: Argument list too long." when it exhausts the between process's execve buffer's in the kernel (its only 128k).

However, a simple solution is as below.

find . -name '*' -print0 | xargs -0 rm

Original info provided by John simpson, I just googled it when I hit the above limit one day.

http://leap-cf.org/oldarchive/2004-May/038802.html

0

Share this post


Link to post
Share on other sites

If you make a mistake typing a command you can change something specific and then re-execute using the following bash trick:

[n3xg3n@enigma ~]$ wgt http://www.whatismyip.org
bash: wgt: command not found
[n3xg3n@enigma ~]$ ^wgt^wget
wget http://www.whatismyip.org
--2010-11-21 20:35:48-- http://www.whatismyip.org/
Resolving www.whatismyip.org...

basically, ^old^new will replace 'old' with 'new' in the previous command and re-execute.

1

Share this post


Link to post
Share on other sites

whereis firefox

Really nice one! I still consider myself a noob, and I'm not yet entirely confident with the folder structure. I anticipate that this command will help me a lot. A web search tells me that the command

which

does roughly the same thing.

0

Share this post


Link to post
Share on other sites

Okay I don't know if this was posted already but to me it's really handy sometimes.

rm'ing files of a certain size in a directory of my choosing all in one go.

find ~/build/ -type f -size 0 -exec rm {} \+

0

Share this post


Link to post
Share on other sites

Record a desktop session, with audio.


#!/bin/bash
cd $HOME/recordings
SavePath=$(zenity --file-selection --save --confirm-overwrite)
echo "Saving video to $SavePath"

INFO=$(xwininfo -frame)

WIN_GEO=$(echo "$INFO"|grep -e "Height:" -e "Width:"|cut -d\: -f2|tr "\n" " "|awk '{print $1 "x" $2}')
WIN_POS=$(echo "$INFO"|grep "upper-left"|head -n 2|cut -d\: -f2|tr "\n" " "|awk '{print $1 "," $2}')

ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -s $WIN_GEO -r 15 -i :0.0+$WIN_POS -r 15 -acodec pcm_s16le -sameq "$SavePath.avi"

echo "$WIN_GEO -i :0.0+$WIN_POS -acodec"
echo "$WIN_POS"

The script expects a directory named "recordings" under ~/

1

Share this post


Link to post
Share on other sites

dd if=/dev/zero of=location+filename size=blocksize count=number of blocks desired

simple yes but has some uses :P. like if you want to install linux in one partition without setting up logical partitions if say a oem installation on a laptop can wipe out recovery partition which is usually big enough for linux, use the above and

mkswap -filelocation/name-

or you can make your own vaults/encrypted volumes by formating the file like

mkfs.ext4 -filename/location-
mount -o -t ext4 -file location/name- -mount dir-

can use gpg or whatever you prefer to encrypt said file, can do multiple times over if you want. could use the same ddfile/image for anything that needs a partition or where a partition could add security like having all your web content for a web server in said partition and only give the webserver privileges to that image. it will also indicate the speed of your hdd/sdd/ram wherever your writing the file as if=/dev/zero has dd actually write all the bytes to 0

1

Share this post


Link to post
Share on other sites

Posted (edited)

!/bin/bash 
usage() {
echo "
HELP:
   -i input file
   -o output/save file
   -t showmount timeout
   -h print this help
"
exit 1;
}
while getopts :t:i:o:h opts
do
	case ${opts} in
		i) 
			INPUT_FILE=${OPTARG};;
		o) 
			OUTPUT_FILE=${OPTARG};;
		t)
			TIMEOUT=${OPTARG};;
		h) 
			HELP=1;;
		\?)
			echo "invalid parameter detected!"; exit 1;;
	esac
done
if [ "$TIMEOUT" = "" ]
	then
	TIMEOUT=3
fi
if [ "$HELP" != 1 ] && [ "$INPUT_FILE"  != "" ]  && [ "$OUTPUT_FILE" != "" ]
	then
	echo "Running showmount check with the following oprions:"
	echo "input file: $INPUT_FILE"
	echo "output file: $OUTPUT_FILE"
	echo "timeout: $TIMEOUT"
	while read -a HOST;
	do
		SCAN_HOST=`echo $HOST | tr "\n" "@" | sed 's/\@//'`;
		echo $SCAN_HOST;
		#sleep 5 && echo "sleep done!" && kill "$!" 2> /dev/null && echo killed "$!" 2> /dev/null & 
		sleep $TIMEOUT && kill $(ps aux | grep showmount | grep -v grep | awk '{print $2}') 2> /dev/null &
		showmount -e -3 $SCAN_HOST >> $OUTPUT_FILE 2> /dev/null;
		#echo $HOST
	done < $INPUT_FILE
	exit 0;
else
	usage;
fi

Unsure what version this was: so it may or may not work. Should log all NFS responses and can be grep'd for 666 privs. Don't end up in the federal pokey - use at your own risk. :-)

Edited by tekio
0

Share this post


Link to post
Share on other sites

Posted (edited)

This is a script I made to check email files and log to a comma delimited file and import into excel. Was a freelance job a few years ago (but I still own the rights so posting here). 

#!/bin/bash
TEMP_FILE="temp.tmp"
CR=`echo "\0015"`
usage () {
echo "     
HELP:
    -e mail export file (exported from Outlook)
    -o output file (Excel import file)
    -m mode: QTC or CAD
    -c save output file with DOS line-breaks
    -v verbose output of parsed records
    -d remove duplicates"
}
exit 1
while getopts :e:o:m:vdc opts
do
	case ${opts} in
		e) 
			MAIL_EXPORT_FILE=${OPTARG};;
		o) 
			OUTPUT_FILE=${OPTARG};;
		m)
			MODE=${OPTARG};;
		v)
  			VERBOSE=1;;
  		d)
  			DEDUPE=1;;
  		c)
			DOS=1;;
		\?)
			usage; exit 1;;
	esac
done

if [ -a "$MAIL_EXPORT_FILE" ] && [ "$OUTPUT_FILE" != "" ] && ([ "$MODE" == "QCT"  ]  || [ "$MODE" == "CAD" ])
	then
		if [ "$MODE" == "CAD" ]
			then
			first=(`cat $MAIL_EXPORT_FILE | egrep FIRSTNAME\:\:\ \ \ [A-Za-z0-9]*.*$ | cut -d: -f3 | sed 's/[ ]//g'`)
			last=(`cat $MAIL_EXPORT_FILE | egrep LASTNAME\:\:\ \ \ \ [A-Za-z0-9]*.*$ | cut -d: -f3 | sed 's/[ ]//g'`)
			serial=(`cat $MAIL_EXPORT_FILE | egrep KEY\:\:[\ ]\{9\}\([0-9A-Z]*\ \)\{3\}\([A-Z0-9]\{4\}\)$ | cut -d: -f3 | tr " " "@"`)
			date=(`cat $MAIL_EXPORT_FILE | egrep DATE::\ \ \ \ \ \ \ \ [0-9].*\([0-9]\|[AM]\)$ | cut -d" " -f14`)
			software=(`cat $MAIL_EXPORT_FILE | egrep EDITION\:\:\ \ \ \ \ QuiltCAD.*$ | cut -d: -f3 | sed 's/^[ ]//g' | tr " " "@"`)
			email=(`cat $MAIL_EXPORT_FILE | egrep EMAIL\:\:\ \{7\}.*@.*\..*$ | cut -d: -f3 | sed 's/[ ]//g'`)
			#check that all array counts match to detect offset errors.
			ENDARR=`expr ${#first[@]} - 1`
			for i in `seq 0 $ENDARR`
				do
    				echo ${first[$i]} ${last[$i]},${email[$i]},${date[$i]},`echo ${serial[$i]} | sed -e 's/^@*//g' | tr "@" " "`,`echo ${software[$i]} | sed 's/^@*//g' | tr "@" " "` >> $OUTPUT_FILE
    				
    				if [ $VERBOSE ]
    					then
    					echo "      [ $i ]  ${first[$i]} ${last[$i]}  --> " `echo ${regcode[$i]} | sed 's/^@//g' | tr "@" " "`
					fi
				done
		elif [ "$MODE" == "QCT" ]
			then
			#name, email, date, ugid, computer id, software id, registration code
			first=(`cat $MAIL_EXPORT_FILE | egrep FIRSTNAME\:\:\ \ \ \ \ \ \ \ [A-Za-z0-9]*.*$ | cut -d: -f3 | tr -d ' '`)
			last=(`cat $MAIL_EXPORT_FILE | egrep LASTNAME::\ \ \ \ \ \ \ \ \ \ [A-Za-z0-9]*.*$ | cut -d: -f3 | sed 's/\ //g' | cut -d: -f3`)
			date=(`cat $MAIL_EXPORT_FILE | egrep DATE\:\:\ \{14\}\([0-9]\{1,2\}\/[0-9]\{1,2\}\/[0-9]\{4\}\)$ | cut -d: -f3 | sed -e 's/ //g'`)  #need to fix
			email=(`cat $MAIL_EXPORT_FILE | egrep EMAIL\:\:\ \{7\}.*@.*\..*$ | cut -d: -f3 | sed 's/[ ]//g'`)
			compid=(`cat $MAIL_EXPORT_FILE | egrep COMPUTER\ ID\:\:\ \{7\}\(\-\|[0-9]\)?[0-9]\{7,10\}$ | cut -d: -f3`)
			ugid=(`cat $MAIL_EXPORT_FILE | egrep UGID\:\:\ \{14\}[0-9]\{3\}$ | cut -d: -f3 | sed -e 's/ //g'`)
			softid=(`cat $MAIL_EXPORT_FILE | grep SOFTWARE\ ID | cut -d: -f3 | sed -e 's/ //g' | sed -e 's/^$/Unlisted/g'`)
			regcode=(`cat $MAIL_EXPORT_FILE | egrep REGISTRATION\ CODE\:\:\ [0-9]{4}\ [0-9]{4}\ [0-9]{4}\ [0-9]{4}$ | cut -d: -f3 | tr " " "@"`)
			ENDARR=`expr ${#first[@]} - 1`
			for i in `seq 0 $ENDARR`
				do
    				echo ${first[$i]} ${last[$i]},${email[$i]},${date[$i]},${ugid[$i]},${compid[$i]},${softid[$i]},`echo ${regcode[$i]} | sed 's/^@//g' | tr "@" " "` >> $OUTPUT_FILE

    				if [ $VERBOSE ]
    					then
    					echo "       [ $i ]  ${first[$i]} ${last[$i]}  --> " `echo ${regcode[$i]} | sed 's/^@//g' | tr "@" " "`
					fi
				done
		fi
else 
	echo "     HELP:
    -e mail export file (exported from Outlook)
    -o output file (Excel import file)
    -m mode: QTC or CAD
    -c save output file with DOS line-breaks
    -v verbose output of parsed records
    -d remove duplicates"
exit 1;
fi
if [ $DEDUPE ]
	then
	cat $OUTPUT_FILE | sort | uniq >> $TEMP_FILE
	rm $OUTPUT_FILE
	mv $TEMP_FILE $OUTPUT_FILE
echo "-------------------------------------------------------+"
echo "[ √ ]  Output file de-duplicated!" 
fi
if [ $DOS ]
	then
	cat $OUTPUT_FILE | sed -e "s/$/${CR}/g" >> $TEMP_FILE
	rm $OUTPUT_FILE
	mv $TEMP_FILE $OUTPUT_FILE
echo "-------------------------------------------------------+"
echo "[ √ ]  Output file in DOS line-breaks!"
fi
echo "-------------------------------------------------------+"
echo "  | Parsing completed! "
echo "  | Processed File: $MAIL_EXPORT_FILE"
echo "  | Records parsed: ${#first[@]}"
echo "  | Mode: $MODE "
echo "  | Output File: $OUTPUT_FILE"
echo "  | Total Records:" `cat $OUTPUT_FILE | wc -l`
echo "  | Offset Error: N\A"
echo "-------------------------------------------------------+"
exit 0;

 

Edited by tekio
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now