Today's small blog post will cover four handy tips, which makes your life on a Linux/bash terminal easier and faster!
Search your BASH History on-the-fly
One of the most time-saving features of bash is the "search bash history" quick command Ctrl-r. Find a terminal and see what happens when you issue the command. The prompt will say "(reverese-i-search)". Type in the beginning of a command you have used recently, which had some hard-to-remember extra parameters. This can be a scp-command with that awkward flag for pointing out which certificate to use for the connection, or just finding the path to a script deep in the file-system, which you edited the other day but can't remember where it's located.
Press ctrl-r and type the first one or two letter of the command that you are looking for, and it will find the last executed command for your user which match. Type more letters to qualify the search even further, or press ctrl-r again, to find the next match.
If you are not already familiar with this, you will ask yourself very soon, how you managed to live without it!
Press ctrl-r and type the first one or two letter of the command that you are looking for, and it will find the last executed command for your user which match. Type more letters to qualify the search even further, or press ctrl-r again, to find the next match.
If you are not already familiar with this, you will ask yourself very soon, how you managed to live without it!
Find and Kill a Background Job the Fast Way
You probably know how to put a process in the background by using "&" after a command. You use the command "jobs" to list the processes that you have put in the background, where each command has it's own unique numerical identifier. This ID can be used to kill the process, using "kill %<id>". See image below.
Show Last Exit Status
Scripts and programs in the unix-styled-world has a tendency not to send unnecessary (or even necessary ) information back to the command line. However, the exit code of a program can tell a a lot about why the program did not execute as expected. It can also be very useful in scripts and automation, to be able to determine specific exit codes.
To check the exit code of the last executed program, use "echo $?". Example
[[email protected] ~]$ ./my_script.sh --option=invalidValue
[[email protected] ~]$ echo $?
4
It's customary in the unix-world that 0 means "program executed successfully", and everything else means that the program failed in some way.
To check the exit code of the last executed program, use "echo $?". Example
[[email protected] ~]$ ./my_script.sh --option=invalidValue
[[email protected] ~]$ echo $?
4
It's customary in the unix-world that 0 means "program executed successfully", and everything else means that the program failed in some way.
Go to Last Directory
You know what it's like. You're deep into the file-system, in some obscure configuration directory of an application, and just have to go to your home directory to check a config file. Then you need to find your way back, but was it /opt/app/version/config or /var/app/version/config, or....?
Just use the command "cd -" to go to the directory you were in last!
[[email protected] tmp]$ cd -
/var/www/demoapp/html/api
Just use the command "cd -" to go to the directory you were in last!
[[email protected] tmp]$ cd -
/var/www/demoapp/html/api