3

I have many tar.gz files in a folder on my Linux machine and also text file with tar.gz file names and matching sample names.

Current_directory
      |_______TDF.tar.gz
      |_______DFG.tar.gz
      |_______GHY.tar.gz

names.txt file which is in current directory looks like below:

Tar.gz_filenames       Sample_Names
DFG.tar.gz                Sample2
TDF.tar.gz                Sample1
GHY.tar.gz                Sample3

So, based on their match I want the following output in my Current_directory.

Output:

Sample1.tar.gz
Sample2.tar.gz
Sample3.tar.gz

I tried like following but didn't work:

for j in .tar.gz ; do
  sed "s/\(\w\+\)\s\+\(\w\+\)/mv '*_\1$j' '*_\2$j'/e" names.txt
done

Small update in my question: When I used this command cat -e names.txt I see like following:

Tar.gz_filenames    Sample_Names^M$
DFG.tar.gz  Sample2^M$
TDF.tar.gz  Sample1^M$
GHY.tar.gz  Sample3

4 Answers4

1

Another way assuming no whitespaces is there in files name:

$ xargs -n2 echo mv -i <<<"$(sed '1d; s/$/.tar.gz/' names.txt)"
mv -i TDF.tar.gz Sample1.tar.gz
mv -i DFG.tar.gz Sample2.tar.gz
mv -i GHY.tar.gz Sample3.tar.gz

In sed '1d; s/$/.tar.gz/', the 1d is deleting very first line of the input fileName names.txt then the s/$/.tar.gz/ adds .tar.gz in the end of every line (the $ ponits to the end of line).

In xargs -n2 we are reading two set of strings which separated with Space/ or Tab.

Notes:

  • Based on output of cat -e names.txt your that file is not Unix type end-of-line format, to convert it to Unix type do dos2unix fileName then apply the above command to rename.

  • remove echo for dry-run.

link to What is ^M and how do I get rid of it?

αғsнιη
  • 41,407
  • Hi, what if I have many columns in the names.txt file and "TAr.gz_filenames" is 8th column and "Sample_Names" is 9th column. How to give the command? It would be great if you could explain a little about your command. Thanq – stack_learner Apr 27 '18 at 12:59
  • I have updated my answer including explaining the command. "what if I have many columns in the names.txt file and "TAr.gz_filenames" is 8th column and "Sample_Names" is 9th column. How to give the command?": this is also possible either with sed or awk or cut or ... to select those columns and feed to xargs. I see no such reason and difficulties shows that's different than what you asked already and got answer. – αғsнιη Apr 27 '18 at 16:19
0

Assuming your names never have whitespace and that your names.txt file has a header on the first line as you show, you can do:

tail -n +2 names.txt | while read old new; do
    mv -i "$old" "$new"
done 

The tail -n +2 will print all lines of the file starting from the 2nd, so it will skip the header. Then, the -i option for GNU mv tells it to prompt you before overwriting an existing file (just in case there is a name clash).

terdon
  • 242,166
0

Using the shell: I assume that the tarfile names don't contain spaces and the sample names don't contain spaces:

{
    read header
    while read -r tarfile sample; do
        if [[ -f "$tarfile" ]]; then
            echo mv "$tarfile" "$sample.tar.gz"
        fi
    done
} < names.txt
glenn jackman
  • 85,964
0

One sed command should be enough, given that input file format:

sed '1!{s/^/mv /;s/$/.tar.gz/e}' names.txt

Demo, without the e:

sed '1!{s/^/mv /;s/$/.tar.gz/}' names.txt

Output:

Tar.gz_filenames       Sample_Names
mv TDF.tar.gz                Sample1.tar.gz
mv DFG.tar.gz                Sample2.tar.gz
mv GHY.tar.gz                Sample3.tar.gz
agc
  • 7,223