The mv command

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

  1. Rob

    Rob Administrator Staff Member

    Messages:
    573
    Likes Received:
    185
    Trophy Points:
    43
    '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

    Messages:
    24
    Likes Received:
    5
    Trophy Points:
    3
    This is very interesting fact that mv command is also used to rename the files or directories.
  3. JeanhuaJia

    JeanhuaJia New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    nice post!
  4. mrasplund

    mrasplund New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    Very interesting post. I use mv all the time.
  5. lobo

    lobo Active Member

    Messages:
    130
    Likes Received:
    74
    Trophy Points:
    28
    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

    Messages:
    115
    Likes Received:
    41
    Trophy Points:
    28
    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

    Messages:
    130
    Likes Received:
    74
    Trophy Points:
    28
    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

    Messages:
    115
    Likes Received:
    41
    Trophy Points:
    28
    Well, it does really change the path IF you believe that the path includes the leaf name.
  9. Videodrome

    Videodrome Active Member

    Messages:
    161
    Likes Received:
    64
    Trophy Points:
    28
    Maybe I want to change it to /dev/null
  10. GrumpyOldMan

    GrumpyOldMan Active Member

    Messages:
    115
    Likes Received:
    41
    Trophy Points:
    28
    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

    Messages:
    232
    Likes Received:
    136
    Trophy Points:
    43
    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

    Messages:
    161
    Likes Received:
    64
    Trophy Points:
    28
    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

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Love the multi-function tip, renaming and moving. Well demonstrated.
  14. Amit_*nix

    Amit_*nix New Member

    Messages:
    14
    Likes Received:
    4
    Trophy Points:
    3
    This article covers some common commands with well written description and a pinch of humor Enjoyed it :)
  15. Carmatious

    Carmatious New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Is there any method of accessing these tutorials in order?

Share This Page