I am trying to pass an array of file paths to xargs to move them all to a new location. My script is currently working as follows:

我正在尝试将文件路径数组传递给xargs,以将它们全部移动到一个新的位置。我的脚本目前工作如下:

FILES=( /path/to/files/*identifier* )
if [ -f ${FILES[0]} ]
  then
    mv ${FILES[@]} /path/to/destination
fi

The reason for having FILES as a array was because the if [ -f /path/to/files/*identifier* ] fails if the wildcard search returns multiple files. Only the first file is checked, because the move will be executed if any files exist.

将文件作为数组的原因是,如果通配符搜索返回多个文件,则if [-f /path/to/ FILES /*identifier*]失败。只检查第一个文件,因为如果存在任何文件,将执行移动。

I want to replace mv ${FILES[@]} /path/to/destination with a line that passes ${FILES[@]} to xargs to move each file. I need to use xargs as I expect to have enough files to overload a single mv. Through research I have only been able to find the methods of moving files that I already know which search for the files again.

我想用一行代码替换mv ${FILES[@]} /path/to/destination,该代码将${FILES[@]}传递给xargs以移动每个文件。我需要使用xargs,因为我希望有足够的文件来重载一个mv。通过研究,我只找到了移动文件的方法,我已经知道了在搜索文件的方法。

#Method 1
ls /path/to/files/*identifier* | xargs -i mv '{}' /path/to/destination

#Method 2
find /path/to/files/*identifier* | xargs -i mv '{}' /path/to/destination

Is there a way to can pass all elements in an existing array ${FILES[@]} to xargs?

Below are methods I've tried and their errors.

下面是我尝试过的方法和它们的错误。

Attempt 1:

尝试1:

echo ${FILES[@]} | xargs -i mv '{}' /path/to/destination

Error:

错误:

mv: cannot stat `/path/to/files/file1.zip /path/to/files/file2.zip /path/to/files/file3.zip /path/to/files/file4.zip': No such file or directory

Attempt 2: I wasn't sure if xargs can be executed directly or not.

尝试2:我不确定xargs是否可以直接执行。

xargs -i mv ${FILES[@]} /path/to/destination

Error: No error message was output, but it hung after that line until I stopped it manually.

错误:没有输出错误消息,但是它挂在这一行之后,直到我手动停止它。

Edit: Find works

I tried the following and it moved all the files. Is this the best way to do it? And is it moving the files one by one, so the terminal is not overloaded?

我尝试了以下操作,它移动了所有的文件。这是最好的方法吗?它是一个一个移动文件,所以终端没有过载?

find ${FILES[@]} | xargs -i mv '{}' /path/to/destination

Edit 2:

For future reference, I tested the accepted answer method versus the method in my first edit using time(). After running both methods 4 times, my method had an average of 0.659s and the accepted answer was 0.667s. So neither method works any faster than the other.

为了以后的参考,我在第一次使用time()时测试了被接受的答案方法和方法。两种方法同时运行4次后,我的方法的平均值为0.659s,接受的结果为0.667s。所以这两种方法都不能比另一种更快。

1 个解决方案

#1


31

When you do

当你做

echo ${FILES[@]} | xargs -i mv '{}' /path/to/destination

xargs treats the entire line as a singe argument. You should split each element of the array to a new line, and then xargs should work as expected:

xargs将整行视为一个singe参数。您应该将数组的每个元素拆分为一个新行,然后xargs应该按照预期工作:

printf "%s\n" "${FILES[@]}" | xargs -i mv '{}' /path/to/destination

Or if your filenames can contain newlines, you can do

或者如果你的文件名可以包含换行,你可以这样做

printf "%s\0" "${FILES[@]}" | xargs -0 -i mv '{}' /path/to/destination

更多相关文章

  1. Linux学习总结(十五)文件查找 which whereis locate find
  2. 嵌入式Linux文件系统及其存储机制分析
  3. Oracle表按字段和|分格符导出文件
  4. linux使用rz和sz命令,实现小文件上传下载
  5. linux 服务器间文件传输
  6. 拆分gzip压缩日志文件而不将未压缩的拆分存储在磁盘上
  7. Linux 环境变量与文件查找
  8. 为什么Linux不需要磁盘碎片整理?——借以复习文件系统方面的知识
  9. linux下《UNIX环境高级编程》(apue2)源码编译出错的处理方法

随机推荐

  1. 加载AnimationDrawable 从xml file
  2. Android——拖动条SeekBar
  3. android selector的使用
  4. Android EditText 输入校验
  5. Android Maven Plugin
  6. android网络编程注意事项之一:移动网络下,
  7. Android静态,动态广播示例
  8. android中View.measure方法详解
  9. android apk 为程序增加代码混淆
  10. Android SDK下载和更新失败的解决方法