- scp in place of sftp for single-file copies
- scp is more convenient for just copying files than sftp:
scp file.py username@server.edu:Documents/project/ or scp username@server.edu:Documents/project/*txt ~/Desktop/textfiles/ The syntax is the username, @, the server address, :, then the path on the server. You can copy to or from the server.
- scp is more convenient for just copying files than sftp:
job control with C-z, &, fg and bg. Control-Z suspends your current job. fg brings back the most recently suspended (or backgrounded) job and bg starts it running in the background. This is great for running an interactive program and then jumping back to run some shell commands. For example:
$ python2.5 >>> 1+1 2 >>> ^Z [1]+ Stopped python2.5 $ fg python2.5 2+2 4 >>> ^Z [1]+ Stopped python2.5 $ bg [1]+ python2.5 & $ fg python2.5 3+3 6 >>>
You can also use & after a command to start it running in the background by default. This is nice for long-running programs: python2.5 ice.py London.txt Scotland.txt &
- nice
- If you write a CPU-hogging program, you can be nice to the other users by making it yield time to other processes more easily:
nice -n 6 python2.5 ice.py London.txt Scotland.txt &
- If you write a CPU-hogging program, you can be nice to the other users by making it yield time to other processes more easily:
- nohup
nohup prevents a long-running program from quitting when you log out (which sends SIGHUP* to programs, I think). Output redirects to nohup.out so you can read it later:
nohup nice -n 6 python2.5 ice.py London.txt Scotland.txt & There are other ways to do this, but I don't know what they are. Maybe GNU screen?
- pushd/popd
pushd ~/Desktop puts the current directory on a stack and cds to the Desktop. Run pushd with no arguments to switch back to the first directory. Or run popd to switch back to the first directory and pop the Desktop off the stack. You can use this something like back and forward in the Finder, except bash doesn't suck.
$ pwd src/project $ pushd ~/Desktop ~/Desktop src/project $ pushd src/project ~/Desktop $ popd ~/Desktop $ popd directory stack empty
- .bash_profile (this is really basic knowledge but it took me a couple of years to find it)
You can set startup settings in .bash_profile. Some sites say to use .bashrc, which is why I used the system-wide /etc/bashrc on my laptop for so long.
- ls -G
You can alias ls as ls -G to get different colours for files, executables, directories, symlinks, etc. There is an environment variable you can set in .bash_profile to customise the colours but I can't remember what it is. In .bash_profile, add a line: alias ls="ls -G"
- redirection to pbpaste/pbcopy on Mac OS
On Mac OS, redirecting output to pbcopy will copy something and redirecting input from pbpaste will paste something. For example, copy something from TextEdit and count the number of words:
pbpaste | wc Or run a program and instead of saving the results to a file, copy them:
a.out > pbcopy Run a Python interactive session that you copied from Safari? (I don't actually know if this works)
pbpaste | grep '^>>> ' | cut -b 5- | python
Ctrl-R searches bash history backward, even between sessions. So, for me, Ctrl-R + ssh li finds ssh lingdept@veritas.ucs.indiana.edu, and I can just press Enter to log in.
set -o vi will make bash behave like a mini-vim instead of a mini-emacs. You can press Esc to get out of insert mode and use the normal vi keys. e.g. k is the same as pressing up, / searches bash history backward. Say set -o emacs to get back to the normal behaviour.
top displays the running programs and their memory use. Use top -o rsize to sort by memory use and top -o cpu to sort by CPU use. I always use -R as well, since it skips calculation of private memory use (RPRVT), thereby using less CPU. RSIZE is the only relevant measure of memory use anyway.
Just to see if something is running, ps -aux | grep program will work.
- umask xyz, where x, y, and z are ints from 0 to 7, is used to set the default permissions for files and directories. To find the appropriate value of x, y, and z, realize that x applies to the owner, y applies to group members, and z applies to all others. Then realize that read=4, write=2, and execute=1. Add up the permissions you want to give the type of user, then subtract from 7. Do this for all three types of user.
which tells which programs are installed on the computer and where. which perl returns /usr/bin/perl.
w tells who is logged on and how long the computer has been up. I think it's short for 'who'?
*HUP is an abbreviation for hang up.
- find . -name what_your_looking_for. It can be a regex (but not a Perl-compat regex without switches)
- ls -r filename
- mdfind gives command line access to Spotlight on a Mac
- awk is useful when you pipe output of other, columnar commands.
ls -l | awk '{ print $1 $2}' will grab just the first two columns from ls -l` and print them. `ls *pdf | awk '{ print $1 } | sed -r 's/a(.+)/b\1/g'
#!bash (-) for f in *pdf; do mv $f $f.bak; done
- wc, wc -l, wc for a whole directory.
- head -100 and tail -20. tail -f will show tail of the file as it's being written.
- man is useful
- rsync is useful for syncing whole directories from local machine to server:
rsync -va courses/ mdickinson@jones....:courses Notice the slash after the local directory to back up. This looks at the date on the local machine and copies all changed/new files on the local directory.
