Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
0
answers
49
views
pushd within a pipeline says it's adding to the stack, but it's not?
I'm working on a modification of [this approach](https://superuser.com/a/608622) to automatically saving and restoring bash's directory stack state when using `pushd` and `popd`. For some reason, my code to restore the saved state isn't working -- I'm calling `pushd` on the right entries, and it say...
I'm working on a modification of [this approach](https://superuser.com/a/608622) to automatically saving and restoring bash's directory stack state when using
pushd
and popd
.
For some reason, my code to restore the saved state isn't working -- I'm calling pushd
on the right entries, and it says it's pushing those onto the stack, but afterwards, dirs -v
is unchanged!
I have some directories, one per line, in a file, and I'm using this to recreate the stack:
tac $BASH_DIR_STACK_FILE | sort | uniq | while read -r dir
do
echo $dir
pushd -n "$dir" > /dev/null
done
When I run that, it works as expected, and claims to be pushing each of the directories. But afterwards, when I do dirs -v
, there's no change to the stack!
Using pushd
manually for the same directories works as expected.
I also tried the declare
approach with $DIRSTACK
from [this post](https://unix.stackexchange.com/a/366852/632891) but that doesn't work either.
Why isn't my directory stack getting updated via pushd
inside that loop?
Dan Drake
(151 rep)
Aug 3, 2024, 01:51 PM
• Last activity: Aug 3, 2024, 04:43 PM
1
votes
1
answers
44
views
`pushd` inside a while loop from file contents does not behave the same depending on read method
I'm pushing directories to my stack using a while loop that reads the contents of a file. I've tried two approaches that should be equivalent, but they behave different. # Approach 1 ``` bash export DIRSTACK_SAVEFILE="/path/to/file" # file contains full paths to folders in each line while read line...
I'm pushing directories to my stack using a while loop that reads the contents of a file. I've tried two approaches that should be equivalent, but they behave different.
# Approach 1
bash
export DIRSTACK_SAVEFILE="/path/to/file"
# file contains full paths to folders in each line
while read line ; do
pushd -n "$line"
done < <(tac $DIRSTACK_SAVEFILE)
Using this, I can later use dir
and pushd +n
and all the folders are loaded into the stack.
# Approach 2
bash
export DIRSTACK_SAVEFILE="/path/to/file"
# file contains full paths to folders in each line
tac $DIRSTACK_SAVEFILE | while read line ; do
pushd -n "$line"
done
After executing this approach, the directory stack in my shell has no new folders.
# Question
Why only the 1st approach changes the directory stack in my shell?
I've searched to understand how process substitution and pipe work. I think I understand what I read [here](https://unix.stackexchange.com/a/17117) about process substitution but I haven't found any explanation about pipe that helps me understand this behavior.
PD.: I'm using GNU bash, version 5.1.8(1)-release (x86_64-redhat-linux-gnu)
Jorge Ricardo Alonso Fernández
(13 rep)
Jun 17, 2024, 09:26 AM
• Last activity: Jun 17, 2024, 09:44 AM
62
votes
15
answers
20970
views
Do we have more history for cd?
`cd -` can move to the last visited directory. Can we visit more history other than the last one?
cd -
can move to the last visited directory. Can we visit more history other than the last one?
Tim
(106430 rep)
Sep 26, 2014, 04:07 PM
• Last activity: Jun 27, 2023, 08:33 PM
2
votes
3
answers
480
views
Confusing pushd/popd behaviour when sourcing script in zsh
I have a script which I source while in bash. It does various things and retains the `$PWD` I was in before sourcing it. pushd ~/.dotfiles >/dev/null || exit 1 # Do various things popd >/dev/null || exit 1 The script runs (mostly) fine in zsh too, but when I source it from the `~/.dotfiles` location...
I have a script which I source while in bash. It does various things and retains the
$PWD
I was in before sourcing it.
pushd ~/.dotfiles >/dev/null || exit 1
# Do various things
popd >/dev/null || exit 1
The script runs (mostly) fine in zsh too, but when I source it from the ~/.dotfiles
location, I end up in the previous $OLDPWD
after sourcing it.
It seems that zsh disregards the pushd
line if the current location is already the same, so the popd
command goes to the $OLDPWD
from before when the script was sourced.
Is there a way of stopping zsh from ignoring the "redundant" pushd
command, while also keeping the script compatible with bash?
I do have the following in my .zshrc
, but it also happens when I unset them:
setopt AUTO_PUSHD PUSHD_SILENT PUSHD_IGNORE_DUPS
paradroid
(1245 rep)
May 15, 2023, 10:28 PM
• Last activity: May 16, 2023, 10:50 AM
493
votes
11
answers
514286
views
How do I use pushd and popd commands?
What are the practical uses of both `pushd` and `popd` when there is an advantage of using these two commands over `cd` and `cd -`? **EDIT**: I'm looking for some practical examples of uses for both of these commands or reasons for keeping stack with directories (when you have tab completion, `cd -`...
What are the practical uses of both
pushd
and popd
when there is an advantage of using these two commands over cd
and cd -
?
**EDIT**: I'm looking for some practical examples of uses for both of these commands or reasons for keeping stack with directories (when you have tab completion, cd -
, aliases for shortening cd ..
, etc.).
syntagma
(12801 rep)
May 25, 2013, 05:41 PM
• Last activity: Feb 21, 2023, 09:36 AM
0
votes
1
answers
101
views
how to "execute command on file in multiple subdirectories within containing folder" i.e cd into each subdirectory containing file and execute command
I have a file named filename.sh in different directories. I am trying to simultaneously submit this file to run on a number of cluster nodes. I need to find this file wherever they may be, cd into their containing folder and execute the command "sbatch run_code.sh" This is what I have tried. find ....
I have a file named filename.sh in different directories. I am trying to simultaneously submit this file to run on a number of cluster nodes. I need to find this file wherever they may be, cd into their containing folder and execute the command "sbatch run_code.sh" This is what I have tried.
find . -mindepth 1 -type f -name filename.sh -exec sbatch run_code.sh {} \;
And it works except it runs command from current folder which does not contain dependencies needed to run the code.
alternatively I am leaning towards a for loop like this
for d in dirname; do
cd "$(dirname "$(find . -type f -name filename.sh)")" && sbatch run_code.sh)
done
which does not work. I look forward to getting some help here. Please.
Daniel Ajuzie
(3 rep)
Mar 30, 2022, 04:33 PM
• Last activity: Mar 30, 2022, 07:08 PM
2
votes
2
answers
397
views
What is the zsh equivalent of "pushd -n" in bash?
I want to push a directory onto the directory stack in order to refer to it using "tilde shorthand" (eg. `~1` refers to the second entry in the directory list), but I don't want to actually switch to the directory. In bash, it seems this can be done using the `-n` flag to `pushd`. What's the equival...
I want to push a directory onto the directory stack in order to refer to it using "tilde shorthand" (eg.
~1
refers to the second entry in the directory list), but I don't want to actually switch to the directory. In bash, it seems this can be done using the -n
flag to pushd
.
What's the equivalent in zsh?
chb
(699 rep)
Nov 27, 2021, 01:40 AM
• Last activity: Nov 27, 2021, 07:05 PM
1
votes
1
answers
78
views
Getting tcsh dextract-like pushd functionality in bash
I love `bash` (4.1.2(1)-release) but I strongly prefer the way `tcsh` implements `pushd +N` with the `dextract` option enabled, so much so that I refuse to use `bash` as my default shell because of it. Has anyone implemented a `dextract`-like directory stack set of commands in `bash` or maybe I have...
I love
bash
(4.1.2(1)-release) but I strongly prefer the way tcsh
implements pushd +N
with the dextract
option enabled, so much so that I refuse to use bash
as my default shell because of it. Has anyone implemented a dextract
-like directory stack set of commands in bash
or maybe I haven't figured out how to coax bash
to behave like tcsh
with dextract
enabled?
The sticking point: The order that bash
leaves the directory stack in after a pushd +N
. For example:
$ cd /tmp
$ pushd a
/tmp/a /tmp
$ pushd ../b
/tmp/b /tmp/a /tmp
$ pushd ../c
/tmp/c /tmp/b /tmp/a /tmp
$ pushd +1
/tmp/b /tmp/a /tmp /tmp/c
Why does bash
(and default tcsh
) rotate the positions of all the other directories when I do a pushd +1
? Why is this useful? (Perhaps if someone explained, I might appreciate the behavior and get used to it.) Compare to tcsh
with dextract
, which just extracts it and puts it on top:
% set dextract
% cd /tmp
% pushd a
/tmp/a /tmp
% pushd ../b
/tmp/b /tmp/a /tmp
% pushd ../c
/tmp/c /tmp/b /tmp/a /tmp
% pushd +1
/tmp/b /tmp/c /tmp/a /tmp
Note that the remaining order of the directories is untouched. I consider this important because it's easier to keep track in my mind when the order doesn't get rotated, and so I don't have to be always doing a dirs
and searching for the directory I want.
If I were to take a crack at it, where would I start? I see there is a variable DIRSTACK
, but it is not correct (it's empty when the stack has four entries), although changing it does alter the stack (not correctly though).
Vercingatorix
(799 rep)
Apr 4, 2020, 06:31 PM
• Last activity: Apr 12, 2020, 09:38 PM
6
votes
1
answers
832
views
How is the pushd directory stack stored?
In a related question [somebody states](https://unix.stackexchange.com/a/556729/202178) that the directory stack of the `pushd` command is emptied when your shell terminates. But _how_ is the stack actually stored? I use [fish](https://fishshell.com/) instead of bash and the commands work the same w...
In a related question [somebody states](https://unix.stackexchange.com/a/556729/202178) that the directory stack of the
pushd
command is emptied when your shell terminates. But _how_ is the stack actually stored? I use [fish](https://fishshell.com/) instead of bash and the commands work the same way. I would assume pushd
(and popd
) works independently of the shell you're using. Or do both shells have their own implementation?
jallersma
(65 rep)
Jan 7, 2020, 09:39 AM
• Last activity: Jan 7, 2020, 04:56 PM
2
votes
2
answers
2465
views
Directory stack (pushd and popd) behaviour in bash scripts
If I poorly write a script that uses `pushd /etc` but I don't finish it with `popd`: Will `/etc` still be in the `pushd`+`popd` directory stack/in RAM after the `bash` script has finished executing and Bash has terminated? Also does the `pushd`+`popd` directory stack clear (like variables do) if I c...
If I poorly write a script that uses
pushd /etc
but I don't finish it with popd
: Will /etc
still be in the pushd
+popd
directory stack/in RAM after the bash
script has finished executing and Bash has terminated?
Also does the pushd
+popd
directory stack clear (like variables do) if I close the terminal emulator session (which should terminate the bash
shell process for that terminal emulator session) without manually clearing it with popd
?
Bean6754
(21 rep)
Dec 11, 2019, 02:44 PM
• Last activity: Dec 11, 2019, 04:28 PM
0
votes
1
answers
106
views
$@ in alias inside script: is there a "local" $@?
I have aliased `pushd` in my bash shell as follows so that it suppresses output: alias pushd='pushd "$@" > /dev/null' This works fine most of the time, but I'm running into trouble now using it inside functions that take arguments. For example, test() { pushd . ... } Running `test` without arguments...
I have aliased
pushd
in my bash shell as follows so that it suppresses output:
alias pushd='pushd "$@" > /dev/null'
This works fine most of the time, but I'm running into trouble now using it inside functions that take arguments. For example,
test() {
pushd .
...
}
Running test
without arguments is fine. But with arguments:
> test x y z
bash: pushd: too many arguments
I take it that pushd
is trying to take . x y z
as arguments instead of just .
. How can I prevent this? Is there a "local" equivalent of $@
that would only see .
and not x y z
?
Théophile
(103 rep)
Aug 15, 2019, 06:41 PM
• Last activity: Aug 15, 2019, 07:36 PM
93
votes
3
answers
112806
views
Difference between "cd -" and "cd ~-"
The Bash command cd - prints the previously used directory and changes to it. On the other hand, the Bash command cd ~- directly changes to the previously used directory, without echoing anything. Is that the only difference? What is the use case for each of the commands?
The Bash command
cd -
prints the previously used directory and changes to it.
On the other hand, the Bash command
cd ~-
directly changes to the previously used directory, without echoing anything.
Is that the only difference? What is the use case for each of the commands?
dr_
(32078 rep)
Dec 16, 2016, 03:22 PM
• Last activity: Jun 27, 2019, 06:46 PM
7
votes
1
answers
1297
views
Making 'pushd' directory stack persistent
Ok this is a short question. I just happened to know that with `pushd` command, we can add more working directories into our list, which is handy. But is there a way to make this list permanent, so it can survive reboots or logoffs?
Ok this is a short question. I just happened to know that with
pushd
command, we can add more working directories into our list, which is handy. But is there a way to make this list permanent, so it can survive reboots or logoffs?
Sollosa
(1993 rep)
May 16, 2017, 08:48 AM
• Last activity: May 26, 2019, 08:45 AM
3
votes
1
answers
101
views
Can I rearrange the directories in `dirs` in the order of their most recent visits?
What is the order for the pathnames of the directories stored in the stack shown by `dirs -l`? Are they ordered by their last `pushd` commands? Is it possible to order them so that they are ordered by their most recent visit times (a natural extension of `$OLDPATH`)?
What is the order for the pathnames of the directories stored in the stack shown by
dirs -l
?
Are they ordered by their last pushd
commands?
Is it possible to order them so that they are ordered by their most recent visit times (a natural extension of $OLDPATH
)?
Tim
(106430 rep)
Mar 20, 2019, 01:13 PM
• Last activity: Mar 20, 2019, 02:16 PM
2
votes
2
answers
3674
views
Difference between pushd/popd and sub-shell+cd
I'm trying to understand if there are any benefits of using: pushd my_dir make all # ... or something else popd vs ( cd my_dir make all # ... or something else ) or is it merely a preference thing? I guess the latter notation can have issues like you may need to `set -e` (and other flags again), but...
I'm trying to understand if there are any benefits of using:
pushd my_dir
make all # ... or something else
popd
vs
(
cd my_dir
make all # ... or something else
)
or is it merely a preference thing?
I guess the latter notation can have issues like you may need to
set -e
(and other flags again), but it still carries out the exit code of its last command, and looks better in terms of syntax.
ahmet alp balkan
(741 rep)
May 25, 2018, 05:14 PM
• Last activity: Jan 4, 2019, 09:10 AM
12
votes
3
answers
4185
views
How can I view the stack used by `pushd` and `popd`?
I would like to use the recently accessed directories list for logging purposes. Is the directory stack as used by `pushd` and `popd` stored somewhere, perhaps as a list of folders in a text file? If so, where?
I would like to use the recently accessed directories list for logging purposes.
Is the directory stack as used by
pushd
and popd
stored somewhere, perhaps as a list of folders in a text file? If so, where?
B.Kocis
(277 rep)
Dec 22, 2015, 09:15 AM
• Last activity: Nov 15, 2018, 11:24 PM
-4
votes
2
answers
852
views
Why does `pushd` always output the stack to stdout?
`pushd` push a directory into the directory stack, and change the working directory. I guess that is all we need. But besides, why does `pushd` always output the stack to stdout? I think that is unnecessary and add clutter to the screen, or I miss its point. Thanks.
pushd
push a directory into the directory stack, and change the working directory. I guess that is all we need.
But besides, why does pushd
always output the stack to stdout? I think that is unnecessary and add clutter to the screen, or I miss its point. Thanks.
Tim
(106430 rep)
Nov 8, 2018, 03:03 AM
• Last activity: Nov 8, 2018, 04:22 PM
1
votes
1
answers
3245
views
-ksh: pushd: not found
I am trying to used pushd, popd and dirs in SUSE Linux but I am getting -ksh: pushd: not found while I try to check pushd which pushd which: no pushd in (/usr/bin:/bin:/usr/sbin:/sbin) I am getting there is not pushd.
I am trying to used pushd, popd and dirs in SUSE Linux but I am getting
-ksh: pushd: not found
while I try to check pushd
which pushd
which: no pushd in (/usr/bin:/bin:/usr/sbin:/sbin)
I am getting there is not pushd.
Amrit Dhungana
(111 rep)
Jan 5, 2015, 03:44 AM
• Last activity: Oct 12, 2018, 09:21 AM
2
votes
1
answers
1675
views
Why can't I which pushd
I've been using `pushd` and `popd` for a long time while writing bash script. But today when I execute `which pushd`, I get nothing as output. I can't understand this at all. I was always thinking that `pushd` is simply a command, just like `cd`, `ls` etc. So why does `which pushd` give me nothing?
I've been using
pushd
and popd
for a long time while writing bash script. But today when I execute which pushd
, I get nothing as output. I can't understand this at all. I was always thinking that pushd
is simply a command, just like cd
, ls
etc.
So why does which pushd
give me nothing?
Yves
(3401 rep)
Aug 8, 2018, 01:12 AM
• Last activity: Aug 8, 2018, 01:40 AM
3
votes
2
answers
699
views
How can I popd to a specific directory?
I have used `pushd` to move about to various directories and now if I run dirs -v I get: 0 ~/Desktop 1 /etc 2 /var/log 3 ~/Downloads 4 /tmp How can I `popd` to a specific directory in the middle of the stack?, e.g `option 2: /var/log` `man bash` says > +n Removes the nth entry counting from the left...
I have used
pushd
to move about to various directories and now if I run
dirs -v
I get:
0 ~/Desktop
1 /etc
2 /var/log
3 ~/Downloads
4 /tmp
How can I popd
to a specific directory in the middle of the stack?, e.g option 2: /var/log
man bash
says
> +n Removes the nth entry counting from the left of the list shown
> by dirs, starting with zero. For example: \``popd +0'' removes the
> first directory, ``popd +1'' the second.
I've tried
* ``popd +0''
* popd +3
And it pops the correct directory off the stack, but doesn't change the current working directory.
How can I popd
the particular directory and change the current working directory to the "popped" dir?
the_velour_fog
(12770 rep)
Apr 12, 2015, 04:59 PM
• Last activity: Jun 19, 2018, 01:19 PM
Showing page 1 of 20 total questions