Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

new to ubuntu and i keep reading the stuff posted but it isn't working for me, help!!! I am missing something hope someone can help. I am trying to copy files from one folder to another. when i do this it copies all folders into the the folder. the problem is i didnt want copies of all the folders i just wanted the files in the one folder to copy to another folder:

cp -a /home/troy/Downloads/ . /home/troy/.gs/
share|improve this question

3 Answers 3

up vote 1 down vote accepted

Open the terminal and run:

find /home/troy/Downloads/ -type f -exec cp {} /home/troy/.gs/ \;
share|improve this answer
    
Thank you for the help it worked great i have messed with this for a few hours. –  tbuilt62 Oct 24 '13 at 2:24

You can use these commands to copy only files to any specific directory

Suppose you want to copy all the files under /home/troy/Downloads to /home/troy/.gs then:

First go to /home/troy/Downloads and:

find . -type f | xargs -I '{}' cp {} /home/troy/.gs

If you want to copy any specific file say *.mp3 then you can execute this command:

find . -iname "*.mp3" -type f | xargs -I '{}' cp {} /home/troy/.gs
share|improve this answer

find is an incredibly powerful command, but I can't ever remember how to use it.

I realized that off the top of my head I didn't know the answer to your question, but thought that file globs might be useful, so here's what I tried:

/tmp/askubuntu $ ls # notice no output because this directory is currently empty
/tmp/askubuntu $ mkdir src dest # create source and desination directories
/tmp/askubuntu $ ls # now we have the two directories we created
dest  src
/tmp/askubuntu $ cd src
/tmp/askubuntu/src $ ls # no files in src, yet
/tmp/askubuntu/src $ touch file-1 file-2 # create a couple of empty files
/tmp/askubuntu/src $ ls # now we can see the files that we created
file-1  file-2
/tmp/askubuntu/src $ mkdir dir-to-ignore # make a directory that we don't want to copy
/tmp/askubuntu/src $ ls -l # we can use 'ls -l' to view the files as well as their types. note the 'd' to the left of dir-to-ignore -- this says it is a directory
total 4
drwxr-xr-x 2 josh josh 4096 Oct 24 00:06 dir-to-ignore
-rw-r--r-- 1 josh josh    0 Oct 24 00:05 file-1
-rw-r--r-- 1 josh josh    0 Oct 24 00:05 file-2
/tmp/askubuntu/src $ cd ../dest/
/tmp/askubuntu/dest $ ls
/tmp/askubuntu/dest $ touch file-3 # make an empty file here too
/tmp/askubuntu/dest $ cd ../src/
/tmp/askubuntu/src $ ls
dir-to-ignore  file-1  file-2
/tmp/askubuntu/src $ cp * ../dest/ # copy all files from our current directory (* matches anything) to ../dest
cp: omitting directory ‘dir-to-ignore’
/tmp/askubuntu/src $ ls # our current directory is unchanged
dir-to-ignore  file-1  file-2
/tmp/askubuntu/src $ cd ../dest/
/tmp/askubuntu/dest $ ls # but we copied all of files we wanted, and nothing more
file-1  file-2  file-3

The reason for this works isn't immediately obvious. Had we given cp the -r flag (so cp -r * ../dest/), we would have also copied over dir-to-ignore in addition to all of its contents. This is because -r indicates to copy directories recursively (as we can see by typing man cp at a terminal). So in your case this might become cp -a /home/troy/Downloads/* /home/troy/.gs/. Or you can omit the -a if you desire.

I don't claim that relying on this slightly odd behavior of cp (not copying directories without the -r flag, but also not giving an error) is the right thing to do. My goal was to illustrate how I might try to construct a solution to this problem and illustrate a few commands along the way.

Maybe you'll find it useful, maybe you won't, maybe you already know all of this. Just my two cents. But regardless, I think it's nice to know how/why things work and be able to explore solutions on your own.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.