-2

How do I mount a block device using only one command?

Such as mount sdb1 (create and select directory automatically)
or mount sdb1 /mnt/USB1/. (create directory automatically in one step.)

Linux sometimes does not mount automatically.

neverMind9
  • 1,680
  • 2
  • 18
  • 38
  • 4
    After creating `/mnt/USB1` you shouldn't need to create it again. Have a look into `fstab` for mount points you use often – Torin Aug 09 '18 at 07:43
  • 1
    Possible duplicate of [What command does nemo use to mount drives](https://unix.stackexchange.com/questions/394320/what-command-does-nemo-use-to-mount-drives) – muru Aug 09 '18 at 07:53
  • 2
    Automounting is actually a thing, and has been for many years now. – jasonwryan Aug 09 '18 at 07:58
  • 2
    You can do it with `udisksctl mount --block-device /dev/sd...` in Ubuntu. `alias udm='udisksctl mount --block-device'` to mount a disk with just 3 letters and no root privileges needed. – undercat Aug 09 '18 at 08:01
  • @undercat That's the solution. Do you consider posting it as answer, or should I? – neverMind9 Aug 09 '18 at 19:31
  • 1
    @neverMind9 I'm glad this helped! Your quesiton is currently locked because several users were of the opinion it was "opinion-based". If it does get unlocked in the future, you can feel free to use my comment as the basis for a new answer. – undercat Aug 09 '18 at 22:31

1 Answers1

3

Add a function to the shell initialization file of your choice:

function qmount() {
    # qmount DEVICE DIR
    sudo sh -c 'mkdir -p "/mnt/$2" && mount "/dev/$1" "/mnt/$2"' sh "$1" "$2"
}
nohillside
  • 3,221
  • 16
  • 27
  • 2
    Use `mkdir -p "/mnt/$2"` to avoid having to test for existence. And you can't mount if the `mkdir` failed. E.g. `sudo sh -c 'mkdir -p "/mnt/$2" && mount "/dev/$1" "/mnt/$2"' sh "$1" "$2"` (also note quoting and properly passing arguments to the child shell). – Kusalananda Aug 09 '18 at 07:53
  • 1
    @Kusalananda Ah, didn't know that `-p` also ignored existing target dirs. And thanks for the long version, was too lazy to figure out the parameter passing stuff :-) – nohillside Aug 09 '18 at 07:55