Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I am a little confused on how the removal works. If I delete an empty directory, do all of the subdirectories automatically get moved up? Or would I need to copy all of the contents in each directory up one level and then delete the bottom directory?
Thank you for the link :-)
It was super helpful-- I do have one last question though...so let's say I have directories a/b/c/d and I want to remove empty directory b and the other up. As mentioned previously I would copy c into b using -dpr ... does the directory that I copied into retain it's original name of 'b'? so that my new directory layout would be a/b/c or would it look like a/c/d ?
There's probably a misconception here: a directory which contains other subdirectories (and no regular files) cannot be defined as empty.
To move subdirectories up one level and remove the then empty b directory (referring to your example of a/b/c/d) you can run these commands:
mv a/b/* a # this moves c and everything else contained in b into a (c also contains the d subdirectory)
rm -r a/b # this removes b and all its contents (none because the previous command moved them up one level)
Brilliant. Thank you!! In the case that you have a directory and a subdirectory with the same name, how would it know which directory to move into? Can I just write out the entire path like so? (assuming we have a/a/c/d)
mv a/a/c a
rm -r a/a
Last edited by calvarado777; 09-11-2012 at 09:12 AM.
The shell doesn't care what names those paths have. You could have a/a/a/a/a/a and need to move the 4th "a" directory contents upward by one directory and the commands needed wouldn't change a lot, because the one whom needs to pay attention to the names he's entering it's you
The commands you entered work as long as a/a contains only one subdirectory (like "c"): if there were more of them, you'd better use the * to select them all at once instead of writing down all the directory names.
Oh no, I think I have gone and confused myself. I understand what you mean about needing to include the /* if there are more than one subdirectories--that makes sense... it isn't possible to have two directories with the same name right? if directory 'a' has two subdirectories 'b' which is empty and 'b' which has contents how would it know which b to remove? (given a/b/b/c/d)
mv a/b/* a
rm a/b # I figure no recursion here since we know it's empty--but this is where I am lost--it doesn't really know which b I want-- would it
# make sense to maybe create a temporary file that I could then move back into my a directory?
I don't understand the issue.
You can't have same-named files (and directories, as they're treated like files) in the same location, so you'll never encounter a directory "a" which contains -- on the same level -- two "b" subdirectories.
You continue to make the wrong assumption. Looking at your last example of directory structure (a/b/b/c/d) you say that a/b is empty and a/b/b is not, but the truth is that none of them is empty.
Perhaps, a tree representation of the structure can help you understand better. This is the output of the command "tree" performed on a directory structure like that of your example:
4 directories, 0 files
As you can see, "a" contains b/b/c/d, "b" (the first one) contains b/c/d and so on.
Are you trying to write a generic script to automate all this, by chance?
No sir, I am studying our last class lecture and am trying to get familiarized with some of the commands. You have cleared up my 'empty' misconception. I think I understand this now--if my 1st b in a/b/b/c/d contains no files, just the subdirectories which may contain files, if I just mv -f a/b/* a that should do it right? It will force the move and overwrite the first b?
Ah ok, now I understand what you were asking (or maybe, I hope so!)
If you try that, you'll get that you can't move the directory because it's not empty.
The real issue is that you're trying to move a "b" folder from inside of another "b" folder to replace it, but those two have the same name and mv is not happy with that.
Simply rename the parent "b" folder as "b1" and then you'll see that mv won't complain anymore. You'll still need to get rid of the now empty "b1" folder, though.
At the lowest level, all the files on your system actually exist only as simple blocks of binary data on your hard disk, and are tracked by the system by inode numbers. The file tree is just an overlay that associates inodes with human-readable names, and organizes them into a tree-like hierarchical structure.
"Filenames" are more accurately referred to as hardlinks. An inode can have any number of hardlinks associated with it. That is, a single file can have multiple names in multiple directories, as long as they are all on the same filesystem. (These are different from symlinks, BTW, which are a special type of file that redirects access calls to the location of another file.)
"Deleting" a file actually means removing a hardlink association (and removing the inode entry itself, when the last hardlink is removed). "Renaming" a file means altering a hardlink association to an inode. "Moving" is the same as renaming, the only difference being that it also involves changing the path part of the symlink as well, that is the "location" on the directory tree. That's why there's no separate "rename" command.
Then try thinking of directories not so much as containers and more as lists. The filesystem tracks all hardlinks that have the same path prefix together in a special kind of file, which is also given an inode/hardlink of its own. e.g. the hardlink /home/david/documents/myfile is tracked in a list called /home/david/documents, which itself exists as an entry inside the /home/david list, which is an entry inside the /home directory list, etc.