Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I tried to use sudo cd name_of_dir but getting following error message: sudo: cd: command not found.

Is there any other way to open a directory which has 700 permission?

share|improve this question
1  
please add an ls -l of the directory itself. –  Rinzwind Aug 19 '11 at 11:12
7  
And please explain why you keep leaving negative comments against the valid answers here. If you think there is (or should be) a better way, please let us know what it might be. –  Oli Aug 19 '11 at 12:29
1  
sudo chmod 0755 name_of_dir; do you business; cd ../; sudo chmod 0700 name_of_dir Seems to be the only answer you'll be happy with. –  Marco Ceppi Aug 19 '11 at 13:31
1  
please add an ls -l of the directory itself... –  Rinzwind Aug 19 '11 at 16:28
1  
The answers given here are correct, but you are voting them down and saying they are wrong. Don't just dismiss what is being said because of something else you have seen. –  Richard Holloway Aug 19 '11 at 16:33
show 2 more comments

5 Answers 5

up vote 21 down vote accepted

sudo cd won't work because the cd command is built into the shell. So you are saying become root and then run this command. You become root and then the command after sudo is searched for but there is no cd command to find.

The method to use is to switch to the user that owns the directory. Permission 700 is meant as "owner can read, write and execute".

So if root owns the directory sudo su, password and then cd {dir} is the only correct method. If someone else owns the directory you can still use the 1st method but can also change to that user with su {username} and then use cd as that user.

share|improve this answer
1  
+1 A good explanation. –  Richard Holloway Aug 19 '11 at 16:26
    
Side question: will root be able to enter a 700 dir even if he is not the owner? –  MestreLion Aug 19 '11 at 23:38
1  
yes MestreLion,700 will not stop root from enterting. Permissions '000' will stop the user from entering himself (yes, a user can create a dir he himself can not enter...) but still root can enter that one. –  Rinzwind Aug 20 '11 at 6:28
add comment

sudo -i

to open "root console" and then

cd /path/to/directory

(cd is a shell builtin command, so it can't be the sudo target)

share|improve this answer
1  
I don't agree with you. –  Bakhtiyor Aug 19 '11 at 11:11
10  
@Bakhtiyor: why not? explain, please –  enzotib Aug 19 '11 at 11:21
add comment

To open a root directory we may run a root shell, e.g.:

sudo su
# cd /root
share|improve this answer
1  
I wouldn't like to work as a root. NEVER!!! And also experts recommend never login as a root. -1 –  Bakhtiyor Aug 19 '11 at 11:10
6  
Well that's exactly why you don't want permissions to open root's directories, isn't it? –  Takkat Aug 19 '11 at 11:12
6  
There is nothing wrong going into root mode. Experts will always recommend going into root when it is -needed-. –  Rinzwind Aug 19 '11 at 11:15
1  
Experts? Never seen expert recommending such stupid thing... Only thing that is not recommended is login to graphical interface as root (and this is also impossible in Ubuntu). –  Vojtech Trefny Aug 19 '11 at 11:16
2  
If you're working with things owned by root, sometimes sudo suing (or another method) is the only practical or even possible method. "Experts" only tell you to not use root as a habit - it's fine when you actually need it. And @Vojtech, nothing's impossible... –  Oli Aug 19 '11 at 12:21
show 3 more comments

As others pointed out -- it's shell built-in:

~ % which cd
cd: shell built-in command

So, why don't you sudo the shell itself?

~ % sudo $SHELL -c "cd name_of_dir"
share|improve this answer
4  
+1 for the sudo bash -c idea... and a suggestion: avoid using which. Its merely a script, and it wont handle all possibilities (binary, builtin, aliases, etc). Use type instead. Much safer, powerful and portable. And type -p for the path of an executable. –  MestreLion Aug 19 '11 at 23:44
    
Well, depends ;-) type [ -wfpams ] name ... Equivalent to whence -v., which [ -wpams ] name ... Equivalent to whence -c. (man zshbuiltins) –  Daniel Bauke Aug 20 '11 at 14:08
    
It does not depend. whenceis zsh-only... it does not work in bash, it is not even installed by default in ubuntu. While type is POSIX, meaning it will work in any modern shell... bash, csh, ksh.. and even zsh. –  MestreLion Aug 21 '11 at 16:35
    
Sure, it is and that's why I upvoted your comment just when it appeared, but what I ment was that in zsh actually it's not a script and gives similar results as type as both are aliases to the same command. –  Daniel Bauke Aug 21 '11 at 20:26
    
Oh, I understand now. Well, but thats in zsh. In bash, which is the default (and usually only) terminal shell in Ubuntu, which is not a builtin... so type(or type -p) is preferred. –  MestreLion Aug 23 '11 at 4:34
show 2 more comments

I have had the sudo cd problem in the past. In the versions of bash and sudo I have with kubuntu lucid, sudo cd now works – without any workarounds. I think this is a fairly recent change.

As far as the to su or not to su debate, I think it's silly. su is against the religion of Ubuntu and it's not something to do carelessly. It's amazing what an rm -rf * can do if you're root. But, if you're comfortable with the command line interface (CLI) and have system level tasks to do, there's no reason not to use su. I've used several distros where no one even mentioned using sudo. It's just a matter of what kind of work you're doing and which method you're most comfortable with. I use both.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.