make mv command quiet

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
dormantgp
Level 1
Level 1
Posts: 4
Joined: Tue Dec 26, 2023 10:52 am

make mv command quiet

Post by dormantgp »

I have a script to remove all extra dots in filenames:

Code: Select all

for f in *.*
do
    pre="${f%.*}"
    suf="${f##*.}"
    mv -n -i -- "$f" "${pre//./_}.${suf}"
done
If there are filenames with no extra dots, mv tells me, even when I use the -n switch.
mv: 'fred_1.txt' and 'fred_1.txt' are the same file
mv: 'fred__2.txt' and 'fred__2.txt' are the same file
mv: 'fred__3_.txt' and 'fred__3_.txt' are the same file
mv: 'fred.txt' and 'fred.txt' are the same file
Is there a way to silence mv?
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: make mv command quiet

Post by Koentje »

Code: Select all

for f in *.*
do
    pre="${f%.*}"
    suf="${f##*.}"
    mv -n -i -- "$f" "${pre//./_}.${suf}" 2>/dev/null
done
2>/dev/null redirects stderr to /dev/null
If something goes wrong, you'll never know! ;)
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: make mv command quiet

Post by xenopeek »

Instead of making mv quiet, don't rename the file when not needed:

Code: Select all

for f in *.*
do
    pre="${f%.*}"
    suf="${f##*.}"
    if [[ "$f" != "${pre//./_}.${suf}" ]]; then
        mv -n -i -- "$f" "${pre//./_}.${suf}"
    fi
done
Image
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: make mv command quiet

Post by Koentje »

That's indeed way better.
What i don't understand is the ${suf}? That doesn't do anything.. wouldn't it be better to use $suf instead?
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: make mv command quiet

Post by Shiva »

dormantgp wrote: Sat Jan 06, 2024 2:06 pm Is there a way to silence mv?
Pre-select files when mv would have no "objections" :

Code: Select all

find . -type f -name "*..*" | while read file; do mv "$file" "${file//../.}"; done
replaces double dots with single dots on a whole tree (find is recursively searching - subfolders too).

However commands like mv, rm.. are definitive and may prove harmful if misused. So I would first think running a dummy :

Code: Select all

find . -type f -name "*..*" | while read file; do echo mv "$file" "${file//../.}"; done
The supplemental echo outputs what will happen if the command is executed. If it's satisfactory then run the first one (without echo).
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: make mv command quiet

Post by xenopeek »

I understood the command to find files like fred.flinstone.txt and rename them to fred_flinstone.txt. Not fred..txt to fred_.txt.
Image
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: make mv command quiet

Post by Shiva »

xenopeek wrote: Sun Jan 07, 2024 5:21 am I understood the command to find files like fred.flinstone.txt and rename them to fred_flinstone.txt. Not fred..txt to fred_.txt.
And you're probably right. My code just replaces two consecutive dots with one.
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: make mv command quiet

Post by Shiva »

A right start for a pipeline version and the same renaming algorithm could have been :
printf "%s\n" *.* | grep '\..*\.' |

One line version :

Code: Select all

printf "%s\n" *.* | grep '\..*\.' | while read fn; do Suf=".${fn##*.}"; Pre="${fn%.*}"; echo mv "$fn" "${Pre//./_}$Suf"; done
Multi-line version with comments :

Code: Select all

printf "%s\n" *.* \
 | grep '\..*\.' \
 | while read fn; do Suf=".${fn##*.}"; Pre="${fn%.*}"; \
 echo mv "$fn" "${Pre//./_}$Suf"; done
- line 1 : prints out everything that has an extension one per line
- line 2 : keep only those having at least 2 dots : we are sure that from here on all files remaining have to be renamed according to replacing . with _ and their names will be different, so no mv error
- line 3 : while loop begin extracting Suf (extension) and Pre (prefix)
- line 4 : dummy running with echo and ending loop

If it's OK, run same command without echo.
Post Reply

Return to “Scripts & Bash”