20 Command line shortcut tips in Linux
Command line shortcuts in Linux for command edit options and other activities.
- Ctrl + a => Return to the start of the command you’re typing
- Ctrl + e => Go to the end of the command you’re typing
- Ctrl + u => Cut everything before the cursor to a special clipboard
- Ctrl + k => Cut everything after the cursor to a special clipboard
- Ctrl + y => Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to
- Ctrl + t => Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
- Ctrl + w => Delete the word / argument left of the cursor
- Ctrl + l => Clear the screen
- tty => Typing the tty command tells you what virtual terminal you’re currently working in.
- Shift + PageUp => Scroll terminal output up.
- Shift + PageDown => Scroll terminal output down.
- Ctrl + l => Does exactly the same as typing the clear command.
- Reset => If you mess up your terminal, use the reset command. For example, if you try to cat a binary file, the terminal starts showing weird characters. Note that you may not be able to see the command when you’re typing it.
- Ctrl + r => Find the last command that contained the letters you’re typing. For example, if you want to find out the last action you did to a file called "file42.txt", you’ll press Ctrl + r and start typing the file name. Or, if you want to find out the last parameters you gave to the "cp" command, you’ll press Ctrl + r and type in "cp".
- Ctrl + c => Kill the current process.
- Ctrl + z => Send the current process to background. This is useful if you have a program running, and you need the terminal for awhile but don’t want to exit the program completely. Then just send it to background with Ctrl+z, do whatever you want, and type the command fg to get the process back.
- Ctrl + d => Log out from the current terminal. If you use this in a terminal emulator under X, this usually shuts down the terminal emulator after logging you out.
- Ctrl + Alt + Del => Reboot the system. You can change this behavior by editing /etc/inittab if you want the system to shut down instead of rebooting.
- Ctrl + Alt + Backspace => Reboot the Graphics.
PostgreSQL Error and Slow Query Log
Set the following variable in postgresql.conf to log all queries that take longer than 100 milliseconds:
logging_collector = on # This parameter allows messages sent to stderr, and CSV-format log output, to be captured and redirected into log files.
log_directory = (string) # When logging_collector is enabled, this parameter determines the directory in which log files will be created.
log_filename = (string) # When logging_collector is enabled, this parameter sets the file names of the created log files.
log_rotation_size = 10M # this parameter determines the maximum size of an individual log file.
log_min_duration_statement = 100 # Causes the duration of each completed statement to be logged if the statement ran for at least the specified number of milliseconds. Setting this to zero prints all statement durations.
log_min_error_statement = error # Controls whether or not the SQL statement that causes an error condition will be recorded in the server log.
They will show up in the main postgresql.log file
Example Settings (on PostgreSQL 8.2):
redirect_stderr = on # Enable capturing of stderr into log
log_directory = ‘pg_log’ # Directory where log files are written
log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log’ # Log file name pattern.
log_rotation_size = 2MB # Automatic rotation of logfiles will
log_min_error_statement = warning # Values in order of increasing severity:
log_min_duration_statement = 5000 # -1 is disabled, 0 logs all statements
PostgreSQL general memory tuning parameters
There are several postmaster options that can be set that drastically affect performance, below is a list of the most commonly used and how they effect performance:
- max_connections = <num> — This option sets the maximum number of database backend to have at any one time. Use this feature to ensure that you do not launch so many backends that you begin swapping to disk and kill the performance of all the children. Depending on your application it may be better to deny the connection entirely rather than degrade the performance of all of the other children.
- shared_buffers = <num> — Editing this option is the simplest way to improve the performance of your database server. The default is pretty low for most modern hardware. General wisdom says that this should be set to roughly 25% of available RAM on the system. Like most of the options I will outline here you will simply need to try them at different levels (both up and down ) and see how well it works on your particular system. Most people find that setting it larger than a third starts to degrade performance.
- effective_cache_size = <num> — This value tells PostgreSQL’s optimizer how much memory PostgreSQL has available for caching data and helps in determing whether or not it use an index or not. The larger the value increases the likely hood of using an index. This should be set to the amount of memory allocated to shared_buffers plus the amount of OS cache available. Often this is more than 50% of the total system memory.
- work_mem = <num> — This option is used to control the amount of memory using in sort operations and hash tables. While you may need to increase the amount of memory if you do a ton of sorting in your application, care needs to be taken. This isn’t a system wide parameter, but a per operation one. So if a complex query has several sort operations in it it will use multiple work_mem units of memory. Not to mention that multiple backends could be doing this at once. This query can often lead your database server to swap if the value is too large. This option was previously called sort_mem in older versions of PostgreSQL.
- max_fsm_pages = <num> — This option helps to control the free space map. When something is deleted from a table it isn’t removed from the disk immediately, it is simply marked as "free" in the free space map. The space can then be reused for any new INSERTs that you do on the table. If your setup has a high rate of DELETEs and INSERTs it may be necessary increase this value to avoid table bloat.
- fsync = <boolean> — This option determines if all your WAL pages are fsync()’ed to disk before a transactions is committed. Having this on is safer, but can reduce write performance. If fsync is not enabled there is the chance of unrecoverable data corruption. Turn this off at your own risk.
- commit_delay = <num> and commit_siblings = <num> — These options are used in concert to help improve performance by writing out multiple transactions that are committing at once. If there are commit_siblings number of backends active at the instant your transaction is committing then the server waiting commit_delay microseconds to try and commit multiple transactions at once.
- random_page_cost = <num> — random_page_cost controls the way PostgreSQL views non-sequential disk reads. A higher value makes it more likely that a sequential scan will be used over an index scan indicating that your server has very fast disks.
SVN Tips
How can I make svn diff show me just the names of the changed files, not their contents?
svn diff doesn’t have an option to do this, but
- If you only are interested in the diffs between, say, revision 10 and the revision just before it,
svn log -vq -r10
does exactly what you want;
- otherwise, if you’re using Unix, this works for any range of revisions:
svn log -vq -r123:456 | egrep ‘^ {3}[ADMR] ‘ | cut -c6- | sort | uniq
How can I use wildcards or globbing to move many files at once?
You want to do something like
svn mv svn://server/trunk/stuff/* svn://server/trunk/some-other-dir
but it fails with
svn: Path 'svn://server/trunk/stuff/*' does not exist in revision 123
… or some other inscrutable error message.
Subversion doesn’t expand wildcards like "*" in URL arguments. (Technically speaking, Subversion does not expand wildcards in local paths either, but on most operating systems the shell expands wildcards in local paths in the command line before passing the resulting list to Subversion.)
You have to generate the list of source URLs yourself. You could do it like this (in Bash):
s=svn://server/trunk/stuff
items=$(svn ls "$s")
urls=$(for item in $items; do echo $s/$item; done)
svn mv $urls svn://server/trunk/some-other-dir -m "Moved all at once"
In any case, you can always accumulate a list of the names of the source files, and then run "svn mv" on each item in that list, like this:
s=svn://server/trunk/stuff
svn ls "$s" | \
while read f
do svn mv "$s/$f" svn://server/trunk/some-other-dir -m "Moved just one file"
done
Note, however, that this will generate one commit per source file; that’s in contrast to the above method (using a working copy) which generates just one commit total.
How do I deal with spaces in the editor path? Also, how can I define command line options for the editor?
The Subversion command line client will invoke the editor defined in the environment variable SVN_EDITOR. This environment variable is passed directly to the operating system along with the name of a temporary file used to enter/edit the log message.
Due to the fact that the SVN_EDITOR string is passed as-is to the system’s command shell, spaces in the editor name, or in the path name to the editor, will not work unless the editor name is in quotes.
For example, on Windows if your editor is in C:\Program Files\Posix Tools\bin\vi you would want to set the variable as follows:
set SVN_EDITOR="C:\Program Files\Posix Tools\bin\vi"
Note that there is no need to escape the quotes in the Windows shell as they are not part of the syntax for the set command.
On UNIX systems you would need to follow your shell’s specific methods for setting the variable. For example, in a bash shell, the following should work:
SVN_EDITOR='"/usr/local/more editors/bin/xemacs"'
export SVN_EDITOR
In case a command line option would be needed for the invocation of the editor, just add that after the editor name in the SVN_EDITOR environment variable just like you would us on the command line. For example, if the options -nx -r would be wanted for the above editors, the following will provide those options:
For Windows:
set SVN_EDITOR="C:\Program Files\Posix Tools\bin\vi" -nx -r
For UNIX/bash:
SVN_EDITOR='"/usr/local/more editors/bin/xemacs" -nx -r'
export SVN_EDITOR
Note that SVN_EDITOR is the Subversion specific environment variable setting for the editor selection. Subversion also supports using the more generic EDITOR variable but if you need special behaviors with Subversion it is best to use the SVN_EDITOR variable.
Useful UNIX Tips and Tricks
Here are a few pieces of Unix wisdom, I picked up during my 10 years of experience…
You’ve done something that has completely screwed up your terminal. Everything you type is either invisible or incomprehensible. Try the following:
$ ^C
$ stty sane^J
If this doesn’t work, try:
$ echo ^V^O
To determine what program dumped an anonymous core file, use gdb:
$ gdb -core core
Core was generated by `a.out’.
If you are on a system without gdb, try the file command:
$ file core
core: ELF 32-bit LSB core file of ‘a.out’ (signal 6)
Some keyboard shortcuts for editing
There are some pretty useful keyboard shortcuts for editing in bash. They might appear familiar to Emacs users:
Ctrl + a => Return to the start of the command you’re typing
Ctrl + e => Go to the end of the command you’re typing
Ctrl + u => Cut everything before the cursor to a special clipboard
Ctrl + k => Cut everything after the cursor to a special clipboard
Ctrl + y => Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to
Ctrl + t => Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
Ctrl + w => Delete the word / argument left of the cursor
Ctrl + l => Clear the screen
To create a vi macro that will wrap the current paragraph to a reasonable number of characters, add the following to your .exrc or .vimrc file:
map Q {!} fmt -c -u^M
Now, when you press ‘Q’ in visual mode, the current paragraph will be wrapped to approximately 70 characters.
When recursive copying, cp (cp -Rip, etc.) may not be the best tool for the job. For example, cp copies hard links as separate files, which is probably not what you want. To get a true copy of a directory, try:
$ tar cf – <dir> | (cd <destdir>; tar xf -)
This will create an exact copy of ‘dir’ in ‘destdir’. The same principle can be used to create a recursive copy on a remote machine:
$ tar cf – <dir> | ssh remotehost “(cd <destdir>; tar xf -)”
To list the files in another directory that match more than one pattern, it is easiest to do:
$ ls -l /usr/local/foo/{*.conf,*.local,*.rc}
which is equivalent to:
$ ls -l /usr/local/foo/*.conf /usr/local/foo/*.local /usr/local/foo/*.rc
This syntax is supported by (at least) bash, ksh, csh and sh.
You can extend this idea to make renaming files in another directory, for example, a little easier:
$ mv -i /usr/local/foo/bar/baz/{stuff,stuff~}
A little insurance against running ‘rm -rf *’ in the wrong directory — create an empty file called -i in any critical directory:
$ >-i
or…
$ touch — -i
If the ‘rm -rf *’ command is issued in that directory, the shell will expand the ‘-i’ early on and go into interactive mode, thus giving you a chance to say ‘Whoa, that was close!’, which always sounds better than ‘Oh fsck!’.
This tip works because the -i option to rm will override any previous -f.
Be forewarned that this tip only protects against ‘rm -fr *’, i.e., files and directories in the current directory.
To remove the -i file (or any other file beginning with ‘-’):
$ rm ./-i
or…
$ rm — -i
To return to the previous directory in ksh or bash, use:
$ cd -
To use the last argument of the previous command line as an argument, use $_. For example:
$ ls -l /usr/home/dmr/somefile
$ vi $_
To make the up and down arrow keys work and thereby enable command line editing and recall in ksh, include the following lines in your ~/.profile:
set -o emacs
alias __A=’^P’
alias __D=’^B’
alias __B=’^N’
alias __C=’^F’
alias __H=’^A’
You need to enter the actual control characters ^P, ^B, etc.
You can kill any process with ‘kill -9′ as root, right? Not necessarily. It’s quite easy to write a process that cannot be terminated, even with ‘kill -9′.
This was a hard-learned lesson that will only be given out via email request, if at all.