44

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
1
  • 3
    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*/ Apr 25, 2014 at 7:51

8 Answers 8

44

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.

2
  • 17
    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, 2014 at 22:14
  • 1
    Works in Windows as well. ( for people who come from google.. ) Apr 25, 2014 at 10:04
30
cd $(ls -d */|head -n 1)

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

3
  • 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, 2014 at 18:48
  • 6
    @tomsmeding If you are writing a script that needs to cd to the first directory, you can't do tabs.
    – user12205
    Apr 25, 2014 at 18:58
  • -n 1 shows the last 1 file of the file. If you want to use this for a variable order, you can use: cd $(ls -d -1 */ |sed -n '4p') to open the 4th folder in a directory. Adding it to alias can make you faster with folder navigation. Nov 17, 2018 at 7:32
22

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!

7
  • 6
    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, 2014 at 18:26
  • Try it out. Add your results to your answer and you'll probably get some reputation for it ;-)
    – Rinzwind
    Apr 24, 2014 at 18:35
  • cd * won't work for me. Apr 24, 2014 at 19:20
  • 5
    cd */ might do the trick Apr 24, 2014 at 20:24
  • 3
    cd * probably fails more often than it succeeds. Apr 24, 2014 at 21:44
2

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

1

I wrote a bash function which you can put in your .bashrc:

gd() {
  if [ "$*" == "" ]; then
    cd */
    return
  fi
  for i in "$@"; do
    if [ -d "${PWD}"/$i ]; then
      cd $i
    else
      cd "${PWD}"/*$i*
    fi
  done
}

Usage:

gd   # will moves you to the first directory in folder
gd a # will moves you to the firest directory containing 'a'

gd a*b # will moves you to the directory matching the pattern

gd a b # will moves you to the directory containing 'a' and then to a subdirectory containg 'b', you can privide arbitary subfolder names 
1
  • 1
    Good script thank you.
    – aristotll
    Dec 10, 2022 at 9:12
0

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

cd $(find -inum directory_inode_number)
0

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.

0

Extending on ace's answer:

cd $(ls -d -1 */ |sed -n '1p')

to open the 1st folder in a directory. '1p' can be changed to '2p' for the second directory.

ls -d -1 -la */

to list only directories from a parent directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .