LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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!

Notices


Reply
  Search this Thread
Old 09-10-2012, 06:08 PM   #1
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Question Removing a directory and moving up subdirectories


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?
 
Old 09-10-2012, 06:13 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Hi and welcome to LQ.

Yes, if you delete a directory, you'll delete its contents (ie. subdirectories) as well. You are right in saying that you'd have to move/copy the subdirectories first.

Some bedtime reading:
http://rute.2038bug.com/index.html.gz
 
Old 09-11-2012, 07:32 AM   #3
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
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 ?

Thanks again!!
 
Old 09-11-2012, 07:53 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836
It would be /a/c/d
 
Old 09-11-2012, 07:55 AM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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:
Code:
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)
 
1 members found this post helpful.
Old 09-11-2012, 08:07 AM   #6
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Smile

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 08:12 AM.
 
Old 09-11-2012, 08:21 AM   #7
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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.

Last edited by 414N; 09-11-2012 at 08:22 AM.
 
Old 09-11-2012, 08:30 AM   #8
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-11-2012, 08:48 AM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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:
Code:
a
└── b
    └── b
        └── c
            └── d

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?
 
Old 09-11-2012, 09:00 AM   #10
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-11-2012, 09:11 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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.
 
1 members found this post helpful.
Old 09-11-2012, 09:13 AM   #12
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Talking

Yes!! That makes perfect sense! Thank you sir..you have made my morning!
 
Old 09-11-2012, 09:15 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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.

I hope that helps make things clearer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] directory and subdirectories congos Linux - Newbie 2 01-19-2012 08:52 AM
Copying a Directory into Another Directory with Subdirectories oadvantage Linux - Newbie 4 03-06-2006 05:27 PM
Moving subdirectories to another partition or it's own? b18b Linux - Hardware 2 02-22-2006 07:27 PM
Shell question: moving files into subdirectories by dates timothee Linux - Newbie 5 12-20-2005 10:14 AM
Script For Moving Files In Subdirectories darkmage Linux - General 3 12-17-2002 06:34 PM


All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration