Linux copy all but one folder

Linux – How To Copy a Folder [ Command Line Option ]

How To Copy a Folder with cp Command

The cp command is a Linux command for copying files and directories. The syntax is as follows:

Linux cp command examples

In this example copy /home/vivek/letters/ folder and all its files to /usb/backup/ directory:
cp -avr /home/vivek/letters /usb/backup
Where,

  • -a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
  • -v : Verbose output.
  • -r : Copy directories recursively.

More examples of cp command to copy folders on Linux

Copy a folder called /tmp/conf/ to /tmp/backup/:
$ cp -avr /tmp/conf/ /tmp/backup/
Sample outputs:

Fig.01: cp command in action

Use Linux rsync Command to copy a folder

You can also use rsync command which is a fast and extraordinarily versatile file copying tool. It can make copies across the network. The syntax is as follows for the rsync command

To backup my home directory, which consists of large files and mail folders to /media/backup, enter:
$ rsync -avz /home/vivek /media/backup
I can copy a folder to remote machine called server1.cyberciti.biz as follows:
$ rsync -avz /home/vivek/ server1.cyberciti.biz:/home/backups/vivek/
Where,

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

  • -a : Archive mode i.e. copy a folder with all its permission and other information including recursive copy.
  • -v : Verbose mode.
  • -z : With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted something that is useful over a slow connection.

Fig.02: rsync command in action

Conclusion

You just learned how to copy a folder on a Linux like operating system using the cp command and rsync command. In conclusion, use rsync for a network folder transfer and cp for a local disk transfer.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How can I copy the contents of a folder to another folder in a different directory using terminal?

I am trying to copy the contents of a folder to another folder in a different directory using terminal.

Would somebody be able to provide me an example of the command line syntax required to achieve this?

8 Answers 8

You can copy the content of a folder /source to another existing folder /dest with the command

The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.

Читайте также:  Pubg lite не запускается windows 10

An alternate is rsync :

The advantages of rsync are:

  1. After the initial sync, it will then copy only the files that have changed.
  2. You can use it over a network, convenient for files in $HOME, especially config files.

Lets say you have a folder called folder1 in your

, inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.

To copy all the contents of

/new_folder1 you would use

new_folder1 would then contain all the files and folders from folder1 .

cp is the command to copy using a terminal, -r makes it recursively (so, current directory + further directories inside current)

/folder1 is the origin folder,

/new_folder1 is the destination folder for the files/folders inside the origin.

sign). Maybe because the destination folder was in /opt, which resides in another file system. And thank you Portablejim to remember the hidden file thing!

/new_folder1/folder1 instead of copying the contents over.

Simple example.

Copy the directory dir_1 and its contents (files) into directory dir_2:

Copy only the contents (files) of dir_1 into directory dir_2:

_files_ is a placeholder for the actual files located in the directory.

Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.

cp is a Linux command for copying files and directories. The syntax is as follows:

In this example copy /home/vivek/letters folder and all its files to /usb/backup directory:

-a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.

-v : Explain what is being done.

-r : Copy directories recursively. Example

Copy a folder called /tmp/conf to /tmp/backup:

I like this command

Some of the commonly used options in rsync command are listed below:

  • -v, –verbose: Verbose output
  • -q, –quiet: suppress message output
  • -a, –archive: archive files and directory while synchronizing ( -an equal to following options -rlptgoD)
  • -r, –recursive: sync files and directories recursively
  • -b, –backup: take the backup during synchronization
  • -u, –update: don’t copy the files from source to destination if destination files are newer
  • -l, –links: copy symlinks as symlinks during the sync
  • -n, –dry-run: perform a trial run without synchronization
  • -e, –rsh=COMMAND: mention the remote shell to use in rsync
  • -z, –compress: compress file data during the transfer
  • -h, –human-readable: display the output numbers in a human-readable format
  • –progress: show the sync progress during transfer

If there are two folders: (with write permission)

If you are inside the folder called PORTAL where you want to copy all content of another folder say DATA at the same level then you will do

vimal@vimal-D3H:/var/www/html/PORTAL$ cp -a ../DATA/. .

You have to notice 2 dots. Last dot says copy here in present folder

one following /DATA/. says that all the CONTENTS inside DATA folder to be copied, and not the DATA folder itself.

