Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
36
votes
5
answers
8242
views
How to tell if I'm actually in a symlink location from command line?
Suppose I have a folder: cd /home/cpm135/public_html and make a symbolic link ln -s /var/lib/class . Later, I'm in that directory: cd /home/cpm135/public_html/class The `pwd` is going to tell me I'm in `/home/cpm135/public_html/class` Is there any way to know that I'm "really" in `/var/lib/class` ?...
Suppose I have a folder:
cd /home/cpm135/public_html
and make a symbolic link
ln -s /var/lib/class .
Later, I'm in that directory:
cd /home/cpm135/public_html/class
The
pwd
is going to tell me I'm in /home/cpm135/public_html/class
Is there any way to know that I'm "really" in /var/lib/class
? Thanks
Oliver Williams
(1425 rep)
Dec 18, 2016, 12:16 PM
• Last activity: Jun 11, 2025, 09:27 PM
13
votes
4
answers
6140
views
Create history log per working directory in bash
I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in `/user/temp/1/` and typed a few commands, these commands would be saved in `/user/temp/1/.his` or something. I am using bash.
I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in
/user/temp/1/
and typed a few commands, these commands would be saved in /user/temp/1/.his
or something. I am using bash.
tafelplankje
(323 rep)
Aug 24, 2016, 06:37 PM
• Last activity: May 14, 2025, 02:33 PM
1
votes
3
answers
1133
views
tmux change default working directory of a session without attaching
I'm looking for a way to do something like this *without* attaching to the session. tmux attach-session -c -t ^^^^^^^^^^^^^^ Per `tmux(1)`, there isn't a way to change the default working directory (new windows and new panes) of an entire session without attaching to it. I cannot attach to the sessi...
I'm looking for a way to do something like this *without* attaching to the session.
tmux attach-session -c -t
^^^^^^^^^^^^^^
Per
tmux(1)
, there isn't a way to change the default working directory (new windows and new panes) of an entire session without attaching to it.
I cannot attach to the session because I'm doing this in some automated scripts where attaching would break the automation.
iBug
(3638 rep)
Feb 13, 2019, 10:39 AM
• Last activity: Apr 12, 2025, 07:49 PM
1
votes
1
answers
51
views
Is there a built-in way to copy a file from current directory to previous directory?
My pwd is `/long_path/lots/of/subs/waydeep/`. If I `cd -`, it will return me to `/lastdirectory/long_pathway/very_deep/`. Is there a shortcut terminal/cli command to copy `file1.ex` from the current working directory to the last working directory? (It might look like `cp file1.ex -/`.) If I moved fi...
My pwd is
/long_path/lots/of/subs/waydeep/
. If I cd -
, it will return me to /lastdirectory/long_pathway/very_deep/
.
Is there a shortcut terminal/cli command to copy file1.ex
from the current working directory to the last working directory? (It might look like cp file1.ex -/
.) If I moved files between these two frequently, I could create a soft link, but I want to find or make a more general solution.
If there is no built-in facility to do this, how could I add it -- let's say to my shell (bash)? I ask because the issue comes up often enough that it would be a useful thing to have ready.
Currently using linux Mint 23.1 and Fedora 40, but the question is general for most/any distribution, esp. using terminal (and possibly bash).
Old Uncle Ho
(139 rep)
Dec 13, 2024, 05:00 PM
• Last activity: Dec 13, 2024, 05:04 PM
-2
votes
1
answers
93
views
A directory showing up in itself
I do `cd ~` and then `pwd`. It shows `/root`. Then I do `ls`. It again shows `root`. This is NOT another directory with the same name because when I do `cd root`, it says `-bash: cd: root: Not a directory`. Somehow, I have messed up something. **UPDATE:** The output of `ls -l ~/root` is: ```none -rw...
I do
cd ~
and then pwd
. It shows /root
. Then I do ls
. It again shows root
. This is NOT another directory with the same name because when I do cd root
, it says -bash: cd: root: Not a directory
.
Somehow, I have messed up something.
**UPDATE:**
The output of ls -l ~/root
is:
-rw-r--r-- 1 root root 215040 Oct 15 10:01 /root/root
The output of ls -ld /root/root
is:
-rw-r--r-- 1 root root 215040 Oct 15 10:01 /root/root
The output of file /root/root
is:
/root/root: POSIX tar archive (GNU)
a_fan
(123 rep)
Oct 15, 2018, 10:16 AM
• Last activity: Nov 19, 2024, 11:07 AM
1
votes
1
answers
118
views
Bash `cd -L` vs. `cd -P` vs. Bash Reference manual description
**Premise** I've read what's listed in Bibliography regarding `cd`, `pwd`, `set -P`. > By default, or when the `-L` option is supplied, symbolic links in *directory* are resolved after `cd` processes an instance of ‘`..`’ in directory. (ref. Bash reference manual - sec. "[4 Shell Builtin Commands" -...
**Premise**
I've read what's listed in Bibliography regarding
cd
, pwd
, set -P
.
> By default, or when the -L
option is supplied, symbolic links in *directory* are resolved after cd
processes an instance of ‘..
’ in directory. (ref. Bash reference manual - sec. "[4 Shell Builtin Commands" - cd
paragraph])
> If ‘..
’ appears in *directory*, it is processed by removing the immediately preceding pathname component, back to a slash or the beginning of *directory*. (ibid)
My version of Bash is
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
**Problem**
To test things out I have the following file scheme in my current working directory that is the root of my experiment (dir111
is a symbolic link to dir010
).
dir010/
dir020/
dir110/dir111 -> ../dir010
my_dir="$PWD"
cd dir110/dir111 # 1
cd .. # 2
cd dir111 # 3
cd ../dir020 # 4
cd "$my_dir" # 5
cd dir110/dir111/../dir020 # 6
cd - # 7
cd dir110/dir111/../../dir020 # 8
1) sets the current working directory to "$my_dir"/dir110/dir111
, as expected[^1].
2) sets the current working directory to "$my_dir"/dir110
, as expected[^1].
3) back to 1.
4) sets the current working directory to "$my_dir"/dir020
, **unexpected**. I would expect cd
to fail at "$my_dir"/dir110/dir020
(see below for explanation).
5) back to the root of my experiment.
6) **same as 4**.
7) back to the root of my experiment.
8) sets the current working directory to "$my_dir"/dir020
, as expected[^1], but **in contrast with 6.**
Line 4 should fail for the reason in the following paragraph.
The reference manual and the man page (see quote in the Premise) both tell that ..
make cd
only remove the preceding pathname component, instead of looking at the ..
of the inode entry. So the parent directory of "$my_dir/dir110/dir111
as seen by cd
(in default behaviour) is "$my_dir/dir110
, which does not contain dir020
.
I thought that this behaviour may be an exception caused by ..
being the prefix of the «dirname»
provided to cd «dirname»
. However, using ..
as an infix (line 6) contrast with this.
cd
is behaving as if cd -P
has been executed (or set -P
(or similar) command issued before), that is by not following symbolic links. With cd -P
, at 1., the current working directory would be set to $my_dir/dir010
.
set -o
tells me
[omissis]
physical off
pipefail off
posix off
[omissis]
Is my (and the ones like me in other posted questions) interpretation of the reference manual correct? What am I missing? How can the behaviour of cd
be described?
**Bibliography**
+ [Bash man page] sec. "SHELL BUILTIN COMMANDS"
+ [Bash reference manual] sec. "4 Shell Builtin Commands"
+ [Changing parent directory (../) with symlinks]
+ [Path resolution depending on pwd (symlinked dirs)]
+ [How do I cd up and down again with symlinks in bash?]
the_eraser
(185 rep)
Jul 24, 2024, 04:27 PM
• Last activity: Jul 24, 2024, 05:21 PM
0
votes
4
answers
1340
views
How Can I Use the sed Command to Replace /home/user With ~
Suppose this is the output of `pwd`: ``` /home/amarakon/.local/src/amarakon/scripts/src ``` How can I change it to this: ``` ~/.local/src/amarakon/scripts/src ``` I tried this command: ``` pwd | sed 's|/home/.*|~|' ``` But this it the output: ``` ~ ```
Suppose this is the output of
pwd
:
/home/amarakon/.local/src/amarakon/scripts/src
How can I change it to this:
~/.local/src/amarakon/scripts/src
I tried this command:
pwd | sed 's|/home/.*|~|'
But this it the output:
~
Amarakon
(373 rep)
Jun 15, 2022, 01:28 AM
• Last activity: Mar 26, 2024, 04:30 AM
0
votes
1
answers
447
views
Open a new window in tmux only if no window is opened in the given path, otherwise attach to it
Currently I have my terminal (`alacritty`) run `tmux new -A -s 0` at start to start a new session `0`, or attach to it if it already exists. One problem is, I sometimes use open in directory from file manager (Dolphin). In that case, I am just reconnected to the older session and not in the desired...
Currently I have my terminal (
alacritty
) run tmux new -A -s 0
at start to start a new session 0
, or attach to it if it already exists. One problem is, I sometimes use open in directory from file manager (Dolphin). In that case, I am just reconnected to the older session and not in the desired directory.
What I want to achieve is to open a new window in the session 0
with my desired path as working directory if no such window exists in the given session. Otherwise, I want to switch to that existing window.
If that is not possible, I'm happy with just creating a new window every time. Maybe it can be detected if a process is running in the current active window, and if not, using cd
to switch the directory?
If anyone has any ideas on how to achieve this, it will be greatly appreciated.
Sntn
(123 rep)
Mar 12, 2024, 08:09 AM
• Last activity: Mar 13, 2024, 07:34 AM
0
votes
1
answers
212
views
Can I reprogram the pwd command to add a trailing slash?
WHen I use the `pwd` command, it prints e.g `/opt` instead of `/opt/`. I would like it to print the trailing slash. However, I tried adding the following line to my `~/.bash_aliases` file: ``` alias pwd=" echo "${PWD}/" " ``` But it doesn't work properly. Instead of printing my present working direc...
WHen I use the
pwd
command, it prints e.g /opt
instead of /opt/
. I would like it to print the trailing slash. However, I tried adding the following line to my ~/.bash_aliases
file:
alias pwd=" echo "${PWD}/" "
But it doesn't work properly. Instead of printing my present working directory with a trailing slash, it prints the working directory I was in **at the start of the terminal session**, or at the last time I ran the source ~/.bashrc
command. Even when I name the command something different, like cwd
, it still behaves this way.
So - my question is, can I have some way to have an alias that prints my present working directory, followed by a trailing slash?
Thanks.
K.defaoite
(153 rep)
Jan 18, 2024, 05:37 PM
• Last activity: Jan 18, 2024, 07:23 PM
3
votes
1
answers
2244
views
Interactive bash shell: Set working directory via commandline options
The short question is: I need to launch an interactive bash in a certain directory, and I can craft the command that launches bash, but I can't modify the system. At the moment, the command that best fits my needs is ``` bash -c 'cd some-directory; bash' ``` That is, I am calling bash, to use `cd` w...
The short question is: I need to launch an interactive bash in a certain directory, and I can craft the command that launches bash, but I can't modify the system. At the moment, the command that best fits my needs is
bash -c 'cd some-directory; bash'
That is, I am calling bash, to use cd
within bash, and then creating a child instance of bash within that to handle my interactive bashing.
I'm aware this isn't the most egregious bash usage in the world, but it feels weird to me. Is there an easier way to do what I'm looking for?
---
Just for unnecessary context:
This is a part of my tooling that deals with launching a shell within a docker instance (using docker exec) via docker-compose. I could find myself inside any of about 15 docker containers that represent projects - some of them have a "default" directory that I would want to be in, and others wouldn't. The tooling is meant to bridge the gap, and give me a smoother experience switching from one project to another.
Some things that I have tried that I thought might work but didn't include:
- PWD='~/some/directory' bash
- this just gets ignored
- bash -i -c "cd ~/some/directory
- it creates a shell marked as interactive, but doesn't wait for any input like a normal interactive shell.
- echo "cd ~/some/directory" | bash --rcfile -"
was worth a try
Some things that I know would work but don't in my case:
- Change the working directory then launch my command: I'm in docker, so my host's working directory doesn't matter. I'm intrigued as to how bash knows what the current working directory of a parent shell is.
- docker-compose
does have a -w
that will take an absolute path, doesn't know how to handle tildes.
- cd ~/some/directory ; bash
- docker-compose requires an actual executable
- Put it in an rcfile in the container, or create a "launcher" script: Kinda works, but I want to be able to configure it from the outside, and don't want to edit each of the containers
So is there a way to set the initial directory that I've overlooked, or is this just the way it's done? I know that shell integrations such as Gnome's "Open folder in terminal" must be doing something to tell bash what folder to start in, I can't work out what magic it is using though.
user208769
(231 rep)
Jan 4, 2024, 02:05 PM
• Last activity: Jan 4, 2024, 02:44 PM
46
votes
6
answers
13733
views
Make pwd result in terms of "~"?
`pwd` gives me /data/users/me/some/random/folder Is there an easy way of obtaining ~/some/random/folder from `pwd`?
pwd
gives me
/data/users/me/some/random/folder
Is there an easy way of obtaining
~/some/random/folder
from pwd
?
Sibbs Gambling
(1746 rep)
Jun 3, 2015, 06:16 AM
• Last activity: May 12, 2023, 03:20 PM
11
votes
1
answers
19718
views
What is the difference between cwd and pwd?
What is the difference between cwd and pwd? I've tried googling it, and one of the answers mentioned that depending on some factor (which I sadly do not remember), the implementation (the code I'm assuming) is not the same? I don't suppose this is like the difference between `print('x')` vs `return...
What is the difference between cwd and pwd?
I've tried googling it, and one of the answers mentioned that depending on some factor (which I sadly do not remember), the implementation (the code I'm assuming) is not the same? I don't suppose this is like the difference between
print('x')
vs return str(x)
(to use a Python analogy)?
account
(113 rep)
Jul 14, 2022, 09:35 PM
• Last activity: Mar 30, 2023, 12:57 PM
32
votes
1
answers
2677
views
How to get the cd shell-builtin to stop guessing?
For example: $ ls -aF ./ ../ bin/ $ cd tin # with a tee, not bee bin $ pwd /home/user/bin In other words, `cd` guesses that what I really meant was `cd bin`, and *successfully* (huh?) changes the current directory accordingly. I do not find this behavior documented in `man bash` or the [Bash Referen...
For example:
$ ls -aF
./ ../ bin/
$ cd tin # with a tee, not bee
bin
$ pwd
/home/user/bin
In other words,
cd
guesses that what I really meant was cd bin
, and *successfully* (huh?) changes the current directory accordingly. I do not find this behavior documented in man bash
or the [Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins) .
I would like Bash to produce an error, write something informative to standard error, _and leave the current directory unchanged_ if no directory matching the dir
argument (accounting for [expansion](https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions)) is found.
FYI,
$ type cd
cd is a shell builtin
$ ps -p $$
PID TTY TIME CMD
46959 pts/8 00:00:00 bash
$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ hostnamectl
Static hostname:
Icon name: computer-server
Chassis: server
Machine ID:
Boot ID:
Operating System: Red Hat Enterprise Linux
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
Kernel: Linux 3.10.0-1160.83.1.el7.x86_64
Architecture: x86-64
$ #DELETED: Static hostname, Machine ID, Boot ID
Ana Nimbus
(687 rep)
Feb 6, 2023, 06:42 PM
• Last activity: Feb 7, 2023, 05:15 PM
0
votes
1
answers
291
views
pwd add '\' in folder spaces
i know that on Linux when you try to `cd` with folder that have spaces you must add \ in there. for example, i have folder named `folder one`. when i try `pwd` the output is : ``` /home/user/folder one ``` what i want is when i try to `pwd` the output is : ``` /home/user/folder\ one ``` how to do th...
i know that on Linux when you try to
cd
with folder that have spaces you must add \ in there.
for example, i have folder named folder one
.
when i try pwd
the output is :
/home/user/folder one
what i want is when i try to pwd
the output is :
/home/user/folder\ one
how to do that?
Tzy
(1 rep)
Dec 9, 2022, 10:22 PM
• Last activity: Dec 11, 2022, 10:36 PM
2
votes
2
answers
1031
views
Get the Absolute Path from feh
The [feh][1] command allows you to view images within a folder recursively: feh --recursive --auto-zoom While viewing images, it also allows you to associate custom commands with keys 0-9 on your keyboard. For example, if I wanted the terminal to output the filename of the image I was viewing (to th...
The feh command allows you to view images within a folder recursively:
feh --recursive --auto-zoom
While viewing images, it also allows you to associate custom commands with keys 0-9 on your keyboard. For example, if I wanted the terminal to output the filename of the image I was viewing (to the terminal), I could make it do that by pressing the zero key while the image is being displayed by running
feh
with an --action
argument like this:
feh --recursive --auto-zoom --action "echo '%f'"
--action
binds the command echo '%f'
to the zero key. %f
is the relative path and looks like this when outputted ./filename.jpg
.
However, I need feh
to give me the absolute path instead of a relative path. So, I need to cut off that dot and then append what's left onto the pwd
.
This is my attempt to do that:
feh --recursive --auto-zoom --cache-size 2048 --action "echo $(pwd)$(echo '%f' | cut -c 2-)}"
but the output looks like this:
/absolute/pathf
(notice the 'f' on the end of the pwd)
How can I instead achieve an output like this? :
/absolute/path/filename.jpg
Lonnie Best
(5415 rep)
Jan 3, 2022, 11:10 AM
• Last activity: Nov 5, 2022, 09:17 PM
1
votes
1
answers
111
views
Enter directory without knowing its name?
Suppose we have a user `pepe` who has a directory `~/pepe_cant_see` in his home directory in which he possesses `-wx` permissions, and inside of this directory, another directory `~/pepe_cant_see/no_restrictions` in which pepe has `rwx` permissions. Then pepe will be able to `cd` into the `pepe_cant...
Suppose we have a user
pepe
who has a directory ~/pepe_cant_see
in his home directory in which he possesses -wx
permissions, and inside of this directory, another directory ~/pepe_cant_see/no_restrictions
in which pepe has rwx
permissions. Then pepe will be able to cd
into the pepe_cant_see
directory, but he won't be able to list its contents - so while in theory he can cd
into no_restrictions
, he wouldn't have any way of knowing that this directory exists or what its name is.
My question is: if I'm in a directory where I lack reading permissions, but there exist other files/directories inside of this directory for which I have full permissions, is there any way to detect their existence? For example, is there a command that will allow me to enter a random/arbitrary directory from my CWD or edit a random/arbitrary preexisting file?
(Note: this is purely out of curiosity, I have no reason to think this kind of thing would ever happen in practice.)
Franklin Pezzuti Dyer
(111 rep)
Oct 23, 2022, 12:51 PM
• Last activity: Oct 23, 2022, 01:41 PM
15
votes
1
answers
2712
views
Why did 'scp 10.0.0.11:/home/someuser/.*' start copying from /home as well?
I'm doing my first ever moving of user files from an old system to a new system. My goal is to use SCP for it's simple syntax of `scp -r source destination`. I tried the following command to copy the files first: ``` scp -r root@10.0.0.11:/home/someuser/* . ``` In retrospect, and from past experienc...
I'm doing my first ever moving of user files from an old system to a new system. My goal is to use SCP for it's simple syntax of
scp -r source destination
. I tried the following command to copy the files first:
scp -r root@10.0.0.11:/home/someuser/* .
In retrospect, and from past experience, this copied all files without a leading .
. In my attempt to fix this, I did this:
scp -r root@10.0.0.11:/home/someuser/.* .
meaning wildcard for anything starting with a .
. Obviously (why I'm asking the question) it didn't do what I wanted. The observed result, was that it interpreted the .
as moving up a level in the path, and it started copying /home/*
instead, also (I think) placing the files one level up from my working directory, rather than the working directory itself.
Is my interpretation of the execution of the second command correct? I think it was easy to fix since I was in ~/backup
, so one level up was ~
. I just rm -rf ~/someuser
on each username that had copied before interrupted the command. Those someuser
directories were supposed to be in ~/backup
I have since learned how to copy the files I wanted by specifying the directory only, not the files contained in the directory.
Brian
(292 rep)
Sep 25, 2022, 03:51 PM
• Last activity: Sep 26, 2022, 07:51 PM
4
votes
2
answers
6059
views
How to change the tmux working directory with a shell command
I want to create a *shell command* that, when run, sets the working directory of the *current tmux session* to the *current working directory*. There are numerous answered questions on how to set the session working directory; the answer seems to be to run `attach-session -t . -c /path/to/directory`...
I want to create a *shell command* that, when run, sets the working directory of the *current tmux session* to the *current working directory*.
There are numerous answered questions on how to set the session working directory; the answer seems to be to run
attach-session -t . -c /path/to/directory
in the tmux command prompt. Under my (default) configuration, that'd be C-b : attach-session -t . -c /path/to/directory
.
I don't want to manually interact with the tmux command prompt; I want a simple command, mostly because I don't want to deal with typing out or copy-pasting the target directory. I know it's possible to send commands to tmux from a shell prompt; for instance, I can run tmux display-message MESSAGE
at the shell instead of C-b : display-message HELLO
to show a message in the status bar. However, running tmux attach-session -t (tmux display-message -p '#S') -c (pwd)
throws an error: "sessions should be nested with care, unset $TMUX to force". Of course, I don't have any desire to nest a tmux session, I only want to change the directory of the *current* session.
I've also tried using tmux send-keys
and tmux-send-prefix
, but these seem to forward the key sequences to the underlying tty (where the shell responds to them) rather than directly to tmux.
I'm using the fish shell, but I'm assuming the solution would fit in a shell script, so I'm happy with a solution in any language.
Lucretiel
(212 rep)
Nov 11, 2020, 09:10 PM
• Last activity: Jun 20, 2022, 12:04 PM
1
votes
1
answers
661
views
Display recursively all parent directories relative to the current one
Display recursively all parent directories relative to the current one. #!/bin/bash IFS=/ for var in $(pwd) do echo "$var" done
Display recursively all parent directories relative to the current one.
#!/bin/bash
IFS=/
for var in $(pwd)
do
echo "$var"
done
Fluffy
(7 rep)
Mar 31, 2022, 07:04 PM
• Last activity: Mar 31, 2022, 08:34 PM
0
votes
1
answers
46
views
How to list contents my present directory but only show the absolute file path and name?
I have an alias: ``` alias lp=find $(pwd) ``` I would like this to be similar to ls and ll (list path) but this command searches through every directory instead of my present working directory. I would mostly pipe this with grep but for now, I just want to get the command to display what I want. Any...
I have an alias:
alias lp=find $(pwd)
I would like this to be similar to ls and ll (list path) but this command searches through every directory instead of my present working directory.
I would mostly pipe this with grep but for now, I just want to get the command to display what I want.
Any help is greatly appreciated!
echo_LOGNAME
(1 rep)
Jul 29, 2021, 07:40 PM
• Last activity: Jul 29, 2021, 07:57 PM
Showing page 1 of 20 total questions