We help IT Professionals succeed at work.

MV --suffix Not Working

Thomas Grassi
on
Ubuntu 16.04.1

I run Nagios on this server.

I have a directory that contains .cfg files

I would like to move those .cfg files to and archive directory  

Tying to use the mv command and add a suffix to the end of the files.

#!/bin/sh

today=$(date +%Y-%m-%d)


mv -v -b --suffix=.'today'-TG *.cfg archive

Open in new window


 root@tgcs017:/usr/local/nagios/etc/objects/desktops# ./update.sh
'TGKW001.cfg' -> 'archive/TGKW001.cfg'
'TGKW003.cfg' -> 'archive/TGKW003.cfg'
'TGKW004.cfg' -> 'archive/TGKW004.cfg'
'TGKW005.cfg' -> 'archive/TGKW005.cfg'
'TGKW006.cfg' -> 'archive/TGKW006.cfg'
'TGKW007-10.cfg' -> 'archive/TGKW007-10.cfg'
'TGKW008-10.cfg' -> 'archive/TGKW008-10.cfg'
'TGKW009.cfg' -> 'archive/TGKW009.cfg'
'TGKW012.cfg' -> 'archive/TGKW012.cfg'

the script is simple and it runs it moves the files but never adds the suffix.

Any ideas?

Thank you
Comment
Watch Question

Distinguished Expert 2019

Commented:
Double check your mv accepts the options.....

Are you moving or copying?

Try
Instead of.. Your mv....

Use find /path/to/files -name "*.cfg" |while read a; do
echo "$a->/path/to/archive/$a.$today"
cp "$a" "/path/to/archive/$a.$today"
done
Thomas GrassiSystems Administrator

Author

Commented:
Arnold

I want to move them so I can create fresh files.

I found out why the --suffix did not work was because the destination folder was empty it only puts suffix on when file already exists in destination folder

#!/bin/sh

today=$(date +%Y-%m-%d)


mv -v -b --suffix=.'$today'-TG *.cfg archive

Open in new window


I can not get the date to show at all

root@tgcs017:/usr/local/nagios/etc/objects/desktops# mv -v -b --suffix=.'$today'-TG *.cfg archive
'TGKW001.cfg' -> 'archive/TGKW001.cfg' (backup: 'archive/TGKW001.cfg.$today-TG')
'TGKW003.cfg' -> 'archive/TGKW003.cfg' (backup: 'archive/TGKW003.cfg.$today-TG')
'TGKW004.cfg' -> 'archive/TGKW004.cfg' (backup: 'archive/TGKW004.cfg.$today-TG')
'TGKW005.cfg' -> 'archive/TGKW005.cfg'
'TGKW006.cfg' -> 'archive/TGKW006.cfg' (backup: 'archive/TGKW006.cfg.$today-TG')
'TGKW007-10.cfg' -> 'archive/TGKW007-10.cfg' (backup: 'archive/TGKW007-10.cfg.$today-TG')
'TGKW008-10.cfg' -> 'archive/TGKW008-10.cfg' (backup: 'archive/TGKW008-10.cfg.$today-TG')
'TGKW009.cfg' -> 'archive/TGKW009.cfg' (backup: 'archive/TGKW009.cfg.$today-TG')
'TGKW012.cfg' -> 'archive/TGKW012.cfg' (backup: 'archive/TGKW012.cfg.$today-TG')


root@tgcs017:/usr/local/nagios/etc/objects/desktops/archive# ls
TGKW001.cfg            TGKW003.cfg.$today-TG  TGKW005.cfg            TGKW007-10.cfg            TGKW008-10.cfg.$today-TG  TGKW012.cfg
TGKW001.cfg.$today-TG  TGKW004.cfg            TGKW006.cfg            TGKW007-10.cfg.$today-TG  TGKW009.cfg               TGKW012.cfg.$today-TG
TGKW003.cfg            TGKW004.cfg.$today-TG  TGKW006.cfg.$today-TG  TGKW008-10.cfg            TGKW009.cfg.$today-TG


Thoughts?
nociSoftware Engineer
Distinguished Expert 2019

Commented:
The suffix is only used when in the target directory a file would be removed/overwritten.

beside the need to use "$today"  in stead of 'today'...
You may want to consider the use of mmv which can handle these cases better.
    http://packages.debian.org/unstable/utils/mmv
Thomas GrassiSystems Administrator

Author

Commented:
Noci

Did you see my last posting?

I changed the today to $today but it is still not changing

I installed mmv    do you have a good example ?
Distinguished Expert 2019

Commented:
why not use the example I provided that would loop trough, replace the cp to mv.

Another option could be to use csv, subversion to handle the versioning of the configuration files....


Unfamiliar with the mv with the suffix option.

Find | while is a quick

in my example. $today is set by your script earlier on, it is not defined within my example without that.
Thomas GrassiSystems Administrator

