-2

When I am trying to copy directory from Linux home directory to usb drive(pen drive), In the case of following command cp -r /home/directoryname /media/usbname(pendrivename) it is working fine.

But I am looking the command , copy the directory with out giving "usbname(pendrivename)"

ArnonZ
  • 3,822
  • 4
  • 32
  • 42
nalini
  • 112
  • 5

2 Answers2

0

Not sure I have understood your demand, but if I have a script like this should do the trick if your USB drive is always monted with the same tag :

#!/bin/bash
cp -r $1 /media/usbname(pendrivename)

If you save the script as ~/cpusb.sh, you can do :

chmod cpusb.sh
echo "alias cpusb=cpusb.sh" >> .bash_aliases
source

and then use cpusb when you want.

Shan-x
  • 1,146
  • 6
  • 19
  • 44
  • in shell script cp -r $1 /media/usbname(pendrivename) its work for any pendrive or only specific one. – nalini Mar 31 '15 at 12:57
  • You have to replace `usbname(pendrivename)` by the tag of your USB device. If it is classicaly partioned, the tag is fixed. You can modify it using a tool such like gparted. – Shan-x Mar 31 '15 at 13:00
  • When you use the USB drive, what is its name ? Its mount point should be `/media/name` or `/media/user/name`, depending on your distribution. You simply put this mount point in the script *if* the name of the USB drive is fixed. If it is not, the mount point would be a random string, so you can easily see that. – Shan-x Mar 31 '15 at 13:24
  • @Shan-x No need for the alias there - just drop the `.sh` suffix from your script. Having to maintain both a script and an alias that points to the script is needlessly complex. – twalberg Mar 31 '15 at 17:23
  • But what if he wnts to use it from another directory ? – Shan-x Mar 31 '15 at 18:20
  • @Shan-x Then that alias won't work, anyway, unless the location where the script is stored is in one of the directories listed in `$PATH`. And if it's in one of those directories, the alias still accomplishes nothing. Just name the script `cpusb`, put it in `${HOME}/bin/`, and make sure `$PATH` contains `${HOME}/bin`, and it should just work... – twalberg Mar 31 '15 at 18:43
0

I think i'd use mount to textually infer the connected usb's dir using sed or awk.

Maybe even save mount's result when nothing is connected and 'subtract' it from mount's result after connecting a new usb device.

Or even better, run your script before you connect the device:
- the script will run mount every second and will wait for a change in the result.
- when a change is detected, the newly added device is your usb.

Something like:

#!/bin/bash

mount_old="$(mount)"
mount_new="${mount_old}"

while [[ "${mount_new}" == "${mount_old}" ]]; do
    sleep 1
    mount_new="$(mount)"
done

# getting added line using sort & uniq
sort <(echo "${mount_old}") <(echo "${mount_new}") | uniq -u | awk '{ print $3 }'

# another way to achieve this using diff & grep
# diff <(echo "${mount_old}") <(echo "${mount_new}") | grep ">" | awk '{ print $4 }'

It's merely a sketch, you might need/want to refine it.

ArnonZ
  • 3,822
  • 4
  • 32
  • 42