Score:0

I get an error when copying files to a USB drive with my script

fr flag

I want to copy my data to a USB drive and get an error message.

The reason is that when copying all data I get a "/" at the beginning.

Does anyone know why this is?

Below you can see my code:

if ( [ -e $Dirname ] && [ -e /dev/sdb1 ] )
then
cp /home/jason/tmp/*_backup* /$Dirname
rm -r /home/jason/tmp/*_backup*
    echo "Your data was successfully copied."

else
    echo "Your USB stick is not connected or not mounted."
fi
$Dirname < /home/jason/Programs/Script/dirname_backup2usb.txt

And the parameter that i am giving to $Dirname is:

#!/bin/bash

/media/xyz

David avatar
cn flag
Is this even on Ubuntu? You have not said.
H.B. avatar
fr flag
Hi David, yes. It is Ubuntu.
in flag
If you’re passing `/media/xyz` as the parameter, the slash in `/$Dirname` may be messing things up
H.B. avatar
fr flag
Thank you @matigo, it was for the slash. But It says now all the data has been successfully copied. but they will not be present in the destination directory. Am I doing something wrong with PASSING?
H.B. avatar
fr flag
@matigo because when i am copying these with the command cp without passing the adress, it works well.
bac0n avatar
cn flag
Don't use *copy* and *remove* (at least not without some form of verification), if the *copy* fails, it's still going to remove your backup. `mv` or `rsync` is probably a better choice.
H.B. avatar
fr flag
@bac0n Thank you. I recognized that every time all backups are deleted although the process failed.
bac0n avatar
cn flag
maybe this [example](https://askubuntu.com/questions/1231737/a-udev-rule-for-automatically-executing-rsync-scrips-is-not-working/1231839#1231839) can work as a foundation for your script instead, starting from "An example how you can..."
Score:1
jp flag

Testing for /dev/sdb1 does not guarantee anything. You would like to identify the mountpoint of the file system on the USB drive. Try with the a command line with lsblk, for example

lsblk -f

or I think better, with

lsblk -o NAME,SIZE,HOTPLUG,FSTYPE,LABEL,MOUNTPOINT,MODEL | grep -v '/snap/'

You can make an alias to make it easier:

alias mylsblk="lsblk -o NAME,SIZE,HOTPLUG,FSTYPE,LABEL,MOUNTPOINT,MODEL | grep -v '/snap/'"

and edit it into your ~/.bashrc file near the other aliases. Run

source ~/.bashrc

and you can start using it:

$ mylsblk
NAME          SIZE HOTPLUG FSTYPE LABEL           MOUNTPOINT          MODEL
sda         238,5G       0                                            SanDisk SD6SB1M2
├─sda1        500M       0 ntfs   System
├─sda2      139,4G       0 ntfs   Windows
├─sda3       1000M       0 ntfs   Recovery
├─sda4          1K       0
├─sda5       89,7G       0 ext4   lubionic        /
└─sda6          8G       0 swap                   [SWAP]
sdb           3,7T       0                                            WDC WD4002FYYZ-0
├─sdb1        510M       0 ext4   boot-nvme-focal
├─sdb2         30G       0 ext4   xubufocal-hdd
├─sdb5          1M       0
├─sdb6        100G       0 ext4   studio12.04
├─sdb7        3,5T       0 ext4   multimed-2      /media/multimed-2
└─sdb8          5G       0 swap
sdc            15G       1                                            Transcend 16GB
└─sdc1         15G       1 vfat   KEEP_ME         /media/nio/KEEP_ME
sr0          1024M       1                                            DVD-RW DH16AESH
nvme0n1     232,9G       0                                            KINGSTON SA2000M8250G
├─nvme0n1p1 232,9G       0 ext4   nvme-focal
└─nvme0n1p2     1M       0

In the example from my computer there are many drives and even more partitions. But now you can see that HOTPLUG is 1 for /dev/sdc, and you can see the size and model, to make it easy to identify. So in this case you should copy to the mountpoint of the FAT file system /media/nio/KEEP_ME.

H.B. avatar
fr flag
thank you @sudodus for your detailed answer. I'm going to try it. My biggest problem is now, that i cant pass or redirect the directory for copying the files. Can you see in my Code where is the failure is?
sudodus avatar
jp flag
I'm sorry, @H.B., but I don't understand your shellscript. it is very far from how I would do the programming. I think you must explain in words what you want to do (where you have your original data), Please run an lsblk command similar to what I show in the answer. Copy and paste the output from the command into the original question. Indent each line by 4 spaces to render it as code. Otherwise it is difficult to read. When I see the output I will understand more and I should be able to help you.
sudodus avatar
jp flag
@H.B., Have you used command lines in a terminal window before? You can copy and paste between your web browser and the command line window. Press the Enter key to execute the command line.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.