Jump to content

CL-04: How can I use shortcuts in the command line?


Recommended Posts

Browse: [About the FAQ Forum] [Table of Contents] [FAQs] [Contribute] [CL: Command Line questions]

 

CL-04: How can I use shortcuts in the command line?

 

I am getting tired of trying to remember commands and syntax for frequently used commands. Is there a quick way to type commands?

 

Answer

 

Tabbing One quick way to remember or type a command is to type the first letter of the command and then press the [TAB] key. Linux will immediately present you with a list of available commands that match what you have already typed.

 

For example, you are trying to remember the urpmi command. You know it starts with ur something.

 

root / $ur[TAB]

urpme                  urpmi.addmedia         urpmiupdate

urpmf                  urpmi.removemedia      urpmi.update

urpmi                  urpmi_rpm-find-leaves  urpmq

root / $urpmi

 

You can also use the [TAB] function to complete file and path names.

 

Command History The command line terminal remembers the commands you have typed. It will remember up to a specified number of previously typed commands. You can use the up and down arrow keys to rapidly select a command you have already typed. Some other commands allow you to browse the command line history.

 

!!         - repeat last command

history       - list all commands stored in history

history 10  - list the last 10 commands stored in history

 

You can also use [control-r] to put the shell into reverse search mode. Press [control-r] and then type the first few letters of a command in history. The command you previously typed will appear.

 

Alias An alias is a shortcut that allows you to create a keyword that is associated with one or more commands. To see a list of pre-defined aliases, open a terminal and type alias. You can create your own alias and save them for permanent use.

 

For example, you would like to clear the terminal and then list all files/directories using the more command to control scrolling.

 

alias k='clear;ls -al | more'

 

Now you can type the letter 'k' at any time and the screen will clear, followed by ls -al | more. Each command must be separated by a semi-colon. You may also use an alias with a parameter.

 

For example: you would like to untar and gzip a file. Gotta look up the options each time? Try an alias instead.

 

alias targz='tar xzf '

 

Let's say you want to uncompress a file called myBackup.tar.gz

 

targz myBackup.tar.gz

 

Alias can also called other aliases already defined. To make your alias commands permanent, create a file such as alias.sh and store all your alias commands within. Place this file in /etc/profile.d and it will be automatically executed whenever you login or open a new command line terminal.

 

Functions Sometimes an alias is not enough. You have more than one parameter or the parameter does not fall at the end of the command and is instead in the middle. In this case, you can define a function.. The format for a function is

 

function fname() { command;command; }

 

where command is any command you would type into a terminal. To represent parameters use $1 for the first parameter, $2 for the second, and so on. You can use $* to represent only one parameter. E.g. ls $1 or cp $1 $2.

 

For example, you would like to make a compressed tar.gz of a file or folder.

function maketar() { tar -cvf $*.tar $*; gzip $*.tar;echo 'Created tar.gz'; }

 

In a terminal you can type

 

cd /

maketar /etc

 

This will create a backup of your /etc directory as etc.tar.gz.

 

Again, you can save this permanently by storing the function into your alias.sh file.

 

A more detailed description of shortcuts and command line tricks can be found at Using the Shell

 

[PREV: "CL-03: What is the size of a directory?"] [NEXT: "CL-05: How do I check if my CD/ISO is not corrupted (md5sum)?"]

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...