Solved

Copy files of a certain pattern and add prefix

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now