Free Hosting Online for WorkStations

< Previous | Contents | Next >

Shortly, we will discuss a couple of command-line tools for looking at plain text files. These tools will not work when used on the wrong type of files. In the worst case, they will crash your terminal and/or make a lot of beeping noises. If this happens to you, just close the terminal session and start a new one. But try to avoid it, because it is usually very disturbing for other people.


3.3.2. Creating and deleting files and directories


3.3.2.1. Making a mess...


... Is not a difficult thing to do. Today almost every system is networked, so naturally files get copied from one machine to another. And especially when working in a graphical environment, creating new files is a piece of cake and is often done without the approval of the user. To illustrate the problem, here's the full content of a new user's directory, created on a standard RedHat system:


[newuser@blob user]$ ls -al

total 32

drwx------ 3 user user 4096 Jan 16 13:32 .

drwxr-xr-x 6 root root 4096 Jan 16 13:32 ..


-rw-r--r--

1

user

user

24

Jan

16

13:32

.bash_logout

-rw-r--r--

1

user

user

191

Jan

16

13:32

.bash_profile

-rw-r--r--

1

user

user

124

Jan

16

13:32

.bashrc

drwxr-xr-x

3

user

user

4096

Jan

16

13:32

.kde

-rw-r--r--

1

user

user

3511

Jan

16

13:32

.screenrc

-rw-------

1

user

user

61

Jan

16

13:32

.xauthDqztLr

On first sight, the content of a "used" home directory doesn't look that bad either:



mp3/

OpenOffice.org638/

mp3/


Nautilus/ staroffice6.0/ nqc/

desktoptest/ Machines@ ns_imap/ Documents/

Emacs@

Nautilus/ staroffice6.0/ nqc/

desktoptest/ Machines@ ns_imap/ Documents/

Emacs@

olduser:~> ls

app-defaults/ crossover/ articles/

bin/ brol/ C/

closed/

olduser:~> ls

app-defaults/ crossover/ articles/

bin/ brol/ C/

closed/

Desktop/

Desktop1/

Desktop/

Desktop1/

Fvwm@ GNUstep/

images/

Fvwm@ GNUstep/

images/

mail/

Mail/

mail/

Mail/

nsmail/

nsmail/

training/

webstart/ xml/

training/

webstart/ xml/

office52/ Xrootenv.0

office52/ Xrootenv.0

But when all the directories and files starting with a dot are included, there are 185 items in this directory. This is because most applications have their own directories and/or files, containing user-specific settings, in the home directory of that user. Usually these files are created the first time you start an application. In some cases you will be notified when a non-existent directory needs to be created, but most of the time everything is done automatically.


Furthermore, new files are created seemingly continuously because users want to save files, keep different versions of their work, use Internet applications, and download files and attachments to their local machine. It doesn't stop. It is clear that one definitely needs a scheme to keep an overview on things.


In the next section, we will discuss our means of keeping order. We only discuss text tools available to the shell, since the graphical tools are very intuitive and have the same look and feel as the well known

point-and-click MS Windows-style file managers, including graphical help functions and other features you expect from this kind of applications. The following list is an overview of the most popular file managers for GNU/Linux. Most file managers can be started from the menu of your desktop manager, or by clicking your home directory icon, or from the command line, issuing these commands:


nautilus: The default file manager in Gnome, the GNU desktop. Excellent documentation about working with this tool can be found at http://www.gnome.org.

konqueror: The file manager typically used on a KDE desktop. The handbook is at http://docs.kde.org.

mc: Midnight Commander, the Unix file manager after the fashion of Norton Commander. All documentation available from http://gnu.org/directory/ or a mirror, such as http://www.ibiblio.org.


These applications are certainly worth giving a try and usually impress newcomers to Linux, if only because there is such a wide variety: these are only the most popular tools for managing directories and files, and many other projects are being developed. Now let's find out about the internals and see how these graphical tools use common UNIX commands.


3.3.2.2. The tools


3.3.2.2.1. Creating directories


A way of keeping things in place is to give certain files specific default locations by creating directories and subdirectories (or folders and sub-folders if you wish). This is done with the mkdir command:


richard:~> mkdir archive


richard:~> ls -ld archive

drwxrwxrwx 2 richard richard

4096 Jan 13 14:09 archive/

richard:~> mkdir archive


richard:~> ls -ld archive

drwxrwxrwx 2 richard richard

Creating directories and subdirectories in one step is done using the -p option:


richard:~> cd archive


richard:~/archive> mkdir 1999 2000 2001

richard:~> cd archive


richard:~/archive> mkdir 1999 2000 2001


richard:~/archive> ls

1999/ 2000/ 2001/


richard:~/archive> mkdir 2001/reports/Restaurants-Michelin/

