2

I can't find a quick solution for this simple problem: I' ve got this tree:

fatherDir
   -File1.txt
   -SomeFile.txt
    ...
   - A name.txt
   -sonDir

I want to move all the files in fatherDir inside fatherDir/SonDir.

mv * sonDir

Since sonDir is inside fatherDir, I get an error.

How to achive this?

3 Answers 3

2

To find all files in fatherDir rather than using * (which gives you the sonDir too)

find fatherDir -type f -maxdepth 1 -exec mv {} fatherDir/sonDir \+

The {} will be replaced by each filename found, and all will be moved to the target directory.

1

One approach could be to use grep in combination with xargs:

ls -A | egrep -v sonDir | xargs -i mv {} sonDir
  • first list all files and folders in the current directory (ie. fatherDir) with ls
  • make sure to read out hidden files as well with the -A flag
  • egrep -v lets you exclude sonDir of the printed output
  • create command line from standard input with xargs and replace {} with regarding filename. Option -i is required here!

That's it! I use this approach rather frequently since it lets you exclude not only a folder, but different folders and files at the same time. In case you want to exclude e.g. sonDir daughterDir and nephew.file you can do so by simply specifying egrep -v '(sonDir|daughterDir|nephew.file)', the rest of above shown command stays the same... and there is lots of variations!

-1

Does it matter? The Move still happens. What you get is more of a warning. You can redirect the stderr to /dev/null. Also, if it still bothers you, use find as explained by slhck or something like this:

mv `ls * | grep -v sonDir` sonDir
1
  • 1
    This fails if any file contains a space in its name, e.g. with "A name.txt" as in the example above.
    – slhck
    Commented Apr 24, 2014 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .