Linux Bible. Christopher Negus
a regular user, you cannot change ownership of files or directories to have them belong to another user. You can change ownership as the root user. For example, suppose that you created a file called
memo.txt
in the user joe
's home directory while you were root user. Here's how you could change it to be owned by joe
:
# chown joe /home/joe/memo.txt # ls -l /home/joe/memo.txt -rw-r--r--. 1 joe root 0 Dec 19 11:23 /home/joe/memo.txt
Notice that the chown
command changed the user to joe
but left the group as root
. To change both user and group to joe
, you could enter the following instead:
# chown joe:joe /home/joe/memo.txt # ls -l /home/joe/memo.txt -rw-r--r--. 1 joe joe 0 Dec 19 11:23 /home/joe/memo.txt
The chown
command can be use recursively as well. Using the recursive option (-R
) is helpful if you need to change a whole directory structure to ownership by a particular user. For example, if you inserted a USB drive, which is mounted on the /media/myusb
directory, and you wanted to give full ownership of the contents of that drive to the user joe
, you could enter the following:
# chown -R joe:joe /media/myusb
Moving, Copying, and Removing Files
Commands for moving, copying, and deleting files are fairly straightforward. To change the location of a file, use the mv
command. To copy a file from one location to another, use the cp
command. To remove a file, use the rm
command. These commands can be used to act on individual files and directories or recursively to act on many files and directories at once. Here are some examples:
$ mv abc def $ mv abc ~ $ mv /home/joe/mymemos/ /home/joe/Documents/
The first mv
command moves the file abc
to the file def
in the same directory (essentially renaming it), whereas the second mv
command moves the file abc
to your home directory (~
). The next mv
command moves the mymemos
directory (and all its contents) to the /home/joe/Documents
directory.
By default, the mv
command overwrites any existing files if the file to which you are moving exists. However, many Linux systems alias the mv
command so that it uses the -i
option (which causes mv
to prompt you before overwriting existing files). Here's how to check if that is true on your system:
$ alias mv alias mv='mv -i'
Here are some examples of using the cp
command to copy files from one location to another:
$ cp abc def $ cp abc ~ $ cp -r /usr/share/doc/bash-completion* /tmp/a/ $ cp -ra /usr/share/doc/bash-completion* /tmp/b/
The first copy command (cp
) copies abc
to the new name def
in the same directory, whereas the second copies abc
to your home directory (~
), keeping the name abc
. The two recursive (-r
) copies copy the bash-completion
directory and all of the files it contains, first to new /tmp/a/
and /tmp/b/
directories. If you run ls -l
on those two directories, you see that for the cp
command run with the archive (-a
) option, the date/time stamps and permissions are maintained by the copy. Without the -a
, current date/time stamps are used, and permissions are determined by your umask.
The cp
command typically also is aliased with the -i
option in order to prevent you from inadvertently overwriting files.
As with the cp
and mv
commands, rm
is also usually aliased to include the -i
option. This can prevent the damage that can come from an inadvertent recursive remove (-r
) option. Here are some examples of the rm
command:
$ rm abc $ rm *
The first remove command deletes the abc
file; the second removes all of the files in the current directory (except that it doesn't remove directories and/or any files that start with a dot). If you want to remove a directory, you need to use the recursive (-r
) option to rm
or, for an empty directory, you can use the rmdir
command. Consider the following examples:
$ rmdir /home/joe/nothing/ $ rm -r /home/joe/bigdir/ $ rm -rf /home/joe/hugedir/
The rmdir
command in the preceding code only removes the directory (nothing
) if it is empty. The rm -r
example removes the directory bigdir
and all of its contents (files and multiple levels of subdirectories), but it prompts you before each is removed. When you add the force option (-f
), the hugedir
directory and all of its contents are immediately removed, without prompting.
CAUTION
When you override the -i
option on the mv
, cp
, and rm
commands, you risk removing some (or lots) of files by mistake. Using wildcards (such as *
) and no -i
makes mistakes even more likely. That said, sometimes you don't want to be bothered to step through each file you delete. You have other options as follows:
As noted with the -f option, you can force rm to delete without prompting. An alternative is to run rm, cp, or mv with a backslash in front of it (\rm bigdir). The backslash causes any command to run unaliased.
Another alternative with mv is to use the -b option. With -b, if a file of the same name exists at the destination, a backup copy of the old file is made before the new file is moved there.
Summary
Commands for moving around the filesystem, copying files, moving files, and removing files are among the most basic commands that you need to work from the shell. This chapter covers lots of commands for moving around and manipulating files as well as commands for changing ownership and permission.
The next chapter describes commands for editing and searching for files. These commands include the vim
/vi
text editors, the find
command, and the grep
command.
Exercises
Use these exercises to test your knowledge of efficient ways to get around the Linux filesystem and work with files and directories. When possible, try to use shortcuts to type as little as possible to get the desired results. These tasks assume that you are running a Fedora or Red Hat Enterprise Linux system (although some tasks work on other Linux systems as well). If you are stuck, solutions to the tasks are shown in Appendix B (although in Linux, there are often multiple ways to complete a task).
1 Create a directory in your home directory called projects. In the projects directory, create nine empty files that are named house1, house2, house3, and so on up to house9. Assuming that there are lots of other files in that directory, come up with a single argument to ls that would list just those nine files.
2 Make the $HOME/projects/houses/doors/ directory path. Create the following empty files within this directory path (try using absolute and relative paths from your home directory): $HOME/projects/houses/bungalow.txt $HOME/projects/houses/doors/bifold.txt $HOME/projects/outdoors/vegetation/landscape.txt
3 Copy the files house1 and house5 to the $HOME/projects/houses/ directory.
4 Recursively copy the /usr/share/doc/initscripts* directory to the $HOME/projects/ directory. Maintain the current date/time stamps and permissions.
5 Recursively