Bash commands for navigation, sharing, and find

Learn how to use Bash on a Linux server to change command line navigation, switch around in directories, file first redirection, three find commands, and how to show people the command line without giving them access through rerouting.

This tip introduces five interesting and helpful Bash and command line tricks and shortcuts that are designed to make your life easier, from saving a few keystrokes to changing the way you use the command line. This is a sequel of sorts to my Five things you didn’t know Bash could do on Linux tip. 

Changing Bash navigation
Most people know about Bash’s navigation commands. They’re pretty familiar to anyone who uses Emacs as their editor. They include:

 Ctrl + a - Go to the start of the current command
Ctrl + e - Go the end of the current command
Ctrl + w - Delete the next item left of the cursor (argument or command)
Ctrl + u - Cut everything before the cursor
Ctrl + k - Cut every after the cursor
Ctrl + y - Paste back whatever you cut with Ctrl +u or Ctrl + y
Ctrl + p - Scroll through previous commands
Ctrl + n - Next command
Ctrl + l - Clear the screen

What you might not know is that Bash can use not only an Emacs edit mode but also a vi edit mode (for those of who prefer vi to emacs commands). You can change into vi edit mode like so:

 $ set -o vi

You can make this a permanent change by adding this command to your .bashrc file:

 $ echo "set -o vi" >> ~/.bashrc

Now you can use the command line just like you were in a vi session. For example, you can navigate using the vi command mode. Press Esc to enter command mode and then you can use command like:

 l - Move forward one character
h - Move back one character
0 (zero) - Move cursor to the start of the line
$ - Move cursor to the end of the line
w - Move forward one word
b - Move back one word

You can then use the Esc key to exit command mode or like vi use A or I to enter insert mode where you can edit the command line. Hitting enter at any time will execute the current command.

If you decide to don’t want vi mode you can change back to emacs mode by typing:

 $ set -o emacs

You can also use the command:

 $ bind -P

To show you a list of all commands and their key bindings. The bind command can also be used to set your own key bindings for various functions.

The cd - command

You can use:

 $ cd -

To change directory back to the directory you were previously in, you can use it repeatedly to go back to previous directories.

Route your command output
Sometimes you want to show people what’s happening on the command line without giving them access. There’s a quick and easy way to output what’s happening on your command line to the network using script and nc. Type:

 $ script -f >(nc -l 8000)

This will pipe anything typed on the command line to the network and anyone listening on port 8000 will be able to see what you have typed using the nc command.

 $ nc host 8000

File first redirection
I like any tip that saves me typing a few characters. Often you want to work with the same file multiple different ways. Using redirection we can specify the name of the file first and the command to be executed at the end. For example:

 $ </var/log/messages grep dhclient
$ </var/log/messages grep kernel
 $ </var/log/messages grep yum

Here we’ve specified the file we’d like to search preceded with < and then grep command and the text we’re searching for. Now if we were to retrieve the command from history we merely need to edit the trailing text to search again for another term.

Three useful find commands
Finally, I’m going to share three useful find commands that I use regularly. Find is an immensely powerful little tool, especially paired with the -exec option that allows you to execute commands on the results of a search. I recommend you read through the find man page for further useful tidbits.

The first command finds and lists all files in the current directory modified in the last day.

 $ find ./ -type f -mtime -1 -exec ls -al {} \;

You can obviously modify it to find in other places and for differing periods (change -1 to a different period).

The next command finds files and then performs a Perl-based find-and-replace on them.

 $ find . -name '*.txt' -type f -exec perl -pi -e 's/goldfish/tiger/g' {} \;

In this case we’re finding all files with an extension of .txt, finding the word “goldfish” in those files and replacing it with the word “tiger”.

Our last find command is one I use frequently to recursively remove .svn (and others) directories from my path.

 find . -name ".svn" -exec rm -rf {} \;


Dig Deeper on Linux servers