Manipulating Files and Directories
Linux has many ways to create, move, copy, and delete files and directories. Some features are so easy to use that you need to be careful: Unlike other operating systems, Linux doesn’t tell you that you’re about to overwrite a file — it just follows your orders and overwrites!WarningWe have said it elsewhere in this book, and we’ll say it again: Make sure that you’re not logged in as root when you read through these sections. You can unintentionally harm your computer when you’re logged in as root. As root, or the superuser, you can erase any file or directory — regardless of which permissions are set. Be careful!
Creating directories
To create a new directory in Linux, you use the mkdir command (just like in MS-DOS). The command looks like this:
[lidia@cancun lidia]$mkdir newdirectory
This command creates a subdirectory under your current or working directory. If you want the subdirectory under another directory, change to that directory first and then create the new subdirectory.Create a new directory named cancun. Go ahead — do it:
mkdir cancun
(Can you tell where we would rather be right now?)Create another directory named vacation:
mkdir veracruz
Then, change the directory to put yourself in the cancun directory:
cd cancun
Now verify that you’re in the directory cancun:
pwd
Moving and copying files and directories
The commands for moving and copying directories and files are mv for move and cp for copy. If you want to rename a file, you can use the mv command. No, you’re not really moving the file, but in Linux (and Unix), the developers realized that renaming something was much like moving it. The format of the move command is
mv source destination
Create a file that you can practice moving. The touch command updates the time stamp on an existing file or creates an empty file if it doesn’t exist. In this case, the file test doesn’t exist and will be created by touch:
touch go
Move the new file:
mv go to
This command leaves the file in the same directory and changes its name to to. The file wasn’t really moved — just renamed.Now try moving the to file to the veracruz directory. To do that, you have to first move the file up and then move it into the veracruz directory. You can do it with one command:
mv to ../veracruz
RememberThe destination file uses the double-dot (..) designation; every directory contains a double-dot directory that points to the parent directory. This command tells Linux to go up one directory level and look for a directory named veracruz and then put the file into that directory with the name newgoto because you didn’t specify any other name. If you do this instead:
mv go ../veracruz/now
the go file moves to the veracruz directory named now. Note that in both cases (with the file maintaining its name of go or taking the new name now), your current directory is still cancun and all your filenames are relative to that directory.Technical StuffStrictly speaking, the file still hasn’t really moved. The data bits are still on the same part of the disk where they were originally. The file specification (the directory path plus the filename) you use to talk about the file is different, so it appears to have moved.
Removing files and directories
The command for removing, or deleting, a file is rm. Using rm is straightforward. Create a dummy file to erase:
touch junk
You can delete the file with this command:
rm junk
You have removed the dummy file from the current directory. To remove a file from another directory, you need to provide a relative filename or an absolute filename. For example, if you want to expunge now from the veracruz directory, you type this line:
rm ../veracruz/now
WarningYou can use metacharacters (similar in many ways to Windows wildcards) with rm, but be very careful if you do so! When files are removed in Linux, they are gone forever — kaput, vanished — and can’t be recovered.This command removes everything in the current directory and all the directories under it that you have permission to remove:
[lidia@cancun lidia]$rm -r *
WarningDo not give this command as root (the superuser)! You should always be careful when running any command as root, but be especially careful with commands that can erase entire directories and file systems.TipTo decrease the danger of removing lots of files inadvertently when you use metacharacters, be sure to use the -i option with rm, cp, mv, and various other commands. The -i option, which means interactive, lists each filename to be removed (with the rm command) or overwritten (with the mv or cp command). If you answer either y or Y to the question, the file is removed or overwritten, respectively. If you answer anything else, Linux leaves the file alone.You can remove not only files but also directories. Suppose that you have an old directory, /tmp/junk, that you don’t need any more. You can remove it and all its contents:
[lidia@cancun lidia]$rm -rf /tmp/junk
Giving the rm command these options (r and f) removes the /tmp/junk directory and all files and directories under it. The r option means to remove recursively; in recursion, the command works through every subdirectory in the parent directory. The f option issues the command forcefully. No prompts are given.