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 »
- TAGS:
- bash
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:
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 »
- TAGS:
- bash
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 »
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 »
- TAGS:
- history
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 »
- TAGS:
- tar
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:
alias aptitude at awk bash bc cal cat cd colrm comm cp csh curl cut date dd df dialog diff dirname dpkg du fc find fuser grep gs gzip history iconv kill ksh last less ln ls lsof lynx m4 md5sum mkdir mkfifo mkisofs mv mysql nc netstat openssl OSX perl ping popd ps pushd python read redirection rm scp screen sed sort ssh stat sudo svn tail tar tee test top tr uniq vim wc wget xargs