How do I copy files and folders using the command line?

0
51
Asked By CuriousExplorer42 On

I've got a USB flash drive with multiple folders (Folder A, Folder B, Folder C) and several files (File1, File2, etc.), including hidden ones. I want to know how to do the following using the command line:

1) Copy everything from the USB drive to /home/user/here/, including all files, folders, subfolders, and hidden items.

2) Copy only the top-level files from the USB drive to /home/user/here/ without including any folders or subfolders.

3) Copy Folder A and all its contents (files and subfolders) to /home/user/here/.

4) Copy only the subfolders and their contents from Folder B, without any of the files in Folder B itself, to /home/user/here/.

Thanks for your help!

3 Answers

Answered By PunnyUnixGeek On

Remember, if you're using SSH, `scp` might be what you need. It allows secure copying across networked machines. But for local stuff, `cp` is straightforward. Just keep your terminal commands at hand, and you should be all set!

Answered By TechWizard99 On

For your tasks, here are some command suggestions:

1) Use `cp -r /path/to/USB/* /home/user/here/` to copy everything, including hidden files. You might want to use `rsync` instead for better control.

2) To just copy the top-level files, use `cp /path/to/USB/*.* /home/user/here/` to exclude folders.

3) To copy Folder A and everything inside it, the command is `cp -r /path/to/USB/FolderA /home/user/here/`.

4) For copying only the subfolders from Folder B, try `find /path/to/USB/FolderB/ -mindepth 2 -type d -exec cp -r {} /home/user/here/ ;`. This will get you the subdirectories without the files at the top level.

UserFeedback07 -

Awesome! Thanks for breaking that down. It makes so much sense now.

Answered By CommandLineNinja On

If you're looking to dive deeper, check out `man cp`, `rsync`, and understand wildcards. Those commands can be powerful when you get the hang of them. For instance, `rsync -avh /path/to/USB /home/user/here/` will guide you through preserving file attributes, too!

HelpfulHelper1 -

Thanks for the tips! I appreciate you explaining the `rsync` part.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.