Linux 如何通过*将包括隐藏文件在内的所有文件移动到父目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20192070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move all files including hidden files into parent directory via *
提问by TroodoN-Mike
Its must be a popular question but I could not find an answer.
它一定是一个受欢迎的问题,但我找不到答案。
How to move all files via * including hidden files as well to parent directory like this:
如何通过 * 将所有文件(包括隐藏文件)移动到父目录,如下所示:
mv /path/subfolder/* /path/
This will move all files to parent directory like expected but will not move hidden files. How to do that?
这将像预期的那样将所有文件移动到父目录,但不会移动隐藏文件。怎么做?
采纳答案by fedorqui 'SO stop harming'
You can find a comprehensive set of solutions on this in UNIX & Linux's answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.
您可以在 UNIX 和 Linux 对如何将所有文件(包括隐藏文件)从一个目录移动到另一个目录的回答中找到有关此问题的全面解决方案。. 它显示了 Bash、zsh、ksh93、标准 (POSIX) sh 等的解决方案。
You can use these two commands together:
您可以同时使用这两个命令:
mv /path/subfolder/* /path/ # your current approach
mv /path/subfolder/.* /path/ # this one for hidden files
Or all together (thanks pfnuesel):
或者一起(感谢 pfnuesel):
mv /path/subfolder/{.,}* /path/
Which expands to:
其中扩展为:
mv /path/subfolder/* /path/subfolder/.* /path/
(example: echo a{.,}b
expands to a.b ab
)
(例如:echo a{.,}b
扩展为a.b ab
)
Note this will show a couple of warnings:
请注意,这将显示几个警告:
mv: cannot move ‘/path/subfolder/.' to /path/.': Device or resource busy
mv: cannot remove /path/subfolder/..': Is a directory
Just ignore them: this happens because /path/subfolder/{.,}*
also expands to /path/subfolder/.
and /path/subfolder/..
, which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).
忽略它们:发生这种情况是因为/path/subfolder/{.,}*
也扩展到/path/subfolder/.
and /path/subfolder/..
,它们是目录和父目录(参见“.”和“..”在文件夹中是什么意思?)。
If you want to just copy, you can use a mere:
如果你只想复制,你可以只使用:
cp -r /path/subfolder/. /path/
# ^
# note the dot!
This will copy all files, both normal and hidden ones, since /path/subfolder/.
expands to "everything from this directory" (Source: How to copy with cp to include hidden files and hidden directories and their contents?)
这将复制所有文件,包括普通文件和隐藏文件,因为/path/subfolder/.
扩展为“此目录中的所有内容”(来源:如何使用 cp 复制以包括隐藏文件和隐藏目录及其内容?)
回答by devnull
This will move all files to parent directory like expected but will not move hidden files. How to do that?
这将像预期的那样将所有文件移动到父目录,但不会移动隐藏文件。怎么做?
You could turn on dotglob
:
你可以打开dotglob
:
shopt -s dotglob # This would cause mv below to match hidden files
mv /path/subfolder/* /path/
In order to turn off dotglob
, you'd need to say:
为了关闭dotglob
,你需要说:
shopt -u dotglob
回答by TomOnTime
Let me introduce you to my friend "dotglob". It turns on and off whether or not "*" includes hidden files.
让我向您介绍我的朋友“dotglob”。无论“*”是否包含隐藏文件,它都会打开和关闭。
$ mkdir test
$ cd test
$ touch a b c .hidden .hi .den
$ ls -a
. .. .den .hi .hidden a b c
$ shopt -u dotglob
$ ls *
a b c
$ for i in * ; do echo I found: $i ; done
I found: a
I found: b
I found: c
$ shopt -s dotglob
$ ls *
.den .hi .hidden a b c
$ for i in * ; do echo I found: $i ; done
I found: .den
I found: .hi
I found: .hidden
I found: a
I found: b
I found: c
It defaults to "off".
它默认为“关闭”。
$ shopt dotglob
dotglob off
It is best to turn it back on when you are done otherwise you will confuse things that assume it will be off.
最好在完成后重新打开它,否则你会混淆假设它会关闭的东西。
回答by Vanderstaaij
I think this is the most elegant, as it also does not try to move ..
:
我认为这是最优雅的,因为它也不会尝试移动..
:
mv /source/path/{.[!.],}* /destination/path
回答by kenorb
Alternative simpler solution is to use rsync
utility:
另一种更简单的解决方案是使用rsync
实用程序:
sudo rsync -vuar --delete-after --dry-run path/subfolder/ path/
Note: Above command will show what is going to be changed. To execute the actual changes, remove --dry-run
.
注意:上面的命令将显示将要更改的内容。要执行实际更改,请删除--dry-run
.
The advantage is that the original folder (subfolder
) would be removed as well as part of the command, and when using mv
examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.
优点是原始文件夹 ( subfolder
) 将被删除以及命令的一部分,并且在mv
此处使用示例时,您仍然需要清理文件夹,更不用说将隐藏和非隐藏文件合二为一的额外麻烦图案。
In addition rsync
provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were (-a
).
此外,rsync
还支持在远程设备之间复制/移动文件,并确保文件完全按照原样复制 ( -a
)。
The used -u
parameter would skip existing newer files, -r
recurse into directories and -v
would increase verbosity.
used-u
参数将跳过现有的较新文件,-r
递归到目录中并-v
增加详细程度。
回答by teancum144
By using the find
command in conjunction with the mv
command, you can prevent the mv
command from trying to move directories (e.g. ..
and .
) and subdirectories. Here's one option:
通过将find
命令与命令结合使用mv
,您可以防止mv
命令尝试移动目录(例如..
和.
)和子目录。这是一种选择:
find /path/subfolder -maxdepth 1 -type f -name '*' -exec mv -n {} /path \;
There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:
提供的其他一些答案存在问题。例如,以下每个都将尝试从源路径移动子目录:
1) mv /path/subfolder/* /path/ ; mv /path/subfolder/.* /path/
2) mv /path/subfolder/{.,}* /path/
3) mv /source/path/{.[!.],}* /destination/path
Also, 2) includes the . and .. files and 3) misses files like ..foobar, ...barfoo, etc.
此外,2) 包括 . 和 .. 文件和 3) 遗漏了 ..foobar、...barfoo 等文件。
You could use, mv /source/path/{.[!.],..?,}* /destination/path
, which would include the files missed by 3), but it would still try to move subdirectories. Using the find
command with the mv
command as I describe above eliminates all these problems.
您可以使用, mv /source/path/{.[!.],..?,}* /destination/path
,这将包含 3) 遗漏的文件,但它仍会尝试移动子目录。将find
命令与mv
我上面描述的命令一起使用可以消除所有这些问题。
回答by Rajneesh Gadge
My solution for this problem when I have to copy all the files(including .
files) to a target directory retaining the permissionsis: (overwrite if already exists)
当我必须将所有文件(包括.
文件)复制到保留权限的目标目录时,我对此问题的解决方案是:(如果已存在则覆盖)
yes | cp -rvp /source/directory /destination/directory/
yes
is for automatically overwriting destination files,
r
recursive,
v
verbose,
p
retain permissions.
yes
用于自动覆盖目标文件,
r
递归,
v
详细,
p
保留权限。
Notice that the source path is not ending with a /
(so all the files/directory and . files are copied)
请注意,源路径不以 a 结尾/
(因此所有文件/目录和 . 文件都被复制)
Destination directory ends with /
as we are placing contents of the source folder to destination as a whole.
目标目录以/
我们将源文件夹的内容作为一个整体放置到目标中时结束。