Basic Command Line Tools
A co-worker expressed interest in learning more Unix command line operations. This collection seeks to supplement his basic knowledge with some additional tips and tools.
Searching Source Code
- ack can be downloaded at betterthangrep.com and is a great, free tool for searching a project’s source code. Usage: ack string will return the relative paths to all the files containing the matched string, the line on which the string is found with the match highlighted, and the line number on which the match lives, all while ignoring those files contained within .git or .svn directories.
ack --js searchtextwill perform a search on all *.js files.
- If you still prefer grep, the following would return the relative paths to all the files in which the text “string” is found:
grep -rl string . | grep -v .svn, excluding files in .svn directories.
diff -rq somedirectory someotherdirectorywill recursively diff all files in somedirectory and someotherdirectory, printing the files containing any descrepencies to the terminal.
diff fileone filetwowill output to the terminal all instances of descrepencies between fileone and filetwo, as well as outputing the line/column number in which the differences exist.
- Alternatively, colordiff is a tool which provides colorized ouput to diff. If you’re using homebrew, colordiff can be installed with brew install colordiff. See colordiff man for usage.
Miscellaneous Operations on Files
cat filenamewill output the contents of filename to the terminal.
less filenameprovides a much better way to inspect large files from the command line. It outputs only a full screen view of a file’s contents. Use j and k to scroll up and down. b and f navigate a full screen up and down. Use /pattern to search for the text pattern.
cat file1 >> file2will write the contents of file1 to file2.
touch filenamewill create a file named filname within the current directory.
- Once installed, tree produces a depth-indented listing of files.
chmod 760 filenamechanges the permissions on filename to 760.
Maintainenance, Monitoring, and Debugging
ps xalists all the running processes on your machine.
ps xa | grep javawill will check if a process named java is running. kill -9 xxxx, where xxxxx is the process id, will kill the java process. This is useful if something has frozen.
- top is a system monitor tool that produces a frequently-updated list of processes. By default, the processes are ordered by percentage of CPU usage.
tail -f logfilenameoutputs the log file’s content to the terminal, which is helpful when troubleshooting code such as PHP or Apache.
- Cheat is a RubyGem that provides command-line access to simple cheat sheets, not unlike a simplified, user-edited man page. An out-of-the-box Cheat installation provides a few basic starter cheat sheets which can be edited and extended easily. It’s also relatively easy to add your own additional cheat sheets. Example Usage: cheat html.
- Most commands have a -help option which will list a command’s arguments and options. Example: cd -help.
- Similarly, man provides more detailed documentation. Example: man mysqldump.
Working With a Server
ssh firstname.lastname@example.org ssh acess.
sftp email@example.com ftp and sftp access, respectively.
scp /path/to/local/file username@hostname:path/to/copy/tocan be used to copy files to a server over ssh.
scp username@hostname:/path/to/remote/file /local/path/to/copy/to/copies a file from a server to your local machine.
- wget can be used to retrieve content from a web server. Example:
wget http://hostname.com/somefile.zipwill download somefile.zip from http://hostname.com.
curl -O http://hostname.com/download.tar.gzwill download download.tar.gz