#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0

    Need help with mv command & directories


    Hi,

    Am using unix bash shell on cygwin, confused about a thing,pls help me out..
    In my home ie,login directory i have a directory "lesson" in which i have file named file1, when i try to move file1 to admnistrator directory(my home directory) by command "mv file1 /home/~", it does not show file1 in administrator,when i go to common home directory i see this :-

    Administrator@comp /home
    $ ls -al
    total 1
    drwxrwxrwt+ 1 Administrator root 0 Oct 3 04:28 .
    drwxr-xr-x+ 1 Administrator root 0 Oct 3 04:06 ..
    drwxr-xr-x+ 1 Administrator None 0 Oct 3 04:04 Administrator
    -rw-r--r-- 1 Administrator None 28 Oct 3 04:24 ~



    tht means file1 has been renamed as "~" in home directory. But if i try to access this file by doing "cat ~" it gives error that ~ is a directory, obviously ~ is actually a notation for
    my administrator directory..so even if file1 has been renamed as ~, system will obviously refer anything denoted by ~ as administrator directory,a directory not a file...tried to
    remove this file "~" by doing rm but wasnt able to since its taking ~ as directory..ultimately removed it anyhow..So basically my question is when i did "mv file1 /home/~" it considered it to be renaming a file in common home directory, so i thought i should've done it as
    "mv file1 /home/~/" but the thing is we do this many times,i mean we dont always put forward slash '/' everytime when refererring to directories still files get moved to directories, so why this time it renamed it as a file & not moved into administrator directory...i tried some other example..like again in administrator directory i made a sudirectory D1, in D1 i made subdirectory D2 in which two files f1,f2..when while being in D2 directory i did "mv f1 /home/administrator/D1" it moves f1 to D1 & doesnt rename file f1 as file D1 in administrator directroy...please help me what am not understanding....am confused..please help.......
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    In bash (and some other shells) the tilde character (~) has special meaning. It generally is a shortcut/alias for 'my home directory'. Thus to move a file into your home directory from elsewhere you could use:
    Code:
    mv /a/file/from/somewhere/file1 ~
    or to move that file into a sub-directory under your home directory:
    Code:
    mv /a/file/from/somewhere/file1 ~/folder1
    What you have done with the
    Code:
    mv file1 /home/~
    is to take the file called file1 which is in your current working directory and move it to the /home directory and rename it to ~
    So, to move the file into your home directory you shoudl have used:
    Code:
    mv file1 ~
    Now, with the things as they stand currently you should be able to correct matters by using:
    Code:
    mv "/home/~" ~/file1
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0
    Hey hi, thanks much for explaining this well...so "~" is actually not user's home directory itself, but it is actually shortcut for the path to user's home directory?

    Also, while practising for 'cp' & 'mv' command i saw that when we use 'mv' command on files it doesn't retain originals which should be the case, but while i use 'mv' command on directories, the original directory with its file is retained in its previous place along with getting moved to the new place,means it behaves like 'cp' command while dealing with directories..
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Originally Posted by Amukherji
    Hey hi, thanks much for explaining this well...so "~" is actually not user's home directory itself, but it is actually shortcut for the path to user's home directory?
    Correct, yes. But as you have seen it will depend upon context!

    Originally Posted by Amukherji
    Also, while practising for 'cp' & 'mv' command i saw that when we use 'mv' command on files it doesn't retain originals which should be the case, but while i use 'mv' command on directories, the original directory with its file is retained in its previous place along with getting moved to the new place,means it behaves like 'cp' command while dealing with directories..
    That does not sound correct. The mv command, when used upon a directory name will move (or rename) that directory. Can you give us an example of what you have tried?

    If you are just in the process of learning all this, this is a very good time to get some basic concepts learned so do take the time to understand why, for example, the mv and cp command act in certain ways. Here is a little question for you to ponder upon (don't worry, there's no scoring!)> is the following a valid unix command?
    Code:
    cp *
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0
    Originally Posted by SimonJM
    Correct, yes. But as you have seen it will depend upon context!


    That does not sound correct. The mv command, when used upon a directory name will move (or rename) that directory. Can you give us an example of what you have tried?

    If you are just in the process of learning all this, this is a very good time to get some basic concepts learned so do take the time to understand why, for example, the mv and cp command act in certain ways. Here is a little question for you to ponder upon (don't worry, there's no scoring!)> is the following a valid unix command?
    Code:
    cp *

    Hello, i thought this cp * would not be valid command since destination directory or file is not mentioned,
    still when i tried it, as i mentioned above in my administrator or home directory I have directory D1 having files k1,k2 & sub-directory D2...in directory D2 i have files f1,f2,f3..

    So, when i did
    Code:
    cp *
    in directory D2 ,i got this
    Code:
    cp: target `f3' is not a directory
    Then when i did
    Code:
    cp *
    in directory D1, files k1,k2 got copied to subdirectory D2 resulting in k1,k2,f1,f2,f3 in D2


    Similarly when i did
    Code:
    mv *
    in D1 it moved k1,k2 to directory D2.
    Don't get why

    Seems like hv to go through whole file system of unix as well as its basics once..can u suggest any online book where file systems & basics of unix are explained in detail & clearly..

    Thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    The man pages are your friend!!!

    As for the cp * command. What does the * get replaced with? It gets replaced with the directory listing of the current directory. So if you had in your current directory f1, f2, f3, f4 then before execution the shell would expand the * into f1 f2 f3 f4, making the actual command to be run:
    Code:
    cp f1 f2 f3 f4
    Now, depending on what those directory entries actually are will dictate whether the command will work or not. If they are all files then, as you have seen, the shell complains that the target (the last entry in the list) is not a directory. If f4 had been a directory then the first three files (assuming f1, f2 and f3 are files) will be copied into that directory. If any of f1, f2 or f3 are directories the shell will complain that you are trying to copy a directory without using the -r (recursive) option (if memory serves!).
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0
    Originally Posted by SimonJM
    The man pages are your friend!!!

    As for the cp * command. What does the * get replaced with? It gets replaced with the directory listing of the current directory. So if you had in your current directory f1, f2, f3, f4 then before execution the shell would expand the * into f1 f2 f3 f4, making the actual command to be run:
    Code:
    cp f1 f2 f3 f4
    Now, depending on what those directory entries actually are will dictate whether the command will work or not. If they are all files then, as you have seen, the shell complains that the target (the last entry in the list) is not a directory. If f4 had been a directory then the first three files (assuming f1, f2 and f3 are files) will be copied into that directory. If any of f1, f2 or f3 are directories the shell will complain that you are trying to copy a directory without using the -r (recursive) option (if memory serves!).

    Thanks cleared! and the doubt that i had about moving the directories with originals beng retained, cant recall that now , will come here again if wud come across anything like that anytime....was checking man pages for something yesterday & didn't get much there that is why wanted to read something online..ok will ask that here only...i wanted to know what is the "total" that "ls -l" or "ls -al" displays..hv read that it is the total block size occupied by that directory or file..if its in kilobytes that how much bytes a block consists of..checked in man page for ls command...tried "ls -s", "ls -k", "ls -hl" still

    just an example:-

    Administrator@comp ~/D1
    $ ls -s
    total 13
    1 f1 1 f2 0 D2 1 f3 4 f4 1 f5


    Administrator@pc ~/D1
    $ ls -al
    total 13
    drwxr-xr-x+ 1 Administrator None 0 Oct 7 06:03 .
    drwxr-xr-x+ 1 Administrator None 0 Oct 6 05:05 ..
    -rwxr-xr-x 1 Administrator None 49 Oct 3 05:55 f1
    -rwxr-xr-x 1 Administrator None 27 Oct 3 05:55 f2
    -rwxr-xr-x 1 Administrator None 596 Oct 3 05:55 f3
    -rwxr-xr-x 1 Administrator None 1044 Oct 3 05:55 f4
    drwxr-xr-x+ 1 Administrator None 0 Oct 7 03:49 D2
    -rwxr-xr-x 1 Administrator None 262 Oct 3 05:55 f5

    please help with this too & i will read about these things from start...

    Thanks a lotttt !
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Yes, I agree - there's a lot of stuff that the man pages say that is only helpful if you know what they are talking about in the first place! One of those areas is 'size'. There are a few commands (ls, df, du, etc.,) that will output a 'size'. Generally, without any further options used, that size will be in blocks. But no-one tells you what a block is. Well, a block is 512 bytes (0.5KB). Many commands give the option of a -h parameter (h for human-readable) which will convert blocks into the best units (KB, MB, GB, etc.) for displaying. Some have a -k (for kilobyte) to force output in a specific unit - which is very useful for scripts etc., to use as you then know exactly what size the thing is.
    A lot of those are implementation specific, so check the local man pages to see if they are valid.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    block size varies


  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0
    Thanks SimonJm & b49P23TIvg, ya man pages are most authentic but have to dig into that if try to get something!....am still not able to get then why my f4 script/file is showing 4 blocks? when i did "ls -s", if its 1044/1k...similarly why f3 is 1 block? if its 596k and the same thing about other files...please correct me where am i wrong..thanks..
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    ls reports size in bytes - sorry, mistakenly suggested it did otherwise in my previous post!

    The second column (that shows up as being 1 most of the time in your listing) is the number of links that entry has. That will usually be 1 for a file (unless, of course, you create alink with the ln command). An entry will only ever actually get deleted when it's link count is reduced to 0. Thus if you had a file with two links doing an rm command on one of those links would not actually remove the file.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0
    Thanks, but then "ls -s" shows size of a file in blocks? so if my file f2 is of 1044 bytes then why in "ls -s" command its showing 4
    Since file f3 being of 596 bytes is showing 1...also why for directory d2 it shows 0..i still dint understand the concept
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Not quite sure why that'd be, as ...
    Originally Posted by man pages
    -s Display the number of file system blocks actually used by each file, in units of 512 bytes, where partial units are rounded up to the next integer value. If the output is to a terminal, a total sum for all the file sizes is output on a line before the listing. The environment variable BLOCKSIZE overrides the unit size of 512 bytes.
    Being 1044 bytes would suggest f2 would use 3 blocks and f3 use 2. Directory entries do not, themselves, take up blocks on disk, just in the inode tables.
    Maybe a quirk of the OS?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    26
    Rep Power
    0
    Thanks..okay..ya that might be the case...or might be file which is 1044 bytes is distributed over 4 data blocks where each block contains only part of the file.. ,i think is should leave it here assuming it to be quirk only

IMN logo majestic logo threadwatch logo seochat tools logo