An occasional outlet for my thoughts on life, technology, motorcycles, backpacking, kayaking, skydiving...

Thursday, February 27, 2014

Linux Oneliners

I am moving entries of my old one-liners text file to a web page for easier access.
# mount cdrom
[ -d "/mnt/cdrom" ] || mkdir -p /mnt/cdrom; mount /dev/cdrom /mnt/cdrom/

# mount a samba/cifs share (hostess is used by vmware in NAT mode)
user=rbronosky; [ -d "/mnt/smb" ] || mkdir -p /mnt/smb; mount -t cifs //hostess/$user -o username=$user /mnt/smb

# remove the leading path from a find
find . | sed 's/^..//'

# basic bash for-looping
for f in $(cat changed_files.txt) ;do vimdiff trunk/$f branches/iteration3/$f; done;
# if you have spaces in filenames
find . -type f |sed 's/^..//'|while read f; do ls "$f"; done
# or
(IFS=$'\n';for f in $(find . -type f) ; do cat ../Logs2/$f $f>../LogsCat/$f; done;)

# version number comparison using the power of the Python standard lib
[ "1" == "$(python -c "from distutils.version import *;print int(LooseVersion('3')>=LooseVersion('2.5.1'))")" ] \
&& echo Yes || echo No
# above is True, below is False...
[ "1" == "$(python -c "from distutils.version import *;print int(LooseVersion('2.3')>=LooseVersion('2.5.1'))")" ] \
&& echo Yes || echo No

# basic anonymous cvs
cvs -z3 ls

# make wget use filenames as suggested by the HTTP header Content-Disposition (requires wget>=1.11)
# becomes ps_color.vim
echo -e '\ncontent-disposition=on'>>~/.wgetrc

# copy files using ONLY ssh, not scp or sftp
tar cvf - $files_and_dirs_to_send | gzip | ssh $destination_server "cd $destination_dir; tar zxvf -"
# or to leave them compressed on the remote side...
tar cvf - $files_and_dirs_to_send | gzip | ssh $destination_server "cat > $destination_dir\archive.tar.gz"

### Help dad out via ssh (and telephone) ###
## I configure my router to forward $secret_port to port 22 on my machine.
## Because Ubuntu does not default to having and ssh server, I tell dad to do:
sudo apt-get install openssh-server
## I to tell dad to connect to my computer via:
ssh -p $secret_port -R $secret_port:localhost:22 -N $my_username@$my_router_ip & screen; kill %1
## I connect to his computer via:
ssh -v -p $secret_port $dad_username@localhost screen -x
## Now we are both looking at the same shell.  We can both type and collaborate.

## Convert/get a date from a timestamp using only Gnu Date
date -d @1199163600

## Sync the date of one server to that of another.  (Useful when firewalls prevent you from using NTP.)
sudo date -s  "$(ssh "date -u")"

## Get 255 random chars from /dev/random
uuencode -m - < /dev/random |sed 1d|tr -d \\n|head -c 255
# That is Base64 [/+a-zA-Z0-9], but you can add a substirution  to the sed.
# This one limits it to hex chars:
uuencode -m - < /dev/random |sed '1d;s/[^0-9A-F]//g'|tr -d \\n|head -c 255
# However, for storing into a DB, I like to keep the newlines so:
uuencode -m - < /dev/random |sed 1d|head -c 255

# Time stamp for a file name
date +%s
# a readable one
date +%Y%m%d%H%M%S

# Get ip addresses for all network interfaces
ifconfig | awk -e '/^[a-z]+/{sub(":",""); f=$1} /inet /{print f " " $2}'

# DiG format as a clean single line for batch processing

dig +nocmd +noall +answer