Move files to directories based on extension

后端 未结 6 1448
慢半拍i
慢半拍i 2020-12-14 02:06

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 f

相关标签:
6条回答
  • 2020-12-14 02:16

    Another way is:

    mv -v {*.mp3,*.ogg,*.wav} ../Music
    mv -v {*.mp4,*.flv} ../Videos
    

    PS: option -v shows what is going on (verbose).

    0 讨论(0)
  • 2020-12-14 02:22

    incron will watch the filesystem and perform run commands upon certain events.

    You can combine multiple commands on a single line by using a command separator. The unconditional serialized command separator is ;.

    command1 ; command2
    
    0 讨论(0)
  • 2020-12-14 02:23

    Two ways:

    1. find . -name '*mp3' -or -name '*ogg' -print | xargs -J% mv % ../../Music
    2. find . -name '*mp3' -or -name '*ogg' -exec mv {} ../Music \;

    The first uses a pipe and may run out of argument space; while the second may use too many forks and be slower. But, both will work.

    0 讨论(0)
  • 2020-12-14 02:28

    I like this method:

    #!/bin/bash                                                                                                                                                                                                 
    
    for filename in *; do
      if [[ -f "$filename" ]]; then
          base=${filename%.*}
          ext=${filename#$base.}
        mkdir -p "${ext}"
        mv "$filename" "${ext}"
      fi
    done
    
    0 讨论(0)
  • 2020-12-14 02:31

    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.

    0 讨论(0)
  • 2020-12-14 02:40

    You can use for loop to traverse through folders and subfolders inside the source folder. The following code will help you move files in pair from "/source/foler/path/" to "/destination/fodler/path/". This code will move file matching their name and having different extensions.

    for d in /source/folder/path/*; do
        ls -tr $d |grep txt | rev | cut -f 2 -d '.' | rev | uniq | head -n 4 | xargs -I % bash -c 'mv -v '$d'/%.{txt,csv} /destination/folder/path/'
        sleep 30
    done 
    
    0 讨论(0)
提交回复
热议问题