1

I have a directory structure like this:

./BBC_english/

 2017-09-19.20.00.3-0.rec/00001.mpeg
 2017-09-19.21.00.3-0.rec/00001.mpeg
 2017-09-19.22.00.3-0.rec/00001.mpeg
 2017-09-19.23.00.3-0.rec/00001.mpeg

And I need to rename the 00001.mpeg as the parent folders like this for every folder in ./ I need the date of day and time of recording in this format for example - I don't need 2017 in the new file names

And in the end I need to move all renamed mpeg files to a new path like this :

./newpath/

BBC_english__09-19__20.00.mpeg   
BBC_english__09-19__21.00.mpeg    
BBC_english__09-19__22.00.mpeg
BBC_english__09-19__23.00.mpeg

First of all I use Fedora 20 (I know here is Ubuntu forum) and when I execute mv command it does not support -v option and only have this options:

-f -i -n

And you should know in the source path my Video recorder make new file every 1 hour.

So I need a recursive script to do that for all files during source folder only for .mpeg file (I will use the script after that worked for me in cron) but none of scripts you send here works for me by now.

closed as off-topic by karel, muru, dessert, George Udosen, Zanna Sep 21 '17 at 8:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – muru, George Udosen, Zanna
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This will help you rename the files according to their parent folders name. – Russo Sep 20 '17 at 11:31
  • I'd say it's not quite duplicate of what @dessert linked. The linked question asks to append directory name to filename ( OP of that post said "...adding the folder name before the current name"). This question asks to move to new location with old location appended to filename AND alter filename in a few places. Very different questions, folks ! – Sergiy Kolodyazhnyy Sep 21 '17 at 8:22
  • 1
    @SergiyKolodyazhnyy still off-topic. – muru Sep 21 '17 at 8:26
  • @dessert Partially, it might be useful, but the question and solutions are different, and that's two key components. Attempting to apply answers from duplicate to match this question won't work. Marking as "duplicate" implies that solutions should work for either case. Post can be linked in comments as helpful, but if solutions don't work - it's not a valid duplicate. – Sergiy Kolodyazhnyy Sep 21 '17 at 8:43
  • 1
    @lockheed In addition to providing all the information I requested in my comment on Sergiy Kolodyazhnyy's answer in your edit, if possible please also add the full output of sh --help, mv --help, uname -a, cat /etc/issue, and cat /etc/lsb-release, to your question. It's OK if some are error messages. Run them from the VDR machine, as that's the machine on which you want to rename the files and on which you are unable to use the answers that have been posted so far. (If I'm misunderstanding you, please explain that too.) – Eliah Kagan Sep 21 '17 at 10:46
2

Since you mentioned that you have Fedora, here's a Python script for you, which uses Python 2 syntax, which should work without any issues or differences on both Ubuntu or Fedora. ( Would be nice to use prename which Debian derivatives ship by default, but unfortunately Fedora doesn't. I'll leave that as an exercise for future, and prename is still useful for Ubuntu users. )

Here's the script itself:

#!/usr/bin/env python

import os
import sys
import shutil

def find_files(start_dir):
    ext_paths = []
    for root,dirs,files in os.walk(start_dir):
        if root == start_dir: continue        
        ext_paths = ext_paths + map( lambda x: os.path.join(root,x),files )
    return ext_paths

def alter_path(path,new_dir):
    newpath = path.replace(".3-0.rec/00001","").replace("2017-","")
    newpath = newpath.replace("BBC_english/","BBC_english__").replace("19.","19__")
    return os.path.join(new_dir,newpath)    

def main():
    for i in find_files(sys.argv[1]):
        print(i,alter_path(i,sys.argv[2]))
        shutil.copy(i,alter_path(i,sys.argv[2]))

if __name__ == '__main__':
    main()

Here it is in action:

$ ./rename_mpegs.py  BBC_english/ newpath/                                                                                        
('BBC_english/2017-09-19.23.00.3-0.rec/00001.mpeg', 'newpath/BBC_english__09-19__23.00.mpeg')
('BBC_english/2017-09-19.20.00.3-0.rec/00001.mpeg', 'newpath/BBC_english__09-19__20.00.mpeg')
('BBC_english/2017-09-19.22.00.3-0.rec/00001.mpeg', 'newpath/BBC_english__09-19__22.00.mpeg')
('BBC_english/2017-09-19.21.00.3-0.rec/00001.mpeg', 'newpath/BBC_english__09-19__21.00.mpeg')
$ tree newpath/                                                                                                                   
newpath/
├── BBC_english__09-19__20.00.mpeg
├── BBC_english__09-19__21.00.mpeg
├── BBC_english__09-19__22.00.mpeg
└── BBC_english__09-19__23.00.mpeg

I'd suggest you replace shutil.copy() part with shutil.move(), or you can just get rid of old directory in the end, but I'll leave that up to you to decide.