If you remove this trailing «.» from /DATA/

then whole DATA folder will be copied inside PORTAL(from where you are coping).

Источник

How to move (and overwrite) all files from one directory to another?

I know of the mv command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?

6 Answers 6

From the man page:

It’s just mv srcdir/* targetdir/ .

If there are too many files in srcdir you might want to try something like the following approach:

In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.

Читайте также:  Горячие клавиши выключения линукс

It’s also possible by using rsync , for example:

  • -v , —verbose : increase verbosity
  • -a , —archive : archive mode; equals -rlptgoD (no -H,-A,-X )
  • —delete-after : delete files on the receiving side be done after the transfer has completed

If you’ve root privileges, prefix with sudo to override potential permission issues.

For moving and overwriting files, it doesn’t look like there is the -R option (when in doubt check your options by typing [your_cmd] —help . Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.

When you type in mv —help it returns the description of all options.

For mv, the syntax is mv [option] [file_source] [file_destination]

To move simple files: mv image.jpg folder/image.jpg

To move as folder into destination mv folder home/folder

To move all files in source to destination mv folder/* home/folder/

Use -v if you want to see what is being done: mv -v

Use -i to prompt before overwriting: mv -i

Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn’t exist yet: mv -u

Tie options together like mv -viu , etc.

If you simply need to answer «y» to all the overwrite prompts, try this:

In linux shell, many commands accept multiple parameters and therefore could be used with wild cards. So, for example if you want to move all files from folder A to folder B, you write:

If you want to move all files with a certain «look» to it, you could do like this:

Which copies all files that are blablabla.txt to folder B

Star (*) can substitute any number of characters or letters while ? can substitute one. For example if you have many files in the shape file_number.ext and you want to move only the ones that have two digit numbers, you could use a command like this:

Or more complicated examples:

For files that look like fi _ .e

Unlike many commands in shell that require -R to (for example) copy or remove subfolders, mv does that itself.

Remember that mv overwrites without asking (unless the files being overwritten are read only or you don’t have permission) so make sure you don’t lose anything in the process.

For your future information, if you have subfolders that you want to copy, you could use the -R option, saying you want to do the command recursively. So it would look something like this:

By the way, all I said works with rm (remove, delete) and cp (copy) too and beware, because once you delete, there is no turning back! Avoid commands like rm * -R unless you are sure what you are doing.

Источник

Copy files from one directory into an existing directory

In bash I need to do this:

take all files in a directory

copy them into an existing directory

How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don’t want that, I need the files in t1 to go directly inside t2. How do I do this?

8 Answers 8

What you want is:

The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.

Or if you have directories inside dir1 that you’d want to copy as well

If you want to copy something from one directory into the current directory, do this:

Читайте также:  Как записать linux live usb

This assumes you’re not trying to copy hidden files.

Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:

Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:

Or you can navigate to the folder with files (t1) and do this:

Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).

For inside some directory, this will be use full as it copy all contents from «folder1» to new directory «folder2» inside some directory.

$(pwd) will get path for current directory.

Notice the dot (.) after folder1 to get all contents inside folder1

The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand’s answer has (copying t1/* misses invisible files, copying `t1/* t1/.*’ copies t1/. and t1/. which you don’t want).

This copies all the contents of folder1 to an existing folder2 :

Depending on some details you might need to do something like this:

. this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.

The extra fussing is to handle relative or absolute targets.

(This doesn’t rely on subtle semantics of the cp command itself . about how it handles source specifications with or without a trailing / . since I’m not sure those are stable, portable, and reliable beyond just GNU cp and I don’t know if they’ll continue to be so in the future).

Источник

How do I copy folder with files to another folder in Unix/Linux? [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 8 years ago .

I am having some issues to copy a folder with files in that folder into another folder. Command cp -r doesn’t copy files in the folder.

3 Answers 3

The option you’re looking for is -R .

  • If destination doesn’t exist, it will be created.
  • -R means copy directories recursively . You can also use -r since it’s case-insensitive.
  • To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 ‘s / @Anton Krug ‘s comment):

You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.

If the directory you’re copying is called dir1 and you want to copy it to your /home/Pictures folder:

Linux is case-sensitive and also needs the / after each directory to know that it isn’t a file.

is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command pwd .

When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:

at a terminal prompt.

Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.

Источник

Оцените статью