I’m always interested in learning more about useful development tools. In college, most programmers get an intro to the Linux command line environment, but I wanted to share some commands I use daily that I’ve learned since graduation.
Being comfortable on the command line is a great skill to have when a customer is looking over your shoulder on a Webex. They could be watching a software demo or deployment to their environment. It can also be useful when learning a new code base or working with a product with a large, unfamiliar directory structure with lots of logs.
If you’re on Windows, you can use Cygwin to get a Unix-like CLI to make these commands available.
Useful Linux commands
Find
The command find helps you find files by recursively searching subdirectories. Here are some examples:
find . Prints all files and directories under the current directory.
find . -name ‘*.log’ Prints all files and directories that end in “.log”.
find /tmp -type f -name ‘*.log’ Prints only files in the directory “/tmp” that end in “.log”.
find . -type d Prints only directories.
find . -maxdepth 2 Prints all files and directories under the current directory, and subdirectories (but not sub-subdirectories).
find . -type f -exec ls -la {} \; The -exec flag runs a command against each file instead of printing the name. In this example, it will run ls -la filename on each file under the current directory. The curly braces take the place of the filename.
Grep
The command grep lets you search text for lines that match a specific string. It can be helpful to add your initials to debug statements in your code and then grep for them to find them in the logs.
grep foo filename Prints each line in the file “filename” that matches the string “foo”.
grep foo\\\|bar filename Grep supports regular expressions, so this prints each line in the file that matches “foo” or “bar”.
grep -i foo filename Add -i for case insensitive matching.
grep foo * Use the shell wildcard, an asterisk, to search all files in the current directory for the string “foo”.
grep -r foo * Recursively search all files and directories in the current directory for a string.
grep -rnH foo filename Add -n to print line numbers and -H to print the filename on each line.
find . -type f -name ‘*.log’ -exec grep -nH foo {} \; Combining find and grep can let you easily search each file that matches a certain name for a string. This will print each line that matches “foo” along with the file name and line number in each file that ends in “.log” under the current directory.
ps -ef | grep processName The output of any command can be piped to grep, and the lines of STDOUT that match the expression will be printed. For example, you could use this to find the pid of a process with a known name.
cat file.txt | grep -v foo You can also use -v to print all lines that don’t match an expression.
Ln
The command ln lets you create links. I generally use this to create links in my home directory to quickly cd into long directory paths.
ln -s /some/really/long/path foo The -s is for symbolic, and the long path is the target. The output of ls -la in this case would be foo -> /some/really/long/path .
Bashrc
The Bashrc is a shell script that gets executed whenever Bash is started in an interactive terminal. It is located in your home directory, ~/.bashrc . It provides a place to edit your $PATH, $PS1, or add aliases and functions to simplify commonly used tasks.
Aliases are a way you can define your own command line commands. Here are a couple useful aliases I’ve added to my .bashrc that have saved a lot of keystrokes on a server where I’ve installed Oracle WebCenter:
WC_DOMAIN=/u01/oracle/fmw/user_projects/domains/wc_domain alias assets="cd /var/www/html" alias portalLogs="cd $WC_DOMAIN/servers/WC_Spaces/logs" alias domain="cd $WC_DOMAIN" alias components="cd $WC_DOMAIN/ucm/cs/custom" alias rpl="portalLogs; vim -R WC_Spaces.out"
After making changes to your .bashrc, you can load them with source ~/.bashrc . Now I can type rpl , short for Read Portal Logs, from anywhere to quickly jump into the WebCenter portal log file.
alias grep=”grep –color”
This grep alias adds the –color option to all of my grep commands. All of the above grep commands still work, but now all of the matches will be highlighted.
Vim
Knowing Vim key bindings can be convenient and efficient if you’re already working on the command line. Vim has many built-in shortcuts to make editing files quick and easy.
Run vim filename.txt to open a file in Vim. Vim starts in Normal Mode, where most characters have a special meeting, and typing a colon, : , lets you run Vim commands. For example, typing Shift-G will jump to the end of the file, and typing :q while in normal mode will quit Vim. Here is a list of useful commands:
:q Quits Vim
:w Write the file (save)
:wq Write and quit
:q! Quit and ignore warnings that you didn’t write the file
:wq! Write and quit, ignoring permission warnings
i Enter Insert Mode where you can edit the file like a normal text editor
a Enter Insert Mode and place the cursor after the current character
o Insert a blank line after the current line and enter Insert Mode
[escape] The escape button exits insert mode
:150 Jump to line 150
shift-G Jump to the last line
gg Jump to the first line
/foo Search for the next occurrence of “foo”. Regex patterns work in the search.
?foo Search for the previous occurrence of “foo”
n Go to the next match
N Go to the previous match
* Search for the next occurrence of the searched word under the cursor
# Search for the previous occurrence of the searched word under the cursor
w Jump to the next word
b Jump to the previous word
“ Jump to the last action
dw Delete the word starting at the cursor
cw Delete the word starting at the cursor and enter insert mode
c$ Delete everything from the cursor to the end of the line and enter insert mode
dd Delete the current line
D Delete everything from the cursor to the end of the line
u Undo the last action
ctrl-r ctrl-r Redo the last action
d[up] Delete the current line and the line above it. “[up]” is for the up arrow.
d[down] Delete the current line and the line below it
d3[down] Delete the current line and the three lines below it
r[any character] Replace the character under the cursor with another character
~ Toggle the case (upper or lower) of the character under the cursor
v Enter Visual Mode. Use the arrow keys to highlight text.
shift-V Enter Visual Mode and highlight whole lines at a time.
ctrl-v Enter Visual Mode but highlight blocks of characters.
= While in Visual Mode, = will auto format highlighted text.
c While in Visual Mode, c will cut the highlighted text.
y While in Visual Mode, y will yank (copy) the highlighted text.
p In Normal Mode, p will paste the text in the buffer (that’s been yanked or cut).
yw Yank the text from the cursor to the end of the current word.
:sort Highlight lines in Visual Mode, then use this command to sort them alphabetically.
:s/foo/bar/g Highlight lines in Visual Mode, then use search and replace to replace all instances of “foo” with “bar”.
:s/^/#/ Highlight lines in Visual Mode, then add # at the start of each line. This is useful to comment out blocks of code.
:s/$/;/ Highlight lines in Visual Mode, then add a semicolon at the end of each line.
:set paste This will turn off auto indenting. Use it before pasting into Vim from outside the terminal (you’ll want to be in insert mode before you paste).
:set nopaste Make auto indenting return to normal.
:set nu Turn on line numbers.
:set nonu Turn off line numbers.
:r!pwd Read the output of a command into Vim. In this example, we’ll read in the current directory.
:r!sed -n 5,10p /path/to/file Read lines 5 through 10 from another file in Vim. This can be a good way to copy and paste between files in the terminal.
:[up|down] Type a colon and then use the arrow keys to browse through your command history. If you type letters after the colon, it will only go through commands that matched that. (i.e., :se and then up would help find to “:set paste” quickly).
Vimrc
The Vimrc is a configuration file that Vim loads whenever it starts up, similar to the Bashrc. It is in your home directory.
Here is a basic Vimrc I’d recommend for getting started if you don’t have one already. Run vim ~/.vimrc and paste in the following:
set backspace=2 " backspace in insert mode works like normal editor syntax on " syntax highlighting filetype indent on " activates indenting for files set autoindent " auto indenting set number " line numbers colorscheme desert " colorscheme desert set listchars=tab:>-,trail:.,extends:>,precedes:< set list " Set up whitespace characters set ic " Ignore case by default in searches set statusline+=%F " Show the full path to the file set laststatus=2 " Make the status line always visible
Perl
Perl comes installed by default on Linux, so it is worth mentioning that it has some extensive command line capabilities. If you have ever tried to grep for a string that matches a line in a minified Javascript file, you can probably see the benefit of being able to filter out lines longer than 500 characters.
grep -r foo * | perl -nle’print if 500 > length’
Conclusion
I love learning the tools that are available in my development environment, and it is exciting to see how they can help customers as well.
Recently, I was working with a customer and we were running into SSL issues. Java processes can be run with the option -Djavax.net.ssl.trustStore=/path/to/trustStore.jks to specify which keystore to use for SSL certificates. It was really easy to run ps -ef | grep trustStore to quickly identify which keystore we needed to import certificates into.
I’ve also been able to use various find and grep commands to search through unfamiliar directories after exporting metadata from Oracle’s MDS Repository.
Even if you aren’t on the command line, I’d encourage everyone to learn something new about their development environment. Feel free to share your favorite Vim and command line tips in the comments!