Use sed to add a newline to the end of a file (this will perform an in-place replacement and create a backup file, MYFILE.bak):
$ sed -i.bak '$G' MYFILE
Some programs, such as Emacs, will omit the trailing newline from a file unless configured to add one. Emacs can be configured to add a newline automatically.
backupcommandsconfigurationeditorsemacsin-placeline-endingsnewlinesedshell
To create a Postgres user sam and a database samdb that is owned by the sam user:
$ su - postgres $ createuser -P -A -D sam $ createdb -O sam samdb
(Change the -A, and -D options to affect the new user's abilities.)
Edit the user's password with:
template1=# alter user username_here with password 'password_here';
accountcommandscreatedbcreateuserpermissionspostgresqluser
To add a service to boot automatically on startup, do the following:
# chkconfig --level 345 <service_name> on
bootchkconfigcommandsconfigurationservicestartup
BSD is a little paranoid about where your passwords come from, so they'll insist on getting it from a stream. Here's an example:
(edit the file '/tmp/pass' and deposit the password there) % su root -c sh Password: # pw useradd -n test -c "Test User" -m -h 3 3< /tmp/pass # grep test /etc/master.passwd test:$1$T2tu0BET$UGPrNB1FavzjlzhTwUWRN.:1002:1002::0:0:Test User:/home/test:/bin/sh # exit % su test Password: [typed "foobar" here...] $ exit
bsdcommandsfreebsdneatparanoidpasswordspwsecurity
Add line numbers to your source code with expand and some quick perl:
expand /etc/motd | perl -pe 's/^/\t=$.=\t/'
commandsexpandlanguagesperlprogramming
Instead of running:
$ ls -lh `which zsh`
use this:
$ ls -l =zsh
commandswhichzsh
To have zsh automatically cd to a directory without requiring you to type cd, set this option:
setopt auto_cd
So, if you have a directory foo in the current directory (and no command foo exists in your path), typing simply foo will be equivalent to cd foo.
auto_cdcommandszsh
If you need a process to survive your logging out of the system (or being knocked off of it), you can either screen it or wrap it with nohup. If you nohup, you'll lose control of it entirely, but the output will be written to nohup.out. With screen you can re-attach and take control later:
# nohup program -opts args & -or- # screen (press 'ctrl+a d' to detach after you've run the proc)
commandsloginlogoutnohupprocessscreenshell
Use these screenrc commands to bind specific key sequences to commands outside of the escape sequence (normally C-a). You can bind key combos to commands this way so you don't always have to prefix commands with your escape key sequence:
# Bind Control-PageDown to 'next', Control-PageUp to 'prev' # to navigate between windows bindkey ^[[5;5~ prev bindkey ^[[6;5~ next # Bind arrow keys Control-Down to 'next', Control-Up to 'prev' # to navigate between windows bindkey ^[[1;5A prev bindkey ^[[1;5B next
(The keycodes for these keys can be obtained by running cat > /dev/null and pressing the desired key combination.)
bindingbindkeycommandsconfigurationescapekeystrokesscreenscreenrc
%@ is the specifier for timestamps in output formatting:
/fset send_public %@%P<%n$2%P>%n $3- /fset send_action %@%K* %W$1 %n$3-
bitchxcommandsconfigurationirctimestamps
You can use awk to change the field separator in a data stream:
$ cat foo
a,b,c,d,e,f
a,b,c,d,e,f
$ awk 'BEGIN { FS = ","; OFS = ".."; } { $1 = $1; print }' foo
a..b..c..d..e..f
a..b..c..d..e..f
(You just have to touch one of the fields to get it to process the line.)
awkcommandsfieldsparsingshell
Ever been on a machine that was ailing and just wouldn't respond? As soon as you're root, lower the priority of the offending process ID(s) (in this example, 1103) by using the 'renice' command:
# renice -19 1103
commandsconfigurationcontroldebuggingmonitoringpriorityprocessrecoveryrenicerescuesecurityshell
Use the pycolor tool included with ipython. It will format the source using ASCII codes so it looks pretty in your terminal:
$ pycolor foo.py
asciicommandsipythonlanguagesprogrammingpycolorpythonterminal
These commands are used to deal with tagging and branching in SVN:
$ cd /path/to/project && svn copy trunk branches/mybranch-N && svn commit -m 'Created new branch' $ cd /path/to/project/branches/mybranch-N
Make changes and commit:
$ cd /path/to/project/trunk $ svn merge -r R:head svn://path/to/repo/branches/mybranch-N
(In the merge command, 'R' is the revision in which the branch was created. Note that if the trunk is merged into the branch, then merging the branch back into the trunk again will cause conflicts if selective merging is not used. Use the svn merge -r start:stop syntax for selective merging.)
branchingcommandscommitmergesubversionsvntagging
One of the most maddening things I've had to do is compile on Solaris without the warm/fuzzy GNU'ness. Remember these things:
1. add gcc 2. add bison 3. add /usr/ccs/bin & /usr/local/bin to path
commandsgnupackagessolaris
If you want to have a context diff instead of the default that subversion offers, you can give it the binary and options to use on the commandline:
$ svn diff --diff-cmd /usr/bin/diff -x '-crN'
commandscontextdiffsubversionsvn
There are a few different ways to convert a DOS file to Unix format, but the most available is probably this:
$ col -bx < dosfile > unixfile
colcommandsconvertdosfilterline-endingsnewlineshellunix
You can use the convert program (included with imagemagick) to convert a LaTeX file to an image:
$ convert -density 144 -geometry 100% source.dvi dest.jpeg
commandsconvertdviimageimagemagicklatextextypesetting
Convert a man page to HTML for easy viewing online:
$ gunzip < /usr/local/man/man8/lsof.8.gz | nroff -man | man2html -title "lsof" > otmp/lsof.html
commandsconvertfiltergunziphtmlman2htmlmanpagenroffonlineshell
Here is a way to copy an entire filesystem without descending down its subsumed mount points. This example uses the root filesystem:
$ find / -xdev | cpio -pm /desired/location
commandscopycpiofilesystemfindrootshell
If you've run a command that you discover needs a path, you can do something like this on the following line:
$ psql -U postgres mydb_here bash: psql: command not found $ /usr/local/pgsql/bin/!! /usr/local/pgsql/bin/psql -U postgres mydb_here Welcome to psql 7.3.5, the PostgreSQL interactive terminal. mydb_here=#
bashcommandsneat
After you've created two or more linux raid autodetect partitions, you can create a RAID device that uses them by running the mdadm command:
# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/hda2 /dev/hdb1 # mke2fs -j /dev/md0
commandsdebianfilesystemmdadmmke2fspartitionsraid
You can easily create an .ico file (e.g. a favicon.ico file for your website) with the ImageMagick convert program:
$ convert myicon.png favicon.ico
commandsconvertformaticoimageimagemagickwindows
If you need to see what queries are coming through your nameserver, toggle the querylog flag by typing:
$ rndc querylog
bindbind9commandsdnsnameserverquerylogrndcshell
If you want to view your PAT for debugging:
pixfirewall# show xlate debug
ciscocommandsdebuggingfirewallnatpatpix
Tired of typing your SSL password on boot of your webserver? You can decrypt it if you're certain it's safe:
# openssl rsa -in server.key -out server.key.unsecure
apachebootcommandsencryptionkeyopensslsecurityshellsslwebserver
Make a recursive copy of your source tree before any changes (pristine version). E.g., copy project/ to project-pristine/.
After the changes, run this command:
$ diff -crN old new > output.diff - or - $ diff -crN project-pristine project > name_of_change.diff
On the target system in /path/to/project/../:
$ patch -p0 --dry-run < ./name_of_change.diff
Remove --dry-run to make it take effect.
commandsdiffpatchprogrammingshell
For example, to display the 20 largest files owned by joe:
find / -printf "%k\t%p\n" -user joe | sort -n | tail -20
commandsfilesfindpermissionsprintfsorttailusers
If instead of a blank space you'd prefer something that tells you a particular column in a row is NULL, you can set it:
\pset null '(null)'
commandsconfigurationpostgresqlpsql
If you want to run a query or a function and ditch the output, you can do something like this:
xinu=# select my_void_function() \g /dev/null
commandsconfigurationpostgresqlpsql
To create a .ico file that can be used for the favicon.ico on webpages, you can use a nice little program called png2ico. There is no package for this in Debian or Ubuntu so you will need to compile it yourself. Compilation is very straightforward. You do need a libpng package. Most of the time it will be installed but if you see an error, just searching for that file will yield the package you need to get for your distribution.
The session below uses /usr/local/src for the compilation and installed to /usr/local/bin. It can be compiled anywhere and installed anywhere, but having it in your $PATH makes things easier:
$ cd /usr/local/src $ wget http://www.winterdrache.de/freeware/png2ico/data/png2ico-src-2002-12-08.tar.gz $ tar xvzf png2ico-src-2002-12-08.tar.gz $ cd png2ico $ make $ sudo cp png2ico /usr/local/bin/ $ sudo cp doc/png2ico.1 /usr/local/man/man1/
Once compiled, upload your png images. They should be square; sizes 16x16 and 32x32 are suggested. To create the icon you would then call the following:
$ png2ico favicon.ico your_favicon16x16.png your_favicon32x32.png
Afterwards you will then have a favicon.ico file. You then place this in the webroot of your website add the following to <head></head> section of your webpages:
<link rel="shortcut icon" href="/favicon.ico" />
It's good form to include it, but most common browsers automatically look for an icon at that location.
http://www.winterdrache.de/freeware/png2ico/ - includes a windows gui
commandsconvertfaviconiconimagepng2icoshellweb
If you want to see how a query like \du is being built:
\set ECHO_HIDDEN
analysiscommandsconfigurationpostgresqlpsqlsql
You need to get someone into an internal machine that doesn't have a public IP? Use an SSH tunnel. For this example, machine_a is your internal machine and machine_b is external:
$ ssh -R 9000:localhost:22 you@machine_b
Once you've logged in, you should be able to run this on machine_b:
$ ssh -p 9000 you@localhost
commandsnetworkshellsshtunnel
If you want to extract the contents of an RPM to the current directory without installing it, run the following:
rpm2cpio foo.rpm | cpio -idv
commandscpioextractinstallationrpmrpm2cpio
If you want to tail the errors on another terminal, just push them to a fifo:
$ mkfifo pgerror $ psql -U user dbname < ./dbfile 2> pgerror
On your other terminal:
$ tail -f pgerror
Voila! STDOUT on the main terminal, and STDERR on the secondary.
commandserrorsmkfifopipepsqlredirectshelltailterminal
To zip up the contents of a directory (selectively), use find and zip:
$ find . -type f -name "*.jpg" | zip -@ myimages.zip
commandsdirectoryfindshellzip
The apt-file program allows you to search the contents of all debian packages (installed and not installed) for files that you may need. For example, if you're compiling a program and need a header file but don't know which package provides the file, you can use apt-file to find out which package you should install to fix the problem:
# apt-get install apt-file # apt-file update # apt-file search foo.h
Be sure to run apt-file update periodically to update the file listing cache just as you also run apt-get update. The last command above will list any packages whose files match the specified search string, as well as the files that matched:
$ apt-file search bin/lsof lsof: usr/bin/lsof lsof: usr/sbin/lsof
aptapt-fileapt-getcommandsdebianlsofneatutilities
Finger the kernel.org finger server to get current kernel versions:
$ finger @finger.kernel.org [zeus-pub.kernel.org] Trying 204.152.191.5... The latest stable version of the Linux kernel is: 2.6.13.1 The latest snapshot for the stable Linux kernel tree is: 2.6.13-git9 The latest 2.4 version of the Linux kernel is: 2.4.31 The latest prepatch for the 2.4 Linux kernel tree is: 2.4.32-pre3 The latest 2.2 version of the Linux kernel is: 2.2.26 The latest prepatch for the 2.2 Linux kernel tree is: 2.2.27-rc2 The latest 2.0 version of the Linux kernel is: 2.0.40 The latest -ac patch to the stable Linux kernels is: 2.6.11-ac7 The latest -mm patch to the stable Linux kernels is: 2.6.13-mm2
commandsfingerkernelnetwork
If you've botched an SVN log entry (typo, etc.), you can fix it as follows:
$ echo "Here is the new, correct log message" > newlog.txt $ svnadmin setlog myrepos newlog.txt -r 388
brokencommandslogsubversionsvn
To force it to run through the queue (again), you can run:
# ${PATH_TO_POSTFIX}/postfix flush
commandsconfigurationmailmtapostfixqueueserver
Type perldoc -f <function_name> for syntax help. Also, perldoc -q <regexp> will search question headings in the perlfaq[1-9] man pages.
commandsdocslanguagesmanpagesperlperldocprogramming
Use these commands to generate a self-signed SSL certificate (e.g. for Apache):
# openssl genrsa 1024 > server.key # openssl req -new -key server.key -x509 -days 90 -out server.crt
apachecertificatescommandskeysopensslsecurityssl
Use the apg utility to generate strong mnemonic passwords:
$ apg -st Please enter some random data (only first 8 are significant) (eg. your old password):> Coydgoceuk6 (Coyd-goc-euk-SIX) Caculpyep7 (Cac-ulp-yep-SEVEN) otevDet6 (ot-ev-Det-SIX) Jiwacwarj6 (Ji-wac-warj-SIX) gurkOnRyet1 (gurk-On-Ryet-ONE) EbTarIv0 (Eb-Tar-Iv-ZERO)
apgcommandsgeneratemnemonicneatpasswordsecurityutilities
You can have grep show only the filenames that matched (instead of showing all lines in those files that matched) by using the -l switch:
$ grep -l SELECT *.sql file1.sql file2.sql
grep will only list unique filenames (i.e. a file with two matches will only be listed once).
commandsgrepshellunique
If you need to know where a line ends, where unprintable characters lurk, and the difference between white space and tabs, you can always remember to take the cat to the vet:
$ <commands> | cat -vet
catcharacterscommandscrlfshell
Given a directory DIR and a glob of files (e.g. *.php):
$ svn propset svn:ignore \*.php DIR
You can also remove a property:
$ svn propdel svn:ignore DIR
The svn propset format only allows you to set one pattern at a time. To add multiple patterns, run:
$ svn propedit svn:ignore DIR
which opens a file in an editor where you can add ignore patterns, one per line.
commandsignorepropdelpropeditpropertypropsetsubversionsvn
To create a new project with its own repository, do this. The svnadmin command assumes the path exists already:
$ svnadmin create /path/to/repository $ svn import project_name file:///path/to/repository -m 'initial checkin'
Then remove the original copy and check it out:
$ mv project_name project_name.old $ svn checkout file:///path/to/repository/trunk project_name
Note: You can change the file:// to svn:// or whatever protocol you're using.
commandsimportprojectsubversionsvnsvnadmin
This command will replace occurrences of foo with bar in the file X in-place and create a backup of X in X.old:
$ sed -i.old "s/foo/bar/g" filename
backupcommandsin-placesedshell
To use apt-get to install a specific (perhaps older) version of a package, follow the package name with an equals sign and the exact version number. For example:
# apt-get install bash=3.0-17
apt-getcommandsdebianneatpackagetroubleshootingversion
If you need to fool a machine into believing that a host:port pair is local, you can use ipchains to redirect traffic. For example, the desired destination is www.example.com:80 and you want it to go to localhost:8080:
# echo '1' > /proc/sys/net/ipv4/ip_forward # ipchains -A input -j REDIRECT 8080 -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 80
Note: No one really uses ipchains anymore, but it can be found on older systems.
commandsfirewallip_forwardipchainsloopredirectshell
FreeBSD's ldconfig binary doesn't work the same as the one in Linux derivatives. If you happen to type ldconfig -v, for instance, you're going to lose all your configuration information. To fix the problem:
# ldconfig -m /usr/local/lib/compat/pkg # ldconfig -m /usr/libexec # ldconfig -m /usr/X11R6/lib # ldconfig -m /usr/local/lib # ldconfig -m /usr/lib
bsdcommandsfreebsdldconfiglibraries
Update /etc/sysctl.conf and add (or alter):
kernel.shmmax = <max bytes>
and run sysctl -p to update the system settings.
commandsconfigurationshared-memorysysctlsysctl.confsystem
Since lsof is tragically broken in Solaris 10, you have to try other methods of finding out which ports a given PID has opened:
# pfiles 2602 &> output
That will redirect stdout and stderr to a file (output) that will outline any files (and ports; remember, everything is a file) that the process has open.
commandslsofpfilesshellsolaris
If you get strange locale errors when running perl and GTK apps, run these commands and be sure to generate all en_US locales:
$ apt-get install localeconf $ dpkg-reconfigure locales
apt-getcommandsdebiandpkggtklocalelocaleconfperl
Use slocate to build a database of files on your machine and use locate to find them:
# slocate -u # Rebuilds file database by scanning all filesystems # locate foobar # finds all files with 'foobar' somewhere in the path
commandsdiagnosticfindslocateutilities
If you need to move files, logs, or any kind of program output off a compromised system without disrupting evidentiary data on the disk, use netcat.
On your trusted system:
$ nc -v -l -p 2222 > victim.dump
On the victim's system:
$ <program> | nc <trusted_system> 2222
Where program is the discovery application you're running (e.g. netstat -an).
cleanupcommandsforensicsncnetcatparanoidsecuritytroubleshooting
To create a new database:
$ mysqladmin create <database_name>
To grant permissions to a user, run this:
$ mysql -u root -p Password: (enter password) mysql> GRANT ALL ON db_name.* TO username@localhost IDENTIFIED BY "userpasswd";
To flush the privilege tables, run this:
$ mysqladmin flush-privileges
or:
mysql> flush privileges;
To revoke the privileges from a particular user/host pair:
mysql> revoke all privileges, grant option from username;
commandsgotchamysqlmysqladminpermissionsrevokesql
In your ~/.psqlrc, add this to emulate MySQL-style borders in query results:
\pset border 2
commandsconfigurationmysqloutputpostgresqlpsqlpsqlrc
To log into MySQL using the CLI in windows:
(in the mysql\bin directory) c:\mysql\bin> mysql --user=<user> --pass=<pass> --port=3306
commandlinecommandsmicrosoftmysqlshellwindows
You can use the lsof (LiSt Open Files) utility to view information about which processes own file handles on a system. Since sockets map to file descriptors, lsof will show you which processes own socket connections. If you see that your machine is connected to another on TCP port 6234 (source or dest) and you want to find out which process(es) are responsible for the connection, run:
# lsof -ni tcp:6234
Note that when run as an unprivileged user, lsof will only show you file descriptors that you have permission to see. You must run lsof as root to see everything in the kernel.
commandsconnectionsdebuggingdescriptorsfilesystemlsofmonitoringnetworkpermissionsprocesssocketsutilities
If you want the time, and only the time:
$ date | (read u v w x y z; echo $x) 14:05:52
commandsdateextrasparsingshell
If you just need the client and not the binary, you can modify the behavior of the port installation like so:
# cd /usr/ports/postgresql7 # make install clean WITHOUT_SERVER=yes
To see the other tasty build options:
# make build
bsdcommandsfreebsdmakepostgresql
Use this command to purge old configs and files from your system that are in uninstalled packages:
$ dpkg --get-selections | awk '/deinstall/ {print $1}' | xargs dpkg --purge
awkcommandsconfigurationdebiandpkgpackagespurgexargs
Add the following to your ~/.inputrc:
"\e[5~": history-search-backward "\e[6~": history-search-forward
And of course, in order to use your ~/.inputrc you need to set your INPUTRC environment variable in ~/.bash_profile:
export INPUTRC=$HOME/.inputrc
The next time you login you will be able to run "gvim <pageup>" and the last entry that started with "gvim " will appear, <pageup> again will bring the next one up, and so on.
Note that escape codes for PageUp and PageDown vary depending on your terminal type; check out this tip for a technique on how to find out what your terminal expects.
bashcommandshistoryinputrcshell
If you have a URL that you need to crawl and you know the range of numbers in the image, you can do something like this:
$ curl -O http://www.example.com/img/samples[00-99].jpg
That should (at least attempt to) fetch the images samples00.jpg through samples99.jpg. Enjoy!
If you're using more than one range, you'll want to build your filename or a path with the --create-dirs option. For example:
$ curl http://www.example.com/imgs[00-99]/samples[00-27].jpg --create-dirs -o "#1/#2.jpg"
Alternatively, you can just be ghetto and name the files like dirname_filename.jpg:
$ curl http://www.example.com/images[00-99]/samples[00-27].jpg -o "#1_#2.jpg"
commandscurlextrasimageshellurl
Download an entire directory tree:
$ wget -r ftp://username:password@site/path/to/suck
commandsdirectorydownloadftpshelltreewget
If you have a services script in /etc/rc.d, you can add the service to the various run levels to create the appropriate start/kill symlinks:
# chkconfig --level 345 imap on # service xinetd restart
bootchkconfigcommandsconfigurationfedoraredhatservicesstartupsymlinks
Clever kill/cat/sed line to refresh the logs for Apache stolen from some documentation somewhere:
# kill -USR1 $(cat $(httpd -V | sed -n '/DEFAULT_PIDLOG/s/.*"\(.*\)"/\1/p'))
apachecommandskillloggingsed
If your BGP neighbour is a Cisco unit (and possibly others), it is possible to reprocess all learned routes and new announcements without clearing and retransmitting the full contents of the BGP table. This is known as a "soft reconfiguration," and is supported as of IOS 12.0. This avoids fast route cache invalidation, network service disruption, and other results associated with wiping the BGP table that would normally be undesirable, although it is more memory-intensive:
Router# clear ip bgp <neighbor IP, AS number, or *> in
To trigger a release of updates toward a neighbor in a similar fashion:
Router# clear ip bgp <neighbor IP, AS number, or *> out
bgpciscocommandsconfigurationiospeerrefilterrouterouter
If you have packages on your system upon which no others depend, you easily can find out what they are by running the deborphan program. This command will uninstall them:
$ deborphan | xargs apt-get remove
(The --yes switch can be used with apt-get to continue with the removal instead of asking for confirmation.)
apt-getcleanupcommandsdebiandeborphanutilities
Reset the root password:
1. Insert the Solaris install CD. 2. Issue STOP-A (Ctrl-Break). 3. Type: boot cdrom -s 4. fsck /dev/rdsk/c0t0d0s0 5. mnt /dev/dsk/c0t0d0s0 /a 6. cd /a/etc 7. TERM="vt100"; export TERM 8. vi shadow 9. on root line, delete everything in the second ":" delimited field. 10. exit out of file (ESC :wq! -or ESC ZZ)
bootcommandsconfigurationhackpasswordresetrootsolarisstartup
If you find yourself on a machine without the semi-ubiquitous ImageMagick packages, you might at least have pnmscale. Starting with a PNG file, you can do the following to resize it to 450 pixels wide:
$ pngtopnm < ./firefox-upgrade.png | pnmscale -x 450 | cjpeg -smoo 100 -qual 100 > firefox-upgrade.jpg
cjpegcommandsconvertfilterimageimagemagickpngpngtopnmpnmscaleresizescaleshell
You can use the -l <kilobits_per_sec> option with scp (NOT ssh or sftp) to restrict the bandwidth used to transfer files:
$ scp -l 200 user@host:~/files .
bandwidthcommandsnetworkrate-limitingscpsftpshellssh
Running this ed command will reverse the lines in the current buffer. This would be useful for logfiles and the like:
:g/^/m0
buffercommandsededitorsvim
You can use the tee program to save the contents of a pipe to a file while also viewing it on standard out:
# tail -0f /var/log/httpd/error_log | tee ~/newest_errors.txt
Note: tail -0 instructs tail to begin at the very end of the file (the default is to show the last ten lines), and -f means tail will periodically check the file for additional data and print the data to standard out.
commandsdebuggingmonitoringpipeshellstdouttailteeutilities
If you're running scp -r, beware of symlinks; they are followed rather than preserved. This might be favorable behavior if you're referencing files outside what you're copying, but if you're copying symlinks which reference other parts of what you're copying, the referenced files will be duplicated on the destination host.
Take, for example, the following files on host A:
~/myfiles/
foo/
a.txt
bar/ -> foo/
When you run this command on host B:
joe@B:~$ scp -r joe@A:~/myfiles .
The result on host B will be:
~/myfiles/
foo/
a.txt
bar/
a.txt
commandsgotcharecursivescpshellsymlinks
If you have a system that might be changing versions multiple times you might orphan some of the lateral utilities (like newaliases). If you want to be certain that you're rehashing the correct aliases file (the one that sendmail.cf is referencing), run sendmail with the -bi option like this:
# /path/to/sendmail -bi
aliasescommandsmailmtanewaliasessendmailsendmail.cfserver
Set the PROMPT1 variable using psql prompt escape sequences:
\set PROMPT1 '[%n@%M:%/]=%# '
commandsconfigurationescapepostgresqlpsqlvariables
Given an executable /path/to/EXEC:
$ ldd /path/to/EXEC
commandsdependenciesdiagnosticexecutablelddlibrariesutilities
A quick way to see all of the databases on the system and (optionally) the number of tables in each:
$ mysqlshow
Use the -v flag to include number of tables.
commandsdatabasesmysqlmysqlshow
You can use this command to show duplicate lines in a file:
$ uniq -d MYFILE
commandsduplicatesfiltershellsortuniq
To determine what version of Solaris you're running, type showrev.
commandssolarissunosversion
You can specify the search path(s) used by ld at runtime to find shared objects by building your program with the -rpath option:
$ gcc -Xlinker -rpath -Xlinker /path/to/my/libraries filename.c
This is the equivalent of:
$ ld -rpath /path/to/my/libraries filename.o
ccommandscompilationgcclanguagesldlinkingprogramming
If you find yourself having to log into various systems with long strange names and usernames that aren't your own, check into ~/.ssh/config. Adding a Host block for each system you need to log into will give you a short name to use as well as let you set the user (and various other things - check out man ssh_config):
Host cs HostName cs.really.long.name.example.com User mrxinu
At this point you can type ssh cs and log right in.
commandsconfigurationsshssh_config
You have a mailbox you need to sort. You can't have it come back through your MTA, of course. Use this script to push it through the procmail filter of your choice:
#!/bin/sh ORGMAIL=/var/spool/mail/$LOGNAME if cd $HOME && test -s $ORGMAIL && lockfile -r0 -l3600 .newmail.lock 2>/dev/null then trap "rm -f .newmail.lock" 1 2 3 15 umask 077 lockfile -l3600 -ml cat $ORGMAIL >>.newmail && cat /dev/null >$ORGMAIL lockfile -mu formail -s procmail <.newmail && rm -f .newmail rm -f .newmail.lock fi exit 0
commandsmailboxmtaprocmailscriptsshellsort
Set this variable in your ~/.psqlrc to stop on error when psql is used to run non-interactive scripts (e.g. cat file | psql ...):
\set ON_ERROR_STOP 1
Or use it from the command line:
$ psql ... -v ON_ERROR_STOP=1 ...
commandsconfigurationdebugginginteractivepipepostgresqlpsqlpsqlrc
If you want to list the symbols of your object code, you can use the nm command:
$ nm object_file.o
ccommandslanguagesnmprogramming
If you would like to have syntax highlighting on a file that would otherwise not have any highlighting at all, you can symlink the file to the appropriate extension and open it:
$ ln -s example.lxp example.html $ vi example.html # opened with pretty highlighting
*Note: This should be used sparingly and you should clean up your symlinks.
commandseditorshighlightingshellsymlinksyntaxvi
A nice way to take the pulse of a BSD machine is to run systat -vm. It updates often and includes quite a bit of useful information.
bsdcommandsfreebsdmonitoringsystat
Tagging is useful for taking snapshots of releases. For example, in /path/to/project:
$ svn copy trunk tags/release-1.0 $ svn commit -m 'Created release 1.0 tag.'
branchescommandscommitcopysnapshotssubversionsvntagging
To transfer files via tar/ssh, do the following:
$ tar cvjf - * | ssh user@remote "(cd /desired/path; tar xjf -)"
commandspipeshellsshtartunnel
A way to test whether your domain login credentials are working is via net use on the commandline like so:
c:\> net use \\<computername>\IPC$ /user:<domain>/<username> *
The * forces it to ask for your password. Otherwise, you can replace * with your password to do it all at once. Also, net use supports smartcard authentication, etc.
Thanks to Scott Morrison (a.k.a. "Plaid") for the tip!
authenticationcommandsnetsmartcardwindows
Netcat is handy little utility for scripting all manners of network functionality. Here we're making sure a web server is responding as we'd expect:
$ (echo "GET / HTTP/1.1"; echo "Host: www.xinu.org"; echo) | nc www.xinu.org 80
commandsdebuggingmonitoringnetcatnetworkshellutilities
Make sure you always specify a path free of symlinks. This can be pretty tough, though. An alternative approach is to use namei to track down symlinks:
# namei /usr/X11/bin/xterm f: /usr/X11/bin/xterm d / d usr l X11 -> X11R6 d X11R6 d bin - xterm
commandsdirectorynameipathshellsymlinkstree
If you need to upload an entire directory structure, check out wput on sourceforge.net. It works the same way as wget only in the other direction (i.e., supporting various protocols).
Thanks to Aronalle for this tip!
commandsdownloadnetworkshellsourceforgeuploadwgetwput
display the route from your server to any email address:
exim -bt email@domain.com
send an email using exim:
exim -v email@domain.com
show the number of emails in the queue:
exim -bpc
display the mail queue:
exim -bp
flush queue:
exim -qff &
view a particular mail in the queue:
exim -Mvh msgid (for headers) exim -Mvb msgid (for body)
commandseximmailmta
Sometimes you need to iterate over variable names and access their values. In shell script, you would do something like this to get the values of FOO and BAR:
$ FOO=apple
$ BAR=orange
$ VARS="FOO BAR"
$ for v in $VARS ; do echo ${!v} ; done
The above works in bash. Use this in Zsh:
$ for v in $VARS ; do echo ${(P)v} ; done
(The 'P' flag on ${v} causes a further variable lookup before ${v} is evaluated.)
Thanks to Cliff for the tip!
bashcommandsenvironmentevaluationiterationloopshellzsh
If you want to use tcpdump to watch initiating connections (that is, the syn flag only is set indicating we're looking at the first third of the three-way handshake) on ports 80 and 443 you could do something like this:
# tcpdump '(tcp[13] & 0x3f = 2) and (dst port 80 or dst port 443)'
commandsconnectionsmonitoringnetworksecurityshelltcpdump
If you have a bunch of files in your home directory and you want to push them into ~/sort, create the directory and then do the following:
$ find . -type f -maxdepth 1 ! -name ".?*" | xargs -I '{}' mv '{}' sort
Note: GNU xargs uses -i. BSD versions use -I.
bsdcommandsdirectoryfindgnushellsortxargs
Let's say you want to check the uptime on a list of servers. We're assuming that you've got a key on each machine otherwise you'll be entering your password often:
$ xargs -i ssh {} uptime < ./server.list
5:46am up 155 days, 17:49, 2 users, load average: 0.12, 0.03, 0.01
5:46am up 147 days, 17:14, 2 users, load average: 0.02, 0.05, 0.01
5:46am up 209 days, 17:26, 0 users, load average: 0.00, 0.00, 0.00
5:46am up 89 days, 6:30, 0 users, load average: 0.00, 0.00, 0.00
5:46am up 82 days, 6:40, 0 users, load average: 0.07, 0.06, 0.01
5:46am up 104 days, 9:51, 0 users, load average: 0.03, 0.03, 0.00
5:50am up 68 days, 9:17, 0 users, load average: 0.00, 0.00, 0.00
5:48am up 68 days, 9:15, 0 users, load average: 0.00, 0.00, 0.00
commandskeyloopremoteserversshellsshuptimexargs