mkdir: cannot create directory `2001/reports/Restaurants-Michelin/':

No such file or directory


richard:~/archive> mkdir -p 2001/reports/Restaurants-Michelin/


richard:~/archive> ls 2001/reports/

Restaurants-Michelin/

richard:~/archive> ls

1999/ 2000/ 2001/


richard:~/archive> mkdir 2001/reports/Restaurants-Michelin/

mkdir: cannot create directory `2001/reports/Restaurants-Michelin/':

No such file or directory


richard:~/archive> mkdir -p 2001/reports/Restaurants-Michelin/


richard:~/archive> ls 2001/reports/

Restaurants-Michelin/

If the new file needs other permissions than the default file creation permissions, the new access rights can be set in one move, still using the mkdir command, see the Info pages for more. We are going to discuss access modes in the next section on file security.


The name of a directory has to comply with the same rules as those applied on regular file names. One of the most important restrictions is that you can't have two files with the same name in one directory (but keep in mind that Linux is, like UNIX, a case sensitive operating system). There are virtually no limits on the length of a file name, but it is usually kept shorter than 80 characters, so it can fit on one line of a terminal. You can use any character you want in a file name, although it is advised to exclude characters that have a special meaning to the shell. When in doubt, check with Appendix C.


3.3.2.2.2. Moving files


Now that we have properly structured our home directory, it is time to clean up unclassified files using the mv

command:


richard:~/archive> mv ../report[1-4].doc reports/Restaurants-Michelin/

This command is also applicable when renaming files:


richard:~> ls To_Do

-rw-rw-r-- 1 richard richard

2534 Jan 15 12:39 To_Do

richard:~> ls To_Do

-rw-rw-r-- 1 richard richard


richard:~> mv To_Do done


richard:~> ls -l done

-rw-rw-r-- 1 richard richard

2534 Jan 15 12:39 done

richard:~> mv To_Do done


richard:~> ls -l done

-rw-rw-r-- 1 richard richard

It is clear that only the name of the file changes. All other properties remain the same.


Detailed information about the syntax and features of the mv command can be found in the man or Info pages. The use of this documentation should always be your first reflex when confronted with a problem. The answer to your problem is likely to be in the system documentation. Even experienced users read man pages every day, so beginning users should read them all the time. After a while, you will get to know the most common options to the common commands, but you will still need the documentation as a primary source of information. Note that the information contained in the HOWTOs, FAQs, man pages and other sources is slowly being merged into the Info pages, which are today the most up-to-date source of online (as in readily available on the system) documentation.


3.3.2.2.3. Copying files


Copying files and directories is done with the cp command. A useful option is recursive copy (copy all underlying files and subdirectories), using the -R option to cp. The general syntax is


cp [-R] fromfile tofile


As an example the case of user newguy, who wants the same Gnome desktop settings user oldguy has. One way to solve the problem is to copy the settings of oldguy to the home directory of newguy:


victor:~> cp -R ../oldguy/.gnome/ . This gives some errors involving file permissions, but all the errors have to do with private files that newguy doesn't need anyway. We will discuss in the next part how to change these permissions in case they really are

a problem.


3.3.2.2.4. Removing files


Use the rm command to remove single files, rmdir to remove empty directories. (Use ls -a to check whether a directory is empty or not). The rm command also has options for removing non-empty directories with all their subdirectories, read the Info pages for these rather dangerous options.


How empty can a directory be?

It is normal that the directories . (dot) and .. (dot-dot) can't be removed, since they are also necessary in an empty directory to determine the directories ranking in the file system hierarchy.

On Linux, just like on UNIX, there is no garbage can - at least not for the shell, although there are plenty of solutions for graphical use. So once removed, a file is really gone, and there is generally no way to get it back unless you have backups, or you are really fast and have a real good system administrator. To protect the beginning user from this malice, the interactive behavior of the rm, cp and mv commands can be activated using the -i option. In that case the system won't immediately act upon request. Instead it will ask for confirmation, so it takes an additional click on the Enter key to inflict the damage:


mary:~> rm -ri archive/

rm: descend into directory `archive'? y

rm: descend into directory `archive/reports'? y

rm: remove directory `archive/reports'? y

rm: descend into directory `archive/backup'? y rm: remove `archive/backup/sysbup200112.tar'? y rm: remove directory `archive/backup'? y

rm: remove directory `archive'? y

mary:~> rm -ri archive/

rm: descend into directory `archive'? y

rm: descend into directory `archive/reports'? y

rm: remove directory `archive/reports'? y

rm: descend into directory `archive/backup'? y rm: remove `archive/backup/sysbup200112.tar'? y rm: remove directory `archive/backup'? y

Top OS Cloud Computing at OnWorks: