LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-23-2010, 09:51 AM   #1
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Rep: Reputation: 0
How to copy *.pdf from one mount to another


Hi,

I am trying to copy a particular type of file from one mounted drive to another, but I am having difficulties.

I would like to copy all of my pdf files from my mounted drive to my USB drive.

I am currently in my /mnt/sda2 (sda2 is a Windows XP) directory and want to copy all of the *.pdf files to /mnt/sdf4 (sdf4 is a USB drive).

I have tried:

copy . *.pdf /mnt/sdf4 (this only copies a couple of files)
copy -R . *.pdf /mnt/sdf4 (this starts copying the directories instead of the files)

So I want to copy only the *.pdf files contained in the entire sda2 mount over to sdf4.

Any help is greatly appreciated!

Thanks
 
Old 05-23-2010, 10:09 AM   #2
serafean
Member
 
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 997
Blog Entries: 15

Rep: Reputation: 136Reputation: 136
hi, go to the directory you want to cpy from (cd command) and copy from there ;
Code:
cp -r *.pdf /mnt/sdf4/
You only need the -r option if you want to copy directories.
If you have the pdfs in multiple directories, then the find command ids appropriate :
Code:
find ./ -iname "*.pdf" -exec cp {} /mnt/sdf4/ ';'
what this does is that it looks through all your directories for any file ending in .pdf, and for each one it finds executes 'cp [pdf it found] /mnt/sdf4' for a more thorough understanding i suggest reading the find manpage.

Serafean
 
Old 05-23-2010, 10:12 AM   #3
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
OK - the fact that these are two different physical drives has nothing to do with it.

The trick is to describe what you want to do in neutral terms - vis:
You want to hunt through all the subdirectories of /mnt/sda2 for files whose names contain the string ".pdf" (what about ".PDF"? - are there any portable document files which do not have the pdf suffix?) and copy them to the directory /mnt/sdf4 - is this correct?

At which point the "find" command springs to mind.

Something like:

Code:
find "/mnt/sda2" -name "*.pdf" -execdir cp {} /mnt/sdf4/;
... someone correct me

[edit] Hah! I was beaten to it ... the above is not case sensitive and avoids race conditions. See: man find.

Last edited by Simon Bridge; 05-23-2010 at 10:16 AM.
 
Old 05-23-2010, 10:47 AM   #4
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Original Poster
Rep: Reputation: 0
Correct... I want to hunt through all the subdirectories of /mnt/sda2 for files whose names contain the string ".pdf" and copy them to the directory /mnt/sdf4.

After I'm done with .pdf, I will move on to .doc and then .jpg, etc.

Thanks
 
Old 05-23-2010, 11:09 AM   #5
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Original Poster
Rep: Reputation: 0
Hmmm... I'm using PuppyLinux and it doesn't like "-exec" or "-execdir" from both suggestions.
 
Old 05-23-2010, 02:21 PM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
A little recursive function could do the job:

Code:
#!/bin/bash

DIR=${1:-`pwd`} # first command-line argument: source dir
TDIR=${2:-/mnt/sdf4} # second: target dir
SUFF=${3:-pdf} # third: file suffix

proc_dir () {
   cd $1

   for file in `ls`; do
      if [ -d $file ]; then
         proc_dir $file # if file is a directory, move into it
      elif echo $file | grep "\.$SUFF$" > /dev/null; then
         cp $file $TDIR # otherwise, if file has right suffix, copy it
      fi
   done
   cd ..
}

proc_dir $DIR

Last edited by Robhogg; 05-23-2010 at 02:28 PM.
 
Old 05-23-2010, 02:34 PM   #7
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
According to other questions about this, Puppy is fine with -exec as an option - try escaping the final character in that line:

find "/mnt/sda2" -name "*.pdf" -execdir cp {} /mnt/sdf4/ \;

what exactly is returned when you try to execute this?

@Robhogg: cool!
 
Old 05-24-2010, 05:14 AM   #8
serafean
Member
 
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 997
Blog Entries: 15

Rep: Reputation: 136Reputation: 136
Hi, escaping should work, I simply put the ; into apostrophes ';' as follows
Code:
find "/mnt/sda2" -name "*.pdf" -execdir cp {} /mnt/sdf4/ ';'
My reasoning behind this is the following : bash uses the ; character as a delimiter between two commands on one line, thus you have to tell it that it has to treat it as a normal string to pass to the command; thats where escaping ( \; ) comes into play. the apostrophes just tell bash to treat it as a normal character (same as escaping)

Last edited by serafean; 05-24-2010 at 05:15 AM.
 
Old 05-24-2010, 05:04 PM   #9
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Original Poster
Rep: Reputation: 0
This worked:

find "/mnt/sda2" -name "*.pdf" -execdir cp {} /mnt/sdf4/ ';'

Thanks so much!

Now, Simon Bridge had asked whether I also wanted to do .PDF (in addition to .pdf). Is there anything special about doing uppercase .PDF??
 
Old 05-25-2010, 08:24 AM   #10
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Well done.

Some windows/dos apps use all upper-case suffixes by default. So you may have somepdf.PDF in the windows drive. If you used -iname option, you would not have got those. However, if you didn't want them then -iname is appropriate. As it stands - you got them.

Study the recursive program though. Learn how to write them
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Mount and copy PMP Linux - Newbie 4 06-01-2009 08:51 AM
A pdf Viewer to copy a text in German? xpucto Linux - Software 6 07-17-2006 09:00 AM
pdf copy of apache 1.3.34 documentation ic_torres Slackware 1 03-26-2006 03:40 PM
How to copy a selected content from a .PDF file satimis Linux - General 3 07-03-2003 04:18 AM
PDF copy & paste dcspyder Linux - Newbie 1 10-29-2002 10:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration