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) | | --> /reallylongnamefolderIs there any way of going into that folder instead of typing out:
cd reallylongnamefolder 1 8 Answers
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.
cd $(ls -d */|head -n 1)ls -d */ lists the directories, head -n 1 gives the first one in this list.
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 /thisislongfolder3If 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!
7I 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
- install git and zsh
sudo apt-get install zsh git
- install Oh My ZSH
curl -L | sh
- 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
You can change directory by inode number.
first we find the inode with ls -il.
then
cd $(find -inum directory_inode_number) 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.
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.
I wrote a bash function which you can put in your .bashrc:
gd() { if [ "$*" == "" ]; then cd */ return 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