Some commands I wish I knew from back in the day. I thought I would share.
- Switch to the last directory: Suppose you end up in a long directory path and then you move to another directory in a totally different path. And then you realize that you have to go back to the previous directory you were in. In this case, all you need to do is to type this command: cd –
- Run a command that you had used in the past: Imagine a situation where you used a long command couple of minutes/hours ago and you have to use it again. Problem is that you cannot remember the exact command anymore. Reverse search is your saviour here. You can search for the command in the history using a search term. Just use the keys ctrl+r to initiate reverse search and type some part of the command. It will look up into the history and will show you the commands that match the search term
- Move to the beginning or end of a long command line: ctrl + a and ctrl + e
- Read long files without opening them: less path_to_file
- Reuse the argument from the previous command line: command (!$). Ex. :> mkdir test and :> cd !$
- Reuse the previous command in the current command (!!). Ex. :> pacman -Syu :> sudo !!
- Search and list a background process: ps -ef | grep vlc
- Kill a zombie thread or process that refuse to die: kill -9 vlc
- Empty a large file without deleting it: > filename
- Find files containing a particular text: grep -Pri searchTerm Path_to_directory
- View the history of commands typed earlier: history followed by !# where # is the index of the command you want to run. If you also would like to see a time stamp on the history command, add the following to your .bashrc file: export HISTTIMEFORMAT=”%d/%m/%y %T “.
- Find the right command: Use apropos like so :> apropos “text”. For example :> apropos “list directory”. on my Arch Linux, it always returns “nothing appropriate”. To fix this run sudo mandb
Leave a Reply