4

Is there a way to batch copy/move files in unix? I thought it might be an option for cp/mv but I can't find any way to do it.

rake
  • 231
  • 2
    cp and mv are pretty good choices. What additional features do you need? For renaming, see http://unix.stackexchange.com/q/1136/8250 – Lekensteyn May 06 '12 at 17:47
  • Yes there is. What files are you going to copy/move? –  May 06 '12 at 18:39
  • 2
    What exactly do you mean by "batch copy" that you couldn't get done by cp or mv? – Mat May 06 '12 at 18:39
  • @Lekensteyn and Mat: I'm trying to move every file in a directory I extracted from an archive into the directory aforementioned directory is contained in. – rake May 06 '12 at 18:57

2 Answers2

5

cp and mv are both good tools for copying files and moving files respectively. Combined with shell constructs (loops like for file in *; do ...; done) or find, they become even more powerful.

If I understood your purpose correctly, you have extracted files like:

somedir/file.png
somedir/file.txt

and want to move all files into the parent directory such that it looks like:

file.png
file.txt

Using shell wildcards, assuming that you are in the directory somedir, you can use:

mv * ../

* is a glob pattern that expands to all files in the current directory. If you have hidden files (files and directories with a leading . in their filename), use shopt -s dotglob in bash).

I've mentioned find before. If you have a tree of files like:

somedir/000.txt
somedir/a/001.txt
somedir/a/004.txt
somedir/b/003.txt
somedir/b/002.txt
somedir/whatever/somethingunique.txt

and want to move all files in one directory (say, seconddir), use:

find -exec mv {} seconddir/ \;

This assumes that all filenames are unique, otherwise files would get overwritten.

Lekensteyn
  • 20,830
0

You might get away nicely with rename as it's more apt to batch processing.

Morgan
  • 101