All Tips


15

Tip #140   Change extension on a group of files using only bash builtins an

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

Read more »

14

Tip #141   Process files whose names may contain spaces

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 Read more »
  • TAGS:

3

Tip #145   swap files

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

14

Tip #147   Find and Grep

Find all files with given name (you can use Bash expansion if you'd like), and Grep for a phrase:
Read more »

12

Tip #149   !$ shortcut

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

Read more »
  • TAGS:

11

Tip #150   Up/down arrow keys for searching history

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
Read more »

15

Tip #151  

How to mount an ISO image file onto a directory:

Read more »
  • TAGS:

9

Tip #152   Finding Newer Files [OR How To Create A Patch File]

You can use find with the '-newer' flag in conjunction with tar to create a patch file:
Read more »

8

Tip #153   In-place multiple-file search and replace

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. Read more »
  • TAGS: