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/

Open the terminal and run:

find /home/troy/Downloads/ -type f -exec cp {} /home/troy/.gs/ \;
  • 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
  • Thanks for solution, but... So easy operation from user point of view is performed so complicated. I am frustrated. – heroin Feb 13 '16 at 17:33
  • @heroin: I'm glad that you commented here. I believe that you just dived in Unix commands. Once you'll get experienced you'll say later how powerful, robust and flexible the commands are.. I can understand your frustration and also can say that you don't know how to use find and xargs commands. I suggest you to go though it. man find and man xargs would help you to learn about these commands. Happy Learning! – Saurav Kumar Feb 14 '16 at 4:38

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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