quantik
quantik

Reputation: 816

Change basename of multiple files in a single directory

I have many files of the form {a}_{b}_R1.txt I would like to change the basenames of these files to {a}.{b}.txt how would I go about this? I can only find out how to do this with extensions. Thanks.

Upvotes: 1

Views: 1089

Answers (2)

quantik
quantik

Reputation: 816

This works as well:

for f in *.txt; do mv "$f" "echo $f | sed s/_R1//"; done then for f in *.txt; do mv "$f" "echo $f | sed s/_/./";

Upvotes: 2

Inian
Inian

Reputation: 85800

You can just a simple loop in the native bash shell under OS X from terminal, after navigating to the folder containing those text files,

# Looping for all the text files in the current 
# directory
for file in *.txt; do
    # Stripping off '_R1' from the file-name
    temp1="${file//_R1/}"
    # Replacing '_' with '.' and renaming the file to the name generated
    temp2="${temp1//_/.}"
    echo "$temp2" "$file"
    #mv -v "$file" "$temp2"
done

Remove the line with echo and uncomment the mv once you find the names are changed accordingly.

for file in *.txt; do temp1="${file//_R1/}"; temp2="${temp1//_/.}"; mv -v "$file" "$temp2"; done

Upvotes: 3

Related Questions