How can I easily cd into a flash drive from cli?

Right now I am accessing all of my files from my native directories. However, I want to also use a flash drive for extra storage. In that case, what would my terminal command be to access my flash drive (for this example, assume flash drive name is "MYDRIVE")

Thank you!

4

3 Answers

It depends, but you can easily find out...

From GUI

...by right-clicking on any of the files or folders inside the drive, and choose > Properties ("Eigenschappen" in Dutch), in nautilus:

enter image description here


As you can see, the directory is

/media/jacob/My Passport

Note that in commands, you need to fix the (possible) space:

/media/jacob/'My Passport'

From cli

run:

lsblk | grep '<drive_name>' | awk -F'part ' '{ print $2 }'

example:

$ lsblk | grep 'My Passport' | awk -F'part ' '{ print $2 }'

outputs:

/media/jacob/My Passport

Again, note that in commands, you need to fix the (possible) space.

CD to the directory in one step

cd "$(lsblk | grep '<drive_name>' | awk -F'part ' '{ print $2 }')"

Where <drive_name> (of course) is the name of your drive.

To figure out where your flash drive is mounted, you can use the command lsblk. Example output:

theone@jakku:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 232.9G 0 disk
├─sda1 8:1 0 39.2M 0 part
├─sda2 8:2 0 753M 0 part
├─sda3 8:3 0 139G 0 part /media/theone/EE4601124600DD69
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 93.1G 0 part /
sr0 11:0 1 1024M 0 rom
loop0 7:0 0 64.7M 0 loop /snap/ubuntu-core/109
loop1 7:1 0 6.3M 0 loop /snap/hangups/1

As you can see, it lists all attached devices and their mount points. Then, all you would have to do is cd /your/flash/mountpoint. For example, if I wanted to access my Windows partition for some reason, I could do cd /media/theone/EE4601124600DD69. If your flash drive is listed, but not mounted, you can also do mount -o gid=1000,uid=1000 /dev/sdXY /media/user/mountpoint where X is is the dev letter (sda, sdb, etc) and Y is the partition number.

If you're coming from Windows, you might be used to old structure of each disk being assigned a "drive". However, everything is a file in Linux. Therefore, even external storage devices can be accessed as "files" stemming down from the root directory.

2

Physical disks are named by sd(a,b,c,...) sd = Sata disk a = first disk b = second disk etc... I hope you understood up to here now you should know which one is your harddisk after that you should mount your Disk if it is not mounted automatically if it is mounted you can find it's mount point by type "df -h" in output search for mountpoint column of your disk you can access or cd to your Disk from it's mount point if it is not mounted you can mount it manually for mounting a disk you should: first create a directory to mount you disk into that for example I create it on my desktop named as mount then mount your disk in to that by sudo mount /dev/sd.. /home/UserName/Desktop/mount first dot (.) is for your hard disk specification and second dot is for specifying your file system or partition.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like