Solved

Copy files of a certain pattern and add prefix

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 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