Article

Fire Up your own Linux Server

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Next

Copying and Moving Files with the cp and mv Commands

The copy command—cp—creates a copy of a file.

[kermit@swinetrek ~]$ cp Hello\ World.txt Copy.txt        
[kermit@swinetrek ~]$ ls        
Desktop  Hello World.txt  Copy.txt

Here, we've created an exact copy of Hello World.txt called Copy.txt. We can create a copy of a file in a different directory like so:

[kermit@swinetrek ~]$ cp Hello\ World.txt \        
> /var/backup/Hello\ World.txt        
[kermit@swinetrek ~]$

Splitting Long Commands
Because some commands are too long for the page width of this book, I have split them into multiple lines. If you ever want to do this on the command line, you need to end each line with a space followed by a backslash (\). When you press Enter when a line that ends this way, you'll get a > prompt to enter the rest of the command. Since you're not writing a book, you can just type these long commands as one long line (the terminal will wrap the command to the next line automatically as you type it).

You can use cp to copy an entire directory hierarchy. The following command will create a /var/backup/kermit directory, which will contain a copy of all of kermit's files.

[kermit@swinetrek ~]$ cp -r /home/kermit/ /var/backup/        
[kermit@swinetrek ~]$

mv (which stands for move) moves a file from one location in the filesystem to another.

[kermit@swinetrek ~]$ mv Hello\ World.txt Moved.txt        
[kermit@swinetrek ~]$

In the above example, the file Hello World.txt is being moved to a new file named Moved.txt in the same directory; effectively, we're just renaming the file. Unlike the cp command, when the mv command is used, the original file is not retained in its original location. To move the file to another directory, the command could be executed like so:

[kermit@swinetrek ~]$ mv Hello\ World.txt /var/backup/        
[kermit@swinetrek ~]$

Switching Users with the su and exit Commands

We've already seen that su, used by itself, logs into the root account, and prompts you for the root user password:

[kermit@swinetrek ~]$ su        
Password:        
[root@swinetrek kermit]#

What the $#?
Note the changes to the prompt when you switch over to root: the character at the end of the prompt changes from a $ to a # to indicate that you're logged in as root. If you were in your home directory, the current directory in the prompt would change from ~ to your old username. This indicates that you're no longer your normal user in your home directory: you're root in another user's home directory. root's home directory is /root.

To switch back to your original user, use the exit command:

[root@swinetrek kermit]# exit        
exit        
[kermit@swinetrek ~]$

You can use su to switch to any other user, as long as you know that user's password.

[kermit@swinetrek ~]$ su gonzo        
Password:        
[gonzo@swinetrek kermit]$

Changing File Permissions with the chmod Command

We looked at permissions, and saw how to change them through the GUI, in Chapter 2, Day-to-day Usage. You can also change these permissions from the command line using the chmod (change mode) tool.

[kermit@swinetrek ~]$ chmod o+w Hello\ World.txt        
[kermit@swinetrek ~]$ ll        
total 16        
drwxr-xr-x  2 kermit kermit 4096 Sep  5 14:21 Desktop        
-rw-rw-rw-  1 kermit kermit   13 Sep  8 07:30 Hello World.txt

Here, we've granted all other users write access to Hello World.txt. We told chmod to do this by passing o+w to it. Let's take a look at what that means:

  • The o character tells chmod to deal with the set of permissions for other users. This could be replaced with u for the user set, g for the group set, or a for all three sets.
  • The + character tells chmod to grant permission. It can be changed to -, which will tell chmod to revoke permission.
  • The w character tells chmod to deal with write permission. This can be changed to r for read permission, or x for execute permissions.

You can also combine these letters. For example, you can use ug-w to revoke write permissions for the user and group.

[kermit@swinetrek ~]$ chmod ug-w Hello\ World.txt        
[kermit@swinetrek ~]$

Deleting Files with the rm Command

As we've already seen, the rm command removes a file. In the example below, rm would remove the file MyCopy.txt.

[kermit@swinetrek ~]$ rm MyCopy.txt        
[kermit@swinetrek ~]$

In order to remove a directory and everything inside it, we must add the "recursive" (-r) option to the command, like so:

[kermit@swinetrek ~]$ rm -r untitled\ folder        
[kermit@swinetrek ~]$

Ordinarily, you must have write permissions to a file in order to delete it; however, if you have write permissions on the directory containing the file, you can delete files if you confirm the action. If you attempt to delete a file to which you only have read permissions, you'll be asked if you really want to delete that file:

