【问题标题】:Linux command to move all the files from var/A to var/Linux命令将所有文件从var/A移动到var/
【发布时间】:2018-11-22 18:03:57
【问题描述】:

我猜这里有人问过这个问题,但我没找到。

我在目录 var/ 中,我有一个文件夹 var/A,里面有一些文件,我想要将 A 中的这些文件移动到 var/。所以我想做的是:

来自:

var
├── A
|   ├── file1.txt
|   └── file2.txt
└── file3.txt

到:

var
├── file1.txt
├── file2.txt
└── file3.txt

在 /var 中我尝试了以下命令

sudo mv A /

sudo mv A/ ./

sudo mv A/ .

而且没有人工作过。提前谢谢你

【问题讨论】:

标签: linux ubuntu command-line path mv


【解决方案1】:

我设法复制了您的情况。

.
└── var
    ├── A
    │   ├── file1.txt
    │   └── file2.txt
    └── file3.txt

2 directories, 3 files
user@host:~/soQ$ mv var/A/*.txt var/ && rm -rf var/A/
user@host:~/soQ$ tree

.
└── var
    ├── file1.txt
    ├── file2.txt
    └── file3.txt

希望这会有所帮助!

如果您想从var 执行此操作,只需执行mv A/*.txt . && rm -rf A/

另外,假设您的目录中有以下结构。

.
├── A
│   ├── file3.txt
│   └── folder1
│       ├── file1.txt
│       └── file2.txt
└── file4.txt

如果你只想复制 var 中的 *.txt 文件,你需要像这样使用 find:

find ./A/* -type f -exec mv {} ${PWD} \; && rm A

这会查看所有文件和文件夹并返回每个文件的路径(例如每个 .txt),然后在每一行上执行 mv 并将文件移动到当前目录 (${PWD})。

【讨论】:

  • 这是一个小例子,在A里面我有更多的文件和文件夹
  • 无论您有多少文件,都应用相同的逻辑。另外,我假设您所有的文件都是.txt 类型。如果您想复制所有内容,您只需将*.txt 替换为*。我看到你说你也有文件夹。让我试试看它是否正常工作,我认为它应该是你想复制文件夹还是只获取每个文件夹中的文件?
  • mv 如果您也有文件夹也可以工作(它会复制文件夹)。如果您只想复制文件,请尝试此操作。 find ./A/* -type f -exec mv {} ${PWD} \;
【解决方案2】:

我找到了答案:

mv A/* .

我希望它可以帮助另一个人

【讨论】:

    【解决方案3】:

    您必须指定正确的路径或匹配 如果要移动整个文件或目录,则必须移动 像这样使用 *(通配符) user@akuma:~/var$ mv ./A/* ./

    您可以在终端中输入mv --help 以获取更多信息,以查看更多选项,如下所示

     user@akuma:~/WORKSPACE/Trash$ mv --help
    
      Usage: mv [OPTION]... [-T] SOURCE DEST
      or:  mv [OPTION]... SOURCE... DIRECTORY
      or:  mv [OPTION]... -t DIRECTORY SOURCE...
      Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
    
      Mandatory arguments to long options are mandatory for short options too.
         --backup[=CONTROL]       make a backup of each existing destination file
     -b                           like --backup but does not accept an argument
     -f, --force                  do not prompt before overwriting
     -i, --interactive            prompt before overwrite
     -n, --no-clobber             do not overwrite an existing file
     If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
     -S, --suffix=SUFFIX          override the usual backup suffix
     -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
     -T, --no-target-directory    treat DEST as a normal file
     -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
     -v, --verbose                explain what is being done
     -Z, --context                set SELinux security context of destination
                                 file to default type
         --help     display this help and exit
         --version  output version information and exit
    
      The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
      The version control method may be selected via the --backup option or through
      the VERSION_CONTROL environment variable.  Here are the values:
    
    none, off       never make backups (even if --backup is given)
    numbered, t     make numbered backups
    existing, nil   numbered if numbered backups exist, simple otherwise
    simple, never   always make simple backups
    
    GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
    Full documentation at: <http://www.gnu.org/software/coreutils/mv>
    or available locally via: info '(coreutils) mv invocation'
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2015-05-27
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2011-11-29
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多