# Guide to Unix/Commands/File System Utilities

| Wikibooks Guide to Unix Computing | Edit template |
   General: Introduction | Explanations | Why Unix-like Platforms: Linux | BSD Quick Reference: Commands | Environment Variables | Files | License
   Commands: Getting Help | File System Utilities | Finding Files | Devices | File Viewing | File Editing | Text Processing | File Compression | File Analysing | Multiuser Commands | Self Information | System Information | Process Management | Kernel Commands | Miscellaneous | SW Development |

### ls

* ls LiSt contents of directory
* FreeBSD manual page
* NetBSD manual page
* OpenBSD manual page
* ls at the GNU core-utils manual
* ls at the Page Linux questions wiki
ls is a utility for listing the files in a directory.
Most used options are:

#### -a

    all files (include files with . prefix)

#### -l

    long detail (provide file statistics)

#### -t

    order by creation time

#### -u

    sort by access time (or show when last accessed together with -l)

#### -S

    order by size

#### -r

    reverse order

#### -F

    mark directories with /, executables with *, symbolic links with @, local sockets with =, named pipes (FIFOs) with |
Other options include:

#### -Fx --color

    color-coded listing

#### -s

    show filesizes

#### -Z

    show the file/directory with there SELinux context ( only on SELinux enable tree)

#### -h

    "human readble"; show filesizes in kilobytes and megabytes (-h can be used together with -l or -s)
Examples
Several systems have an alias ll which does the same as ls -l:
Be careful with the -F option. Here is one example:
We do not know yet if there is a symbolic link "X" and an executable "Xmark" or if "X@" and "Xmark*" are just the names of normal files. (Though "@" and "*" are not much found in filenames, they are possible.) So we check by dropping the -F:
Links:
* ls, opengroup.org

### mkdir

mkdir is a utility for creating a directory.
Examples
The -p option also makes parent-directories as needed. Instead of:
you can just do:
Links:
* mkdir, opengroup.org
* mkdir man page, man.cat-v.org
* 12.3 mkdir in GNU Coreutils manual, gnu.org

### cd

* cd change Current Directory
* cd cd at the Page Linux questions wiki
cd changes the current directory of the shell. This current directory will be used by other programs launched from the shell.
Because "cd" changes the state of the shell, it is a shell built-in command. In contrast, most commands are separate programs which the shell starts.
Examples
Change to 'foobar' directory:
Change to your home directory, cd command used without an option will drop you back into your home directory.
~ (tilde) stores the path to your home directory, this command has same effect as the previous one.
When suffixed with a username, the command will change into that particular user's home directory.
Change to parent directory:
Change to the previous directory:
Tips:
By setting "CDPATH" environment variable in your shell you can take advantage of shell command completion facility.
If you have the $CDPATH set, then you press 'TAB' key and get possible path completions

### pwd

* pwd Print Working Directory
* FreeBSD manual page
* NetBSD manual page
* OpenBSD manual page
* pwd at the GNU core-utils manual
* pwd at the Page Linux questions wiki
pwd (for Print Working Directory) shows the current directory that you are in.
Though "pwd" is often available as an external program (like /bin/pwd), many shells offer an equivalent version as a shell builtin command. Like any external command, "pwd" would inherit the current directory from the shell or other program that starts it.
Examples
You can change the directory, you can also
You can also use "pwd" in scripts. If you have enough experience with scripting, then you would know that the next line complains if the current directory is /home/username.

### chroot

* chroot CHange ROOT directory
* FreeBSD manual page
* NetBSD manual page
* OpenBSD manual page
* chroot at the GNU core-utils manual
* chroot at the Page Linux questions wiki
chroot changes the root filesystem. The "chroot" page at the Linux questions wiki explains why you might want to do this.
Examples
To change the root filesystem so /mnt/usbdrive/ becomes / and files outside of it cannot be seen:
You must be root user to "chroot". Other users would be able to use "chroot" to gain root (superuser) privileges, so their use of "chroot" is disallowed.

### cp

cp copies a file
Most used options are:

#### -r

    copies directories (recursively)

#### -p

    preserves permissions, ownership, and timestamps

#### -i

    prompt before overwrite

#### -v

    verbose, show filenames as they are being copied
Examples
Links:
* cp, opengroup.org
* cp man page, man.cat-v.org
* 11.1 cp in GNU Coreutils manual, gnu.org

### mv

mv move and/or rename files
Examples
Links:
* mv, opengroup.org
* mv man page, man.cat-v.org
* mv in GNU Coreutils manual, gnu.org

### rm

* rm ReMove and delete files
* FreeBSD manual page
* NetBSD manual page
* OpenBSD manual page
* rm at the GNU core-utils manual
* rm at the Page Linux questions wiki
rm deletes a file from the filesystem, like the "del" command in DOS.
The GNU long options (like --directory) are available on Linux, but not most other systems.
Some useful options are:

#### -d, --directory

    unlink FILE, even if it is an empty directory (some systems let superuser unlink non-empty directories too)

#### -f, --force

    ignore nonexistent files, never prompt

#### -i, --interactive

    prompt before any removal

#### -P

    (*BSD only) overwrite file before deletion

#### -r, -R, --recursive

    remove the contents of directories recursively (the force option must often be used to successfully run rm recursively)

#### -v, --verbose

    (GNU only) explain what is being done

#### --help

    (GNU only) display help and exit

#### --version

    (GNU only) output version information and exit
Examples:
The usage of "rm" is considered potentially more dangerous than equivalents in other operating systems because of the way the shell parses wildcards and names of special directories and in its non-verbose actions.
Here is a classic example. Instead of deleting files that end with .o ("*.o") it deletes all files in the directory ("*") and also a file called .o. There is an unwanted space between the asterisk and the period.
To remove a file whose name starts with a `-', for example `-foo', use one of these commands:
It might be useful to create an alias such as "remove" which moves the files to a local "trash" file so you can go there and recover files you accidentally "remove"d.
Secure deletion of files:
Note that if you use rm to remove a file, it is usually possible to recover the contents of that file since rm does not remove it from the hard disk. It simply removes the file systems link to it.
On *BSD systems, the -P option overwrites the data with the file before removing it.
However, as the NetBSD manual page explains it:
    Recent research indicates that as many as 35 overwrite passes with carefully chosen data patterns may be necessary to actually prevent recovery of data from a magnetic disk. Thus the -P option is likely both insufficient for its design purpose and far too costly for default operation.
So while examining the data (using fsdb or making a disk image) will not reveal the secret data, other methods (such as laboratory examination of the disk) will reveal the data. In short, rm -P does not delete data securely. A program that attempts to delete data securely is GNU shred, available on Linux. But "shred" is not always successful in secure deletion; read its entry below.

### rmdir

rmdir is a utility for deleting empty directories.
Examples
If the directory is not empty, the correct way to remove the directory and all its contents recursively is to use
Links:
* rmdir, opengroup.org
* rmdir man page, freebsd.org
* 12.7 rmdir in GNU Coreutils manual, gnu.org

### shred

* shred Attempt to securely delete files
* shred at the GNU core-utils manual
* shred at the Page Linux questions wiki
shred overwrites a file multiple times with special data patterns to make the old contents of the file unrecoverable from a disk, especially a hard disk. This command is part of GNU coreutils, so it is often only available on Linux systems.
Note that this actually is ineffective on most filesystems because they can keep old copies of data. Most popular Linux filesystems (including ext3) keep such copies through journaling. However, "shred" is very useful for destroying the data on entire partitions or disks.
Some useful options are:

#### -u, --remove

    unlink the file after removing it

#### -NUMBER, -n NUMBER, --iterations=NUMBER

    the number of iterations of overwriting the file; default is 25 iterations
Examples: Remove and completely destroy secretfile from a filesystem that overwrites data in place and does not use journaling (for example, the UFS filesystem of *BSD). For the last step, after the data is destroyed, the "-u" option unlinks the file from the filesystem.
Note that if secretfile has multiple hard links (with ln for example), it will continue to exist with those other names, but will contain only random data.

### touch

touch lets you change the date on a file. Can also be used to create a blank file.
Examples
This will change the access date and time of filename to the current time. If filename doesn't exist it will create a blank file.
Links:
* touch, opengroup.org
* touch man page, man.cat-v.org
* 13.4 touch in GNU Coreutils manual, gnu.org

### df

df reports the amount of free disk space available on each partition.
Reports disk usage in human readable format with block-sizes in Kilo,Mega,Gigabytes.
Links:
* df, opengroup.org
* df man page, man.cat-v.org
* 14.1 df in GNU Coreutils manual, gnu.org

### du

Shows disk usage.
Links:
* du, opengroup.org
* du man page, man.cat-v.org
* 14.2 du in GNU Coreutils manual, gnu.org

### ln

ln creates links between files.
Symbolic links are special files that contain the absolute or relative path to a separate file. References to the symbolic link are "forwarded" to the file it (the symbolic link) points to. The file to which the reference is forwarded is not "aware" of the link; if the file pointed to is moved or deleted, the link will just point to nothing (no warnings are given, nor are any attempts made to "refresh" the link to the file's new location).
In Unix all information about files - owner, group, permissions, size, number of links, location on disk - are stored in the inode (with the notable exception of the filename). The filename is stored in directories, where inode-numbers (the way the OS refers to files) are paired with filenames (text-strings; the way the user refers to files). The "number of links" entry in the inode, keeps track of how many times the inode-number has thus been paired with a name in some directory.
When creating a (hard) link with ln, the "source" file is only used to determine the inode-number. A new inode-number-to-filename entry is then made in some directory using that inode-number, and the "number of links" counter is incremented. It's important that this (unlike a symbolic link) is the same file in every way... it's just that the file can be accessed by different names and/or from different locations. Moving or deleting the "original" source-file has no effect on the other links. Deleting one of the file's links only removes its entry from that directory... it's first when the last link in any directory is removed, that the file is actually deleted from the disk.
Hard links can not (unlike symbolic links) be used to refer to a file on another filesystem, nor can hard links usually be used to link to a directory.
Examples:
To make a soft (symbolic) link "hello" to the file "/home/alice/code/bin/world":
To make a (hard) link from the file "foo" to the file "bar":
The 1st number of ls -l's listing, shows the number of (hard) links a file has - in other words, the number of times it's been entered into some directory.
Links:
* ln, opengroup.org
* ln man page, man.cat-v.org
* 12.2 ln in GNU Coreutils manual, gnu.org

### chown

* chown CHanges OWNer of file
* FreeBSD manual page
* NetBSD manual page
* OpenBSD manual page
* chown at the GNU core-utils manual
* chown at the Page Linux questions wiki
chown changes the owner and group of files. Normally, only root is allowed to do this, but if a user owns a file, then that user can change the group, but only to groups containing that user. On old systems, the ability of users to give files to other users caused abuses, so most systems prohibit non-root users from changing the owner of files.
Some useful options are:

#### -R

    recursively change owner/group on an entire directory tree

#### -h

    do not follow symbolic links i.e. changes owner of the link, not the target file

#### -f

    indicate no errors if change failed
Examples:
Root changes the ownership of "/etc/passwd" and "/etc/shadow" to user root, group wheel:
The same, but only changing the owner:
The same, but only changing the group:
Root gives every file in "/etc/ssh", including files in subdirectories, to user root, group wheel:
The same, but excluding files in directories (and the invisible files "/etc/ssh/.*" will also be missed):
User "tux" changes the directory "/usr/local/src/xc" from group "tux" to group "wheel". Tux is a member of both groups.

### chmod

* chmod CHanges file MODe
* FreeBSD manual page
* NetBSD manual page
* OpenBSD manual page
* chmod at the GNU core-utils manual
* chmod at the Page Linux questions wiki
chmod changes permissions of files. One must be familiar with Unix file permissions to understand this command. There are three permissions: read ("r"), write ("w"), and execute ("x"). There are three sets of permissions: for the owning user of the file ("u"), for the group of the file ("g"), and for other users ("o").
For a file, "execute" means to run it as a program. For a directory, "execute" permission is required to use anything in that directory tree, so doing anything with "/usr/share/doc/README" requires execute permissions on all of "/", "/usr", "/usr/share", and "/usr/share/doc".
If you are interested in more advanced topics like the set-uid, set-gid, sticky bits and octal numbers, try reading the FreeBSD manual page at http://www.FreeBSD.org/cgi/man.cgi (type "chmod" in the form and submit).
A useful option is:

#### -R

    recursively change or set permissions on an entire directory tree
Examples:
We wrote a shell script called "configure". We make it executable ("+x") and then execute it as a command. Usually, "+x" is the same as "u+x" or "ug+x", depending on the status of the file mode creation mask.
Allow the owning user to run "configure":
Deny the group and other users from running "configure":
For all users except the owner ("go"), disable all access to "~/mail" and "~/private" ("-rwx"). This way, the contents are private and only their owner (or root) can access them.
Note that in the previous example, "-R" was not specified. By disabling the execute bit ("-x"), all files inside ~/{mail,private} are protected even if their group and other read bits are enabled. Thus, simply moving some file from inside ~/{mail,private} to some public place like /tmp can make the files available to other users again.
The "root" user wants to set up /usr/local/src so that all users in group "wsrc" (including "tux") can create files there. Root will continue to own the directory. This is done by changing the group of /usr/local/src to "wsrc" and then by granting to the group ("g") the read, write, and execute permissions ("+rwx").
All Unix-like systems should allow all users to create temporary files in "/tmp" and "/var/tmp". Thus root gives everyone ("a", short for "ugo") all permissions ("+rwx") on the files.
The problem with the above is that because all users have write access to /tmp and /var/tmp, every user can delete and rename files, even ones not created by them. For example, "tux" could create "/tmp/socket.3908" and another user could delete it or rename it to "/tmp/garbage", thus annoying Tux. To keep temporary files safe, we use the sticky bit called "t". This limits the deletion and renaming of files in /tmp to root, the owner of /tmp (also root), and the owner of the file (Tux for "/tmp/socket.3908"). It does the same for /var/tmp. So what we should do is:

### chgrp

Changes the group ownership of files.
Links:
* chgrp in The Open Group Base Specifications Issue 7, pubs.opengroup.org
* 13.2 chgrp: Change group ownership in GNU Coreutils at gnu.org
* W:Chgrp

### link

Makes a hardlink. See also ln.
Links:
* link, opengroup.org
* link man page, man.cat-v.org
* link, freebsd.org
* link in GNU Coreutils, gnu.org

### unlink

Deletes a file.
Links:
* unlink, opengroup.org
* unlink, freebsd.org
* unlink in GNU Coreutils, gnu.org