0

I'm trying to copy all of the files/directories inside a folder but would like to exclude one folder as I want all the other file to be inside that folder. To better demonstrate below is the folder structure

- VideosFolder
      L File1
      L File2
      L File3
      L TutorialFolder

Now I want File1 File2 File3 to be moved to TutorialFolder like so

- VideosFolder
      L TutorialFolder
          L File1
          L File2
          L File3

I tried this command

mv ~/Desktop/VideosFolder/!(TutorialFolder) TutorialFolder

But I get this error

mv: cannot stat '/home/user/Desktop/VideosFolder/!(TutorialFolder)': No such file or directory

I checked if the shopt extglob is enabled and it is indeed enabled. So I'm not sure how to go about this.

  • Is that the actual command that you are using? What directory is your current directory when executing that command? Are you sure that you are using bash and that the extglob shell option is set? I can only reproduce the error if I quote the destination path (meaning the glob won't expand). Assuming you are located in ~/Desktop/VideosFolder, and that you have set the extglob shell option with shopt -s extglob in bash, than your command will work. – Kusalananda Apr 17 at 5:44
1

You could just run

mv * TutorialFolder/

While in VideosFolder/. This will move all files and directories (not starting with a .) into TutorialFolder/. As you can't move a directory into itself, you will likely get a warning message along the lines of

mv: cannot move 'TutorialFolder' to a subdirectory of itself, 'TutorialFolder/TutorialFolder'

This is expected and just means that mv hasn't moved TutorialFolder.

  • This does work but it's gives a warning. I was thinking of a more cleaner way like what if I want to exclude 2 folder or more then this solution will not fix it. I was really hoping to get an answer about extglob approach as this is more flexible – MadzQuestioning Apr 17 at 2:21
0

You can try Rsync command.Rsync is a fast and versatile command line utility that synchronizes files and folders between two locations over a remote shell.

 rsync -a --exclude 'TutorialFolder' ~/Desktop/VideosFolder/  ~/Desktop/VideosFolder/TutorialFolder/
0

The other answer works but this is a cleaner way with no errors assuming that you are inside VideosFolder:

find . -maxdepth 1 ! \( -name TutorialFolder -o -name '.' \) -exec mv {} TutorialFolder/ \;

That will find every file and directory in the current directory where the name is not equal to TutorialFolder and . (which is the current directory itself) and then use -exec to mv them into TutorialFolder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.