MV --suffix Not Working

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
LVL 23
Thomas GrassiSystems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
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 AdministratorAuthor 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 EngineerCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Thomas GrassiSystems AdministratorAuthor 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 ?
arnoldCommented:
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 AdministratorAuthor Commented:
Arnold

please post it using the CODE option for easier reading


Thanks
arnoldCommented:
#!/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 AdministratorAuthor 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
arnoldCommented:
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 AdministratorAuthor 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
arnoldCommented:
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 AdministratorAuthor 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
nociSoftware EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
thanks noci. corrected one, forgot the other (important one)
nociSoftware EngineerCommented:
Done both... now.
Thomas GrassiSystems AdministratorAuthor Commented:
Thanks guys

it is now working
nociSoftware EngineerCommented:
It was Arnold solution, i just fine tuned it...,
He should get most of the points... (assigned).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.