Linux map folder to folder

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?

I know this is old question, but what about «rsync -az src/ dest/» it will copy all files which do not exist in dest/ directory from src/, and then just remove destination directory with «rm -rf dest/»

6 Answers 6

-f, --force do not prompt before overwriting 

mv -f /a/b /c/d moves b inside d (so I have /c/d/a ) instead of overwriting d with a (I want /c/a ). How can I do such overwrite?

@Xenos you would need to delete the file d before you can create a directory d . That would require a couple of commands, there’s no force overwrite of a directory with a file that I know of. You could script that with just a few lines, but it would be better taken up as a separate question since it deviates from question the OPasked.

It’s just mv srcdir/* targetdir/ .

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

cd srcdir find -exec mv <> targetdir/ + 

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

just a note: on directories with a couple of thousands files, you wont be able to use the * since the argument list will be too long for the shell to handle, so you would need to use «find» instead or similar solution

I’ve tried the following command «find . -exec mv <> ../clean-copy/Ressources/img/ +» but got the following error : «find: -exec: no terminating «;» or «+»» — Any idea why ?

@musiKk Thanks for the tip. Although my comment is a question, it’s here because I feel the answer is 99% helping but missing the 1% that would make it perfect.

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

rsync -va --delete-after src/ dst/ 
  • -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.

yup, just lost all my files. Shame on me for thinking people online know what they are talking about.

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/

Читайте также:  Базовая настройка безопасности linux

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.

Источник

Mapping the home folder to a different location in fstab

I’m looking to map my /home folder to a different location/drive on the machine. When I view the fstab file I see the following:

/dev/mapper/cl-home /home xfs defaults 0 0 /dev/mapper/cl-swap swap swap defaults 0 0 /dev/sda1 /mnt/store/hd2 ntfs defaults,auto 0 0 
/mnt/store/hd2/home/ /home ntfs defaults 0 0 

fstab does not copy data, it only controls which volumes are mounted by the system. If you want to change the drive your home directory, copy the data to it, then change your fstab to match. What you are trying to do in the 2nd example is mount a directory on a directory, which won’t work.

In regards to your question about what «cl» is, I’m guessing it’s a volume group and the system is using LVM. To confirm, what are the outputs of these commands run as root or with sudo ? pvs , vgs , lvs

1 Answer 1

In /etc/fstab , the first column is a volume location and the second column is a directory. The directory is the mount point, i.e. where the files will be accessible. The volume location indicates where the files are stored; there are different types of locations depending on the filesystem type. For a “normal” filesystems, the files that are stored on a disk and the volume location is a disk partition. For a network filesystem such as nfs or cifs , this indicates a host name and an exported path on the host, and so on.

What you currently have, /dev/mapper/cl-home , designates a partition using Linux’s volume format (LVM). The volume name is in two parts: cl is a volume group (which covers a section of one or more disks), and home is a logical volume inside this volume group. The system doesn’t care that the logical volume home and the directory /home have the same name, but it’s convenient for humans to use the same name.

If you want to put your home directory on an existing Windows partition, then you can’t just change the volume name here: /home would not be the place where a disk filesystem is mounted. There are several ways you can do this:

    You can use a bind mount to make /mnt/store/hd2/home also accessible through /home . The fstab entries would be

/dev/sda1 /mnt/store/hd2 ntfs /mnt/store/hd2/home /home bind 
sudo usermod --home /mnt/store/hd2/linux-home --move-home joe 

I don’t recommend any of these options, because NTFS can’t store all the Linux file names, types and attributes. All of these options have further gotchas:

  • Bind mounts are a very useful tool, but they do have downsides. Files are listed at all the locations in enumerations, which has consequences on locate , etc , etc.
  • A symbolic link doesn’t have these downsides, but occasionally some software will record the location of your home directory with the symbolic links expanded. Having a symbolic link for /home can also cause problems due to AppArmor policies.
  • Even having a home directory outside /home can cause gotchas with security policies, though it should be ok with any major distribution nowadays.
Читайте также:  Com port usb linux

Rather than put your home directory on an NTFS filesystem, I recommend keeping it on a Linux filesystem. To access your Windows files from Linux, access them under /mnt/store/hd2 . Create symbolic links in your home directory to places under /mnt/store/hd2 for convenience.

Источник

moving files and folders to a subfolder

I would like to move all files and folder from one directory to one of its subfolders. How do I do that? I am using BusyBox and linux. ex: move all files and folder in /my/path/ to /my/path/subfolder/. Copy, and then delete solutions are not affordable. Thanks.

6 Answers 6

Of course, it will fail moving the «subfolder» directory into itself, but everything else will move

Is this in mv’s specification? or is this hoping to be lucky? it may stop at first failure and I will end up with some of files and folder being moves but not all.

Files with name starting with a dot (like .git) are excluded to. To hotfix this run mv .* subfolder (mv dot-start subfolder)

Solutions that use * (expanded by shell) won’t work with too many objects in /my/path/ . In such case you’ll get:

cd /my/path/ && find . -mindepth 1 -maxdepth 1 ! -name subfolder -exec mv -t subfolder/ <> + 

Unfortunately -mindepth and -maxdepth options of find are not POSIX-compliant; neither -t option of mv is, I think.

This variant should satisfy POSIX:

(I adapted this Unix & Linux SE answer). Sadly it calls mv for every object found, thus it’s slow.

Fast alternative approach, if only you can create directories anew (initially neither /my/path/subfolder/ nor /my/subfolder/ should exist):

Note on inode-based filesystem this should be equally fast, no matter how many objects there are in path/ . The code:

cd /my/ && test ! -e subfolder/ && mv path/ subfolder/ && mkdir path/ && mv subfolder/ path/ 

In this case I used && a lot to emphasize the procedure should abort if any of its steps fails. However this approach is inconvenient if you need path/ or subfolder/ to have non-default permissions, ownership etc.

Источник

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.

@DylanValade: -a already implies —preserve=all , that is wider than -p = —preserve=mode,ownership,timestamps .

@BennyNeugebauer: scp is used to copy over a network (through ssh ) and only encrypts the communication channel, not the files on the destination filesystem.

«The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.», this is not related to cp, but related to bash. The dot means «this location» and avoids the use of bash globbing, where by default files/directories starting with a . are not expanded.

Can someone explain the need behind use of mypath/. instead of the existing bash wildcard expansion character, mypath/* ?

rsync -a source/ destination 

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.
Читайте также:  Год разработки ос linux

This will not copy hidden files, since bash expands * only to non-hidden file. The solution by @Joschua is safer.

If it’s a big folder you may wish to use one of these options to view progress while it’s copying askubuntu.com/questions/609303/…

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 ~/folder1 to ~/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.

Thank you Bruno! It helped me to understand the syntax, though I had to change it a bit(removing ~ 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!

The trailing period is important. Without it, sometimes it may create a new subdirectory ~/new_folder1/folder1 instead of copying the contents over.

@Alex78191 [root@ home]# mkdir food [root@ home]# cd food/ [root@ food]# mkdir .fruit [root@ food]# mkdir veggies [root@ food]# touch veggies/carrots [root@ food]# touch .fruit/apple [root@ food]# ls * carrots [root@ food]#

Simple example.

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

cp -r ./dir_1 ./dir_2 # or cp -r ./dir_1/ ./dir_2/ # Results in: ./dir_2/dir_1/_files_ 

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

cp -r ./dir_1/. ./dir_2 # or cp -r ./dir_1/. ./dir_2/ # Results in: ./dir_2/_files_ 

_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:

cp source destination cp dir1 dir2 cp -option source destination cp -option1 -option2 source destination 

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

cp -avr /home/vivek/letters /usb/backup 

-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:

This code with Flag «-R» copies perfectly all the contents of «folder1» to existing «folder2»:

Flag «-R» copies symbolic links as well but Flag «-r» skips symbolic links so Flag «-R» is better than Flag «-r».

-R, --dereference-recursive For each directory operand, read and process all files in that directory, recursively, following all symbolic links. 
-r, --recursive For each directory operand, read and process all files in that directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. Note that if no file operand is given, grep searches the working directory. This is the same as the ‘--directories=recurse’ option. 

Источник

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