Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Sunday, June 5, 2011

UNIX Command History

All the commands typed by the user are stored in a file named ".bash_history" (for bash) in the Home directory of the User. It is a hidden file as its name is preceded by a dot (.) and therefore you can view this  in the File manager only when you hit the Ctrl-H combination. To view this file in "bash" the option -a has to be used with the ls command, i.e., to view this file in the command line interface, the command ls -a has to be used.

This file contains the list of all the commands typed since the system formatting. The operations on the command history such as retrieval or reuse of the commands depends on the contents of this file.

Type history at the command prompt and you will get the last 500 commands used. If you only want to look at the last 10 commands, you could type the command history 10. Commands are remembered so you can reuse them.



You can reuse any command in the history list by typing its number (as shown by the history command), preceded by an exclamation mark (!). If you want a view a file listing of the Documents folder, and ls Documents is command #31 in the list, you could type !31.




Say you’ve just typed a long file-name copy (cp) command, and want to use it again. Typing !cp will find the last line in your history list that begins with cp and reuse it.



Typing two bangs (!!) will cause the last command you typed to be reused. Finally, typing !? and then part of a recently-typed command will cause bash to auto-complete using the nearest match it can find in the history.


Monday, January 31, 2011

Linux -- Directory Stack

I am just surprised at the way Data Structures are used in our day-today life not just the computers field. Now, its time to discuss the directory stack. The basic idea of stack is also applied to the directory stack, a list of recently-visited directories. Like any other stack, the directory stack also allows us to view the contents of stack, push an element into the stack, pop an element from the stack. But the difference is that the insertion and deletion operations can be done on any element in the stack, not just the top element of the stack.

Push operation is carried upon by the pushd command while Pop operation is carried upon by the popd command. These two operations can quickly navigate around the file-system. If the pop operation is issued when the stack is empty, the appropriate message is issued. The cd command changes the current element at the top of the stack i.e., the $DIRSTACK variable. The dirs builtin displays the contents of the directory stack. The contents of the directory stack are also visible as the value of the DIRSTACK shell variable.

In short, the pushd builtin adds directories to the stack as it changes the current directory, and the popd builtin removes specified directories from the stack and changes the current directory to the directory removed.

The pushd command is similar to the cd command, but pushd retains the directory that you are currently in, in its directory-stack. For example, the command pushd /usr/share displays the entire directory-stack. It knows that I came to /usr/share from the home directory. If I issue popd, this is going to take my directory-stack and bring me back to the previous directory in the directory-stack. The command saves the current directory on the top of the directory stack and then cd to dir. With no arguments, pushd exchanges the top two directories.

The popd command removes the top entry from the directory stack, and cd to the new top directory. When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory.

The basic behavior of pushd, popd and dirs are shown in the figure below:



The DIRSTACK array needs some explanation at this point in time. The contents of the array can be displayed by the command "echo ${DIRSTACK[*]}". If only the second element in the array needs to be printed, then "echo ${DIRSTACK[1]}" as the arrays start with index as 0. The DIRSTACK array is shown in the below figure:




Variations of dirs:
dirs [+N | -N] [-clvp]
+N: Displays the Nth directory (counting from the left of the list printed by dirs when invoked without options), starting with zero.
-N: Displays the Nth directory (counting from the right of the list printed by dirs when invoked without options), starting with zero.
-c: Clears the directory stack by deleting all of the elements.
-l:  Produces a longer listing; the default listing format uses a tilde to denote the home directory.
-p: Causes dirs to print the directory stack with one entry per line.
-v: Causes dirs to print the directory stack with one entry per line, prefixing each entry with its index in the stack.

The variations of the dirs command are shown in the below figure:


Variations of pushd:
pushd [dir | +N | -N]
+N: Brings the Nth directory (counting from the left of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
-N: Brings the Nth directory (counting from the right of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
dir: Makes the current working directory be the top of the stack, and then executes the equivalent of `cd dir'.

Variations of popd:
popd [+N | -N] [-n]
+N: Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
-N: Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
-n: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.

The variations of pushd and popd are shown in the figure below:




Sources: Bash Reference
Related Posts Plugin for WordPress, Blogger...