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
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:
- How can i create a new blog?Best solution by Yahoo! Answers
- How can I create a new font?Best solution by Yahoo! Answers
- How can I delete a folder that I created in Yahoo mail?Best solution by Yahoo! Answers
- How Can I Create an XML to Create a Menu?Best solution by Drupal Answers
- How can I create a new account on yahoo?Best solution by overview.mail.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.