Managing Your Own Files and Directories

Managing files : Commands covered in this section:   mkdir, rmdir, cp, mv, rm,chmod 

Let's begin with directories. You create directories with mkdir and remove them with rmdir. 

Exercise 1.1 

Create two new directories with the mkdir command (use the ls command to confirm the new directories were created). 
          % mkdir test test2 

Now remove the second directory you just created (and list the files in the current directory again to see if the second directory was removed). 
          % rmdir test2 

rmdir will only remove empty directories. If a directory contains files, you must remove them first before using rmdir. Alternatively, you can recursively delete the directory and its contents using the appropriate option with the rm command as discussed later in this section. 

Let's move on to working with files. First, you'll need to copy some existing files from elsewhere in the filesystem into your test directory. You use the cp command to do this. 

Exercise 1.2 

Before going on, move into the new "test" directory you just created. You will use this area to experiment (thus minimizing any potential damage to other files in your home directory!). 
          % cd test
          % pwd
          /home/natasha/test 

Now you can copy some files into your test directory with cp. 
          % cp /etc/motd .
          % cp /etc/hosts .
          % cp /etc/group .
          % ls
          group   hosts   motd 

The three cp commands above copy existing files from the "/etc" directory into the current directory (repesented by "."). 

Now remove (delete) a file using the rm command. 

% rm group
          % ls
          hosts   motd 

Finally, move (rename) one of the files using mv. 
          % mv motd message-of-the-day
          % ls
          hosts   message-of-the-day 

Since cp, mv, and rm have the potential to destroy data, they offer an interactive option which prompts you before proceeding. This is invoked with the "-i" option. Your system administrator may have already configured your account to use this option by default. 

Both cp and mv require two arguments: the existing location and the destination of the file to be copied or moved. The destination can be another file or a directory. Let's compare the results in each case. 

Exercise 1.3 

First, let's make a new directory within the test directory. 
          % mkdir junk
          % ls
          hosts     junk/     message-of-the-day 

Then try the following command: 
          % cp hosts hosts2
          % ls
          hosts           junk/
          hosts2          message-of-the-day 

The result in this case is a second copy of the file (called "hosts2") in the current directory. Now try the following: 
          % cp hosts junk
          % ls
          hosts           junk/
          hosts2          message-of-the-day
          % ls junk
          hosts 

This time, the file was copied to the "junk" directory, keeping the same name as the original ("hosts"). 

The shell allows the use of wildcards to reference more than one file at a time. The "?" character stands for a single character with any value and the "*" character stands for any number of characters with any value. For example, "host*" would match "hosts" and "hosts2" in the above example. 

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.