Carolyn28
Posts: 6
Joined: Sun Jun 17, 2018 10:36 am

Copying files

Sun Jun 17, 2018 10:45 am

Hi guys!

I’m so excited because just got my first Raspberry.

I have very limited programming skills and i didn’t find from net any easy project which works like this in latest Raspbian:

Move (not copy) all files from sd-card which is in sd-card reader to time stamped folder in usb hard drive and tell user if copying files was okay or not by blinking leds. It could do this when booted (or it notices when any sd-card is attached).

I found multiple projects but all work differently than what i need or look too complicated to me to setup. I can follow normal instructions like type this to terminal etc.

Thanks

Carolyn28
Posts: 6
Joined: Sun Jun 17, 2018 10:36 am

Re: Copying files

Tue Jun 19, 2018 6:10 am

Anybody know any project like this?

User avatar
rpdom
Posts: 12590
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: Copying files

Tue Jun 19, 2018 7:02 am

That sounds like a fairly simple bash script to me.

Do you want it to run automatically when the card is inserted, or type a command manually?

I haven't got time to look at it now (bus to catch), but maybe later.

Carolyn28
Posts: 6
Joined: Sun Jun 17, 2018 10:36 am

Re: Copying files

Tue Jun 19, 2018 7:48 am

Automatically when any SD-card or USB-stick is connected. This Raspberry don't have display at all. Thanks

jbudd
Posts: 625
Joined: Mon Dec 16, 2013 10:23 am

Re: Copying files

Tue Jun 19, 2018 4:40 pm

I guess you could break the project down into bits:

How to make the USB hot swappable - so that the SD card is automatically mounted (made accessible) when it's plugged in.
The method described by Reinis in this thread seems to work viewtopic.php?p=1309302

How to start a program to move the files, bearing in mind that you have 2 USB devices, you will have to make sure that you can reliably define which to move From and which to move To.
Here is a similar project viewtopic.php?p=950319

How to flash an LED (either connected to GPIO or one of the on-board LEDs)
The way to control the on-board green or red led depends on what model Raspberry Pi you have.

Carolyn28
Posts: 6
Joined: Sun Jun 17, 2018 10:36 am

Re: Copying files

Sat Jun 23, 2018 5:39 pm

Thanks guys.

Those two links have many versions of scripts and instructions and for newbie it's hard to figure out what to do.

jbudd
Posts: 625
Joined: Mon Dec 16, 2013 10:23 am

Re: Copying files

Sat Jun 23, 2018 11:08 pm

The bit I followed to allow a USB drive to be available as soon as it's plugged in was this (between the lines of dashes):
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The good news is that I found a very simple alternative (at least in terms of installation, not sure about how "native" the solution is) - a package called usbmount. I can confirm that with usbmount I can auto-mount on boot and hot-plug any USB device in Raspbian Stretch Lite on my Raspberry Pi 2!

Installation steps of usbmount:

1. Install the package:

Code: Select all

sudo apt-get install usbmount
2. Make sure it works in Stretch by changing MountFlags=slave to MountFlags=shared here:

Code: Select all

sudo nano /lib/systemd/system/systemd-udevd.service
3. Reboot and it works!

Best,
Reinis
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Actually, I feel that your project is a bit complicated for a complete beginner.
I was going to suggest following a few of the projects at https://projects.raspberrypi.org/en/projects but then I looked at what's there and didn't see anything that resembled your aim of quietly moving files around. There are many good projects in the MagPi magazine too https://www.raspberrypi.org/?s=magpi. It's free to download.

Maybe you would find it useful to read an introduction to the Bash command line and shell scripts. I'm sure someone here could recommend a good one.

Carolyn28
Posts: 6
Joined: Sun Jun 17, 2018 10:36 am

Re: Copying files

Sun Jun 24, 2018 8:51 am

I installed usbmount and it works now.

I presume i need udev rule or something similar?

And copy script but all those scripts i have found look way too complicated to me to try to modify.
jbudd wrote:
Tue Jun 19, 2018 4:40 pm
How to flash an LED (either connected to GPIO or one of the on-board LEDs)
The way to control the on-board green or red led depends on what model Raspberry Pi you have.
I have Raspberry Pi 3 Model B and i would like to keep this simple and use on-board LEDs.

The script could ignore Raspberry boot SD-card and copy everything from smaller USB-memory to bigger.

Carolyn28
Posts: 6
Joined: Sun Jun 17, 2018 10:36 am

Re: Copying files

Mon Jun 25, 2018 2:57 am

I'm sure i can figure out how to blink LEDs.

jbudd
Posts: 625
Joined: Mon Dec 16, 2013 10:23 am

Re: Copying files

Mon Jun 25, 2018 8:30 am

all those scripts i have found look way too complicated to me to try to modify.
I wrote an example script to move files from one drive to another. It is complicated but I have put lots of comments.
All the work is done by the single line mv $FROMDIR/* $TODIR. The rest is definitions and checks.
Apart from checking that it runs, I have not tested that it works, there's plenty of work for you still to do!

You need to copy this into a file on the Pi and make the file executable by chmod +x filename Then you can run it by typing ./filename

Code: Select all

#! /bin/bash

# Define where to move files from and to
FROMDIR=/media/usb1
TODIR=/media/usb0/moved_files          # A directory (moved_files) in the target drive prevents moving in the wrong direction!


#Dummy function to flash an LED - prints "flash" instead
# A function is defined first and can be executed by inserting it's name in code further down the script. 
# A function can accept "arguments" to affect how it operates. In this case number of flashes and duration of flash.
function flashled {
   FL_NO=$1         # First argument number of flashes
   FL_TIME=$2       # Second argument flash duration in seconds

   for i in $(seq 1 $FL_NO)    # Loop $FL_NO times
   do
      printf "flash! "          # in lieu of turning an LED on print the word flash
      sleep $FL_TIME     # a short delay
                                       # Insert code to turn the LED off again here
   done
   printf "\n\n"
}
#---------------------------------------------------------------- Start of script -------------------------------

# Flash the LED to indicate script started
printf "Starting\n"
flashled 3 0.1      # 3 flashes of 0.1 seconds

# check FROM directory exists
if [ ! -d $FROMDIR ]             # test -d returns true if it is a directory
then
   printf "FROMDIR $FROMDIR does not exist!\n"
   flashled 1 0.5
   exit 1                           # exit the script. Exit code 1 is Linux convention for an error condition
fi

# check it contains files
if [ -z "$(ls -A $FROMDIR)" ]    # ls lists directory contents, test -z returns true if the result of ls is an empty string
then
   printf "FROMDIR $FROMDIR is empty!\n"
   flashled 2 0.5
   exit 1
fi

# check TO directory exists and is writable
if [ ! -w $TODIR ]                          # test -w returns true if directory is writable
then
   printf "Can't write to TODIR $TODIR\n"
   flashled 3 0.5
   exit 1
fi

# Move the files
mv $FROMDIR/* $TODIR

# Check that the move worked - $FROMDIR should now be empty
if [ ! -z "$(ls -A $FROMDIR)" ]
then
   printf "FROMDIR $FROMDIR still contains files - something went wrong!\n"
   flashled 4 0.5
   exit 1
fi

# Signal success and exit
printf "All finished!\n"
flashled 5 0.1
exit 0

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 37 guests