-2

I have around 255 files in the current directory. A list of some of them is

ALLT042194_TAB20.tsv_20180117_001434_0084
ALLT194_TAB20.tsv_20180117_0083
ALLT194_TAB20.tsv_20180117_0084
ALLT194_TAB20.tsv_20180117_0089
ALLT194_TAB20.tsv_20180117_0085
ALLT194_TAB20.tsv_20180117_0082
ALLT194_TAB20.tsv_20180117_0060
ALLT194_TAB20.tsv_20180117_0044
ALLT194_TAB20.tsv_20180117_0064
ALLT194_TAB20.tsv_20180117_0094

I want to remove all chars after .tsv. For this I have used the code below. In the code I am removing the last 14 characters, so I am getting the file name up through .tsv. But my code is giving output for 15 files only and removing the rest of the files from the 255.

for i in *
do
  j=`echo $i | sed -e 's/..............$//'`
  mv $i $j
done

How can I remove everything after .tsv?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Your code has some quoting errors which http://shellcheck.net/ will helpfully point out to you. But the basic problem is that the file names are not unique if you remove the stuff after `.tsv` and so your code is doing exactly what you are saying you want it to do -- it's moving a number of files to the same name, with most iterations ending up replacing a file you have previously moved to the same name. The real problem seems to be that you *actually* want something else. We can't guess what that is. – tripleee Jan 17 '18 at 13:14
  • Possible duplicate of https://stackoverflow.com/a/602770/4486184 – Camusensei Jan 17 '18 at 13:47

1 Answers1

0

Here is a version with correct quoting:

for original_filename in *; do
  mv "$original_filename" "${original_filename%.tsv*}.tsv"
done

N.B: As pointed out by @tripleee, you will overwrite most files by doing that.

Camusensei
  • 1,475
  • 12
  • 20
  • Please find a suitable duplicate instead of posting new answers which duplicate existing answers. – tripleee Jan 17 '18 at 13:25
  • 2
    Also `"${original_filename%.tsv}"` is not correct here; you need to trim everything after `.tsv` too. – tripleee Jan 17 '18 at 13:26
  • Indeed, edited. What am I supposed to do with a suitable duplicate? https://stackoverflow.com/a/602770/4486184 is similar but not the same answer, do you think I should mark it as duplicate nonetheless? – Camusensei Jan 17 '18 at 13:39
  • 1
    At your privilege level you can only add a comment but if you put a comment like "Possible duplicate of (URL)" it will be picked up as a proposed duplicate if others with more rep want to vote to close as dupe. Five close votes by users with the required privilege (3k+ rep) and it will be closed. – tripleee Jan 17 '18 at 13:42