Solved

Copy files of a certain pattern and add prefix

Posted on 2013-12-23
5
446 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 400 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 100 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

696 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