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 was recently in a predicament where I was inside a folder, and I wanted to cd to a sub directory folder. There was only one folder, inside this folder and it was a really long name.

Home (I am here)
 |
 |
  --> /reallylongnamefolder

Is there any way of going into that folder instead of typing out:

 cd reallylongnamefolder
share|improve this question
1  
many ways: cd */ or cd rea*/ if unsure there is only 1 subdir at all times (if using bash, you can also cd rea[TAB] .. in other shells too but the way to expand name may vary). If "/reallylongnamefolder" is an absolute path, same thing, but with '/' in front, for ex: cd /rea*/ –  Olivier Dulac Apr 25 at 7:51

6 Answers 6

up vote 25 down vote accepted

I would advice either to type first few letters and pressing tab. Bash has autocomplete feature that is really useful. If it is only directory in in current path just pressing tab will fill whole directory.

Typing in cd and pressing tab twice will display all options in current directory.

tab is generally really useful in bash as you have accessible almost all executables at one or two keypresses.

cd * as suggested above works only if the directory is first in the listing and not hidden. If there is file alphabetically before your directory this cd won't change your directory at all.

share|improve this answer
13  
You shouldn't even need to type any letters at all, if there's just one directory present: cd <tab> will be smart enough. –  amalloy Apr 24 at 22:14
    
Works in Windows as well. ( for people who come from google.. ) –  Koray Tugay Apr 25 at 10:04

I think I actually figured it out actually

cd * 
cd */

But I haven't tested it if there are multiple files and one folder!


As @Rinzwind mentioned in the comments!

Let's say you have three long folders:

 /thisislongfolder1
 /thisislongfolder2
 /thisislongfolder3

If you type the first letter of the file, then hit tab it will autocomplete the file name! CRAZY STUFF!

So in the example above, you can type: t tab and it will autocomplete as much as it can: cd thisislongfolder (then type the number yourself).

Or you can do cd t*1 would take you into thisislongfolder1

Thank you Rinzwind!

share|improve this answer
5  
cd first_letter_or_some_letters and then {tab} is the easiest (also solves your problem of lots of folders. You can even mixed letters with more than 1 tab and letters. The system will auto-complete as far as it can. You can also do cd r*r and hope it is the only folder beginning and ending with r. –  Rinzwind Apr 24 at 18:26
    
@Rinzwind wow! I didn't know that! thanks! –  Arian Apr 24 at 18:32
    
Try it out. Add your results to your answer and you'll probably get some reputation for it ;-) –  Rinzwind Apr 24 at 18:35
4  
cd */ might do the trick –  gniourf_gniourf Apr 24 at 20:24
3  
cd * probably fails more often than it succeeds. –  David Richerby Apr 24 at 21:44
cd $(ls -d */|head -n 1)

ls -d */ lists the directories, head -n 1 gives the first one in this list.

share|improve this answer
    
Yeah and you're going to type that... I'd rather go tab-tab and look up the name than typing this. It's 100% correct though, nice answer! –  tomsmeding Apr 25 at 18:48
    
@tomsmeding If you are writing a script that needs to cd to the first directory, you can't do tabs. –  ace Apr 25 at 18:58

You can change directory by inode number.
first we find the inode with ls -il.
then

cd $(find -inum directory_inode_number)
share|improve this answer

I have a nice setup for this it allows me not only to cd to ~/somereallylongfoldername but also cd to there even when I'm in the / folder

the first thing I use is zsh with oh-my-zsh this will also allow you to cd without having to worry about case or even without typing cd

  1. install git and zsh
sudo apt-get install zsh git
  1. install Oh My ZSH

curl -L http://install.ohmyz.sh | sh

  1. Change the default shell to ZSH
chsh -s /bin/zsh

open and edit your .zshrc that is located in your home folder not it is hidden

nano ~/.zshrc

then add the following line to the bottom of the file export CDPATH=$CDPATH:/:/home/$USER/:/media/$USER/

If you like my theme you can also change the line #ZSH_THEME="robbyrussell" to ZSH_THEME="pygmalion"

Save and close the file then restart ie. close and reopen the shell or just open a new tab and try it out

not you can use the tab key to complete names and in ZSH you don't even have to have the case right you can type docu and press tab and it will turn it into ~/Documents

enter image description here

share|improve this answer

If you need to do it in a script or to support directories with undecodable names:

cd $(python -c $'import os\nfor entry in os.listdir(b"."):\n if os.path.isdir(entry):\n  os.write(1, entry);break\nelse: os.write(1, b".")')

It works if there are files in the directory, regardless of their sorting order relative to directories.
It works for hidden directories when cd */ fails.
It works if the name is not representable in the current locale when ls only print ?? instead of a name.

share|improve this answer

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.