Customizing the Terminal: 6 Command Line Tips and Tricks

Written by BinnyVA on April 26, 2009 – 9:06 pm -

Terminal

A few tips and tricks on the terminal to make you more efficient when using it. If you know of any other tips, add it in the comments section.

1. ls Without ls

When you are trying to cd into a deep folder, you might not know the correct folder name some levels deep. You might have to do something like…

$ cd ~/Scripts/Perl
$ ls
bin	SedGUI       ToSee	Cronjobs  Maintenance
$ cd Maintenance

There is an easier way – go to the wanted folder

$ cd ~/Scripts/Perl

Now, without pressing enter, double tap the TAB key. You will get a list of files. And the command prompt waiting to be filled…

$ cd ~/Scripts/Perl/[TAB TAB]
bin	SedGUI       ToSee	Cronjobs  Maintenance
$ cd ~/Scripts/Perl/_

You can also use double-TAB to auto-complete commands.

2. Searching the history with Ctrl+R

If you have to use a command you have already used before, press CTRL+R and then type a few characters of the command. The latest command with those characters will be shown – if that is the command you want to execute, press enter and it will be executed. If not, just press CTRL+R again and it will show the next command.

You have no idea how useful this tip is if you haven’t been using it. I use this all the time.

For more details, read this article.

3. Open Terminal using a Shortcut

If you are a GUI user, chances are you prefer using a Terminal emulator(like gnome-terminal or konsole) instead of going into the Terminal mode by pressing CTRL+ALT+F1. If so, assign a shortcut to those emulator apps. I prefer using the shortcut ‘Ctrl+Alt+A’ to do this.

Gnome

If you are in gnome, there is a very easy way to do this…

  • Go to System > Preferences > Keyboard Shortcuts
  • Find ‘Run a Terminal’ – assign the shortcut ‘Ctrl+Alt+A’

KDE

  • Right Click on the K-Menu > Menu Editor
  • Find your terminal application in the list(usually System > Terminal Applications > Terminal)
  • Select the ‘Current Shortcut Key’ option and set it to ‘Ctrl+Alt+A’

You can also do this by opening the KHotKeys application.

4. Bash Keyboard Shortcuts

Learn the bash keyboard shortcuts – these are the ones I use the most…

CTRL+R
Search the history. We already talked about this.
CTRL+L
Clears the screen. Use this instead of the clear command.
CTRL+D
Use this instead of the exit command.
CTRL+C
Kill whatever is running
CTRL+Z
Puts whatever is running into a suspended background process. Use fg to restore it.

5. Find Command using apropos

Find the command you want using the apropos command. Just type in a description of the command as the first argument. For example, lets say you want to find the command to list the directory contents. Use the command…

$ apropos "directory contents"
dir                  (1)  - list directory contents
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents
ntfsls               (8)  - list directory contents on an NTFS filesystem
vdir                 (1)  - list directory contents

The only problem is that I can never spell ‘apropos’ – so I keep this in my .bashrc file…

alias apox='apropos'

6. Learn New Commands