Author

Commented:
Arnold

please post it using the CODE option for easier reading


Thanks
Distinguished Expert 2019

Commented:
#!/bin/sh
today=$(date+"%Y-%m-%d")
dir='/path/to/file/source'
archive='/path/to/file/archive'

find "$dir" -name "*.cfg" |while read a; do
echo "$a->/path/to/archive/$a.$today"
mv "$a" "$archive/$a.$today"
done 

Open in new window

Thomas GrassiSystems Administrator

Author

Commented:
Arnold getting close

#!/bin/sh

today=$(date +"%Y-%m-%d")

dir='/usr/local/nagios/etc/objects/windowsservers'
archive='/usr/local/nagios/etc/objects/windowsservers/archive'

find "$dir" -name "*.cfg" |while read a; do
echo "$a->/path/to/archive/$a.$today-TG"
mv "$a" "$archive/$a.$today-TG"
done

Open in new window


NOTE I am running the update.sh from the source directory '/usr/local/nagios/etc/objects/windowsservers'


My results
/usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg->/path/to/archive//usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg.2018-10-20-TG
mv: cannot move '/usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive//usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg.2018-10-20-TG': No such file or directory

Open in new window


Path problem?

ALso I added -TG to the suffix


Thoughts
Distinguished Expert 2019

Commented:
Yes path, the find with the full path returns the explicit ....

#!/bin/sh

today=$(date +"%Y-%m-%d")

dir='/usr/local/nagios/etc/objects/windowsservers'
archive='/usr/local/nagios/etc/objects/windowsservers/archive'
#cd "$dir" and change the search to . if this does not work
find "$dir" -name "*.cfg" |while read a; do
filename=$(basename "$a") # return the filename from the results.
echo "$a->$archive/$filename.$today-TG"
mv "$a" "$archive/$filename.$today-TG"
done

Open in new window

Thomas GrassiSystems Administrator

Author

Commented:
Arnold

#!/bin/sh

today=$(date +"%Y-%m-%d")

dir='/usr/local/nagios/etc/objects/windowsservers'
archive='/usr/local/nagios/etc/objects/windowsservers/archive'

find . -name "*.cfg" |while read a; do
filename=$(basename "$a")
echo "$a->/path/to/archive/$a.$today-TG"
mv "$a" "$archive/$a.$today-TG"
done

Open in new window


root@tgcs017:/usr/local/nagios/etc/objects/windowsservers# ./update.sh
./TGCS021-N2.cfg->/path/to/archive/./TGCS021-N2.cfg.2018-10-20-TG
./TGCS010-2019.cfg->/path/to/archive/./TGCS010-2019.cfg.2018-10-20-TG
./TGCS006-2012R2.cfg->/path/to/archive/./TGCS006-2012R2.cfg.2018-10-20-TG
./TGCS014-N1.cfg->/path/to/archive/./TGCS014-N1.cfg.2018-10-20-TG
./TGCS015.cfg->/path/to/archive/./TGCS015.cfg.2018-10-20-TG
./TGCS020-2016.cfg->/path/to/archive/./TGCS020-2016.cfg.2018-10-20-TG
./TGCS004.cfg->/path/to/archive/./TGCS004.cfg.2018-10-20-TG
./TGCS005-S1.cfg->/path/to/archive/./TGCS005-S1.cfg.2018-10-20-TG
./TGCS014-N2.cfg->/path/to/archive/./TGCS014-N2.cfg.2018-10-20-TG
./TGCS002-2016.cfg->/path/to/archive/./TGCS002-2016.cfg.2018-10-20-TG
./TGCS005-N1.cfg->/path/to/archive/./TGCS005-N1.cfg.2018-10-20-TG
./TGCS012.cfg->/path/to/archive/./TGCS012.cfg.2018-10-20-TG
./TGCS006-2016.cfg->/path/to/archive/./TGCS006-2016.cfg.2018-10-20-TG
./TGCS013.cfg->/path/to/archive/./TGCS013.cfg.2018-10-20-TG
./TGCS019-2012R2.cfg->/path/to/archive/./TGCS019-2012R2.cfg.2018-10-20-TG
./TGCS005-N2.cfg->/path/to/archive/./TGCS005-N2.cfg.2018-10-20-TG
./TGCS003-2012R2.cfg->/path/to/archive/./TGCS003-2012R2.cfg.2018-10-20-TG
./TGCS008.cfg->/path/to/archive/./TGCS008.cfg.2018-10-20-TG
./TGCS007-2012R2.cfg->/path/to/archive/./TGCS007-2012R2.cfg.2018-10-20-TG
./TGCS001-2012R2.cfg->/path/to/archive/./TGCS001-2012R2.cfg.2018-10-20-TG
./TGCS009-2012R2.cfg->/path/to/archive/./TGCS009-2012R2.cfg.2018-10-20-TG
./TGCS016.cfg->/path/to/archive/./TGCS016.cfg.2018-10-20-TG
./archive/TGCS009.cfg->/path/to/archive/./archive/TGCS009.cfg.2018-10-20-TG
mv: cannot move './archive/TGCS009.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive/./archive/TGCS009.cfg.2018-10-20-TG': No such file or directory
./archive/TGCS021-N2.cfg->/path/to/archive/./archive/TGCS021-N2.cfg.2018-10-20-TG
mv: cannot move './archive/TGCS021-N2.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive/./archive/TGCS021-N2.cfg.2018-10-20-TG': No such file or directory
./archive/TGCS006-2012R2.cfg->/path/to/archive/./archive/TGCS006-2012R2.cfg.2018-10-20-TG
mv: cannot move './archive/TGCS006-2012R2.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive/./archive/TGCS006-2012R2.cfg.2018-10-20-TG': No such file or directory
./archive/TGCS014-N1.cfg->/path/to/archive/./archive/TGCS014-N1.cfg.2018-10-20-TG

Open in new window



I moved them but look like a loop issue as it tried to do it again?????

Thoughts
Distinguished Expert 2019

Commented:
Sorry, did not make it clear. and your subordinate (archive is within the search path.
You have to choose whether to use the local (.) or use basename to strip out the filename from the results.
at which point the cd "$dir" has to be before to make it the working directory.


#!/bin/sh

today=$(date +"%Y-%m-%d")

dir='/usr/local/nagios/etc/objects/windowsservers'
archive='/usr/local/nagios/etc/objects/windowsservers/archive'

find "$dir" -name "*.cfg" |grep -vi 'archive' | while read a; do
filename=$(basename "$a")
echo "$a->/path/to/archive/$a.$today-TG"
mv "$a" "$archive/$a.$today-TG"
done

Open in new window



The grep will exclude the results from the archive subdirectory.
Thomas GrassiSystems Administrator

Author

Commented:
#!/bin/sh

today=$(date +"%Y-%m-%d")

dir='/usr/local/nagios/etc/objects/windowsservers'
archive='/usr/local/nagios/etc/objects/windowsservers/archive'

find "$dir"  -name "*.cfg" |grep -vi 'archive' |while read a; do
filename=$(basename "$a")
echo "$a->/path/to/archive/$a.$today-TG"
mv "$a" "$archive/$a.$today-TG"
done

Open in new window



/usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg->/path/to/archive//usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg.2018-10-20-TG
mv: cannot move '/usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive//usr/local/nagios/etc/objects/windowsservers/TGCS021-N2.cfg.2018-10-20-TG': No such file or directory
/usr/local/nagios/etc/objects/windowsservers/TGCS010-2019.cfg->/path/to/archive//usr/local/nagios/etc/objects/windowsservers/TGCS010-2019.cfg.2018-10-20-TG
mv: cannot move '/usr/local/nagios/etc/objects/windowsservers/TGCS010-2019.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive//usr/local/nagios/etc/objects/windowsservers/TGCS010-2019.cfg.2018-10-20-TG': No such file or directory
/usr/local/nagios/etc/objects/windowsservers/TGCS006-2012R2.cfg->/path/to/archive//usr/local/nagios/etc/objects/windowsservers/TGCS006-2012R2.cfg.2018-10-20-TG
mv: cannot move '/usr/local/nagios/etc/objects/windowsservers/TGCS006-2012R2.cfg' to '/usr/local/nagios/etc/objects/windowsservers/archive//usr/local/nagios/etc/objects/windowsservers/TGCS006-2012R2.cfg.2018-10-20-TG': No such file or directory

Open in new window



No file moved this time and none renamed

Thoughts
Software Engineer
Distinguished Expert 2019
Commented:
In the last code:
#!/bin/sh

today=$(date +"%Y-%m-%d")

dir='/usr/local/nagios/etc/objects/windowsservers'
archive='/usr/local/nagios/etc/objects/windowsservers/archive'

find "$dir"  -name "*.cfg" |grep -vi 'archive' |while read a; do
filename=$(basename "$a")
echo "$a->/path/to/archive/$filename.$today-TG"
mv "$a" "$archive/$filename.$today-TG"
done

Open in new window

$filename  in stead of $a in destination.

Using mmv:
#!/bin/bash
today=$(date +"%Y-%m-%d")
mmv "*.cfg"  "archive/#1.$today-TG"

Open in new window

Distinguished Expert 2019

Commented:
thanks noci. corrected one, forgot the other (important one)
nociSoftware Engineer
Distinguished Expert 2019

Commented:
Done both... now.
Thomas GrassiSystems Administrator

Author

Commented:
Thanks guys

it is now working
nociSoftware Engineer
Distinguished Expert 2019

Commented:
It was Arnold solution, i just fine tuned it...,
He should get most of the points... (assigned).