Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED] BASH - How to add name to file name of copied file

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
makh
Posts: 651
Joined: 2011-10-09 09:16

[SOLVED] BASH - How to add name to file name of copied file

#1 Post by makh »

Hi

I am trying to make a script to copy 00_STD_Letter_Serial01_To_ABC_-_Title.odt to current directory:

Code: Select all

ls
aaa.txt
abc.txt
pqr.txt
to get in current directory:

Code: Select all

ls
Letter_Serial01_To_ABC_-_aaa.odt
Letter_Serial02_To_ABC_-_abc.odt
Letter_Serial03_To_ABC_-_pqr.odt
but this fails in renaming exactly:

Code: Select all

for i in *.txt;do cp -v ../00_STD_Letter_*.odt  "${i/.txt}".odt ;done
I also get an error for:

Code: Select all

filename1=../00_STD_Letter_*.odt; for i in *.txt;do cp -v ../00_STD_Letter_*.odt  "$filename1""${i/.txt}".odt ;done
Kindly inform any possibility?

Thanks in advance!
:)
Last edited by makh on 2019-02-08 14:21, edited 1 time in total.
ThinkPad E14: Arch, Debian Stable
GUI: Xfce

For new: Try MX Linux, Linux Mint; later join Debian Stable

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: [Bash] How to add suffix to the file name of copied file

#2 Post by pylkko »

So is this the problem or did I misunderstand? You want to copy a file and append a running number to it (iterator in for loop) and then add to that a suffix (.txt)?

User avatar
makh
Posts: 651
Joined: 2011-10-09 09:16

Re: [Bash] How to add suffix to the file name of copied file

#3 Post by makh »

Hi

I have files named as topics:
  • topic-1.txt
    topic-2.txt
    topic-3.txt
... and so on...

I want to copy the same STD_FILE.odt file from another location, to the current directory, but named as, using script:
  • Letter_topic-1.odt
    Letter_topic-2.odt
    Letter_topic-3.odt
Thankyou
ThinkPad E14: Arch, Debian Stable
GUI: Xfce

For new: Try MX Linux, Linux Mint; later join Debian Stable

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: [Bash] How to add suffix to the file name of copied file

#4 Post by Head_on_a_Stick »

For just 3 files we don't really need bash:

Code: Select all

#!/bin/sh
for n in 1 2 3
   do cp /other/location/STD_FILE.odt Letter_topic-${n}.odt
done
Or if it's for 100 files (for example) then take advantage of brace expansion:

Code: Select all

#!/bin/bash
for n in {1..100}
   do cp /other/location/STD_FILE.odt Letter_topic-${n}.odt
done
But I don't think I'm understanding you correctly.
deadbang

User avatar
makh
Posts: 651
Joined: 2011-10-09 09:16

Re: [Bash] How to add suffix to the file name of copied file

#5 Post by makh »

Hi

I have something like this:
railroad-matter.txt
water-filter-availability.txt
change-in-policy.txt
These are more then 50 files!

I have made a formatted Letter in odt in another directory. So copying to current directory to get:
railroad-matter.txt
water-filter-availability.txt
change-in-policy.txt
Letter_Serial_01_railroad-matter.odt
Letter_Serial_02_water-filter-availability.odt
Letter_Serial_03_change-in-policy.odt
My problem is to retain the original file name, and add as suffix in file name the "title of letter":

"water-filter-availability" ... Should have a copy of odt named as: "Letter_Serial_02_water-filter-availability"

I think I may be able to add serial number if for loop is used.

Thankyou
ThinkPad E14: Arch, Debian Stable
GUI: Xfce

For new: Try MX Linux, Linux Mint; later join Debian Stable

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: [Bash] How to add suffix to the file name of copied file

#6 Post by pylkko »

The design of the script depends a little on if you want it to rename all file always that exist in the folder or if you want it to keep a specific order. I mean, do you want it to rename all files in case you add one more file to the folder, or that once they are renamed, they are never renamed again.

In theory all you need to do is have the file names and iterator and a text suffix, then you concatenate them. With ls you can get a list of the files in the directory, you can iterate over the lines of the output of ls to get the original names and an iterator number at the same step, then concat to that your suffix.

This would be possible to do with perl with the following steps

Code: Select all

$name = 'foo';
$filename = "/tmp/${name}.tmp";
I personally like to use perl for string substitutions and concatenations

but I believe you specifically want to use bash,

so how about this:

Code: Select all

bash-4.4$ touch a.txt
bash-4.4$ touch b.txt
bash-4.4$ touch c.txt
bash-4.4$ ls
a.txt  b.txt  c.txt
created a list of files with .txt suffix for testing

Code: Select all

bash-4.4$ ((count=0)); for item in *; do mv "$item" "Letter_Serial_${count}_${item%.txt}.odt"; ((count++));done
bash-4.4$ ls
Letter_Serial_0_a.odt  Letter_Serial_1_b.odt  Letter_Serial_2_c.odt
There are probably more eloquent ways to do this. In Python and many other scripting languages you can iterate a list of items pulling the names and a order numer (more precisely, the iteration step) at the same time thus not requiring the creation of a math variable. I don't know how to do that in bash. Does anyone else?

Also, if you use this in the same directory again it will attempt to rename *. But if you know that the files will always end in .txt you could loop over *.txt instead, and the in the future the script would not ever attempt to rename already once processed files.

Also, the use of mv here renames the file without retaining the original, but using cp instead of course would retain the original

Also realize that if you remove b.txt and rerun the script, c.txt will get a different serial number.

User avatar
makh
Posts: 651
Joined: 2011-10-09 09:16

Re: [Bash] How to add suffix to the file name of copied file

#7 Post by makh »

pylkko wrote:The design of the script.
Hi
You got closest to my issue!

Editing a bit; it partly resolves to my exact requirement:

Code: Select all

((count=1)); for item in *.txt; do cp -v ../LETTER_STANDARD.odt "Letter_Serial_${count}_${item%.txt}.odt"; ((count++));done
Yes I prefer to use standard bash and commands.

Thankyou
ThinkPad E14: Arch, Debian Stable
GUI: Xfce

For new: Try MX Linux, Linux Mint; later join Debian Stable

Post Reply