简体   繁体   English

如何移动/复制目录中的很多文件(不是所有文件)?

[英]How to move/copy a lot of files (not all files) in a directory?

I got a directory which contains approx 9000 files, the file names are in ascending number (however not necessarily consecutive). 我得到一个包含大约9000个文件的目录,文件名以升序排列(但是不一定是连续的)。

Now I need to copy/move ~3000 files from number xxxx to number yyyy to another direcotory. 现在,我需要将约3000个文件从编号xxxx复制/移动到yyyy到另一个目录。 How can I use cp or mv command for that purpose? 我该如何使用cp或mv命令呢?

find -type f | while read file; do if [ "$file" -ge xxxx -o "$file" -le yyyy ]; then echo $file; fi; done | xargs cp -t /destination/

If you want to limit to 3000 files, do: 如果要限制为3000个文件,请执行以下操作:

export i=0; find -type f | while read file; do if [ "$file" -ge xxxx -o "$file" -le yyyy ]; then echo $file; let i+=1; fi; if [ $i -gt 3000 ]; then break; fi; done | xargs cp -t /destination/

If the files have a common suffix after the number, use ${file%%suffix} inside the if (you can use globs in the suffix). 如果文件的数字后缀有公共后缀,请在if内使用$ {file %% suffix}(可以在后缀中使用glob)。

This isn't the most elegant, but how about something like: 这不是最优雅的,但是类似:

cp 462[5-9] 46[3-9]? 4[7-9]?? 5??? 6[0-2]?? 63[0-4]? 635[0-3]  otherDirectory

which would copy files named 4625 to 6353 inclusive to otherDirectory . 这会将名为4625的文件复制到6353(含)到otherDirectory (You wouldn't want to use something like 4* since that would copy the file 4 , 42 , 483 , etc.) (你不会想用喜欢的事, 4* ,因为这将复制文件442483 ,等)

You can use the seq utility to generate numbers for this kind of operation: 您可以使用seq实用程序为这种操作生成数字:

for i in `seq 4073 7843` ; do cp file_${i}_name.png /destination/folder ; done

On the downside, this will execute cp a lot more often than QuantumMechanic's solution; 不利的一面是,与QuantumMechanic的解决方案相比,这将更频繁地执行cp but QuantumMechanic's solution may not execute if the total length of all the filenames is greater than the kernel's argv size limitation (which could be between 128K and 2048K, depending upon your kernel version and stack-size rlimits; see execve(2) for details). 但是,如果所有文件名的总长度大于内核的argv大小限制(可能在128K和2048K之间,具体取决于内核版本和堆栈大小rlimits;有关详细信息,请参阅execve(2) ,但QuantumMechanic的解决方案可能不会执行。 。

If the range you want spans orders of magnitudes (eg, between 900 and 1010 ) then the seq -w option may be useful, it zero-pads the output numbers. 如果您想要的范围跨几个数量级(例如,介于9001010之间),那么seq -w选项可能有用,它将输出数字零填充。

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

相关问题 无论文件夹深度或数量如何,复制或移动目录中的所有文件 - Copy or move all files in a directory regardles of folder depth or number 如何通过*将包括隐藏文件在内的所有文件移动到父目录中 - How to move all files including hidden files into parent directory via * 如何对目录中的所有文件进行tar压缩并将该tar移至另一个目录 - how to tar all files in a directory and move that tar to another directory 如何将单个目录中的所有文件移动到多个目录? - How to Move all files within a single directory to multiple directories? 如何移动目录中具有特定前缀的所有文件? - How to move all files in a directory that have a specific prefix? 如何将所有文件按扩展名移动到指定DIR目录中的子目录? - How to move all files to subdirectories by extension name in the specified DIR directory? 如何将所有文件从一个目录移动(并覆盖)到另一个目录? - How to move (and overwrite) all files from one directory to another? 如何将具有相同后缀的所有文件复制到另一个目录? -Unix - How to copy all the files with the same suffix to another directory? - Unix 如何将目录及其子目录中的所有文件复制到另一个目录? - How to copy all files from a directory and its subdirectories to another dir? 监视触发文件并复制该目录中的所有文件 - Monitor for trigger file and copy all files in that directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM