?
Solved

Copy files of a certain pattern and add prefix

Posted on 2013-12-23
5
Medium Priority
?
454 Views
Last Modified: 2013-12-23
On linux:

I have the following files:

popups.csv
search.csv
bla.popups.csv
bla.search.csv

I want to copy popups.csv and search.csv and prepend xyz so that after executing my command I have:

popups.csv
search.csv
bla.popups.csv
bla.search.csv
xyz.popups.csv
xyz.search.csv

What's the command I'm looking for?
0
Comment
Question by:SWB-Consulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39736942
One line?

for file in popups.csv search.csv; do cp -p $file xyz.$file;  done

Two commands?

cp -p popups.csv xyz.popups.csv
cp -p search.csv xyz.search.csv

The "-p" flag of "cp" preserves the timestamps. Omit it if you want to create the new files using the current time.
0
 

Author Comment

by:SWB-Consulting
ID: 39736951
Thanks, I would like a command that doesn't have to hardcode the filenames through. Basically take any file hat ends in csv and doesn't already have a prefix and copy it into filename with specified prefix.

In reality I have many more filenames sitting in there and I just wanted to simplify for this example.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39736969
That's what I'd like to call over-simplifying.

Here you go, for always "xyz" (I don't assume that's what you're after, but anyway):

for file in $(ls *.csv); do if [[ "${file#xyz.}" == "$file" ]]; then echo cp -p $file xyz.$file; fi; done

echo is for testing, remove it to actually run the displayed commands.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1600 total points
ID: 39736991
Let's assume that with "have a prefix" you mean that the filename consists of more than two dot-separated elements:

for file in $(ls *.csv); do if [[ $(echo $file | tr "." " " | wc -w) -lt 3 ]]; then echo cp -p $file xyz.$file; fi; done

(echo like above)

Or by which criteria should we identify a "prefix"?
0
 
LVL 23

Assisted Solution

by:savone
savone earned 400 total points
ID: 39736997
The above is very elegant, but it returns all the files, not just the files without the prefix.

Here is my test:
$ for file in $(ls *.csv); do if [[ "${file#xyz.}" == "$file" ]]; then echo cp -p $file xyz.$file; fi; done
cp -p bla.popups.csv xyz.bla.popups.csv
cp -p bla.search.csv xyz.bla.search.csv
cp -p popups.csv xyz.popups.csv
cp -p search.csv xyz.search.csv

Here is what I came up with.  Obviously I am not the best at script writing but it worked for me.

for i in `ls *.csv`; do num=`echo $i | grep -o "\." | wc -w`; if [ "$num" -le "1" ]; then cp $i xyz.$; fi; done

My test:

$ ls
bla.popups.csv  bla.search.csv  popups.csv  search.csv

$ for i in `ls *.csv`; do num=`echo $i | grep -o "\." | wc -w`; if [ "$num" -le "1" ]; then cp $i xyz.$i; fi; done

$ ls
bla.popups.csv  bla.search.csv  popups.csv  search.csv  xyz.popups.csv  xyz.search.csv
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question