基于文件名的第一部分将文件移动到目录? [英] Move files to directories based on first part of filename?

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

问题描述

我有几千元的电子书需要通过SSH运行bash无头的Linux服务器上举办的。所有电子书都令人欣慰的命名与2的一个约定。


  • AuthorFirstName AuthorLastName - 书Title.pdf

  • AuthorFirstName AuthorLastName - 丛书#inSeries - 书
    Title.pdf

我想要做的是把所有的书进入一个有组织的系统,如:

 `DestinationDirectory / FirstLetterOfAuthorFirstName /作者姓名/ pdf's`

例如。以下书籍

 安德鲁·韦纳 -  Changes.pdf
艾伦·科尔 - Timura三部曲01 - 当神Slept.pdf

应放置在以下文件夹

  /书籍/ A /艾伦·科尔/艾伦·科尔 -  Timura三部曲01  - 当神Slept.pdf
/书籍/ A /安德鲁·韦纳/安德鲁·韦纳 - Changes.pdf

我需要如何把这一切变成一个bash脚本,将抓住所有的PDF文件的文件名在当前目录下,然后将文件移动到正确的目录帮助下,创建目录,如果它已经不存在的。


解决方案

 中的* .pdf F;做
    名称=`回声$ F| sed的'S / - * //'`
    信=`回声$名|切-c1`
    DIR =DestinationDirectory / $信/ $名
    MKDIR -p$目录
    MV$ F,$目录
DONE

I have several thousand ebooks that need to be organized on a headless linux server running bash through SSH. All of the ebooks are thankfully named with one of 2 conventions.

  • AuthorFirstName AuthorLastName - Book Title.pdf
  • AuthorFirstName AuthorLastName - Book Series #inSeries - Book Title.pdf

What I would like to do is to move all of the books into an organized system such as:

`DestinationDirectory/FirstLetterOfAuthorFirstName/Author Full Name/pdf's`

e.g. the following books

Andrew Weiner - Changes.pdf 
Allan Cole - Timura Trilogy 01 - When the Gods Slept.pdf

should be placed in the following folders

/books/A/Allan Cole/Allan Cole - Timura Trilogy 01 - When the Gods Slept.pdf
/books/A/Andrew Weiner/Andrew Weiner - Changes.pdf

I need help with how to put this all into a bash script that will grab the filenames of all the PDF files in the current directory, and then move the files to the proper directory, creating the directory if it doesn't already exist.

解决方案

for f in *.pdf; do
    name=`echo "$f"|sed 's/ -.*//'`
    letter=`echo "$name"|cut -c1`
    dir="DestinationDirectory/$letter/$name"
    mkdir -p "$dir"
    mv "$f" "$dir"
done

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

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