5

I have Ubuntu 16.04 installed on my SSD and I have a HDD where I keep all of my C++ files and programs. Ubuntu recognizes that I have the HDD. When I am in the terminal and want to access my c++ files on the hdd how do I get the directory? I tried using cd then the name of the drive but that didn't work.

I'd like to make it so that every time I start my terminal I am automatically in the HDD instead of desktop too.

Zanna
  • 70,465
idknuttin
  • 191

3 Answers3

11

I'm going to use an example from my system. Your system will have different drive names, so please adjust accordingly

First, check that the drive is mounted and find its location:

$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 465.8G  0 disk 
├─sda1        8:1    0   512M  0 part /boot/efi
├─sda2        8:2    0 461.4G  0 part /
└─sda3        8:3    0   3.9G  0 part [SWAP]
sdb           179:0    0   3.7G  0 disk 
└─sdb1        179:1    0   3.7G  0 part 

The filesystem on the other drive, sdb1 isn't mounted, so I'm going to mount it using the udisksctl utility

$ udisksctl mount -b /dev/sdb1
Mounted /dev/sdb1 at /media/zanna/WORK STUFF

Now I can cd to the mountpoint like this:

zanna@xubi:~$ cd /media/zanna/WORK\ STUFF/
zanna@xubi:/media/zanna/WORK STUFF$     

note the \ character in the path, needed to escape the space. You can type this yourself, or just press TAB after typing the first few characters of the name.

To make sure the filesystem gets mounted at boot time, you could add a line for it to your /etc/fstab, if there is not already one present. You can create a mountpoint for the drive with any name you like:

mkdir /media/$USER/mydrive

Exactly what this should look like depends on the filesystem type. It's more robust (IMHO) to mount disks by UUID than by label, so to get the UUID and filesystem type with one command, use

$ sudo blkid
[...]
/dev/sdb1: UUID="2d8afeac-c623-4be7-b261-44920e6b8e71" TYPE="ext4" [...]

The filesystem type in this case is ext4, so what I would then do is

sudo cp /etc/fstab /etc/fstab.bak
sudoedit /etc/fstab

and check that there is not already a line there for the partition - duplicate lines for the same partition will cause errors. If there is not, then I would add a line at the end including the UUID (use the one you got from blkid, not my example below!), the mount point and the filesystem type like this:

UUID=2d8afeac-c623-4be7-b261-44920e6b8e71 /media/zanna/mydrive ext4 defaults 0 0

If the partition is an NTFS partition, the line would be

UUID=2d8afeac-c623-4be7-b261-44920e6b8e71 /media/zanna/mydrive ntfs-3g auto,user,rw 0 0

If you need to revert any changes you made, restore your backup:

sudo mv /etc/fstab.bak /etc/fstab

For more information about /etc/fstab, see the Ubuntu help page.

To set a default directory for terminal sessions, see Setting default path when opening a terminal session.

NB: In general, using etckeeper is preferable to making vulnerable ad-hoc backups of stuff in /etc

Zanna
  • 70,465
11

lsblk as well as mount show a list of all mounted (block) devices and their mountpoint, i. e. the access point to the device's content. To change to this directory, in a terminal do:

cd /path/to/dir

To set a default directory for every new terminal session, just add this line to the .bashrc file in your home directory. The following command does this automatically, you just need to enter the right path after cd:

echo 'cd /path/to/dir' >> ~/.bashrc
dessert
  • 39,982
  • 1
    mount always shows the mountpoint, the -l option just adds the label, which may or may not be part of the mountpoint. – Xen2050 Aug 30 '17 at 00:35
  • @Xen2050 You're totally right, thank you! Also, the output of lsblk is much more clear in this regard. – dessert Aug 30 '17 at 05:45
5

Open Nautilus, which is the the default file browser in Ubuntu. Select the target directory. Then click with the right mouse button on it and choice "Open in Terminal" from the context menu.

enter image description here

Into the open terminal type pwd. This command will print the full path to the current location (the current value of the environment variable $PWD). You can use this path with cd command next time and also you can append this cd command to the end of ~/.bashrc to set this path as 'default terminal directory'. You can do that by the command:

echo -e "\ncd '$(pwd)'" | tee -a ~/.bashrc

Where:

  • echo will print to the stdout the string enclosed with the quotation marks. The option -e will enable interpretation of backslash escapes, so \n will be interpreted as new line.

  • '$(pwd)' will expand the output of the pwd command as string, enclosed with single quotes.

    Instead of this expression we can use '$PWD', which (in this case) will produce the same result.

  • the pipe | will redirect stdout to stdin of the command tee.

  • tee with -a option will append the string to the bottom of the file .bashrc, located in your $HOME==~/ directory (and also will print the string to the stdout).

pa4080
  • 29,831