Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
#!/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
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)
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.