Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I cd to a directory, is there a quick way to delete the directory I am in and move to the top directory? Usually I cd to a directory, ls it and then cd .. then delete the folder. I want to be able to cd to it and ls. Then if I just want to delete it from there.

Basically is there a rm operator that equates rm -r $(pwd); cd ..

Also, how come if I do not add ; cd .. bash will still believe it is in the directory that doesn't exist?

michaelxu@michaelxu-server:~/Desktop$ mkdir test
michaelxu@michaelxu-server:~/Desktop$ cd test
michaelxu@michaelxu-server:~/Desktop/test$ touch test
michaelxu@michaelxu-server:~/Desktop/test$ ls
test
michaelxu@michaelxu-server:~/Desktop/test$ rm -r $(pwd)
michaelxu@michaelxu-server:~/Desktop/test$ pwd
/home/michaelxu/Desktop/test
michaelxu@michaelxu-server:~/Desktop/test$ touch test
touch: cannot touch `test': No such file or directory
michaelxu@michaelxu-server:~/Desktop/test$ cd ..
michaelxu@michaelxu-server:~/Desktop$ cd test
-bash: cd: test: No such file or directory
share|improve this question
1  
It behaves like it's in a directory that doesn't exist because that's exactly what is happening. It still has a pointer to that directory, but that pointer is no longer valid. bash doesn't know that the directory has been deleted. –  Darth Android May 14 '13 at 21:13
    
Part of what’s happening is that bash probably doesn’t check to see what directory it’s in after every command, but only after every cd, pushd, and popd. It depends on exactly how you have defined PS1. For example, try cd test and then mv ../test ../quiz (or (cd ..; mv test quiz)). The prompt will probably still say Desktop/test, because bash has no reason to check whether the directory name has changed. // P.S. May I suggest that you don’t use the same name (test) for two different things in your examples? –  Scott May 14 '13 at 23:25
add comment

1 Answer

Under bash:

rm -rf "$(pwd -P)" && cd ..
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.