The mv command

Discussion in 'Beginner Tutorials' started by Rob, Jul 9, 2013.

  1. Rob

    Rob Administrator Staff Member

    Joined:
    Oct 27, 2011
    Messages:
    606
    Likes Received:
    270
    'mv' is a command that we're going to use to move files around or to rename them. 'mv' sort of has a split-personality because it serves these two functions at the same time.

    'mv' command for renaming files

    Let's go back yet again to Tony's file, 'stuff' again. 'stuff' is not a good name for a file just as 'book' isn't a good name for a book. Just imagine: "The number one bestselling book this week is 'Book' by John Author.

    You should probably re-name this file to something meaningful. I would suggest doing something like this:

    Code:
    mv stuff tonys_jokes
    You may have noticed the underscore '_' in the title. It's there because Linux doesn't really like spaces in the file names. You can do it and Linux will accept it but it will put a \\ between the different words. Spaces are sort of 'faux pas' in Linux but not 'verboten'. It would be to your advantage to use '_' between words though.

    Moving files with the 'mv' command

    Now you can use the 'mv' command to move Tony's jokes into the directory you made to keep his files.


    Code:
    mv tonys_jokes tonyd/
    If you do cd tonyd and then ls to* you will see his file there along with 'toms_jokes' and 'tomato_soup_recipe'. (if you have another friend named Tom and you like to cook)

    You can also move entire directories with this command. You do not have to use the '-r' option as you did with 'cp'. You would just substitute the file name for a directory name

    Code:
    mv tonyd/ my_friends/
    would move the directory 'tonyd' to the directory 'my_friends'.
     
  2. Maverick1

    Maverick1 New Member

    Joined:
    May 12, 2013
    Messages:
    27
    Likes Received:
    7
    This is very interesting fact that mv command is also used to rename the files or directories.
     
  3. JeanhuaJia

    JeanhuaJia New Member

    Joined:
    Sep 26, 2013
    Messages:
    2
    Likes Received:
    0
    nice post!
     
  4. mrasplund

    mrasplund New Member

    Joined:
    Dec 10, 2013
    Messages:
    3
    Likes Received:
    1
    Very interesting post. I use mv all the time.
     
  5. lobo

    lobo Active Member

    Joined:
    Nov 30, 2013
    Messages:
    130
    Likes Received:
    76
    It's more intuitive to think of mv as moving a file to a file with a different name rather than renaming (which is what it does in fact).
     
  6. GrumpyOldMan

    GrumpyOldMan Active Member

    Joined:
    Oct 30, 2013
    Messages:
    220
    Likes Received:
    62
    Perhaps better to say path than name, since most people seem to think of the name as the last part of the path. So, what mv does is change the path of the file - this covers both "rename" and "relocate".
     
  7. lobo

    lobo Active Member

    Joined:
    Nov 30, 2013
    Messages:
    130
    Likes Received:
    76
    Well you can use mv as follows:
    Code:
    $ mv some_file some_other_file
    Which does not involve changing the path.

    This is not renaming, but moving a file to another file - which is also perfectly feasible when they are not in the same directory:
    Code:
    $ mv ~/path/to/some_file ~/other/path_to/some_other_file
     
  8. GrumpyOldMan

    GrumpyOldMan Active Member

    Joined:
    Oct 30, 2013
    Messages:
    220
    Likes Received:
    62
    Well, it does really change the path IF you believe that the path includes the leaf name.
     
  9. Videodrome

    Videodrome Active Member

    Joined:
    Nov 17, 2013
    Messages:
    321
    Likes Received:
    128
    Maybe I want to change it to /dev/null
     
  10. GrumpyOldMan

    GrumpyOldMan Active Member

    Joined:
    Oct 30, 2013
    Messages:
    220
    Likes Received:
    62
    Just tried it:

    $ cat /dev/null
    $ touch tester
    $ sudo mv tester /dev/null
    $ echo "junk" > /dev/null
    $ cat /dev/null
    junk
    $

    (It's a tinycore system. Back to normal after reboot)
     
  11. Cyber-Berserker

    Cyber-Berserker Active Member

    Joined:
    Nov 20, 2013
    Messages:
    232
    Likes Received:
    141
    If one believes?
    The file path either does or does not include {something}. If I believe the path to file name includes the hidden concept of mustard? I could give all my files the names of sandwiches, but my belief would still be wrong.
     
  12. Videodrome

    Videodrome Active Member

    Joined:
    Nov 17, 2013
    Messages:
    321
    Likes Received:
    128
    I moved to Null in Crunchbang. I was able to Cat it back to at first.

    But on reboot, it disappeared. Or /dev/null returned to an empty default state.
     
  13. Collinux

    Collinux New Member

    Joined:
    Mar 3, 2014
    Messages:
    4
    Likes Received:
    0
    Love the multi-function tip, renaming and moving. Well demonstrated.
     
  14. Amit_*nix

    Amit_*nix New Member

    Joined:
    May 9, 2014
    Messages:
    18
    Likes Received:
    4
    This article covers some common commands with well written description and a pinch of humor Enjoyed it :)
     
  15. Carmatious

    Carmatious New Member

    Joined:
    Jun 13, 2014
    Messages:
    2
    Likes Received:
    0
    Is there any method of accessing these tutorials in order?
     

Share This Page