There are a few sites that publish cool commands on a daily/semi-daily basics – subscribe to those and learn new commands…

  • Txt – Linux Commands and Code Snippets – My own site – I wrote about this a while ago.
  • commandlinefu.com
  • shell-fu
  • Bash Snippets
  • Codesnippt.com – Shell Scripts
  • bash code

  • Tags: , , , , , ,
    Posted in Command Line, Configuration, Tools | 5 Comments »

    Free eBook – Linux 101 Hacks

    Written by BinnyVA on February 15, 2009 – 11:09 pm -

    Linux 101 Hacks eBook

    Ramesh, who blogs at The Geek Stuff have released a free eBook ‘Linux 101 Hacks‘. Go download it – if you haven’t done it already.

    About the Book

    There are total of 101 hacks in this book that will help you build a strong foundation in Linux. All the hacks in this book are explained with appropriate Linux command examples that are easy to follow.

    Its kinda like my txt site on Linux Commands – except for the fact that the explanation on the book is much better than my site.

    Here is a copy-paste of the table of contents for the book.

    Table of Contents

    Chapter 1: Powerful CD Command Hacks

    • Hack 1. Use CD PATH to define the base directory for cd command
    • Hack 2. Use cd alias to navigate up the directory effectively
    • Hack 3. Perform mkdir and cd using a single command
    • Hack 4. Use “cd -” to toggle between the last two directories
    • Hack 5. Use dirs, pushd and popd to manipulate directory stack
    • Hack 6. Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

    Chapter 2: Date Manipulation

    • Hack 7. Set System Date and Time
    • Hack 8. Set Hardware Date and Time
    • Hack 9. Display Current Date and Time in a Specific Format
    • Hack 10. Display Past Date and Time
    • Hack 11. Display Future Date and Time

    Chapter 3: SSH Client Commands

    • Hack 12. Identify SSH Client Version
    • Hack 13. Login to Remote Host using SSH
    • Hack 14. Debug SSH Client Session
    • Hack 15. Toggle SSH Session using SSH Escape Character
    • Hack 16. SSH Session Statistics using SSH Escape Character

    Chapter 4: Essential Linux Commands

    • Hack 17. Grep Command
    • Hack 18. Find Command
    • Hack 19. Suppress Standard Output and Error Message
    • Hack 20. Join Command
    • Hack 21. Change the Case
    • Hack 22. Xargs Command
    • Hack 23. Sort Command
    • Hack 24. Uniq Command
    • Hack 25. Cut Command
    • Hack 26. Stat Command
    • Hack 27. Diff Command
    • Hack 28. Display total connect time of users

    Chapter 5: PS1, PS2, PS3, PS4 and PROMPT_COMMAND

    • Hack 29. PS1 – Default Interaction Prompt
    • Hack 30. PS2 – Continuation Interactive Prompt
    • Hack 31. PS3 – Prompt used by “select” inside shell script
    • Hack 32. PS4 – Used by “set -x” to prefix tracing output
    • Hack 33. PROMPT_COMMAND

    Chapter 6: Colorful and Functional Shell Prompt Using PS1

    • Hack 34. Display username, hostname and current working directory in the prompt
    • Hack 35. Display current time in the prompt
    • Hack 36. Display output of any command in the prompt
    • Hack 37. Change foreground color of the prompt
    • Hack 38. Change background color of the prompt
    • Hack 39. Display multiple colors in the prompt
    • Hack 40. Change the prompt color using tput
    • Hack 41. Create your own prompt using the available codes for PS1 variable
    • Hack 42. Use bash shell function inside PS1 variable
    • Hack 43. Use shell script inside PS1 variable

    Chapter 7: Archive and Compression

    • Hack 44. Zip command basics
    • Hack 45. Advanced compression using zip command
    • Hack 46. Password Protection of Zip files
    • Hack 47. Validate a zip archive
    • Hack 48. Tar Command Basics
    • Hack 49. Combine gzip, bzip2 with tar

    Chapter 8: Command Line History

    • Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT
    • Hack 51. Search the history using Control+R
    • Hack 52. Repeat previous command quickly using 4 different methods
    • Hack 53. Execute a specific command from history
    • Hack 54. Execute previous command that starts with a specific word
    • Hack 55. Control the total number of lines in the history using HISTSIZE
    • Hack 56. Change the history file name using HISTFILE
    • Hack 57. Eliminate the continuous repeated entry from history using HISTCONTROL
    • Hack 58. Erase duplicates across the whole history using HISTCONTROL
    • Hack 59. Force history not to remember a particular command using HISTCONTROL
    • Hack 60. Clear all the previous history using option -c
    • Hack 61. Substitute words from history commands
    • Hack 62. Substitute a specific argument for a specific command
    • Hack 63. Disable the usage of history using HISTSIZE
    • Hack 64. Ignore specific commands from the history using HISTIGNORE

    Chapter 9: System Administration Tasks

    • Hack 65. Partition using fdisk
    • Hack 66. Format a partition using mke2fsk
    • Hack 67. Mount the partition
    • Hack 68. Fine tune the partition using tune2fs
    • Hack 69. Create a swap file system.
    • Hack 70. Create a new user
    • Hack 71. Create a new group and assign to an user
    • Hack 72. Setup SSH passwordless login in OpenSSH
    • Hack 73. Use ssh-copy-id along with ssh-agent
    • Hack 74. Crontab
    • Hack 75. Safe Reboot Of Linux Using Magic SysRq Key

    Chapter 10: Apachectl and Httpd Examples

    • Hack 76. Pass different httpd.conf filename to apachectl
    • Hack 77. Use a temporary DocumentRoot without modifying httpd.conf
    • Hack 78. Increase the Log Level temporarily
    • Hack 79. Display the modules inside Apache
    • Hack 80. Show all accepted directives inside httpd.conf
    • Hack 81. Validate the httpd.conf after making changes
    • Hack 82. Display the httpd build parameters
    • Hack 83. Load a specific module only on demand

    Chapter 11: Bash Scripting

    • Hack 84. Execution Sequence of .bash_* files
    • Hack 85. How to generate random number in bash shell
    • Hack 86. Debug a shell script
    • Hack 87. Quoting
    • Hack 88. Read data file fields inside a shell script

    Chapter 12: System Monitoring and Performance

    • Hack 89. Free command
    • Hack 90. Top Command
    • Hack 91. Ps Command
    • Hack 92. Df Command
    • Hack 93. Kill Command
    • Hack 94. Du Command
    • Hack 95. lsof commands.
    • Hack 96. Sar Command
    • Hack 97. vmstat Command
    • Hack 98. Netstat Command
    • Hack 99. Sysctl Command
    • Hack 100. Nice Command
    • Hack 101. Renice Command

    About the Author

    Ramesh Natarajan is the blogger behind The Geek Stuff. To know more about him and the site, take a look at the about page.

    This is what he has to say about himself…

    My name is Ramesh Natarajan. I live in Los Angeles, California. I have been adding 1’s and 0 ’s for more than 15 years in the IT industry. I am very much interested in anything that runs on electricity. I have done intensive programming on several languages and C is my favorite. I have done lot of work on the infrastructure side in Linux system administration, DBA, Hardware and Storage (EMC).


    Tags: , , , ,
    Posted in Command Line, News, Reviews | 5 Comments »