简体   繁体   English

如何通过*将包括隐藏文件在内的所有文件移动到父目录中

[英]How to move all files including hidden files into parent directory via *

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?怎么做?

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?您可以在 UNIX 和 Linux 对如何将所有文件(包括隐藏文件)从一个目录移动到另一个目录的回答中找到有关此问题的全面解决方案 . . It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.它显示了 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 ab ab ) (例如: echo a{.,}b扩展为ab 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/.忽略它们:发生这种情况是因为/path/subfolder/{.,}*也扩展到/path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder? )./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/.这将复制所有文件,包括普通文件和隐藏文件,因为/path/subfolder/. expands to "everything from this directory" (Source: How to copy with cp to include hidden files and hidden directories and their contents? )扩展为“此目录中的所有内容”(来源:如何使用 cp 复制以包含隐藏文件和隐藏目录及其内容?

我认为这是最优雅的,因为它也不会尝试移动..

mv /source/path/{.[!.],}* /destination/path

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

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.使用的-u参数会跳过现有的较新文件, -r递归到目录中, -v会增加详细程度。

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (eg .. and . ) and subdirectories.通过将find命令与mv命令结合使用,您可以防止mv命令尝试移动目录(例如... )和子目录。 Here's one option:这是一种选择:

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 .此外,2) 包括 . and .. files and 3) misses files like ..foobar, ...barfoo, etc.和 .. 文件和 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.您可以使用mv /source/path/{.[!.],..?,}* /destination/path ,其中包含 3) 中遗漏的文件,但它仍会尝试移动子目录。 Using the find command with the mv command as I describe above eliminates all these problems.使用上面描述的find命令和mv命令可以消除所有这些问题。

Let me introduce you to my friend "dotglob".让我向您介绍我的朋友“dotglob”。 It turns on and off whether or not "*" includes hidden files.无论“*”是否包含隐藏文件,它都会打开和关闭。

$ 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.最好在完成后重新打开它,否则你会混淆假设它会关闭的东西。

My solution for this problem when I have to copy all the files (including . files) to a target directory retaining the permissions is: (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)请注意,源路径不以/结尾(因此所有文件/目录和 . 文件都被复制)

Destination directory ends with / as we are placing contents of the source folder to destination as a whole.目标目录以/结尾,因为我们将源文件夹的内容作为一个整体放置到目标中。

Just do做就是了

for I in $(ls -A dir)
do
mv dir/$I newDir
done

Assuming you are in the subfolder run find . -maxdepth 1 -exec mv {} .. \\;假设您在子文件夹中运行find . -maxdepth 1 -exec mv {} .. \\; find . -maxdepth 1 -exec mv {} .. \\;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示当前目录而不是父目录中的所有隐藏文件 - display all hidden files in the current directory not in the parent directory 如何在 Linux 中显示所有文件的列表,包括隐藏文件? - How to display a list of all files including hidden files in Linux? 循环访问linux / android中目录中的所有文件(包括以'。'开头的文件-隐藏文件) - Iterate all files in a directory (including files that start with '.' - hidden files) in linux/android 如何移动/复制目录中的很多文件(不是所有文件)? - How to move/copy a lot of files (not all files) in a directory? 如何使用 ls 命令显示所有文件,包括隐藏文件,但不显示名为 '.' 的文件和 '..' - How to use ls command to show all the files including hidden files, but not showing the files named '.' and '..' 如何对目录中的所有文件进行tar压缩并将该tar移至另一个目录 - how to tar all files in a directory and move that tar to another directory 如何定义 Linux 中所有文件的别名,包括隐藏文件、列表编号、总行数? - How to define the alias with all the files including hidden files, list numbering, total lines in Linux? 将文件移动到当前位置的父目录 - Move files to parent directory of current location bash:将父目录下所有匹配的文件递归移动到新目录下 - bash: recursively move all matched files in sub directory of parent to new sub directory 在shell中按名称对目录内容(包括隐藏文件)进行排序 - Sorting directory contents (including hidden files) by name in the shell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM