简体   繁体   中英

Copying files from multiple directories to another directory using linux command line

I have a bunch of files in separate folders, and all of the folders are in one directory.

/var/www/folder1/file1.txt
/var/www/folder1/file2.txt
/var/www/folder1/file3.txt

/var/www/folder2/file4.jpg
/var/www/folder2/file5.jpg
/var/www/folder2/file6.jpg

/var/www/folder3/file7.pdf
/var/www/folder3/file8.doc
/var/www/folder3/file9.gif

I need everything inside of the folders that are inside of /var/www/ to be copied to another directory (say, /var/my-directory/ ), but not the actual folders. Based on the example above, I need /var/my-directory/` to look as follows:

/var/my-directory/file1.txt
/var/my-directory/file2.txt
/var/my-directory/file3.txt
/var/my-directory/file4.jpg
/var/my-directory/file5.jpg
/var/my-directory/file6.jpg
/var/my-directory/file7.pdf
/var/my-directory/file8.doc
/var/my-directory/file9.gif

I can't seem to figure out the command to do this. I've tried the following:

sudo cp -R /var/www/./. /var/my-directory/

But, that still copies all of the folders.

Is there any way to do what I'm trying to do?

Use find.

find /var/www/ -type f -exec cp '{}' /var/my-directory/  \;

The trick is -type f that only selects file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM