'Permission denied' when I cd into any folder
You probably where the victim of a prank, possibly done by your coworkers or someone who had access to your machine.
Bash functions can shadow the original shell built-in commands as you could see as you did type -a cd
. It has shown you that it first found a function which is possibly declared somewhere.
cd is a function
cd ()
{
echo "-bash: cd: $1: Permission denied"
}
cd is a shell builtin
From this output you can see that somewhere someone declared a shell function called cd
which shadows now the shell built-in command cd
. In some cases something like this might be useful to make commands more versatile or implement extra functionality, but in this case it was been done with ill intend (my guess). This could have been done in several places on the system:
/etc/bash.bashrc
/etc/profile
- any file in
/etc/profile.d/
/etc/environment
~/.bashrc
~/.profile
~/.bash_aliases
- any other file which gets executed or sourced from those files
One possible way to find this maybe is doing a recursive grep
search in /etc/
and in /home/
:
grep -r 'bash: cd: $1: Permission denied' /home/*
sudo grep -r 'bash: cd: $1: Permission denied' /etc/*
Which might yield an output like that:
$ grep -r 'bash: cd: $1: Permission denied' /home/*
/home/videonauth/.bashrc: echo "-bash: cd: $1: Permission denied"
In this example the line or function seems to be in ~/.bashrc
which you can see by the path leading the grep
output. Depending how sufficient the whole thing is hidden this might or might not yield a result. There are other ways to hide such a function declaration which may not be so easy to find.
Related videos on Youtube
Awais Chishti
Updated on September 18, 2022Comments
-
Awais Chishti over 1 year
Whenever I try
cd folder_name/
in bash on any directory it gives this error:-bash: cd: folder_name/: Permission denied
Execution permissions are given to all folders so that doesn't seem to be the problem. E.g. running stat on
Desktop/
outputs:File: 'Desktop/' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 807h/2055d Inode: 13107232 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 1000/caffeine) Gid: ( 1000/caffeine) Access: 2017-12-07 14:39:42.715820915 +0500 Modify: 2017-12-06 01:16:13.985722935 +0500 Change: 2017-12-06 01:16:13.985722935 +0500 Birth: -
Some other possibly relevant information:
- I can read or write files in any directory without any issue.
mkdir
executes normally but I can'tcd
into the new folder.rmdir
executes normally.- I can open a terminal in a particular folder by navigating to that folder and opening a terminal from it.
So can anyone tell what the problem can be?
Thanks in advance.
EDIT:
type -a cd
shows the following output:cd is a function cd () { echo "-bash: cd: $1: Permission denied" } cd is a shell builtin
-
muru over 6 yearsAdd the output of
type -a cd
to your post, please. -
Awais Chishti over 6 years@muru Just did. That function wasn't supposed to show up wasn't it?
-
John1024 over 6 yearsSo, who do you know who would play a practical joke on you like that?
-
George Udosen over 6 yearshahahahahaha, the friends we keep these days :-)
-
John1024 over 6 yearsRun
unset cd
and things should work. Next, rungrep 'Permission denied' ~/.bashrc ~/.bash_profile ~/.profile
and report what you find. -
Awais Chishti over 6 years@John1024 That fixed it. Though the grep didn't return any results. Thanks everyone!
-
George Udosen over 6 years@AwaisChishti you will need to find where that
cd function
is set else it will repeat -
George Udosen over 6 yearsRun that command for dir
/etc/profile
and/etc/bash.bashrc
-
Awais Chishti over 6 yearsRechecked! The guy actually calls another script within .bashrc that does that.
-
Awais Chishti over 6 yearsThe ones in /etc/ seem safe
-
muru over 6 yearsNow add the output of
PS4=' ${BASH_SOURCE}:$LINENO: ' bash -lixc true |& grep Permission
, please. -
Awais Chishti over 6 years@muru No output!
-
muru over 6 years@AwaisChishti in that case, do
PS4=' ${BASH_SOURCE}:$LINENO: '; shopt -s extdebug; set -x; declare -F cd
-
Videonauth over 6 yearsI retracted my close vote for the following reason: The OP uses 16.10, fine but this can happen on any Ubuntu version and I think other Users could benefit from this question to be answered.