Using SSH / PUTTY to move and copy files on QNAP NAS
This page describes how to copy and move files already on the QNAP range of NAS using a SSH terminal session with PUTTY. Obviously you can already move,copy and delete file using native Windows Explorer or Mac Finder on any computer connected to the same network. The advantage of this approach is that all the action takes place directly on the NAS and does not impact on client computer CPU or network resources (simply put it is generally the fastest option for copy,move and delete of any content already on the NAS).
These instructions are intended for people
comfortable using a command line interface.
On a PC you will need a SSH terminal client such as PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/
On a Linux or Mac computer you may use the native command ssh from a Terminal (on a Mac see the Terminal within Utilities in Finder).
Use the QNAP Finder application to find the ip address of the QNAP or the QNAP hostname. e.g. 192.168.1.10 or TS251.local
To connect to the QNAP from a Linux or Mac terminal session use the following commands: Our eaxmple uses 192.168.1.10 as the device address - replace this with the IP address of your QNAP NAS as displayed in the "QNAP Finder" application:
ssh -l admin 192.168.1.10
An alternative syntax combines the username and hostname: admin@hostname e.g. using the ip address
ssh admin@192.168.1.10
or using the hostname
ssh admin@ts121.local
This will attempt to connect to the QNAP (in our example at ip address 192.168.1.10) using user admin. You will be then prompted for the password (default admin). Once connected you will be at a Linux type command prompt (BusyBox). The shell on the QNAP uses a reduced set of Linux commands so some alternative approaches need to be taken.
You may also use the hostname instead of the IP address if known.
Using PUTTY you simply enter the IP address or Hostname and click the "Open" button. You will be prompted for username (admin) and password (admin).
QNAP Folder Structure
The QNAP has a set of default shares created e.g. Public, Multimedia, Photos. These can be accessed from within the SSH session at:
/share/Public /share/Multimedia /share/Download /share/Web
Copying files from one folder to another on the QNAP using SSH
In this example we have some FLAC music files in Multimedia and all sub-folders that we want to COPY to Public. In this example we want just the FLAC files to be copied toa sub-folder called ripcaster within Public.
Use the following commands:
mkdir /share/Public/ripcaster cp -rv /share/Multimedia/*.flac /share/Public/ripcaster
Alternative using rsync:
rsync -av --include="*/" --include="*.flac" --exclude="*" /share/Multimedia/ /share/Public/ripcaster/
Moving files from one folder to another on the QNAP using SSH
In this example we have some FLAC music files in Multimedia and sub-folders that we want to MOVE to a sub-folder ripcaster in Public.
Use the following command:
rsync -av --remove-source-files --include="*/" --include="*.flac" --exclude="*" /share/Multimedia/ /share/Public/ripcaster/
Deleting files with a specific extension on the QNAP using SSH
In this example we have some M4A music files in Multimedia that we want to DELETE in all sub-folders.
*** CAUTION THERE IS NO UNDO IF YOU DELETE THE WRONG FILES ***
Use the following commands:
find /share/Multimedia/ –name *.m4a | while read foo; do rm “$foo” ;done
Shell Script to Move Files on QNAP
This shell script can be used to quickly move selected files from one directory structure to another. The main advantage of this approach is the speed.
Disclaimer: This works for us - it may not work for you. Use at your own risk!
A typical use would be if you had, for example, a directory structure with multiple formats of the same tracks e.g.
- "Bob Dylan\Blood On The Tracks\01 - Tangled Up In Blue.mp3"
- "Bob Dylan\Blood On The Tracks\01 - Tangled Up In Blue.flac"
- "Bob Dylan\Blood On The Tracks\01 - Tangled Up In Blue.m4a"
- "Bob Dylan\Blood On The Tracks\01 - Tangled Up In Blue.wav"
The script allows files of a specific type (e.g. mp3) located in the Multimedia folder (/shares/Multimedia/Bob Dylan/Blood On The Tracks/) to be moved to a separate folder structure e.g. /shares/Public/mp3/Bob Dylan/Blood On The Tracks/01 - Tangled Up in Blue.mp3". The script will process all sub-folders below the specified parent source folder.
From a Putty command line you can download the script and set as executable using:
cd /share/Public wget http://www.ripcaster.co.uk/files/download/ripcaster_mover dos2unix /share/Public/ripcaster_mover chmod 777 ./ripcaster_mover
Once downloaded execute the script using:
./ripcaster_mover -s <source parent folder> -d <destination parent folder> -t <file extension to mover> -p
This example moves all mp3 file (*.mp3) from /share/Multimedia/ (and all sub folders) to the folder /share/Public/mp3/ replicating all the directory structure below the parent source (/share/Multimedia) in the destination parent folder (/share/Public/mp3).
./ripcaster_mover -s /share/Multimedia/ -d /share/Public/ -t mp3 -p
The source of the script is shown for reference:
#!/bin/bash # # ripcaster file mover script # JJN April 2011 # Used to (typically) move file/directory structures on QNAP NAS # # default settings - overridden by command line sourcefolder="/share/Multimedia/" destinationfolder="/share/Public/" filetype="mp3" copyjpg=true while getopts s:d:t:p: optname do case "$optname" in s ) sourcefolder="$OPTARG";; d ) destinationfolder="$OPTARG";; t ) filetype="$OPTARG";; p ) copyjpg=false;; esac done shift $((OPTIND -1)) echo "" echo "============ Ripcaster File Mover ===============" echo "Usage:" echo " -s source folder e.g. /share/Multimedia/ (NOTE trailing slash)" echo " -d destination folder e.g. /share/Public/ (NOTE trailing slash)" echo " -t file type to MOVE e.g. mp3 will auto create folder /share/Public/mp3" echo " -p dont copy picture files e.g. *.jpg from source folders to destination" echo "Example:" echo " ./ripcaster_mover.sh -s /share/Multimedia/ -d /share/Public/ -t mp3 -p" echo "=================================================" echo "-------- Ripcaster File Mover -------------------" echo "=================================================" echo "Source Folder [$sourcefolder]" echo "Destination Folder [$destinationfolder]" echo "File Type [$filetype]" echo "Copy images (jpg) [$copyjpg]" echo "=======================================" read -p "Are you sure? " -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] then exit fi echo "" destinationroot="$destinationfolder$filetype" # check destination root folder exists.... # e.g. /share/Multimedia/mp3 if [ ! -d "$destinationroot" ]; then mkdir "$destinationroot" echo "Created [$destinationroot]" fi strlen=${#sourcefolder} #echo "Length [$sourcefolder] is [$strlen]" # now check that the folder structure exists for all sub folders OLDIFS="$IFS" IFS=$'\n' for thisfolder in `find "$sourcefolder" -type d` do #echo "Source Folder [$thisfolder]" subfolder=${thisfolder:strlen} #echo "Sub folder [$subfolder]" # new folder will be $destination$subfolder newdest="$destinationroot/$subfolder" #echo "Checking [$newdest]" if [ ! -d "$newdest" ] ; then mkdir "$newdest" echo "Created folder [$newdest]" fi done # now the files... for thisfile in `find "$sourcefolder" -type f -name "*.$filetype"` do #echo "Move file [$thisfile]" subfile=${thisfile:strlen} newfiledest="$destinationroot/$subfile" echo "Move [$thisfile ] to [$newfiledest]" mv "$thisfile" "$newfiledest" done if $copyjpg ; then for thisfile in `find "$sourcefolder" -type f -name "*.jpg"` do #echo "Copy file [$thisfile]" subfile=${thisfile:strlen} newfiledest="$destinationroot/$subfile" echo "Copy [$thisfile] to [$newfiledest]" cp "$thisfile" "$newfiledest" done fi IFS="$OLDIFS"