Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

linux - Move files to directories based on extension

I am new to Linux. I am trying to write a shell script which will move files to certain folders based on their extension, like for example in my downloads folder, I have all files of mixed file types. I have written the following script

mv *.mp3 ../Music
mv *.ogg ../Music
mv *.wav ../Music
mv *.mp4 ../Videos
mv *.flv ../Videos

How can I make it run automatically when a file is added to this folder? Now I have to manually run the script each time.

One more question, is there any way of combining these 2 statements

mv *.mp3 ../../Music
mv *.ogg ../../Music

into a single statement? I tried using || (C programming 'or' operator) and comma but they don't seem to work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no trigger for when a file is added to a directory. If the file is uploaded via a webpage, you might be able to make the webpage do it.

You can put a script in crontab to do this, on unix machines (or task schedular in windows). Google crontab for a how-to.

As for combining your commands, use the following:

mv *.mp3 *.ogg ../../Music

You can include as many different "globs" (filenames with wildcards) as you like. The last thing should be the target directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...