Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving files recurisively on linux

Tags:

linux

find

xargs

find ./dir -type f -iname "*.t[argz]*[bz2]" -print | xargs mv --target-directory=dir seems to fail on file that has spaces in the name. how to improve it? or alternative?

thanks for answer below: my mv doesn't support --null or -0, I'm using cygwin:

$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments t
.

like image 404
user217631 Avatar asked Jan 07 '10 19:01

user217631


People also ask

How do I move multiple files in Linux?

The mv (move) command is used to move one or more files or directories from one directory to another directory using terminal in the Linux/Unix operating system. After using the mv command file is copied from source to destination and source file is removed. The mv command is also used to rename the file.

How do you recursively move?

1 Answer. To recursively move files, combine find with mv. To rename your files when you move them it's trickier. One way is to have the loop that passes the file name through tr / _, which converts slashes into your underscores.

How do you move files in Linux?

After you have selected each file (Figure 2), you can either right-click one of the selected files and then choose the Move To option, or just drag and drop them into a new location. The selected files (in this case, folders) will each be highlighted. Moving files on the Linux desktop is incredibly easy.

Is mv recursive?

In that sense, mv already provides the recursive property, i.e. the "renaming" of all entries in the renamed directory, e.g. from a/1 to b/1 .


1 Answers

Use -print0 instead of -print on the find command, and the xargs -0 (or --null) option - then NULs will be used as separators rather than newlines and spaces.

find ./dir -type f -iname "*.t[argz]*[bz2]" -print0 | xargs --null mv --target-directory=dir
like image 142
martin clayton Avatar answered Oct 20 '22 04:10

martin clayton