Working with Files


So let's start actually using the shell. If you've ever used DOS, then you have a head start over most shell beginners, although you'll still need to learn some new commands. Table 14-1 shows various DOS commands alongside their SUSE Linux equivalents. This table also serves as a handy guide to some BASH commands, even if you've never used DOS. In Appendix B, you'll find a comprehensive list of useful shell commands, together with explanations of what they do and examples of typical usage.

Table 14-1. DOS Commands and Their Shell Equivalents

Command

DOS Command

Linux Shell Command

Usage

Copy files

COPY

cp

cp <filename> <new location>

Move files

MOVE

mv

mv <filename> <new location>

Rename files

RENAME mv

mv <old filename> <new filename>[1]

 

Delete files

DEL

rm

rm <filename> [2]

Create directories

MKDIR

mkdir

mkdir <directory name>

Delete directories

DELTREE/RMDIR

rm

rm –rf <directory name>

Change directory

CD

cd

cd <directory name>

View directories graphically

TREE

tree

tree

Edit text files

EDIT

vi

vi <filename>

View text files

TYPE

less

less <filename> [3]

Print text files

PRINT

lpr

lpr <filename>

Compare files

FC

diff

diff <file1> <file2>

Find files

FIND

find

find –name <name of file>

Check disk integrity

SCANDISK

fsck

fsck [4]

View network settings

IPCONFIG

ifconfig

ifconfig[5]

Check a network connection

PING

ping

ping <address>

View a network route

TRACERT

traceroute

traceroute <address>[5]

Clear screen

CLS

clear

clear

Get help

HELP

man

man <command>[6]

Quit

EXIT

exit

exit

[1] The SUSE Linux shell offers a rename command, but this is chiefly used to rename many files at once.

[2]To avoid being asked to confirm each file deletion, you can add the –f option. Be aware that the rm command deletes data instantly, without the safety net of the Recycle Bin, as with the KDE desktop.

[3]Use the cursor keys to move up and down in the document. Press Q to quit.

[4]This is a system command and can be run only on a disk that isn't currently in use. To scan the main partition, you'll need to use a rescue floppy (see Chapter 13).

[5]This is a system command that can be run only by users with root privileges.

[6]The info command can also be used.

image from book
Creating Aliases

If you've ever used DOS, you might find yourself inadvertently typing DOS commands at the shell prompt. Some of these will actually work, because most distribution companies create command aliases to ease the transition of newcomers to Linux.

Aliases mean that whenever you type certain words, they will be interpreted as meaning something else. However, an alias won't work with any of the command-line switches used in DOS. In the long-term, you should try to learn the BASH equivalents.

You can create your own command aliases quickly and simply. Just start a BASH shell and type the following:

alias <DOS command>='<Linux shell command>'

For example, to create an alias that lets you type copy instead of cp, type this:

alias copy='cp'

Note that the SUSE Linux command must appear in single quotation marks.

image from book

Listing Files

Possibly the most fundamentally useful BASH command is ls. This will list the files in the current directory, as shown (with a few other typical commands), in Figure 14-3. If there are a lot of files, they might scroll off the screen. If you're running Konsole, you can use the scroll bar on the right side of the window to view the list.

image from book
Figure 14-3. The ls command lists the files in the current directory.

Having the files scroll off the screen can be annoying, so you can cram as many as possible onto each line by typing the following:

ls -m

The dash after the command indicates that you're using a command option. These are also referred to as command-line flags or switches. Nearly all shell commands have options like this. In fact, some commands won't do anything unless you specify various options. In the case of the ls command, only one dash is necessary, but some commands need two dashes to indicate an option.

You can see a list of all the command options for ls by typing the following (ironically, itself a command option):

ls --help

Once again, the output will scroll off the screen, and you can use the window's scroll bars to examine it. (In Chapter 18, you'll learn a trick you can use to be able to read this output without needing to fiddle around with the scroll bars, even if there's screen after screen of it.)

