mv --suffix=.bak not working

hi,

as given below
http://www.thegeekstuff.com/2010/12/mv-command-examples/
$ ls
file1    file2

$ mv --suffix=.bak file1 file2

$ ls
file2    file2.bak


i tried as below
bash-4.4$ cd root                                                                                                                                  
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
drwxr-xr-x 3 14392 14392 4096 Nov 22 16:58 new                                                                                                    
bash-4.4$ touch d.txt                                                                                                                              
bash-4.4$ mv --suffix=.bak d.txt e.txt                                                                                                            
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
drwxr-xr-x 3 14392 14392 4096 Nov 22 16:58 new                                                                                                    
-rw-r--r-- 1 14392 14392    0 Nov 22 17:03 e.txt                                                                                                  
bash-4.4$                                                                                                                                          
           

it is not working

please advise
LVL 7
gudii9Asked:
Who is Participating?
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:
Please when asking questions, try to explain in your own words what it is you are trying to do.
The results of moving file1 to file2 when you specify the suffix=.bak has a specific meaning, I am not sure what you are expecting or how it changed/differed.
From the manual page of mv :
" -S, --suffix=SUFFIX
              override the usual backup suffix
"

It is easier to answer a direct question with the context of the asker, then try to assemble what the asker is after based on the various external references one might include in the question.

listing directory contents is just that a list of directory content, the short form provides absolutely no useful information.
i.e. directory a
has file1 and file2
you run the command
now you have file2 and file2.bak

file2 remained what it was while file1 was renamed to file2.bak because it could not be move given file2 already existed in the folder.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Kindly give feedback on your other questions as well, the ones where you've gotten expert replies on. Thank you!
1
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
@gudii9

Explaining following requirements and related commands/output:
a)
Requirement:
if file2 is not present
{
           Rename file1 to  file2
}
else
{
         Rename file1 to  file2.bak
}
Related commands:
#!/bin/bash
if [ ! -f file1 ]
then
	/usr/bin/touch file1
fi
if [ ! -f file2 ]
then
	/usr/bin/touch file2
fi
echo "Obtaining output before using /bin/mv"
echo "/bin/ls -ltr file1* file2*"
/bin/ls -ltr file1* file2*
# if file2 is not present
if [ ! -f file2 ]
then
	echo /bin/mv -i file1 file2
	/bin/mv -i file1 file2
	#Rename file1 to  file2
else
	echo /bin/mv --suffix=.bak file1 file2
	/bin/mv --suffix=.bak file1 file2
	#Rename file1 to  file2.bak
fi
echo "Obtaining output after  using /bin/mv"
echo "/bin/ls -ltr file2*"
/bin/ls -ltr file2*

Open in new window


Test1:
$ #Remove file2*
$ /bin/rm -f file2*
$ ./29069891.sh
Obtaining output before using /bin/mv
/bin/ls -ltr file1* file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file1
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file2
/bin/mv --suffix=.bak file1 file2
Obtaining output after  using /bin/mv
/bin/ls -ltr file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file2
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file2.bak

Open in new window


Test2:
$ /bin/ls -ltr file*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file2
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file2.bak
$#This time we are not removing file2*
$ ./29069891.sh
Obtaining output before using /bin/mv
/bin/ls -ltr file1* file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:52 file2.bak
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:54 file2
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:56 file1
/bin/mv --suffix=.bak file1 file2
Obtaining output after  using /bin/mv
/bin/ls -ltr file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:54 file2.bak
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 13:56 file2

Open in new window

From test2, you can see following exception:
Initially file2.bak was present (Nov 23 13:52)
Because of using /bin/mv --suffix command, file2(Nov 23 13:54) has been renamed to file2.bak (after overwriting existing file2.bak)
Then file1 has been renamed to file2
Hence the content of the file file2.bak (Nov 23 13:52) was lost.

will modify existing script and will provide that after testing the same.
0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
@gudii9,

Here goes related script to handle such exceptions:
#!/bin/bash
#Create file1 if file1 is not present
if [ ! -f file1 ]
then
	/usr/bin/touch file1
fi
#Create file2 if file2 is not present
if [ ! -f file2 ]
then
	/usr/bin/touch file2
fi
BACKUP_FILENAME="."''`/bin/date "+%d_%b_%Y_%S"`''
#If file2"$BACKUP_FILENAME" file is present, sleep for 1 second to obtian new BACKUP_FILENAME
if [ -f file2"$BACKUP_FILENAME" ]
then
	/bin/sleep 1
	BACKUP_FILENAME="."''`/bin/date "+%d_%b_%Y_%S"`''
fi
echo "Obtaining output before using /bin/mv"
echo "/bin/ls -ltr file1* file2*"
/bin/ls -ltr file1* file2*
# if file2 is not present
if [ ! -f file2 ]
then
	echo /bin/mv -i file1 file2
	/bin/mv -i file1 file2
	#Rename file1 to  file2
else
	echo /bin/mv --suffix=$BACKUP_FILENAME file1 file2
	/bin/mv --suffix=$BACKUP_FILENAME file1 file2
	#Rename file1 to  file2$BACKUP_FILENAME
fi
echo "Obtaining output after  using /bin/mv"
echo "/bin/ls -ltr file2*"
/bin/ls -ltr file2*

Open in new window

Related testing:
$ /bin/rm file*
$ ./29069891.sh
Obtaining output before using /bin/mv
/bin/ls -ltr file1* file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file1
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2
/bin/mv --suffix=.23_Nov_2017_05 file1 file2
Obtaining output after  using /bin/mv
/bin/ls -ltr file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2.23_Nov_2017_05


$ ./29069891.sh
Obtaining output before using /bin/mv
/bin/ls -ltr file1* file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2.23_Nov_2017_05
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:09 file1
/bin/mv --suffix=.23_Nov_2017_04 file1 file2
Obtaining output after  using /bin/mv
/bin/ls -ltr file2*
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2.23_Nov_2017_04
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:08 file2.23_Nov_2017_05
-rw-r--r-- 1 MurugesanDinesh TES 0 Nov 23 14:09 file2

Open in new window


Few of them prefer to use BACKUP_FILENAME to use the value of date obtained using /bin/ls or /usr/bin/stat command.
However that will not be taken care under following conditions:
1. /usr/bin/stat and /bin/ls are older version
  When the files are old files, difficult to obtain the last modified date/time.
In this case, better to use BACKUP_FILENAME pointing to /bin/date.
Also the backup file last modified date do not get changed on using the given shell script.
0

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
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
1. Assisted solutions
2. Follow required feed back
3. Resolved and tested
4. Inactive for 14 days
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.