Linux CLI (Command Line Interface) Tricks
Here is a small(ish) list of Linux CLI tips and tricks I have learned and researched over the years. This list is by no means completely comprehensive but contains a list of some of the tricks I use on an everyday basis. Living your life “in the shell” can be very cumbersome if you aren’t using the tricks outlined below. Good luck and happy Linuxing.
I will say this: Do not give in and use all the tricks all the time if you are just starting with Linux. It’s always best (in my opinion) to learn the ropes and background to everything before using the GUI or any related tricks. I believe this is true with almost any learning process – technology-based or not.
Command Line File Name Completion
Tired of typing the whole path to a single command? Some commands can be very large and cumbersome. Try “tabbing it out”.
Try for example: where<tab>
The above should return whereis. If you have more than one binary in your path that contains the word where you may not get a result. In this case hit tab again and you’ll be presented with all options.
Print Working Directory (pwd)
The pwd command is useful to tell you what directory you are currently in. Depending on your shell configuration, you will be presented with your full working directory in the title of your shell program. For instance, I use putty and it always shows my Current Working Directory (CWD).
Forget what the last few commands you completed were? Want to diagnose a system and you have no idea what the person before you executed? Try the “history” command in Linux.
Type “history”. This provides a list of all recently entered commands and can be very large. If you type “history 10” it works much like the tail command and shows the last 10 commands in history.
Type “history –c” to clear your command history.
Tons of information from the command you just executed? Redirect the output to a file or another program.
|> – Output Redirection||>> – Append to EOF (end of file)|
|1> – Redirect STDOUT (Standard Out)||< – Input Redirection|
|2> – Redirect STDERR (Standard Error)|
|&> – Redirect all|
# echo “foo” > bar (this example will echo the text “foo” to the file called “bar”)
# echo “foo2” >> bar (this example will echo the text “foo2” to the end of the file “bar”)
# wc –l < bar (this example will do a line count on the file bar)
Typically commands read, by default from STDIN. For instance I could also run wc like this:
# wc -l bar
I would still obtain the same results as explicitly telling the CLI to use STDIN (<).
Using aliases is another way to make entering common commands easier. Think of an alias as a simple shortcut to a longer command. Let’s say you want to remove a directory and you’re tired of always typing “rm –rf <directory”. You can make an alias by typing “alias rmdir=’rm –rf’. Now you can type “rmdir <directory>” on the CLI and achieve the same result.
A symbolic link is simply a pointer to another file/directory. To make a shortcut to a program shorter or as a link inside your home directory use a command like this:
# ln –s /usr/local/program/bin/program ~/program
– or to link an entire directory –
# ln –s /usr/local/program ~/program
apropos Search Whatis Database
Ever wanted to find a command but never knew the name? Do you know what the command does or a description but can’t put your finger on it? Use the command apropos to search the whatis database.
Just type apropos “string to search for”
Whereis – Find a binary or man page
Ever needed to find the location of a binary easily? Try “whereis”. Simply type whereis “binary” and you will be presented with a location to the binary and/or manpage for the given binary.
There are a ton of other shortcuts I am missing but this is just a small list. Have a great day!