Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

write a bash script to organize files by their type

Given a directory with these files for example.

bike.png
car.jpg
comedy.mov
error.log
horror.avi
info.txt
interview.mp3
song.flac
tree-small.jpeg
11.10.2017_y
some-directory
nested-file.log

I am trying to write a bash script that would create 3 folders "Music, Videos, Images" and organize these files into their type folder and deletes .log files.

Music is all flac/mp3 files, Images are jpeg/png files, and Videos are all avi/mov files.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The final directory structure should look like this.

images/
     bike.png
     car.jpg
music/
     interview.mp3
     song.flac
videos/
     comedy.mov
     horror.avi
info.txt
tree-small.jpeg
11.10.2017_y
some-directory/
     nested-file.log

>Solution :

Wouldn’t this do it?

#!/bin/sh

mkdir music videos images
mv *.flac *.mp3 music/
mv *.jpeg *.png images/
mv *.avi *.mov videos/
rm *.log
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading