Files and Directories

This page is devoted to command line tools you can use for working with files and directories.

TODO This page is still under construction.

Listing Files

You list files using the ls command. This is such a common command — with so many potential parameters — that I've given it its own page. See the ls page.

Creating Directories

When you need to create a directory, you use the mkdir command. This is a very simple command, which simply takes the name of the directory or directories you're creating as parameters. (There are some other parameters you can use, but in most cases, you just need to supply the name of the directories you're creating.)

For example, if I download a lot of files from the internet, I might want to create a directory called “downloads” in my home directory. I could do so like this:

$ cd ~
$ mkdir downloads

I could also supply a full path; for example:

$ cd /
$ mkdir ~/downloads

or

$ cd /
$ mkdir /home/serna/downloads

Even though the root directory was the working directory, the downloads directory was created in my home directory.

You can also create more than one directory at a time, by specifying the names separated by spaces. For example, if I wanted to create the downloads directory, and then put a programs directory under that, I could do this:

$ cd ~
$ mkdir downloads downloads/programs

This would be the same as doing both directories one by one:

$ cd ~
$ mkdir downloads
$ cd downloads
$ mkdir programs

The “Parents” Option

I can't create the programs subdirectory if its parent directory doesn't exist, or I'll get an error:

$ cd ~
$ mkdir downloads/programs
mkdir: cannot create directory `downloads/programs': No such file or directory
$

But you can create both directories at once, with the -p (for “parents”) parameter. This would allow you to do things a bit quicker:

$ cd ~
$ mkdir -p downloads/programs

This is really telling mkdir to create the downloads/programs directory, along with any parent directories which are needed, which, in this case, is the downloads directory.

The only other parameter you might find useful is the -v parameter, which stands for “verbose.” This will cause mkdir to output more information about what the command is doing, as it goes along.

Deleting Files and Directories

There are two commands for deleting files and directories: typically you'll use rm, which can be used to delete files or directories, but there is also rmdir, which can be used to delete an empty directory.

Use rm with caution, because it will not send files to the Trash; the files will be completely removed from the system. If you delete a file with rm, you probably won't be able to recover it afterwards1.

In its simplest usage, you simply supply the name of the file or files you want to delete. For example, if I have a file named blah.txt, I can simply type

$ rm blah.txt

Or, if I have two files, file1.txt and file2.txt, I can use

$ rm file1.txt file2.txt

There are some other parameters you might want to use, as well. The following table lists some of the common ones.

Parameter Description
-f This is the “force” parameter; when you supply rm with a list of files to delete, some of those files might not actually exist; this parameter tells rm to ignore these errors.
-i This turns on “interactive” mode, where rm will prompt you before it takes each action.
-r or -R This activates “recursive” mode; if you delete a directory, in recursive mode, any sub-directories of that directory will also be deleted.

This is one of the few cases where a parameter can be used in either upper- or lower-case.
-v This turns on “verbose” mode, where rm will give you much more information about everything it does.

As you're deleting files, you can also use wildcards. For example, suppose I have three files in a directory, all of which have a .txt extension, and a fourth file with a .odt extension:

$ ls
file1.txt  file2.txt  file3.txt  file4.odt
$

You could delete all of the files with a .txt extension, and leave the rest of the files, like this:

$ rm *.txt
$ ls
file4.odt
$

shred

The shred command is a more extreme way to delete files, for those times when you want to make sure that a file won't be recoverable. When you use the rm command, Linux doesn't actually remove the physical 0's and 1's from the hard drive; it simply removes the file's entry from the file allocation table. This means that it's possible to recover a file, using the proper tools. With shred, however, Linux will not just remove the file, but also overwrite the appropriate sections of the hard drive with other data, to obliterate the file.

By default, Ubuntu is installed using the ext3 journaling file system, and, from what I read in the man page for shred, that means that shred may or may not work — depending on if you're using “journalled mode.” (I don't believe Ubuntu installs ext3 in “journalled mode” by default.)

So if you really need to delete a file, and remove all traces of it, and if you're using “journalled mode” (or aren't sure what mode ext3 is using), you might need to find a different tool to do the job.

There are some options you can specify, which are different from the parameters available for rm.

Parameter Description
-f “Force” option; sets file permissions to allow writing, if necessary.
-n By default, shred will overwrite the file with garbage 25 times; with this parameter, you can specify your own number. This parameter takes in an argument; for example, to overwrite the file 100 times, you would use

shred -n=100 filename
-u Removes the file. By default, shred won't actually delete the file, it will just replace it with garbage. With this option, shred will overwrite the file with garbage, and then delete it. This is usually what you want, but it's not the default.
-v “Verbose” mode; tells the command to provide a lot more information about what it's doing.
-z For the truly paranoid, this parameter tells the command to take one final step, after “shredding” the file, to overwrite it with 0's. This will hide evidence of the shredding, if someone is using low-level tools to look at the data on the hard drive.

For example, if you have a file named passwords, you could delete it in the most paranoid way possible by doing this:

shred -f -u -z passwords

Or, if you want to see what shred is doing while it does it, you can also use the -v parameter, and see output similar to the following:

$ shred -f -u -z -v passwords
shred: passwords: pass 1/26 (random)...
shred: passwords: pass 2/26 (6db6db)...
shred: passwords: pass 3/26 (cccccc)...
shred: passwords: pass 4/26 (444444)...
shred: passwords: pass 5/26 (db6db6)...
shred: passwords: pass 6/26 (924924)...
shred: passwords: pass 7/26 (aaaaaa)...
shred: passwords: pass 8/26 (dddddd)...
shred: passwords: pass 9/26 (000000)...
shred: passwords: pass 10/26 (111111)...
shred: passwords: pass 11/26 (b6db6d)...
shred: passwords: pass 12/26 (eeeeee)...
shred: passwords: pass 13/26 (random)...
shred: passwords: pass 14/26 (555555)...
shred: passwords: pass 15/26 (492492)...
shred: passwords: pass 16/26 (ffffff)...
shred: passwords: pass 17/26 (249249)...
shred: passwords: pass 18/26 (666666)...
shred: passwords: pass 19/26 (333333)...
shred: passwords: pass 20/26 (bbbbbb)...
shred: passwords: pass 21/26 (777777)...
shred: passwords: pass 22/26 (222222)...
shred: passwords: pass 23/26 (888888)...
shred: passwords: pass 24/26 (999999)...
shred: passwords: pass 25/26 (random)...
shred: passwords: pass 26/26 (000000)...
shred: passwords: removing
shred: passwords: renamed to 000000000
shred: 000000000: renamed to 00000000
shred: 00000000: renamed to 0000000
shred: 0000000: renamed to 000000
shred: 000000: renamed to 00000
shred: 00000: renamed to 0000
shred: 0000: renamed to 000
shred: 000: renamed to 00
shred: 00: renamed to 0
shred: passwords: removed
$

Deleting Empty Directories

Although rm can remove directories, in addition to removing files, rmdir is a command made especially for removing directories. Similar to rm, you normally just give rmdir the name of the directory you want to remove. However, rmdir will not delete a directory if it has any contents — any files, or sub-directories. To do that, you'll need to use rm, with the appropriate parameters.

There are a couple of params you can use for rmdir, too.

Parameter Description
--ignore-fail-on-non-empty Suppresses any error messages that rmdir might have given, if directories were not empty
-p Removes directory, and parents. See below, for example.
-v “Verbose” mode; gives a lot more information about what the command is doing.

For example, consider the example before, where we'd created a downloads directory, and then a programs directory:

$ cd ~
$ mkdir -p downloads/programs

I wouldn't be able to remove the downloads directory, using rmdir, because it's not empty:

$ rmdir downloads
rmdir: downloads: Directory not empty

But I could remove both at once, by using:

$ rmdir -p downloads/programs/

Of course, this would still fail if either of the two directories contained any files, or other sub-directories.

Copying and Moving Files

You will very often want to make a copy of a file, either giving it a new name, or simply copying it to a different location, to archive it. You can do this using the cp command.

For example, if you have a file named file1.txt, and want to create a copy named file2.txt, you can do so like this:

$ ls
file1.txt
$ cp file1.txt file2.txt
$ ls
file1.txt  file2.txt
$

(The relevant command was the cp command; I simply inserted the ls commands to illustrate the file being created.)

In this case, I created a copy of file1.txt, in the same directory as the original. I could have copied the file to different location instead, and left it with the same name. For example, I could do this:

$ ls
file1.txt
$ mkdir archive
$ cp file1.txt archive/
$ ls -R
.:
archive  file1.txt

./archive:
file1.txt
$

This might be a bit confusing, so I'll walk you through it.

  1. First, I simply used ls, to demonstrate that there was one file in the working directory, called file1.txt.
  2. I then created a sub-directory, called archive, with the mkdir command.
  3. I then used cp to make a copy of file1.txt, and put it in the archive sub-directory.
  4. Finally, I used ls with the -R parameter to recursively list the contents of the current working directory; it showed
    • The current directory contains file1.txt and the archive sub-directory
    • The archive directory contains one file, file1.txt.

Of course, you could also copy a file to another directory and give it a different name. For example:

$ cp file1.txt archive/file1.bak

will make a copy of file1.txt, putting it in the archive directory, and giving it the name file1.bak.

As with most other commands, there are a bunch of parameters you can specify for cp. Here are some of the common ones:

Parameter Description
-i “Interactive” mode; cp will prompt before it copies each file.
-r or -R “Recursive” mode; copies the contents of the current directory, and all sub-directories (and sub-sub-directories, etc.).
-u “Update” mode; most useful when copying entire directories. In “update” mode, Linux will compare the dates of the two files, and only overwrite the destination file if the source file is newer. (Or if the destination file doesn't exist.) If you're backing up a directory on a regular basis, and it has a lot of large files, this might save you a lot of time.
-v “Verbose” mode; cp will give a lot more information about what it's doing.

As shown, there is a “recursive” mode, which can be used to copy the contents of an entire directory.

If I have a directory named archive, I can copy all of its contents to a new directory, called archive2, as follows:

$ ls -r
.:
archive

./archive:
file1.bak
$ cp -r archive/ archive2
$ ls -r
.:
archive    archive2

./archive:
file1.bak

./archive2:
file1.bak
$

Again, I did a few things here, so I'll walk you through it:

  1. First, I used ls -r to recursively show the contents of the working directory, which includes the archive directory.
  2. I then used the cp command — with the recursive option — to copy the archive directory into a new directory, called archive2.
  3. I did another ls -r to demonstrate that the directory had been created, and had a copy of the same file.

TODO

  • mv
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.