Results 1 to 8 of 8

Thread: Move Files containing specific text in terminal

  1. #1
    Join Date
    Sep 2013
    Beans
    57

    Move Files containing specific text in terminal

    Hi, I am able to move files around from one place to another in the terminal, however I have a massive collection of audio files that I would like to sort and move by the album title.

    for example: my file names in the music folder consist of - Artist Album Trk# Title

    In the terminal I imagine the command would look something like this:

    sudo mv /home/user/Music/*Album /home/user/Music/Album\ Title

    I know this doesnt work but could someone please help it would make the whole process a lot easier.

    Thanks.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Move Files containing specific text in terminal

    Are you trying to move them to a different directory location, or rename them? Are the files all in one directory, or in subdirectories?

    It would help if you gave some specific examples.

  3. #3
    Join Date
    Sep 2013
    Beans
    57

    Re: Move Files containing specific text in terminal

    Hi Sorry,

    Right I have files for example 9 files: file album-afi1.mp3 album-afi2.mp3 album-afi3.mp3 album-bfs1.mp3 album-bfs2.mp3 album-bfs3.mp3 album-cod1.mp3 album-cod2.mp3 album-cod3.mp3

    I want to move files album-afi1.mp3 album-afi2.mp3 album-afi3.mp3 to folder afi

    Then move files album-bfs1,2&3.mp3 to folder bfs

    album-cod1,2,3.mp3 to folder cod

    ect.

    so if files album-afi1.mp3 album-afi2.mp3 album-afi3.mp3 album-bfs1.mp3 album-bfs2.mp3 album-bfs3.mp3 album-cod1.mp3 album-cod2.mp3 album-cod3.mp3 are in /home/adam/Music/All

    and I want to move just the files that contain afi from all to /home/adam/Music/AFI

    the command I tried was sudo mv /home/adam/Music/All/*afi* /home/adam/Music/AFI
    move all containing afi to folder called AFI

    hope this make sense.

    Thanks

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Move Files containing specific text in terminal

    The -t target form of the mv command is handy for situations like this where you want to move multiple source files to one destination.
    Code:
           -t, --target-directory=DIRECTORY
                  move all SOURCE arguments into DIRECTORY
    If the target directories already exist, you should be able to do something like

    Code:
    mv -t /home/adam/Music/AFI/ /home/adam/Music/All/*afi*
    or (if you want to restrict the match more precisely)
    Code:
    mv -t /home/adam/Music/AFI/ /home/adam/Music/All/album-afi*.mp3
    or even (to match only names that have one or more digits after the afi)
    Code:
    mv -t /home/adam/Music/AFI/ /home/adam/Music/All/album-afi+([0-9]).mp3
    and so on. If the directories don't already exist, there are ways of generating them progamatically (i.e. by chopping up the filenames) but that's a bit more complicated.

    EDIT: and no need to use sudo if the files and target directories are under your own home dir

  5. #5
    Join Date
    Sep 2013
    Beans
    57

    Re: Move Files containing specific text in terminal

    Thanks, I think I understand.

    Why does this method apear to be in reverse?

    When moving files previously I have used the mv /existing/file.to /new/location
    where as your method seems to be the reverse.

    is there a difference?

    is it to do with the target directory bit at the start?

    thanks

  6. #6
    Join Date
    Oct 2007
    Beans
    1,832

    Re: Move Files containing specific text in terminal

    Also, notice that it's unlikely that you must begin this command with sudo. Unless you know why you should, we'll probably advise against it.

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Move Files containing specific text in terminal

    Quote Originally Posted by adam17 View Post
    Thanks, I think I understand.

    Why does this method apear to be in reverse?

    When moving files previously I have used the mv /existing/file.to /new/location
    where as your method seems to be the reverse.

    is there a difference?

    is it to do with the target directory bit at the start?

    thanks
    it should work if you switch places but the common convention is -options first (-t dir here), arbitrary number of items to process at the end. That makes it easy to determine at a glance what the command is supposed to do. "A-ha! -t dir, so it gathers a bunch of files and moves them in bulk at once to dir!".
    Options often introduce huge differences and having to look for switches all over the place, potentially even at the end, would be a massive pain.
    Last edited by Vaphell; April 20th, 2014 at 11:00 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Sep 2013
    Beans
    57

    Re: Move Files containing specific text in terminal

    Thanks for the help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •