开发者

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

开发者 https://www.devze.com 2023-02-22 15:42 出处:网络
I got 开发者_StackOverflow中文版a directory which contains approx 9000 files, the file names are in ascending number (however not necessarily consecutive).

I got 开发者_StackOverflow中文版a directory which contains approx 9000 files, the file names are in ascending number (however not necessarily consecutive).

Now I need to copy/move ~3000 files from number xxxx to number yyyy to another direcotory. How can I use cp or mv command for that purpose?


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:

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).


You can use the seq utility to generate numbers for this kind of operation:

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; 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).

If the range you want spans orders of magnitudes (e.g., between 900 and 1010) then the seq -w option may be useful, it zero-pads the output numbers.


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. (You wouldn't want to use something like 4* since that would copy the file 4, 42, 483, etc.)

0

精彩评论

暂无评论...
验证码 换一张
取 消