Batch copying/moving files in unix?

17,674

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.

Share:
17,674
rake
Author by

rake

Updated on September 18, 2022

Comments

  • rake
    rake almost 2 years

    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.

    • Lekensteyn
      Lekensteyn about 12 years
      cp and mv are pretty good choices. What additional features do you need? For renaming, see unix.stackexchange.com/q/1136/8250
    • Admin
      Admin about 12 years
      Yes there is. What files are you going to copy/move?
    • Mat
      Mat about 12 years
      What exactly do you mean by "batch copy" that you couldn't get done by cp or mv?
    • rake
      rake about 12 years
      @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.