[英]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*
,因为这将复制文件4
, 42
, 483
,等)
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. 如果您想要的范围跨几个数量级(例如,介于
900
和1010
之间),那么seq -w
选项可能有用,它将输出数字零填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.