根据扩展名将文件移动到目录 [英] Move files to directories based on extension

查看:89
本文介绍了根据扩展名将文件移动到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linux的新手.我正在尝试编写一个Shell脚本,它将根据其扩展名将文件移动到某些文件夹,例如在我的downloads文件夹中,我具有混合文件类型的所有文件.我写了以下脚本

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

变成单个语句?我尝试使用||(C编程或"运算符)和逗号,但它们似乎不起作用.

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

推荐答案

将文件添加到目录时没有触发器.如果文件是通过网页上传的,则可以使该网页执行该操作.

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.

您可以在UNIX机器上(或Windows中计划的任务)将脚本放入crontab中以执行此操作. Google crontab提供了操作方法.

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

您可以根据需要添加任意多个"glob"(带通配符的文件名).最后一件事应该是目标目录.

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

这篇关于根据扩展名将文件移动到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