Rules are simple: call the script with source as argument 1 and destination as argument 2 to the script from the directory one level above BBC_english. Overall, the script is quick and dirty, renaming is hard-coded; one would say it's not ideal, but it works.

  • thanks alot bro.but unfortunately i used a blackbox pc for my VDR that can't run Python script on that (old and limited command list) .so i prefer to lunch commands on it in original command mode.also it haven't any internet connection ( isolated VDR) – lockheed Sep 21 '17 at 8:54
  • 1
    @lockheed You mentioned Fedora 20, but the machine with no Python or mv -v support isn't Fedora, is it? Like Ubuntu, Fedora has GNU Coreutils mv which supports -v. If the machine runs an embedded distro instead, it likely uses BusyBox for its shell and most commands--especially since BusyBox mv supports only -f, -i, and -n. If you edit details into your question--the machine's brand, model, OS/version (if possible), how you access it (SSH? from Fedora?), and how often the renaming must occur (also once per hour?)--I think we can migrate it to Unix & Linux where it's on-topic. – Eliah Kagan Sep 21 '17 at 10:25
  • @Eliah Kagan yes i see busybox on it. It is a light ver of fedora but - v isnt on the options also it is light and only vdr script installed on it to record tv channels. I use ssh to connect via putty renaming can run vua cron to call sh script i think. Im newbi to this site if u perfer to migrate to another one tell me to do that. – lockheed Sep 21 '17 at 18:01
  • 1
    @lockheed You should feel free to post this elsewhere. But, as written, it would likely (a) get answers that don't take the limitations you're working under into account, as happened here on Ask Ubuntu, or (b) be closed as unclear. Once it fully describes what you need, your question could be migrated to Unix & Linux, without you having to repost it. If you think you may want that, you should click the edit link directly under your question and add the information you've given here in your comment as well as the other details I requested in my earlier comments here and on the question. – Eliah Kagan Sep 22 '17 at 10:56
2

As heemayl suggests in answer to the linked post, you could use a series of shell expansions.

Assuming the current working directory is the parent of both the source and the destination, you could use this loop on an Ubuntu system:

$ for d in ./BBC_english/*; do e="${d##*2017-}"; f="${e%%.[0-9]-[0-9].rec}"; g="${f/./__}"; echo mv -v -- "$d"/* ./newpath/BBC_english__"$g".mpeg; done
mv -v -- ./BBC_english/2017-09-19.20.00.3-0.rec/00001.mpeg ./newpath/BBC_english__09-19__20.00.mpeg
mv -v -- ./BBC_english/2017-09-19.21.00.3-0.rec/00001.mpeg ./newpath/BBC_english__09-19__21.00.mpeg
mv -v -- ./BBC_english/2017-09-19.22.00.3-0.rec/00001.mpeg ./newpath/BBC_english__09-19__22.00.mpeg
mv -v -- ./BBC_english/2017-09-19.23.00.3-0.rec/00001.mpeg ./newpath/BBC_english__09-19__23.00.mpeg

Note that we loop over the directories rather than the files. This assumes you really only have one file in each directory, which seems to be the case given the logic.

Remove echo after testing to actually move files. Then -v causes mv to report what it's doing.

If your mv does not support the -v (verbose) flag and you want to see what's happening, you can instead get bash to be verbose and give you a detailed summary of what it's doing using set -x

#!/bin/bash
set -x

for d in ./BBC_english/*; do 
   e="${d##*2017-}"            # remove everything before and including 2017-
   f="${e%%.[0-9]-[0-9].rec}"  # remove the trailing numbers and `.rec`
   g="${f/./__}"               # replace the first `.` with `__`
 echo mv -- "$d"/* ./newpath/BBC_english__"$g".mpeg
 # move to the new path using the edited directory name
done

The output of this after removing echo would include the result of each expansion and the mv statement for every iteration of the loop. For the first iteration, the output would look like this:

+ for d in ./BBC_english/*
+ e=09-19.20.00.3-0.rec
+ f=09-19.20.00
+ g=09-19__20.00
+ mv -- ./BBC_english/2017-09-19.20.00.3-0.rec/00001.mpeg ./newpath/BBC_english__09-19__20.00.mpeg
  • I'd say make the last code snippet a full script, add #! line to it. Also, worth noting for the n00bz that ./ signifies that one should run such command from directory one level above BBC_english. – Sergiy Kolodyazhnyy Sep 21 '17 at 8:18
  • 1
    old topic, we've been over this before thousands of times. Question is still useful for Ubuntu users. So long as it works on our platform that's all that matters. – Sergiy Kolodyazhnyy Sep 21 '17 at 8:24
0

This script should help:

#!/bin/bash  

for i in ./BBC_english/*; do
        # Get the path to the file
        path=$(dirname "$(readlink -f "$i")")         

        # get the names for file rename
        f2=$( echo "$path" | grep -Eo "\-[0-9]{2}-[0-9]{2}" | grep -Eo "[0-9]{2}-[0-9]{2}")            
        f1=$( echo "$path" | grep -Eo BBC_english )           
        f3=$(echo "$path" | grep -Eo "\.[0-9]{2}\.[0-9]{2}" | grep -Eo "[0-9]{2}\.[0-9]{2}")                       
        # Rename the files           
        mv -n "$i" ./newpath/"$f1"__"$f2"__"$f3".mpeg

done
  • 1
    i change the option parameter from -v to -n but it still have problem.script move all folders from source to destenition but i dont see any renamed effect in file in newpath . also you need correct this line "$f3.txt and change it to "$f3".txt – lockheed Sep 21 '17 at 5:41
  • Sorry for the missing " and the extension should have been mpeg ( i tested with .text), I did test it and it works on my end. You may need to change the paths to get it to work. And fedora is off topic here – George Udosen Sep 21 '17 at 6:30
  • did your files renamed to this style in new path ? BBC_english__09-19__20.00.mpeg ?? i saved this script in a .sh file in parent of /BBC_English dir and run it. – lockheed Sep 21 '17 at 6:46
  • Yes but in my case I used bbc in place of BBC_english! – George Udosen Sep 21 '17 at 6:47
  • if we ignored the move and other command can u tell me how can i filtered and optain parents folder name and take some chars of date and time for new file? i need the filtering command for new file name only now.like 2017-09-19.20.00.3-0.rec that i need to grap BBC-english_09-19_20.00. infact i need (satname_date_time) style without 2017 in first and .3-0.rec at the end of new file name – lockheed Sep 21 '17 at 7:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.