Command Line Magic Tricks
May 16, 2014



top-SCREEN

The command line/terminal is a powerful tool that – if used properly – can enable you to perform some amazing magic tricks on your humble machine. The command line is essentially the control room of your computer, and from it you can control your machine to do whatever you want. However, the price for this amazing power is complexity. Many people are put off by how difficult the command line can appear at first, but with a simple understanding of the basics and how to chain those basics together, you can get some amazing things done.

Before the Magic – Basic Commands

Before we look at any super complicated tricks, let’s start off by going over a quick rundown of the most basic terminal commands. All the of the following commands make up the building blocks which are then going to be used used to construct the more complicated processes later. If you are completely unfamiliar with the command line but are familiar with programming, then you can think of the commands below like a library/system method call in a program which you use along with other library/system methods to make your own, more complex methods. Without another metaphor, check out the basic command list:

pwd – print working directory
hostname – my computer’s network name
mkdir – make directory
cd – change directory
ls – list directory
rmdir – remove directory
pushd – push directory
popd – pop directory
cp – copy a file or directory
mv – move a file or directory
less – page through a file
cat – print the whole file
xargs – execute arguments
find – find files
grep – find things inside files
man – read a manual page
apropos – find what man page is appropriate
env – look at your environment
echo – print some arguments
export – export/set a new environment variable
exit – exit the shell
sudo !! DANGER !! become super user root
chmod DANGER change permission modifiers
chown DANGER change ownership
!! – execute the previous command

If you want to know more about how any of these commands works, open up your Command Line and type in:

This will give you the manual entry on whatever command you picked. Let’s take a look at an example. Say I wanted to know more about the pwd command. I would simply type:

man-pwd-SCREEN

and then it would give me the manual entry:

man-pwd-result-SCREEN

This will give you all the information you could ever need about the pwd command. Once you are done looking through the manual entry, you can hit Control-Z to exit (notice this is Control-Z for Mac as well, NOT Command-Z). This same principle can be used for any command you want to know more about. Now seems like a good time to also mention that if you want your command line to look like mine, check out my Customizing Your Terminal Tutorial, but be warned it is mainly for Mac users. It will show you how to customize the look of your command line using your .bash_profile.

Chaining Commands Together

Chaining terminal commands together is essential to understanding the commands that finish off this post. There are nine basic ways to combine commands listed below:

& – Sends process background (so we can run multiple process parallel)
; – Run multiple commands in one run, sequentially.
\ – To type larger command in multiple lines
&& – Logical AND operator
|| – Logical OR operator
! – Logical NOT operator
| – PIPE operator
{} – Command combination operator.
() – Precedence operator

Next we can go over a brief description of each of the commands to get a better understanding of what each does.

& – Runs a command in the background

This operator is used to send a process/script/command to the background, so that you can execute other commands in foreground to increase effective utilization of system resources and to speed up the script execution. This is good for things that take a long time to run or anything you don’t want to see the output from.

; – Runs the commands given sequentially

This operator will run multiple commands sequentially. If we have three commands separated by semicolon, the first command will run, then the second command will run after first command is done, and finally the third command will run after the second command completes. Note that the exit status of the previous command has no effect on the next command, it will run no matter what.

\ – Concatenation operator

This operator is used to spread a command to multiple lines in order to make it easier to input and for the user to read.

&& – Logical AND operator

This operator will execute the second command if the first command has a successful exit status. This operator works just like an if statement in other programming languages. This differs from the ‘;’ operator because the ‘;’ operator does not care what the exit status of the previous command is.

|| – Logical OR operator

This operator will execute the second command if the first command fails. This operator works just like an else statement in other programming languages.

! – NOT operator

This operator is used to execute the command, except not on a certain subset of objects. This is often used in conjunction with the ‘rm‘ command to remove everything except for some extensions.

| – PIPE operator

The pipe operator is used to send output from the first command to the second commands input. This is a very powerful operator.

{ … } – Command combination operator

This operator is used to combine two or more commands to be executed depending on the exit status of the first command. It’s allows the user to use the ‘;‘ operator but have the exit status’ matter.

( … ) –Precedence operator

This operator is used to execute commands in a precedence order. This works just like in math, where it executes whats in the parentheses first.

Command Line Magic Revealed

This next section is a collection of Command Line magic tricks. These are going to be structured as follows:

Descriptive Magic Trick Name

There could be a paragraph here describing the trick in more detail if I feel it is needed… Lorem ipsum dolor sit amet, no petentium referrentur est. Ad sit dicta noster prodesset, magna zril laoreet mea et, mundi regione mei in.

example-command-SCREEN

All of the commands will follow this same format. Below is also an nice directory/list to the commands so you can skip to the one you want.

Enjoy the cool command line tricks that follow. Also please note that I am aware that the output pictures are a little small, but you can click on them to open them up to their full size! Please do that in order to read everything in them. Thanks!


 

Show the Differences Between Two Directories.

The following function can be added to the .bashrc file.

directory-differences-SCREEN


 

Switch to Previous Working Directory.

I know this one seems like a really simply one, but its super useful. Use often and you’ll get so much faster at everything.

There is no real reason to make an alias or function for this since its so short already.

switch-between-working-directories-SCREEN


 

Using Comma’s with File Names. (and Everywhere Else)

This is handy trick for multiple file manipulation, but it can be used in many places. All you need to do is use the { … } and you will have multiple instances of the same command. Below is an example involving files, but you can just as easily do this with ls to list critical directories.

Again, there is no real reason to make an alias or function for this since its so short already.

use-commas-SCREEN


 

Delete All Files in a Directory Except Those with a Given Extension.

The command below will delete all files in your current directory other than the files with the given extensions. You can also of course put more than two extensions, just remember to include the ! -name portion for each extension you want to give.

remove-files-except-extension-SCREEN


See Which Processes are Using the Internet.

The following alias can be added to the .bash_profile file.

processes-using-internet-SCREEN


 

Find File Names with New Lines, Carriage Returns, or Blank Lines.

This command can be used to find all the files in the current directory that have names that could cause problems on a server or something like that. If you are a web developer, this is a very useful thing to learn.

Just due to the number of quotes in this one, I would make it a function in your .bashrc file rather than an alias.

bad-file-names-SCREEN


 

Find Files Below the Current Directory that have Changed in the Last 60 Minutes.

changed-files-SCREEN


 

Make Stats on the Top File Types in Current Directory and Below.

top-files-SCREEN


 

Make All Files in Current Directory Lower Case.

make-files-lower-case-SCREEN


 

Kill a Process in a Spectacular and Hilarious Fashion.

screw-preview-1-SCREEN screw-preview-2-SCREEN

You can of course change “Screw” to whatever makes you happy! This is probably the nerdiest way to blow off steam I’ve ever found.

That’s the end! I hope you enjoyed all these tricks to getting the most out of your command line. If you have any of your own tips or tricks please leave them in the comments below! I am definitely not the biggest command line guru or anything like that, so I would love to hear what you have on your own machines.

As always thank you for reading and please share it around as much as you can! Please feel free to put any questions, suggestions, or ideas in the comments section below. I would really like to know what kind of tutorials everyone wants me to make.


Comments