Wednesday, 22 January 2020

mv and rename Commands

I created a file called myself:
 
Red Hat > ls
Red Hat > touch myself
Red Hat > ls
myself
Red Hat >
 
I decided it should have been called yourself so I used mv to change its name:
 
Red Hat > mv myself yourself
Red Hat > ls
yourself
Red Hat >
 
Then I decided to use the rename command to call it himself instead. To do this I had to provide the part of the name I wanted to change, what I wanted to change it to and the filename to act upon (if that makes sense):
 
Red Hat > rename yourself himself yourself
Red Hat > ls
himself
Red Hat >
 
This approach is more complicated but it gives greater flexibility. To try this out I created several files with .txt suffixes:
 
Red Hat > rm *
Red Hat > touch file{1..5}.txt
Red Hat > ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
Red Hat >
 
I then decided to remove the suffixes and did this as follows:
 
Red Hat > rename .txt '' file*
Red Hat > ls
file1  file2  file3  file4  file5
Red Hat >