0

I have been asked to design a script which when executed, moves the file types given in the arguments to the respective folder(creates the folder with file type if not there).

Like suppose I give command ./M6.sh mp3 txt

This should move all mp3 files in the current working directory into a folder named "mp3" and copy all mp3 files into that and also moves all the text files from current working directory to folder named txt.

I wrote the below code, but I am facing trouble with mv command.

Please help.

enter image description here

  • 5
    Welcome to U&L. Please post code as text, rather than images (see meta.stackexchange.com/questions/51144/… ). Also, post details of what results/errors you get when attempted to run the problematic code. – steve Sep 15 '19 at 17:02
  • 1
    What is the nature of the trouble that you are facing? – Kusalananda Sep 15 '19 at 17:10
  • what does this mean? This should move all mp3 files in the current working directory into a folder named "mp3" and copy all mp3 files into that .... please edit your post – jsotola Sep 15 '19 at 18:03
  • In addition to the improvements in the answers below: instead of myarray=("$@"); path="${myarray[0]}", do path="$1"; shift; myarray=("$@"). Then you wouldn't have to loop from the 2nd element to the last - shift removes the 1st parameter from $@, so it never gets in to myarray. Also, you don't need myarray at all, you could just loop over $@. – cas Sep 16 '19 at 3:53
1

Your code is needlessly complicated. I believe that the issue is that you fail to call mv if the destination directory doesn't already exist. You also seem to try to move thing to a directory in the root directory.

Consider

#!/bin/sh

for suffix do
    mkdir -p -- "$suffix" || exit 1
    mv -- *."$suffix" "$suffix"
done

or,

#!/bin/bash

suffixes=( "$@" )

for suffix in "${suffixes[@]}"; do
    mkdir -p -- "$suffix" || exit 1
    mv -- *."$suffix" "$suffix"
done

(Change the exit 1 to continue if you want to skip forward to the next given suffix when a directory can't be created, rather than to terminate the script).

This would loop over all filename suffixes (not really file types as a file type in Unix are things like "regular file", "directory", "symbolic link" etc.), would create a subdirectory in the current directory named after that suffix, and then move all files with names ending in ."$suffix" to that directory.

The command mkdir -p dirname would not fail if the directory dirname already exists.

Example run:

$ tree
.
|-- file1.mp3
|-- file1.png
|-- file1.txt
|-- file2.mp3
|-- file2.png
|-- file2.txt
|-- file3.mp3
|-- file3.png
|-- file3.txt
|-- file4.mp3
|-- file4.png
|-- file4.txt
|-- file5.mp3
|-- file5.png
|-- file5.txt
`-- script.sh

0 directory, 16 files
$ ./script.sh txt mp3
$ tree
.
|-- file1.png
|-- file2.png
|-- file3.png
|-- file4.png
|-- file5.png
|-- mp3
|   |-- file1.mp3
|   |-- file2.mp3
|   |-- file3.mp3
|   |-- file4.mp3
|   `-- file5.mp3
|-- script.sh
`-- txt
    |-- file1.txt
    |-- file2.txt
    |-- file3.txt
    |-- file4.txt
    `-- file5.txt

2 directories, 16 files
| improve this answer | |
0

When you are using mv commdand you are actually trying to move it to a folder from the root directory. Your command should be from the current directory.

mv *.${myarray[$index]} ./${myarray[$index]}/

Notice the . before directory, it tells you to look for the folder in current directory otherwise you can simply write :

mv *.${myarray[$index]} ${myarray[$index]}/

Above command will also look for the folder in the current directory.

| improve this answer | |
  • @D.BenKnoble That was a typo. I fixed it. Thanks for pointing it out. – Pacifist Sep 15 '19 at 18:02

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.