Other interesting command options for the ls command include -l, which produces "long" output. This lists file sizes and ownership permissions, among other details (permissions are covered in the next chapter).

With most commands, you can use many command options at once, as long as they don't contradict each other. For example, you could type the following:

ls –lh

This tells the ls command to produce "long" output (-l) and also to produce "human-readable" output. The human-readable option (-h) means that rather than listing files in terms of bytes (such as 1029725 bytes), it will list them in kilobytes and megabytes. In other words, you can simply list the options after the dash; you don't need to give each option its own dash.

Caution 

I've said it before, and I'll say it again: don't forget that case sensitivity is vitally important in SUSE Linux! Typing ls –L is not the same as typing ls –l. It will produce different results.

Copying Files

So what other useful commands are there for dealing with files? Well, you can copy files with cp. You can use the cp command in the following way:

cp myfile /home/knthomas/

This will copy the file to the location specified.

One important command-line option for cp is –r. This stands for recursive and tells BASH that you want to copy a directory and its contents (as well as any directories within this directory). Most commands that deal with files have a recursive option.

Note 

Only a handful of BASH commands default to recursive copying. Even though it's extremely common to copy folders, you still need to specify the -r command option most of the time.

One curious trick is that you can copy a file from one place to another but, by specifying a filename in the destination part of the command, change its name. Here's an example:

cp myfile /home/knthomas/myfile2

This will copy myfile to /home/knthomas, but rename it as myfile2. Be careful not to add a final slash to the command when you do this. In the example here, doing so would cause BASH to think that myfile2 is a directory.

This way of copying files is a handy way of duplicating files. By not specifying a new location in the destination part of the command, but still specifying a different filename, you effectively duplicate the file within the same directory:

cp myfile myfile2

This will result in two identical files: one called myfile and one called myfile2.

Moving Files

The mv command can be used in a similar way to cp, except that rather than copying the file, the old one is removed. You can move files from one directory to another, for example, like this:

mv myfile /home/knthomas/

You can also use the mv command to quickly rename files, as shown in Figure 14-4:

image from book
Figure 14-4. You can also use the mv command to rename files.

mv myfile myfile2
Note 

Getting technical for a moment, moving a file in Linux isn't the same as in Windows, where a file is copied and then the original deleted. Under SUSE Linux, the file's absolute path is rewritten, causing it to simply appear in a different place in the file structure. However, the end result is the same.

Deleting Files

But how do you get rid of files? Again, this is relatively easy, but first a word of caution: the shell doesn't operate any kind of Recycle Bin. Once a file is deleted, it's gone forever. (There are utilities you can use to recover files, but these are specialized tools and aren't to be relied on for day-to-day use.)

Removing a file is achieved by typing something like this:

rm myfile

It's as simple as that.

You'll be asked to confirm the deletion after you issue the command. If you want to delete a file without being asked to confirm it, type the following:

rm –f myfile

The f stands for force (that is, force the deletion).

If you try to use the rm command to remove a directory, you'll see an error message. This is because the command needs an additional option:

rm –rf mydirectory

As noted in the previous section, the -r stands for recursive and indicates that any folder specified afterwards should be deleted, in addition to any files it contains.

Tip 

You might have used wildcards within Windows and DOS. They can be used within SUSE Linux, too. For example, the asterisk (*) can be used to mean any file. So, you can type rm –f * to delete all files within a directory, or type rm –f myfile* to delete all files that start with the word myfile.

Changing and Creating Directories

Another handy command is cd, for change directory. This lets you move around the file system, from directory to directory. Say you're in a directory that has another directory in it, named mydirectory2. Switching to it is easy:

cd mydirectory2

But how do you get out of this directory once you're in it? Try the following command:

cd ..

The .. refers to the "parent" directory, which is the one containing the directory you're currently browsing. Using two dots to indicate this may seem odd, but it's just the way that SUSE Linux (and Unix before it) does things. It's one of the many conventions that Unix relies on and that you'll pick up as you go along.

You can create directories with the mkdir command:

mkdir mydirectory




Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net