Ubuntu – How to cd to a directory without writing its name

cd-commandcommand linefiles

Sometimes it’s annoying to access a directory with the folder name. Say I have a directory named a b c d. Apart from using Tab, is there any way to access the folder without typing the name of the directory?

I know that Linux has a unique identifier every particular file. Can I use this to access the folder? I don’t know whether this can be actually done or how to do it.

I that think when Linux implements a filesystem, it compares two directory names’ uniqueness. So each directory must be unique in a space. But I think that it’s like a primary key in a database system. Is the primary key the name of the directory or is there some other unique identifier (perhaps some numbers stored “under the hood”)?

Try to think of this like a process. If you execute the command ps on a terminal, it outputs a process list with the name and number of each process. You have to call that process with the process number. Similarly, is there a number for a directory so that you could call the directory with its number instead of calling it with its name?


On further investigation, I have found that each directory has a unique inode. However, I have not so far found any built-in command to access a directory by its inode.

Best Answer

Solution made by OP

No built-in command found here . But finally I am able to write a C program to use cd(lets call my program icd == (inode cd) ) to enter in a folder using inode value. Here I am posting the raw code.

But there is a fundamental problem I have faced here. While coding execution a C code from a bash needed to create child process under the bash process(parent process). From the child process the directory space is new, and I can not access parent process's directory space from there. So nothing could be done except invoked a new bash window from here. In future I will try to implement a new tab feature if people are interested in this. But I believe I have faced a lot of criticism for doing this. So people might not be interested. I have just done for my amusement.

RAW code is shared here,

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include<dirent.h>
#include <unistd.h>
#include <grp.h>
#include<pwd.h>

using namespace std ;

int main(int argc , char *argv[] ) {

  struct stat ITR ;

  if( argc != 2 ) {
    printf("\nWrong Command\n\n") ;
    return 1 ;
  }

  long long given_inode = 0 ;
  for( int i =0 ; argv[1][i] ; i++ ){
    given_inode *= 10 ;
    given_inode += (argv[1][i]-'0') ;
  }

//  if (stat(argv[1], &ITR) == -1) {
//    perror("stat");
//    return 1  ;
//  }

  printf("%s\n",argv[0]) ;
    char PWD[1000] ; 
    getcwd( PWD , 1000 ) ;

  DIR *d;
  struct dirent *p;
  char path[100000] ;
  d = opendir(".");
  if( d != NULL ) {
    while( (p = readdir(d))!= NULL ) {
        strcpy( path , "./" ) ;
        strcat( path, p->d_name ) ;
        stat(path, &ITR) ;
        //printf("%s --> ",path) ;
        //printf("%ld\n",ITR.st_ino) ;
        if( ITR.st_ino == given_inode ) {
          strcpy( path , "gnome-terminal --working-directory=" ) ;
          strcat( path, PWD ) ;
                    strcat( path, "/" ) ;
                    strcat( path, p->d_name ) ;
                    system(path) ; 
                    //printf("%s\n",path) ;
                    return 0 ;
        }
    }
  }
  printf("icd %lld:No such file or directory\n",given_inode) ;
  return 0 ;

}

I am using gnome terminal here. Obviously For other distro the code will be changed.