LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   copying files and give new unique names to each file by using xargs command (https://www.linuxquestions.org/questions/programming-9/copying-files-and-give-new-unique-names-to-each-file-by-using-xargs-command-336209/)

gnim66 06-22-2005 05:18 PM

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 {}

perfect_circle 06-22-2005 06:09 PM

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 :)

gnim66 06-22-2005 06:45 PM

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.

perfect_circle 06-22-2005 07:00 PM

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?

gnim66 06-22-2005 07:08 PM

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)

perfect_circle 06-22-2005 07:37 PM

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


gnim66 06-22-2005 08:29 PM

Thanks again perfect_circle. This really helps. I'll let you know if it works out or not.


All times are GMT -5. The time now is 05:53 AM.