Every Geek site needs an obligatory Bash Tips post
Copy Files Securely Between Two Machines
I used to always forget the syntax for this, until I realized that the syntax is exactly like the standard cp command. In fact, you can copy files like you normally would using scp, on your local machine. The following are equivalent:
$ cp file file.orig
$ scp file file.orig
Where they differ is, scp lets you copy files over a network, through SSH. Here’s an example:
$ scp contents.txt firstname.lastname@example.org:/tmp
This will copy local file contents.txt to /tmp on the remote machine ssh.domain.com, as user silver. Here are some more examples:
$ scp draft.pdf ssh.domain.com:
(copy draft.pdf to my home dir on remote machine. username is implied to be the same locally and remotely.)
$ scp swine.jpg email@example.com
(read: This will copy swine.jpg to local machine as a file named firstname.lastname@example.org. To make it go remote, append a : to the address, like above)
scp supports, among other things, compression (-C) and recursive copying of directories (-r).
$ scp -rC code/ ssh.domain.com:/archive/code_02032009
Trying to copy to a directory you don’t have permission to (/usr etc) will fail.
Don’t Get Lost Jumping To and Fro Between Directories
You can use cd - to jump to the previous (NOT parent) dir. For example:
kiwi@localhost: ~ $ cd /usr/local/share
kiwi@localhost: /usr/local/share $ cd -
kiwi@localhost: ~ $ cd -
kiwi@localhost: /usr/local/share $
Another way is using pushd/popd – A Last In First Out (LIFO) stack of dirs.
kiwi@localhost: ~ $ pushd /usr/local/share/
pushd is like cd but keeps note of the current dir before cd’ing into a new one. The stack of dirs is listed every time you invoke pushd (the “/usr/local/share ~” output you see above.)
kiwi@localhost: /usr/local/share $ pushd /
/ /usr/local/share ~
Stack is ordered left to right, latest push first. If we pop the first dir off:
kiwi@localhost: / $ popd
/usr/local/share /tmp ~
kiwi@localhost: /usr/local/share $
We’re back in the share dir. We can keep popping until there’s nothing left (throws an error):
kiwi@localhost: /usr/local/share $ popd
kiwi@localhost: /tmp $ pushd /lib
/lib /tmp ~
kiwi@localhost: /lib $ popd
kiwi@localhost: /tmp $ popd
kiwi@localhost: ~ $ popd
bash: popd: directory stack empty
Working with Long Lines
No need for more Bash shortcut cheat sheets, but here are some useful ones to help you work with long lines.
You can jump to the start & end of a line using CTRL+a & CTRL+e respectively. Example (* is the cursor):
kiwi@localhost: ~ $ echo al the ducks are swimming in the w*
and you want to fix the first word. You can hop to the beginning of the line with CTRL+a:
kiwi@localhost: ~ $ *echo al the ducks are swimming in the w
and now you can jump to the end of the misspelled word “al” using CTRL+Right twice to correct it:
kiwi@localhost: ~ $ echo all*the ducks are swimming in the w
Now ctrl+e to jump to the end of line:
kiwi@localhost: ~ $ echo all the ducks are swimming in the w*
Instead of backspacing every character, use ALT+Backspace to backspace entire words. You can also delete all or part of a line using CTRL+u combo. It deletes everything before the cursor. Likewise, CTRL+k wipes out everything after the cursor. I’ve developed a habit of using CTRL+e CTRL+k to delete lines.
Bash has a lot of ALT commands that let you move and manipulate words. ALT+l and ALT+u will make a word in front of the cursor lowercase or uppercase, for example. A neat one I don’t think I ever used is ALT+\ It pulls everything after the cursor left to the first non-whitespace character. Here’s an example, * is the cursor:
$ my spacebar is *sticky
$ my spacebar issticky
Avoid Retyping Commands & Arguments
ESC + . is very useful. Escape followed by a period will output the argument you sent to your last Bash command. Command calls themselves are outputted if they were invoked without any arguments (popd, ls, etc).
Example, unzipping a file and moving the archive to /tmp:
$ unzip archive-with-a-long-ambiguous-name-03092009-5960-1.2.5.zip
$ mv archive-with-a-long-ambiguous-name-03092009-5960-1.2.5.zip /tmp
In the mv command, the archive name was outputted by pressing ESC+. (full command being mv (ESC+.) /tmp) There was no need to type the long archive name twice.
The argument is taken from your bash history. You can keep invoking ESC+. to cycle back through all your recent command arguments. (history -c to clear)
Try not to forget this; You’ll naturally find plenty of uses for it.
Another way to avoid re-typing commands is CTRL+R. It will initiate a search of your command history. Begin typing, and watch Bash try to complete your command from previous ones you entered.
Command Getting Too Big? Send it to your Editor
Sometimes you begin writing what you think will be a simple command, only to realize that it has grown too complex for the command line, and you wish you were in your text editor.
First make sure your default editor is set. This is either in $EDITOR (export EDITOR=/usr/local/bin/vim) or elsewhere depending on the distro.
Use “fc” to open the last executed command in your editor:
ls -paul --sort=size
... ls output ...
Now the ls line will be open in your editor. But what if you hadn’t executed the command yet? No problem. You’re sending off an email, but quickly realize that the command line isn’t ideal for everything:
echo -e "Dear Santa, \n\n\tIt has become evident that your fat ass is contributing to Global Warming, primarily due to the large quantity of coal you distribute annually. We hereby
No matter where you are on the line, hit CTRL+x, CTRL+e to invoke your editor, which now contains what you were typing on the cmd line.
I always find myself wanting to finish a command in vim, but unwilling to type the first few lines over, especially when I’m trying to write a for loop or any ugly multiline Bash code.
IMPORTANT: Whatever you type in your editor is executed automatically after you quit the editor.
Continue reading Bash Tips for Power Users