Find the answer to your Linux question:
Results 1 to 6 of 6
Hi I'm learning to program in linux, and i can't seem to work out how to remove all the file with a directory without leaving the home directory. Also how ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    3

    Unhappy Removing all files in a dir, while in home dir - HELP!

    Hi I'm learning to program in linux, and i can't seem to work out how to remove all the file with a directory without leaving the home directory.

    Also how to copy on directory to another from the home directory.

    Can any help please? Much appreciated.

    Thanks.
    Last edited by patel_ankz; 11-11-2008 at 11:34 AM. Reason: mistake

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Are you looking for the rm and cp commands? To remove a directory (and all its contents) from anywhere on the computer, just use:
    Code:
    rm -rf /absolute/path/to/directory
    To copy a directory from one place to somewhere else, you'd would probably want:
    Code:
    cp -r /source/directory /new/directory
    These are system commands, but you were unclear about how you're programming in linux. Are you doing bash scripting? C? Perl? All of them could use different syntax, but you can almost always implement these system calls in any programming language if necessary.
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    3
    Yeah I'm doing C programming using csh, but I was just touching up on the basics. Erm the way you showed me, that removes the directory itself right? I want to delete everything within a directory from the home directory.

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Code:
    rm -rf /path/to/directory/*
    removes all files and subdirectories underneath that folder

  5. #5
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    weird that it double posted, it told me i had to wait between posts after pressing post, then I waited and pressed post, now its a double post ^_^

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by patel_ankz View Post
    Yeah I'm doing C programming using csh, but I was just touching up on the basics. Erm the way you showed me, that removes the directory itself right? I want to delete everything within a directory from the home directory.
    I am not too sure what are you doing. csh is a shell, and it's not the C programming language. Sorry if that's obvious, just want to make sure we are talking about the same thing.

    Your problem is that maybe you don't understand the different between a relative path and an absolute path. To remove anything no matter where you are, and that includes your home dir, you can always use an absolute path. An absolute path is a path starting with a slash /, the rest are all relative paths.

    If you use an absolute path your current location is completely irrelevant.

    Then you also need to learn the difference between files and dirs. By default, rm only delete files, so, if you want to remove all the files in /a/given/dir, you need to do

    Code:
    rm /a/given/dir/*
    You probably want to specify -f so you don't have to confirm every file.

    rm can also delete dirs if you use -r, that would indeed delete the directory itself as you say. But by default rm can't delete directories.

    In opposition to the absolute paths, there are relative ones. A relative path is specified relative to a given location. For example, if you are in /home/i92guboj, we could re-write /a/given/dir/ as "../../a/given/dir/".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...