Using the mv command in Terminal

Discussion in 'OS X Yosemite (10.10)' started by ScurvyMro, Feb 13, 2015.

  1. ScurvyMro macrumors newbie

    Joined:
    Feb 13, 2015
    #1
    I'm trying to understand how the move command works. I've googled it of course and found some information, but I'm still confused.

    I'm logged into the Mac as myself (call me Dad), and I want to use terminal to move some files around in the Pictures folder in my wife's profile (call her Mom). I have permission to the Pictures folder. In terminal, I issued the command to change to the folder. when I execute 'pwd', it shows the current folder as /users/mom/pictures. Now, assume I want to move all JPG files from the root of the pictures folder to a folder called photos that is one level below pictures. I've tried 'mv /users/mom/pictures/*.jpg /users/mom/pictures/photos' but I get 'No such file or directory'. I tried several variations of that command with the same results. So I decided to try moving just one file. That file was called 'weird.png'. so I issue the command mv weird.png ~/photos. That command executed without error, but now the file is no where to be found. It is not in the pictures folder, and it's not in my pictures folder, and it is not in the photos folder. So, clearly I am not understanding how this works.

    Can anyone help?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Please use CODE tags around commands.

    This command:
    Code:
    mv weird.png ~/photos
    moves the weird.png file to your home folder. It also renames it to "photos", with no extension. That is, its name is exactly "photos", not "photos.png".

    It does this because ~/ stands for your login's home folder. You said you logged in as 'dad', so ~/ refers to dad's home folder, not mom's.

    If you had a folder in your home folder named "photos", then weird.png will be placed inside that folder. Since you didn't say you had that, I'm assuming you don't. That means the 'mv' command sees the second parameter as non-existent, and it will move the first parameter (weird.png) to the given location (~/) and rename it (to "photos").


    Use this command and you should see the file listed:
    Code:
    ls -l ~
    
    Then use this command to determine what the file is:
    Code:
    file ~/photos
    
    In both cases, you can select, then copy and paste Terminal's output into a post. Again, use CODE tags around what you paste.


    Going back to your original task, you wrote:
    Now, assume I want to move all JPG files from the root of the pictures folder to a folder called photos that is one level below pictures. I've tried 'mv /users/mom/pictures/*.jpg /users/mom/pictures/photos' but I get 'No such file or directory'.​
    Question 1 for you to answer:
    Does the folder "photos" already exist in "/users/mom/Pictures" or not?

    If the folder doesn't exist, you have to create it first. You can do that in Finder, or in Terminal.

    If you want the folder to be owned by 'mom', then you should login as 'mom' to create the folder. Otherwise it will be owned by 'dad', because that's who you're logged in as.

    You could change the owner after creating the folder (there's a command for that), so decide how you want to proceed and then tell us.

    I assume you can login as mom without further instructions. If you want to create the folder using Terminal, then ask again and we can tell you what commands to use.


    After the folder "/users/mom/Pictures/photos" exists, then this command should work:
    Code:
    mv /users/mom/pictures/*.jpg /users/mom/pictures/photos
    If it doesn't, then run this command and post the complete output in CODE tags:
    Code:
    echo /users/mom/pictures/*.jpg
    If the 'echo' command says something unexpected, copy and paste the complete output. Don't paraphrase it, retype it, or edit it. Error messages are important details.
     
  3. ScurvyMro thread starter macrumors newbie

    Joined:
    Feb 13, 2015
    #3
    Thanks for your help on this. I tried the command again with the same results.

    This command:

    Code:
    mv /users/mom/pictures/*.jpg /users/mom/pictures/photos
    Results in the following error:

    Code:
    rename /users/mom/pictures/*.jpg to /users/mom/pictures/photos/*.jpg: No such file or directory
    I ran the echo command:

    Code:
    echo /users/mom/pictures/*.jpg
    which produced this output:

    Code:
    /users/mom/pictures/*.jpg
    For the mv command, I ran it both under my profile and my wife's with the same result. So I thought maybe I had created the photos folder under my own profile, resulting in some permissions issue. But in fact, the permissions for my wife are set to read&write. They were set to Read Only for Staff and Everyone. I changed that. But then I looked at the permissions of some of the files in my wife's "pictures" folder. Most of them are Read & Write for my wife but Read Only for everyone else. So clearly I just need to be doing these commands from my wife's profile.
     
  4. ScurvyMro thread starter macrumors newbie

    Joined:
    Feb 13, 2015
    #4
    After fiddling with it for a while, I finally decided to forget about the *.jpg mask, and just move all of the files, which worked, then sort the files in finder by type and delete what I needed to. I don't know why it didn't work the other way, but it's moot.

    Thanks for your help.
     
  5. chown33, Feb 14, 2015
    Last edited: Feb 14, 2015

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    FWIW, it probably didn't work because wildcard (i.e. the "*") expansion needs exact spelling of pathnames. Although the filesystem is case-insensitive, wildcard expansion is case-sensitive.

    The actual pathname of the enclosing directory is something like this:
    Code:
    /Users/mom/Pictures/
    
    so that's what you have to type.


    EDIT
    The pathname components aren't case-sensitive, but the wildcard components are. See post #7 below.
     
  6. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #6
    I don't know why the O.P.'s commands didn't work. Maybe he didn't get the path quite right. But it turns out that wildcard processing does not seem to be case-sensitive in my case:

    Code:
    bob-mbp-wl:~$ pwd
    /Users/bob
    bob-mbp-wl:~$ ls -1 /users/bob/*.txt
    /users/bob/data.txt
    /users/bob/test.txt
    /users/bob/time-machine-pref.txt
    
     
  7. chown33, Feb 14, 2015
    Last edited: Feb 14, 2015

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    You are correct. I edited my earlier post.

    The part that's case-sensitive is the wildcard part. For example, I expect this won't work on your machine:
    Code:
    ls -1 /users/bob/*.Txt
    
    So if the OP's files have the extension ".JPG", then the wildcard "*.jpg" won't match.
     
  8. w0lf macrumors 65816

    w0lf

    Joined:
    Feb 16, 2013
    Location:
    USA
    #8
    I believe this is the correct command:

    Code:
    mkdir -p /users/mom/pictures/photos/
    mv /users/mom/pictures/*.[Jj][Pp][Gg] /users/mom/pictures/photos
    
     
  9. SlCKB0Y macrumors 68040

    SlCKB0Y

    Joined:
    Feb 25, 2012
    Location:
    Sydney, Australia
    #9
  10. dyt1983, Feb 14, 2015
    Last edited: Jun 1, 2015

    dyt1983 macrumors 65816

    Joined:
    May 6, 2014
    Location:
    USA USA USA
    #10
    edit: To remove personally identifying info not relevant to the conversation.
     
  11. w0lf macrumors 65816

    w0lf

    Joined:
    Feb 16, 2013
    Location:
    USA
    #11
    Except any variation of JPG is valid. For example .JpG or .jPg or .JPg are all the same extension.

    Also no point in setting up an alias if you're only gonna use the command once. Unless you add it to the .profile so it's always available to you.
     
  12. dyt1983, Feb 14, 2015
    Last edited: Jun 1, 2015

    dyt1983 macrumors 65816

    Joined:
    May 6, 2014
    Location:
    USA USA USA
    #12
    edit: To remove personally identifying info not relevant to the conversation.
     

Share This Page