Переместить группу файлов linux

Move files and folders recursively on Linux

How do I move the contents of /public-back/templates recursively with permissions into /public/templates ?

9 Answers 9

Unless I am misunderstanding the question, this would work:

mv /public-back/templates/* /public/templates 

Also, unless you have a huge list of files, adding -i will ask before it overwrites anything, which add some safety when using wildcards like * .

Seems that it flattened the directory hierarchy. Did not preserve folders within the original folder for me. All files ended up in the same folder, rather than literally moving the original folder with the same structure.

The man page for cp states:

-p same as --preserve=mode,ownership,timestamps -r same as --recursive=copy directories recursively 
cp -rp /public-back/templates/* /public/templates/ 

cp is way slower than mv . cp forces the computer to copy everything from the disk to RAM, and then write it to the disk again which may take a long time depending on the size of the files. However, mv always happens instantly because only the links have to be rewritten.

@Marius Butuc What? First, move is like cut and paste, while copy preserves the original. that’s not just a semantic difference. Second, why are you talkintg about mv? How in the world does that comment have 16 votes. it’s flat out wrong.

When moving items from my thumb drive to my OSMC system, I’ve found the following very useful:

find /media/Pi\ Hard\ 16GB/ -name '*' -exec mv -v <> /media/External\ HDD/Videos/ \; 

Explanation on how it works below.

BTW, Don’t forget to add a backslash before any spaces in the source or destination directory names (see above).

find finds all files and folders in the destination path. /media/Pi Hard 16GB/ is the path searched. Escape special char such as spaces. -name '*' filters on names. If you do not escape or quote this then the shell will expand it before find sees it. -exec Executes a command, in our case mv -v Verbose, so you can see what's happening (optional) <> is replaced by the name of the found object. 

Effectively, you are finding all files and all folders and moving them one by one (or if a directory gets found first, you are moving that directory and the contents in it). This starts a new process for each move and is very inefficient. Only use this when the regular commands fail.

mv doesn’t seem to do this. But you can use this little trick, works like a charm:

tar cf - . |(cd /targetdir; tar xvf -) 

and preserves permissions and all.

Note: none of the above worked for me, that’s why this workaround.

It is possible to move instead of copy with rsync by using the —remove-source-files argument. This will preserve properties such as permissions and dates modified. It has the added benefit of checking whether files don’t need to be moved to the target directory (i.e., if a newer file with the same name already exists there).

rsync -arctuxz --remove-source-files /public-back/templates /public/templates/ 

Of course you can also copy the files and remove the original directory.

mkdir -p /public/templates rsync -arctuxz --remove-source-files /public-back/templates /public/templates/ rm -rfi /public-back/templates/ 

This is my recommended parameters for rsync but there are other arguments for preserving various properties or handling links and compression/encryption of large files. This command also supports copying to remote file systems via ssh tunnels.

Читайте также:  Delete users arch linux

It doesn’t seem to move hidden files (eg: .htaccess), nor any content within folders which are hidden as well (eg: files within .git/ )

cp -a --link ../public-back/* /public/. && rm -rf ../public-back 

So create hard links in the destination directory and remove the source dir. ‘mv’ simply will not work in your case, and in general works only when source dir and dest have no common subtrees.

Note that I’m assuming that the word ‘move’ in the question means that the source dir should be gone after the operation.

None of the answers in this thread fit my usecase, so I came up with one on my own as a shell script.

At the heart of it is this function:

Which you could invoke like so (for the OP usecase):

recurse /public-back/templates /public/templates 

If source is a file or a directory for which the destination does not exist yet, it simply moves it with mv -u . If source is a directory for which the destination already exists, it iterates through its contents and recursively performs the same check followed by move|recurse with each of its members.

I used -u because I only needed old files freshened and newer files untouched, but you could replace it with -f for an unconditional move or -i for an interactive one. Or not change anything and simply rm -rf your source after the script is done moving things.

[A slightly modified repost from Server Fault]

As noted above, on the same filesystem mv is faster than cp . By example, the following preserves timestamps and ownerships, and recursively moves directories and files including hidden files and directories.

Initial conditions:

[victoria@victoria test]$ tree -La 5 -F . └── hourly.0/ ├── .dir01/ ├── dir01/ │ ├── .dir02/ │ ├── dir02/ │ │ ├── .dir03/ │ │ ├── dir03/ │ │ │ ├── .file03 │ │ │ └── file03 │ │ ├── .file02 │ │ └── file02 │ ├── .file01 │ └── file01 ├── .file00 └── file00 7 directories, 8 files [victoria@victoria test]$ ls -laR .: total 12 drwxr-xr-x 3 victoria victoria 4096 Apr 27 10:04 . drwxrwxr-x 4 victoria victoria 4096 Apr 27 09:19 .. drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 hourly.0 ./hourly.0: total 16 drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 . drwxr-xr-x 3 victoria victoria 4096 Apr 27 10:04 .. drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 .dir01 drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 dir01 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 .file00 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 file00 ./hourly.0/.dir01: total 8 drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. ./hourly.0/dir01: total 16 drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 .dir02 drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 dir02 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 .file01 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 file01 ./hourly.0/dir01/.dir02: total 8 drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. ./hourly.0/dir01/dir02: total 16 drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. drwxr-xr-x 2 root victoria 4096 Apr 27 09:44 .dir03 drwxr-xr-x 2 root victoria 4096 Apr 27 09:45 dir03 -rw-r--r-- 1 root victoria 0 Apr 27 09:17 .file02 -rw-r--r-- 1 root victoria 0 Apr 27 09:17 file02 ./hourly.0/dir01/dir02/.dir03: total 8 drwxr-xr-x 2 root victoria 4096 Apr 27 09:44 . drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 .. ./hourly.0/dir01/dir02/dir03: total 8 drwxr-xr-x 2 root victoria 4096 Apr 27 09:45 . drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 .. -rw-r--r-- 1 root victoria 0 Apr 27 09:17 .file03 -rw-r--r-- 1 root victoria 0 Apr 27 09:17 file03 

Move those files, directories:

  • timestamps
  • recursively moves files, directories (including hidden files, directories)
  • preserves ownerships
[victoria@victoria test]$ mv hourly.0/ hourly.1 

After the move:

[victoria@victoria test]$ tree -La 5 -F . └── hourly.1/ ├── .dir01/ ├── dir01/ │ ├── .dir02/ │ ├── dir02/ │ │ ├── .dir03/ │ │ ├── dir03/ │ │ │ ├── .file03 │ │ │ └── file03 │ │ ├── .file02 │ │ └── file02 │ ├── .file01 │ └── file01 ├── .file00 └── file00 7 directories, 8 files [victoria@victoria test]$ ls -laR .: total 12 drwxr-xr-x 3 victoria victoria 4096 Apr 27 10:05 . drwxrwxr-x 4 victoria victoria 4096 Apr 27 09:19 .. drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 hourly.1 ./hourly.1: total 16 drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 . drwxr-xr-x 3 victoria victoria 4096 Apr 27 10:05 .. drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 .dir01 drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 dir01 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 .file00 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 file00 ./hourly.1/.dir01: total 8 drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. ./hourly.1/dir01: total 16 drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 .dir02 drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 dir02 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 .file01 -rw-r--r-- 1 victoria victoria 0 Apr 27 09:17 file01 ./hourly.1/dir01/.dir02: total 8 drwxr-xr-x 2 victoria victoria 4096 Apr 27 09:44 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. ./hourly.1/dir01/dir02: total 16 drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 . drwxr-xr-x 4 victoria victoria 4096 Apr 27 09:45 .. drwxr-xr-x 2 root victoria 4096 Apr 27 09:44 .dir03 drwxr-xr-x 2 root victoria 4096 Apr 27 09:45 dir03 -rw-r--r-- 1 root victoria 0 Apr 27 09:17 .file02 -rw-r--r-- 1 root victoria 0 Apr 27 09:17 file02 ./hourly.1/dir01/dir02/.dir03: total 8 drwxr-xr-x 2 root victoria 4096 Apr 27 09:44 . drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 .. ./hourly.1/dir01/dir02/dir03: total 8 drwxr-xr-x 2 root victoria 4096 Apr 27 09:45 . drwxr-xr-x 4 root victoria 4096 Apr 27 09:45 .. -rw-r--r-- 1 root victoria 0 Apr 27 09:17 .file03 -rw-r--r-- 1 root victoria 0 Apr 27 09:17 file03 [victoria@victoria test]$ 

Источник

Читайте также:  Adding to path linux permanently

How to move all files and folders via mv command [duplicate]

Good point. If you are using bash, then you can run shopt -s dotglob and then «*» will match hidden files, too.

What happens if there are folders and files with the same name in the destination folder? Are they overwritten?

. it seems folders with the same name are not overwritten. mv: cannot move ‘/a/js’ to ‘/b/js’: Directory not empty

I know I accepted the first answer many years ago, and it’s stupid for me now to change it. But actually, I’ve been always using this method.

This works for me in Bash (I think this depends on your shell quite a bit. )

$ mv source/* /destination/folder/here 

When I try I get mv: overwrite ‘destination/.’? mv: overwrite ‘destination/..’? , but adding -n to mv stops it from trying to overwrite

@Putnik — that’s a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this. )

This works for me in Bash 4.2.46, it moves all files and folders including hidden files and folders to another directory

Notice that .[^.]* means all hidden files except . and ..

I’d say it’s a bit boring, but really bullet-proof (GNU) way is:

cd /SourceDir && find ./ -maxdepth 1 -mindepth 1 -exec mv -t /Target/Dir <> +

P. S. Now you can possibly see why lots of people do prefer Midnight Commander, though.

If you only want to do a cut and paste-like action there is a simple way that worked for me:

$mv /media/dir_source $HOME/Documents/ 

It will move the folder named dir_source located in /media to the directory $HOME/Documents/

Читайте также:  Only linux on mac

yet another way just for the heck of it (because I love convoluted ways to do things I guess)

cd /source for f in $(\ls -QA); do eval mv $f /destination/$f; done 

the -Q and the -A are not POSIX, however the -A is fairly prevalent, and to not use the -Q you need to change the IFS (which then means you don’t need the eval but need to quote the variable)

IFS=" " && for f in $(ls -A); do mv "$f" /destination/"$f"; done 

Источник

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