Pushd and popd

Last updated
pushd & popd
Original author(s) Bill Joy
Developer(s) Various open-source and commercial developers
Operating system Unix, Unix-like, DOS, Windows, ReactOS
Platform Cross-platform
Type Command

In computing, pushd and popd are a pair of commands which allow users to quickly switch between the current and previous directory when using the command line. When called, they use a directory stack to sequentially save and retrieve directories visited by the user. [1] [2]

Contents

They are widely available as builtin commands in many command-line interpreters, such as 4DOS, Bash, [3] C shell, tcsh, Hamilton C shell, KornShell, cmd.exe and PowerShell, and for various operating systems including both Windows and Unix-like systems.

Overview

A representation of a stack. Elements are always 'pushed' to and 'popped' from the top. Data stack.svg
A representation of a stack. Elements are always 'pushed' to and 'popped' from the top.

The directory stack underlies the functions of these two commands. It is an array of paths stored as an environment variable in the CLI, which can be viewed using the command dirs in Unix or Get-Location -stack in PowerShell. The current working directory is always at the top of the stack.

The pushd ('push directory') command saves the current working directory to the stack then changes the working directory to the new path input by the user. If pushd is not provided with a path argument, it changes instead to the next directory from the top of the stack,[ clarification needed ] which can be used to toggle between two directories.

The popd command removes (or 'pops', in the stack analogy) the current path entry from the stack and returns to the path at the top of the stack as the new working directory. [4] [5]

The first Unix shell to implement a directory stack was Bill Joy's C shell.[ citation needed ] The syntax for pushing and popping directories is essentially the same as that used now. [6] [7]

Both commands are available in FreeCOM, the command-line interface of FreeDOS. [8]

In Windows PowerShell, pushd is a predefined command alias for the Push-Location cmdlet and popd is a predefined command alias for the Pop-Location cmdlet. Both serve basically the same purpose as the pushd and popd commands.

Examples

Unix-like

[user@server /usr/ports] $ pushd/etc /etc /usr/ports[user@server /etc] $ popd/usr/ports[user@server /usr/ports] $

Microsoft Windows and ReactOS

C:\Users\root>pushd C:\Users C:\Users>popdC:\Users\root>

CMD batch file

@echo off rem This batch file deletes all .txt files in a specified directorypushd%1del *.txt popdecho All text files deleted in the %1 directory 

Syntax

pushd

pushd [path | ..]

Arguments:

popd

popd

See also

Related Research Articles

cd (command) Computer command in various operating systems

The cd command, also known as chdir, is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with the process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd function, or just current directory. When a process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the working directory of the process. So for example a process with working directory /rabbit-shoes that asks to create the file foo.txt will end up creating the file /rabbit-shoes/foo.txt.

pwd Directory information command on various operating systems

In Unix-like and some other operating systems, the pwd command writes the full pathname of the current working directory to the standard output.

In computing, a command is a directive to a computer program to perform a specific task. It may be issued via a command-line interface or as input to a network service as part of a network protocol, or as an event triggered in a graphical user interface.

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter. Paths are used extensively in computer science to represent the directory/file relationships common in modern operating systems and are essential in the construction of Uniform Resource Locators (URLs). Resources can be represented by either absolute or relative paths.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard and *.txt is a glob pattern. The wildcard * stands for "any string of any length including empty, but excluding the path separator characters ".

cmd.exe Command prompt program

Command Prompt, also known as cmd.exe or cmd, is the default command-line interpreter for the OS/2, eComStation, ArcaOS, Microsoft Windows, and ReactOS operating systems. On Windows CE .NET 4.2, Windows CE 5.0 and Windows Embedded CE 6.0 it is referred to as the Command Processor Shell. Its implementations differ between operating systems, but the behavior and basic set of commands are consistent. cmd.exe is the counterpart of COMMAND.COM in DOS and Windows 9x systems, and analogous to the Unix shells used on Unix-like systems. The initial version of cmd.exe for Windows NT was developed by Therese Stowell. Windows CE 2.11 was the first embedded Windows release to support a console and a Windows CE version of cmd.exe. The ReactOS implementation of cmd.exe is derived from FreeCOM, the FreeDOS command line interpreter.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

alias (command) Command in various command line interpreters

In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. alias is available in Unix shells, AmigaDOS, 4DOS/4NT, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term "batch" is from batch processing, meaning "non-interactive execution", though a batch file might not process a batch of multiple data.

move (command)

In computing, move is a command in various command-line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS/4NT, and PowerShell. It is used to move one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. The command is analogous to the Unix mv command and to the OpenVOS move_file and move_dircommands.

find (Windows)

In computing, find is a command in the command-line interpreters (shells) of a number of operating systems. It is used to search for a specific text string in a file or files. The command sends the specified lines to the standard output device.

help (command) Command in various command line shells

In computing, help is a command in various command line shells such as COMMAND.COM, cmd.exe, Bash, qshell, 4DOS/4NT, Windows PowerShell, Singularity shell, Python, MATLAB and GNU Octave. It provides online information about available commands and the shell environment.

PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

title (command) Command of command line interpreters

In computing, title is a command in various command-line interpreters (shells) on Microsoft Windows and ReactOS that changes the title for the graphical terminal emulator window. The command is also used within DFS and ADFS to change the title of the disc in the current drive.

start (command) Shell command

In computing, start is a command of the IBM OS/2, Microsoft Windows and ReactOS command-line interpreter cmd.exe to start programs or batch files or to open files or directories using the default program. start is not available as a standalone program. The underlying Win32 API is ShellExecute.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a computer program by inputting lines of text called command-lines. Command-line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user-friendly alternative to the non-interactive interface available with punched cards.

In computing, dpath is an internal cmd.exe command on IBM OS/2 and Microsoft Windows that allows using a set of files with the TYPE command and with input redirection as if they are in the current directory. On Windows it is undocumented and deprecated. dpath differs from the append command in the way it operates. dpath informs programs what directories they should search in order to find computer files. It is then up to the applications to recognize %DPATH%. Using the append command on the other side, programs are able to find files without recognizing that the command is in effect.

References

  1. Pushd - change directory/folder - Windows CMD - SS64.com
  2. Popd - Windows CMD - SS64.com
  3. Bash Reference Manual: Directory Stack Builtins
  4. Microsoft TechNet Pushd article
  5. Microsoft TechNet Popd article
  6. Chapter 14 – 14.6 The Shells' pushd and popd Commands
  7. man tcsh "TCSH(1)". Archived from the original on 2014-03-10. Retrieved 2014-11-03.
  8. FreeCOM - FreeDOS

Further reading