How to Create a file in "my documents" folder?

How can I create a folder and move a file from the current folder to the new folder using a shell script?

  • I want to move img/file.jpg to img/logo/file.jpg, and I need to create the logo folder.

  • Answer:

    The mv manpage shows mv will accept multiple arguments. The last argument is the directory; all others are the files that you wish to move. Thus the function move_to_new_directory, below, contains a shell script that will make a directory, move the files into it, change working directory to the newly created directory. move_to_new_directory () { mkdir ${!#} for each_arg in $@ do if [ -d "$each_arg" ] then cd ${!#} else mv -iv "$each_arg" ${!#}; fi done } where ${!#} is the last argument passed to the function. $@ are all the arguments passed to the function. The -iv options have been passed to ensure that files are not accidentally overwritten and a summary of all changes are shown to the user in the shell GUI. The if statement if [ -d "$each_arg" ] ... fi asks if the argument passed is a directory and if true cd's into it; else it moves what must be a file to the previously created directory. If you put this in your .bash_profile you will be able to call it from the terminal, e.g.,

Darren Higgins at Quora Visit the source

Was this solution helpful to you?

Other answers

dir="<your new dir>"; mkdir -p $dir; mv $PWD/<file> $dir/

Pratyush Rathore

how to find list of d...

Mohammed Azaruddin

use the mkdir command to create the logo directory. Then use the mv command to move all the files you want to that directory. Something like: mkdir ./logo mv * logo

Dave Ford

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.