-1

Find all files that names start with Pep
(for example PepExample.txt or PepFirst.txt) and COPY(NOW Rename and Remove) to existing files have names Example.txt , First.txt.(except files that start with Pep)

PepExample=> copy to Example.txt

PepFirst=> copy to First.txt

and so forth

D.Rude
  • 21
  • 5
  • What is your question? Please share what you already have tried and what goes wrong. – koen Jul 24 '19 at 15:53

2 Answers2

3

Use a loop and copy each file individually. To compute the new name you can use sed or bash's built-in parameter expansion.

Both of the following scripts assume that you don't have any directories starting with Pep.

The following script copies each file starting with Pep in the working directory. The new names will have the first Pep removed. The -n option avoids accidentally overwriting of files.

for f in Pep*; done
    cp -n "$f" "${f#Pep}"
done

To include sub-directories you can use bash's globstar built-in.

shopt -s globstar nullglob
for f in ./path/to/main/dir/**/Pep*; do
    cp -n "$f" "${f//\/Pep/}"
done

Always start the path with ./ or / so that the script can distinguish between Pep at the start of a filename and Pep inside a filename, for instance Pep do not replace Pep in the middle.txt.

And here's an equivalent script that should work in every POSIX shell, including ancient bash versions as the one on Macs.

find path/to/your/main/directory -type f -name Pep\* -exec sh -c \
'echo cp -n "$0" "$(printf %s "$0" | sed -E "s:(.*/)Pep:\1:")"' {} \;
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • I understand, but I need to copy and not rename – D.Rude Jul 24 '19 at 12:10
  • @D.Rude Sorry. Fixed it. – Socowi Jul 24 '19 at 12:17
  • It works, but how to set the path to the Main Folder in which the script should do this? in my case it looks like this:      Main Folder => Example Folder (PepExample.txt and Example.txt) and First Folder (PepFirst.txt and First.txt) – D.Rude Jul 24 '19 at 12:48
  • @D.Rude Right know the script can only handle one directory. You can select the directory by a doing a `cd youDir` first. Do you want to include sub-directories? – Socowi Jul 24 '19 at 12:50
  • Yes. I want to include sub directories – D.Rude Jul 24 '19 at 12:55
  • Terminal log: man shopt --No manual entry for shopt. Without "shopt" MacBook:PepMain Rude$ ./script.sh cp: /Users/Rude/Desktop/PepMain/QQ/**/Pep*: No such file or directory Directories : MacBook:PepMain Rude$ ls PepMain.txt QQ script.sh MacBook:PepMain Rude$ cd QQ MacBook:QQ Rude$ ls PepFlow.txt – D.Rude Jul 24 '19 at 13:23
  • Oh, didn't know you where using a Mac. Mac's have a highly outdated bash version. I think its from 2007 (that's 12 years ago!) or so. I added yet another solution that should work for you. By the way: Since `shopt` is a built-in it's not `man shopt` but `help shopt` or `info bash` (search for `4.3.2 The Shopt Builtin`). – Socowi Jul 24 '19 at 13:54
  • find: -execute: unknown primary or operator – D.Rude Jul 25 '19 at 06:29
  • @D.Rude Oops... it's just `-exec`. Fixed it. – Socowi Jul 25 '19 at 09:23
1
#! /bin/bash

find . -name 'Pep*' |
    while read path_to_file
    do
        cp "$path_to_file" "$(dirname "$path_to_file")/${path_to_file#*Pep}"
    done

This should work pretty well I believe and checks all subdirectories too.

Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
  • "*`should work pretty well`*" … as long as the paths do not contain backslashes and linebreaks (I think this assumption is fine) and no directory in the path contains the substring `Pep` (I think this could happen and cause problems). – Socowi Jul 24 '19 at 21:22