I have a folder with 3 subfolders containing 6 files each.There are 2 files in each subfolders with :NOPM in their file names.

I need to create a shell script that checks all the folders and their subfolders. If there are any file names with :NOPM, then this should be removed from the name. If there is another file with the same name, it should be deleted and every interaction should be logged into a log.txt.

  • 1
    welcome to U&L, what have you tried so far ? – Archemar Sep 17 '15 at 12:28
  • find /mydirectoryname -name :nopm: I just can find the files, but i'm start learning unix now, and can't solve this. – KisPogesz Sep 17 '15 at 12:31
  • 3
    What part of this is giving you problems? Are you just expecting us to do your homework for you? – terdon Sep 17 '15 at 12:32
  • You need to get all files, then check their names in cycle and move(rename) or delete files, depending of names. So, welcome to the Linux way! :) – strangeman Sep 17 '15 at 12:46
  • I should complete the task like this. Can you help to repair this? for if find /data/irm/Example_September -name :nopm: mv file --? do echo I rename the file. --? elif echo I remove the file. fi >> /data/irm/Example_September/log.txt \; – KisPogesz Sep 21 '15 at 12:13
find /mydirectory -maxdepth 3 -iname *:nomp* -execdir bash -c 'for f; do base=${f##*/}; mv -- "$f" "${f%/*}/${base//:nomp/whateverYouWant}" && echo $f >> /PATH/log.txt; done' _ {} +

By the way, because I used execdir you might not need to use base=${f##*/} and instead of ${base//:nomp.....} you can replace "base" with "."

| improve this answer | |
  • Thanks for help, but i should complete the task like this, but it doensn't works. – KisPogesz Sep 21 '15 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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