Solved

Copy files of a certain pattern and add prefix

Posted on 2013-12-23
5
451 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

707 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