Saturday, December 11, 2010

Command line simple examples

I like to use the command line a lot, but for many tools, it is hard to find the most basic, common uses. Here is a list of examples I have found to get started.


awk

# print the owner, update time, and filename of all postscript files in a directory
ls -l | awk '$8 ~ /.*\.ps/ {print $3 " " $7 " " $8}'

find

# find all files beginning with "bob" in the current directory and its subdirectories
find . -name "bob*"
# don't show the files in the .svn directories
find . -name "bob*" | grep -v ".svn"

grep

# find an email address in all the files in a directory
grep "bob@example.com" *

rsync

# push a bunch of files to a remote server
rsync -avz --no-t --no-p --no-g --exclude=.svn/ --exclude=*~ --bwlimit=50 /home/bob/localdir/ bob@example.com:/home/bob/remotedir
# pull a bunch of files from a remote server
rsync -avz --no-t --no-p --no-g --exclude=.svn/ --exclude=*~ --bwlimit=50 bob@example.com:/home/bob/remotedir/ /home/bob/localdir

sed

# change all email addresses in a file and make a backup myfile.txt.bu
sed -i.bu -e 's/bob@example\.com/robert@example\.com/g' myfile.txt

tar

# tarball up a directory
tar -cvf tarball.tar *
# list the files in a directory
tar -lvf tarball.tar
# untar a tarball
tar -xvf tarball.tar

zip

# zip all the files in the current directory and its subdirectories
find . -type f | zip files.zip -@

No comments: