cancel
Showing results for 
Search instead for 
Did you mean: 
  • 1,958 Views

Rename on specific character for multiple file name in one command line

Dear all,

I would like to rename specific character all multiple file name at once as following example.

In original file name is ;

- file1_before.tsv

- file2_before.tsv

- file3_before.tsv

I want to change with only one command line without losing data as follow;

- file1.tsv

- file2.tsv

- file3.tsv

I'm very appriciate your idea and request for your suggestion.

Thank you very much.

0 Kudos
2 Replies
Noel
Mission Specialist
Mission Specialist
  • 1,927 Views

@Nyi_Nyi_Lwin 

According to your example, the below command will work!

# rename  _before  ''  file?_before.tsv

 


@Nyi_Nyi_Lwin wrote:

Dear all,

I would like to rename specific character all multiple file name at once as following example.

In original file name is ;

- file1_before.tsv

- file2_before.tsv

- file3_before.tsv

I want to change with only one command line without losing data as follow;

- file1.tsv

- file2.tsv

- file3.tsv

I'm very appriciate your idea and request for your suggestion.

Thank you very much.


 

Jeff_Schaller
Flight Engineer
Flight Engineer
  • 1,818 Views

Similarly to Noel's excellent rename-based answer, you could use shell parameter expansion to accomplish the same result:

for filename in file?_before.tsv; do mv -- "$filename" "${filename%_before.tsv}.tsv"; done

This "one-liner" loops over any/all filenames matching the "file?_before.tsv" pattern. There's only one command inside the loop -- the "mv" to rename each file in turn. The target filename is generated by removing the text "_before.tsv" from the end of the filename and then appending ".tsv" back to the end. I've used "--" to tell "mv" that there are no further options, as a generic safety principle in case you use a similar command in the future where filenames potentially begin with dashes and get mistakenly confused as options to mv.

Join the discussion
You must log in to join this conversation.