[kermit@swinetrek ~]$ rm Read\ Only.txt        
rm: Remove write-protected regular file 'Read Only.txt'? y        
[kermit@swinetrek ~]$

To force a deletion without further confirmation, add the -f option to the command. This can be combined with the -r option to delete a write-protected directory, too.

[kermit@swinetrek ~]$ rm -rf Read\ Only\ Folder        
[kermit@swinetrek ~]$

Don't Force It!
Use the -f option with the rm command very carefully. When logged in as root, deleting directories or files without confirmation can result in serious damage to your installation. Confirming the deletion provides a safeguard against these accidental deletions.

Getting Help
Possibly the most useful of all command line tools is man, the online manual. If you ever need to find out what a command does or what options are available, just enter man commandname at the command prompt.

When reading the manual, the up and down arrow keys scroll through the text, the space bar scrolls through a page at a time, and Q quits the manual and returns you to the command prompt.

Introducing the Shell

In the same way that we can interact with Linux graphically through a desktop environment such as GNOME or KDE, we interact with the Linux command line through a shell. We've been using the shell throughout this chapter; let's now take a look at some of its more advanced features.

The default shell in Fedora Core (and, in fact, in most modern Linux systems) is bash, or the Bourne Again Shell. bash is a modern rewrite of the original Bourne shell, sh, which was written in 1977. While other shell environments exist, such as tcsh, csh and ksh, bash has become increasingly popular due to its useful featureset. We'll look in detail at some of those features here.

Tab Completion

One of the most useful features of many modern shells is tab completion. When a partial command or filename is entered on the command line, and the user presses the Tab key, the command or filename will be completed for you. For example, typing cat Hello, followed by Tab, will result in cat Hello\ World.txt being completed at the command line for you.

However, typing cd /etc/ht, followed by Tab, doesn't immediately return anything. That's because two directories begin with /etc/ht: /etc/httpd and /etc/htdig. Pressing the Tab key a second time will list the possible options:

[kermit@swinetrek ~]$ cd /etc/ht        
httpd/  htdig/        
[kermit@swinetrek ~]$ cd /etc/ht

We can use this list to continue typing until we've typed enough characters to identify a single directory; we can then press Tab again to have the filename completed.

Command History

The bash shell records a limited history of recently issued commands—up to 1000 by default. You can use the up arrow key to scroll back through the command history, and the down arrow key to scroll forward. When the desired command is found, you can execute it by pressing Enter.

To see the full history, simply enter the history command. All your recent commands will be displayed in chronological order, with the oldest at the top.

[kermit@swinetrek ~]$ history        
   1  pwd        
   2  ls        
   3  ls /        
   4  ls -l        
   5  ll -a        
   6  cd /etc/httpd/        
   7  cd        
   8  cd ..        
   9  echo "Hello, World\!"        
  10  cat Hello\ World.txt        
  11  cp Hello\ World.txt Copy.txt        
  12  cp Hello\ World.txt /var/backup/Hello\ World.txt        
  13  cp -r /home/kermit/ /var/backup/        
  14  mv Hello\ World.txt Moved.txt        
  15  mv Moved.txt /var/backup/        
  16  su        
  17  su gonzo        
  18  chmod o+w Hello\ World.txt        
  19  ll        
  20  chmod ug-w Hello\ World.txt        
  21  rm Copy.txt        
  22  rm -r untitled\ folder        
  23  rm Read\ Only.txt        
  24  rm -rf Read\ Only\ Folder

This history is also searchable. Hitting Ctrl-R will change the prompt to (reverse-i-search)`':; now, try typing part of one of the commands in your history. The shell will find within the history the most recent command that contains the entered string, and display it. Just hit Enter to execute it, or Esc to return to the normal prompt. In the example below, I've entered Hello and found the last command that involved the file Hello World.txt.

(reverse-i-search)`Hello': chmod ug-w Hello\ World.txt

Some built-in history shortcuts can further maximize the efficiency with which you utilize the shell history. The !! command will execute the last command in the history file. For example, if the last command entered was ll, !! would re-run that command.

[kermit@swinetrek ~]$ !!        
ll        
total 16        
drwxr-xr-x  2 kermit kermit 4096 Sep  5 14:21 Desktop        
-rw-rw-r--  1 kermit kermit   13 Sep  8 07:30 Hello World.txt

!partial-command will execute the last command beginning with partial-command. For example, !ech would re-run the last echo command.

[kermit@swinetrek ~]$ !ech        
echo "Hello, World\!"        
Hello, World!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links