Ubuntu – How to move Only the files in current folder to a sub folder

command linefilesmv

I don't have to move the folders, only the files.

I have tried mv * but this command moves the folders also.

Best Answer

If you want to move everything except directories from $SOURCE_DIR to $TARGET_DIR, you can use this command:

find "$SOURCE_DIR" -maxdepth 1 -not -type d -exec mv -t "$TARGET_DIR" -- '{}' +

Explained in detail:

  • find: The find search for files in a directory
  • $SOURCE_DIR: The directory to search in
  • -maxdepth 1: Do not look inside subdirectories
  • -not -type d: Ignore directories
    • You could also use -type f if you only want to copy things that are strictly files, but I prefer the above because it also catches everything that's neither a file nor a directory (in particular symbolic links)
  • -exec mv -t "$TARGET_DIR" -- '{}' +: Run the command mv -t "$TARGET_DIR" -- FILES... where FILES... are all the matching files (thanks @DavidFoerster)
Related Question