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]
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-Locationcmdlet and popd is a predefined command alias for the Pop-Location cmdlet. Both serve basically the same purpose as the pushd and popd commands.
@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:
path This optional command-line argument specifies the directory to make the current directory. If path is omitted, the path at the top of the directory stack is used, which has the effect of toggling between two directories.