LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2005, 05:18 PM   #1
gnim66
LQ Newbie
 
Registered: Jun 2005
Location: California, USA
Posts: 4

Rep: Reputation: 0
copying files and give new unique names to each file by using xargs command


Hello there. Can someone help me. I am trying to copy specific files to current directory and give each of the file a new name by using the xargs command. This is what I have so far but I'm stuck:


ls -l |grep "June"| awk '{print $9}|xargs -ti cp {}
 
Old 06-22-2005, 06:09 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
first of all, this:
ls -l |grep "June"| awk '{print $9}'
is wrong.
If the files contain any spaces, you'll mess things up.

Are you searching for files containing June in the filename?

*EDIT*
Welcome to LQ forums

Last edited by perfect_circle; 06-22-2005 at 06:12 PM.
 
Old 06-22-2005, 06:45 PM   #3
gnim66
LQ Newbie
 
Registered: Jun 2005
Location: California, USA
Posts: 4

Original Poster
Rep: Reputation: 0
Yes I am. The command ls -l just gives out a long listing output of all the files under the current directory I'm in. The output then is what I'm trying to manipulate. In this case, I wanted all the June files to be copied with new names. I was hoping to achieve this with the xargs command.
 
Old 06-22-2005, 07:00 PM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
I would use something like this to get the full name:

Code:
find . -type f -name "*June*"| while read filename; do ...... done
and every time the loop is executed the $filename variable will contain the next filename.

When you say new names, do you mean random names, specific names, names that follow a pattern (file00 , file01), etc. What exectly do you want?
 
Old 06-22-2005, 07:08 PM   #5
gnim66
LQ Newbie
 
Registered: Jun 2005
Location: California, USA
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks. The new file name will only contain character
14-19 of the original name and with the new extension of .csv


Example:

rospslpar1_1_050616_0005.nmon (current existing name)

050616.csv (new copied file name)
 
Old 06-22-2005, 07:37 PM   #6
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Try a simple bash script. Something like this should work. I haven't tested it.
Code:
find . -maxdepth 1 -type f -name "*rospslpar*" |while read filename; do
 path_name=${filename%/*}
 base_name=${filename##*/}
 new_name="$(expr substr $base_name 14 6).cvs"
 mv "$filename" "$path_name/$new_name"
done
 
Old 06-22-2005, 08:29 PM   #7
gnim66
LQ Newbie
 
Registered: Jun 2005
Location: California, USA
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks again perfect_circle. This really helps. I'll let you know if it works out or not.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
du on specific files from find command (use xargs?) fireman949 Linux - Software 2 07-10-2005 01:42 AM
create unique user names sabapathys Linux - Software 1 05-12-2005 05:50 AM
create unique user names sabapathys Linux - Newbie 1 05-12-2005 04:09 AM
Unique class names (C++) ToothlessRebel Programming 6 12-25-2004 12:16 AM
opening files with strings for file names veilig Programming 4 11-10-2003 11:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration