//Browsing archives (Page 3 of 13)
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow

Selected Bash Keystrokes:
Ctrl-U - Cuts everything to the left
Ctrl-W - Cuts the word to the left
Ctrl-Y - Pastes what's in the buffer
Ctrl-A - Go to beginning of line
Ctrl-E - Go to end of line

View Comments »

To change the extension on a group of files using only bash builtins and mv:

for i in *.txt; do mv $i ${i%%.txt}.html; done

View Comments »

If the names of some of the files in a directory may contain spaces, combine find's "-print0" option with xargs' "-0" option:

# this will behave incorrectly if some files or directories have spaces:
find . -type f | xargs ls -l

# this will work correctly:
find . -type f -print0 | xargs -0 ls -l

View Comments »

Use `openssl rand <bytes>` to generate random data.

openssl rand 32

View Comments »

# Swap 2 files/dirs that are in the same dir
# Usage: sw f1 f2
function sw {
if [ "x$f2" = "x" ]; then
echo "Usage: sw file1 file2"
echo " swap name of 2 files"
d1=$(dirname $f1)
d2=$(dirname $f2)
if [ "$d1" != "." -o "$d2" != "." ]; then
echo "sw: Can swap only files in current directory"
if [ -e "$f1" -a -e "$f2" ]; then
mv $f1 .sw.$f1
mv $f2 $f1
mv .sw.$f1 $f2
echo "sw: '$f1' and '$f2' must exist"

View Comments »

Find all files with given name (you can use Bash expansion if you'd like), and Grep for a phrase:
find . -name  -exec grep "phrase" {} \;

To display the filename that contained a match, use -print:
find . -name  -exec grep "phrase" {} \; -print

Or, use Grep options to print the filename and line number for each match:
find . -name  -exec grep -Hn "phrase" {} \;

The string `{}` is replaced by the current filename being processed everywhere it occurs in the arguments to the command. See the `find` man page for more information.

View Comments »

!$ contains the last "item" of the previous command.

% cp Inflector.class.php /www/lib/
cp: target `/www/lib/' is not a directory: No such file or directory
% mkdir !$ && cp Inflector.class.php !$
mkdir /www/lib/ && cp Inflector.class.php /www/lib/
% ls !$
ls /www/lib/

View Comments »

Add the following to your ~/.inputrc to make the up and down keys search your history (using what you have typed in already as a prefix) rather than just go through all history items:

"\e[A": history-search-backward
"\e[B": history-search-forward

View Comments »

How to mount an ISO image file onto a directory:

 mount -t iso9660 -o loop image.iso /mnt/isoimage 

Where image.iso is the image file and you want to mount it to /mnt/isoimage.

View Comments »

You can use find with the '-newer' flag in conjunction with tar to create a patch file:
tar -czvf patch-20070321.tar `find /path/to/project/ -newer /path/to/project/last-archive.tgz -print`

In this example 'last-archive.tgz' is the last tarball for the given project. -newer finds files newer that than last-archive.tgz, this way you can tar up only the changed files.

View Comments »

Regular expression search and replace on files "in-place" can be done with perl:

perl -pi -e 's/foobar/fooBar/g' file1 file2 file3

For example, here's how to change a package name for an entire Java source tree from net.widgets.* to com.widgets.*:

mv src/net src/com
find src -name '*.java' -print0 | xargs -0 perl -pi -e 's/^package net.widgets./package com.widgets./'

To have perl make backups of the original files first, follow the -i option with a suffix to append.

View Comments »

If you want to tunnel a long-lived connection over SSH such that the tunnel goes away when the application disconnects, try something like the following example:

ssh -f -q -L 5900:localhost:5900 user@remotehost.com sleep 60

By executing "sleep 60" remotely, the tunnel stays alive for at least 60 seconds, and assuming your application has connected by then, the tunnel will continue to stay alive until the application disconnects.

The options given above are perfect for executing this command from a script; it is quiet (-q) and goes to background after prompting for a password (-f). This particular example forwards the VNC protocol so that when your VNC client connects to localhost, it connects securely to remotehost.com over the tunnel.

View Comments »

The script below will truncate the $PWD to 1/4 of the terminal width and put it into the command prompt, all within the same bash process, no forks to tr and gawk.

function truncate_pwd

local pwdmaxlen=$((${COLUMNS:-80}/4))
if [ ${#newPWD} -gt $pwdmaxlen ]
newPWD=".+${newPWD: -$pwdmaxlen}"

PS1="${ttyname}@\h:\${newPWD}\\$ "

View Comments »

ssh -l <login> -L <port>:<destination:port> <proxymachine> <local addy>
ssh -l foo -L 5000: https://localhost:5000/

Then go to https://localhost:<port>/ to get to destination's website; through the proxy machine.

View Comments »

The official BitTorrent distribution includes several python client applications that can be run from the command line.

To download a torrent try:

/path/to/btdownloadheadless.py --display_interval 5 --spew 1 torrentfile.torrent

To make things easier, add the following to your profile:

alias dltorrent='screen /path/to/btdownloadheadless.py --display_interval 5 --spew 1'

then type 'dltorrent whatever.torrent' to start torrenting. Using screen means that this can be detached and left running even when you aren't logged in. To detach use CONTROL-A, then D. This can then be reattached by using 'screen -ls' to get a list of screens and 'screen -dr screenname' to reattach the chosen screen.

Other options are available to adjust for example download/upload rate. Run btdownloadheadless.py with no arguments for a full list.

View Comments »

This is a simpler password generator.

< /dev/urandom tr -dc A-Za-z0-9_ | head -c8

Note that the 'tr' strips out everything except characters in the ranges (alphanumeric, mixed case and underscores). This is a nice approach as piping to head means the minimum number of bytes required to generate a password of appropriate length are taken from /dev/urandom vs other methods which take more than you should need but still have a chance of not having obtained enough random data to generate a password of the required length. You can change the parameter to head to get passwords of any length.

View Comments »

Create rc* alias for each init.d script. (can be include at /root/.bashrc)

for service in `cd /etc/init.d/; ls`; do
alias "rc${service}"="/etc/init.d/${service}";

SuSE distribution has a rc'service' for each init.d script that enables to easily start/stop service; but Debian and Ubuntu have not.

View Comments »

which directories and trees take up all the diskspace?
du -sm $(find /start/dir/* -type d -maxdepth 1 -xdev) | sort -g

If you want more human readable output try:
du -ha /var | sort -n -r | head -n 10

you want to see ALL directories in the tree
find $1 -type d | xargs du -sm | sort -g

To show all directories size including sub directories, type

du -h

To calculate the current directory size you are in (-s stand for summary)

du -sh

To show all the 1 level sub directories size (which you are not interested at sub sub directories.)

du -sh *

To show the size of specific directory

du -sh /home

To show the size of all sub directories of a specific directory

du -sh /home/*

View Comments »

Find and replace recursively over several files:
perl -pi.bak -e "s/Bob/Steve/gi" *.html

The '.bak' will create copies of your original files with the .bak extension added incase of mistakes. Be careful of running this twice though as the backups will get overwritten.

View Comments »

Resolve all your conflicted files. Use with caution!!

svn st|awk '/^C/{ print $2; }'|xargs svn resolved

View Comments »

The following command creates in the /usr/project directory, a copy of the current working directory structure:

find . -type d -print|sed 's@^\.\{0,1\}@/usr/project@' | sed 's/ /\\ /' | xargs mkdir -p

View Comments »

To create a set of backed up files with the current date added at the end of the file name try the following:

ls *txt | sed "s/.*/cp & &.$(date "+%Y%d%m")/"

This will run the following commands:

cp 1.txt 1.txt.20082703

cp 2.txt 2.txt.20082703
cp 3.txt 3.txt.20082703

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit