In general, all information on your Linux system is stored in files. Although you often use separate commands to manipulate directories and links, both are basically specialized types of files.
This chapter covers the basic tasks, such as creating, selecting, copying and deleting directories and files. It also explains how to view and create links, and how to change file attributes.
[Note]
You cannot manipulate files unless you have permission to do so. This limitation is one of the major differences between Linux and many popular operating systems. See Changing Permissions, Section 9.19.
On the desktop, you can use the gnome Midnight Commander for the most common file functions.
However, for a complete set of options, you must use the command line. In fact, a few tasks, such as merging files can only be done from the command line.
Type: touch file.
If no path is entered, then the files are created in the present directory.
[Note]
You may need to select Edit > Rescan directory before the new directory displays in the gnome Midnight Commander.
Type: mkdir options directory .
The mkdir Command (Make Directory) +-------------------------------------------------------------------+ | Option | Action | Comment | |-------------+------------------------------+----------------------| | |Sets permissions for new |See Changing | |-m mode |directory in absolute mode. |Permissions in | | | |Absolute Mode. (***) | |-------------+------------------------------+----------------------| | * -p |Creates any parent directories| | | * --parents|if they do not already exist. | | +-------------------------------------------------------------------+
***) Changing Permissions in Absolute Mode, Section 9.19.4.2.
img/Files-3.png
Either:
[Note]
If you cannot copy, then you do not have write permission to the target directory or file name. See Changing Permissions, Section 9.19.
[Note]
You can also drag selected files with the left mouse button to any folder in any open Midnight Commander window.
Type: cp options source target .
The cp Command (Copy) +-------------------------------------------------------------------+ | Option | Action | Comments | |-------------------+-----------------------+-----------------------| | * -a |Gives target files the | | | * --archive |same attributes as the | | | |source files. | | |-------------------+-----------------------+-----------------------| | * -b |Backs up files that | | | * --backup |will be over written. | | |-------------------+-----------------------+-----------------------| | | |No confirmation is | | * -f |Over writes any |asked. Consider using | | * --force |existing files with the|with -i or -b to avoid | | |copy. |over writing files by | | | |accident. | |-------------------+-----------------------+-----------------------| | * -i |Asks for confirmation | | | * --interactive |before over writing any| | | |files. | | |-------------------+-----------------------+-----------------------| | * -p |Includes the original |Attributes include the | | * --preserve |file's attributes. |owner, group, | | | |permissions and dates. | |-------------------+-----------------------+-----------------------| | |Copies parent | | | * -P |directories to | | | * --parents |sub-directories of the | | | |target directory. | | |-------------------+-----------------------+-----------------------| | * -r, -R |Copies sub-directories | | | * --recursive |and their contents. | | |-------------------+-----------------------+-----------------------| | |Makes the target a | | | * -s |symbolic link to the |See Working With | | * --symbolic-link|source file, instead of|Symbolic Links. (***) | | |a copy. | | |-------------------+-----------------------+-----------------------| | |Copies only files whose| | | * -u |date are the same or | | | * --update |newer than similarly | | | |named ones in the | | | |target directory. | | |-------------------+-----------------------+-----------------------| | * -v |Displays file names as | | | * --verbose |they are copied. | | +-------------------------------------------------------------------+
Working With Symbolic Links, Section 9.16.
[Warning]
Paths, aliases, links, and other features may be affected by moving a file.
[Note]
If you cannot move a file, then you do not have write permission in the target directory. See Changing Permissions, Section 9.19.
[Note]
You can also drag selected files with the left mouse button to the desktop or to folders.
Type: mv options source target .
The target can be a file or a directory.
This command can have three possible results:
The mv Command (Move) +-------------------------------------------------------------------+ | Option | Action | Comments | |-----------------+----------------------------+--------------------| | * -b |Backs up target files that | | | * --backup |will be over written. | | |-----------------+----------------------------+--------------------| | | |No confirmation is | | | |asked. | | * -f |Over writes any existing | | | * --force |files with the copy. |Consider using with | | | |-i or -b to prevent | | | |over writing by | | | |mistake. | |-----------------+----------------------------+--------------------| | * -i |Asks for confirmation before| | | * --interactive|over writing any file. | | |-----------------+----------------------------+--------------------| | |Moves only files whose date | | | * -u |are the same or newer than | | | * --update |similarly named ones in the | | | |target directory. | | |-----------------+----------------------------+--------------------| | * -v |Displays file names as they | | | * --verbose |are moved. | | +-------------------------------------------------------------------+
[Warning]
Paths, aliases, links, and other features may be disabled if a file is renamed.
To rename files, use the mv
(move) command. See Moving Files From the Command Line, Section 9.10.
Type: mv options source target .
To keep the renamed file in its original directory, keep the destination's path name the same as the original, except for the file name.
You can permanently delete a file or sent it to the Trash in case you want to restore it later.
img/Files-9.png
The trash bin is a directory (.gnome-desktop/Trash in your home directory) for storing deleted files that you might want to restore again.
All users have their own trash bins.
[Note]
From the desktop, you cannot set the gnome Trash to empty when it reaches a certain size.
However, you could use the cron command to delete its contents at a regular time. .
Your Trash folder behaves exactly the same as any other folder on your desktop.
To restore files, move them from the Trash folder to another folder. See Moving Files From the Desktop, Section 9.9.
Type: rm file .
If you want the option to recover files, move them to a storage directory. You can use the.gnome-desktop/Trash directory in your home directory. This is the directory in which files are stored when you send them to the Trash on the desktop.
[Note]
You can also use rmdir to remove empty directories. However, in most cases, you can use rm -r as an alternative and save yourself the trouble of learning another command.
The rm Command (Remove) +-------------------------------------------------------------------+ | Option | Action | Comments | |-----------------+--------------------------+----------------------| | * -d |Removes any |Only the root user can| | * --directory |sub-directories and their |use this option. | | |contents. | | |-----------------+--------------------------+----------------------| | * -f |Removes all files without | | | * --force |confirmation. | | |-----------------+--------------------------+----------------------| | * -i |Asks for confirmation | | | * --interactive|before removing each file.| | |-----------------+--------------------------+----------------------| | * -r, -R |Removes sub-directories |May cause accidental | | * --recursive |and their contents. |deletion of files. | |-----------------+--------------------------+----------------------| | * -v |Displays file names as | | | * --verbose |they are removed. | | +-------------------------------------------------------------------+
Files cannot be merged from the desktop. From the command line, you can use a viewing command with a redirection operator or diff3 .
You can a viewing command with redirection operator to merge files. For
example, if you are using cat
:
Separate file names by a space.
You could also use less
or more
to merge files. Viewing Files From the Command Line, Section
8.6.
The diff3 command can be used for comparing files. However, you can also use the command to merge files. This function is especially useful when two versions of a file have been worked on separately.
Type: diff3 target original version .
Note:
A link is a small file that points to another file. When a command is used by a link, it is passed on directly to the original file.
A link is useful when you need a file in two places and/ or when you want to save disk space. However, if the original file is moved or deleted, the symbolic link no longer works.
Links are also used to create launchers on the desktop. Because the launchers are links, deleting them does not delete the file to which they point.
Linux uses two types of links:
The ls command displays the contents of the working directory. To display the symbolic links in the current directory, open a command line and type: ls -l .
The attributes of each file are displayed on a separate line. If a file is a link, then the first character is an l .
In addition, any symbolic link is displayed with the original file beside it. For example, if the original file was ToDo and the link was Monday, then the entry for the link would be: Monday -> ToDo.
You can also use the --color option to display symbolic links and other file types in their own color, and -F to display symbolic links with the at sign (@) at the end of them,
img/Files-12.png
You can use the cp -l command to create an absolute link or the ln -s command to create a symbolic link. Usually, you will want to create a symbolic link instead of an absolute link.
To add a single link with a different name from the original file, type:
ln -s options file link .
To add a link with the same file name as the original file but in a different directory, type: ln -s options file directory .
If you enter more than one original file name, leave a space between the names.
The ln Command (Link) +-------------------------------------------------------------------+ | Option | Action | Comments | |-----------------+------------------+------------------------------| | * -b |Backs up files | | | * --backup |before over |Useful with the -S option. | | |writing. | | |-----------------+------------------+------------------------------| | * -f |Over writes files |Use with the -b option to | | * --force |if necessary. |avoid accidentally over | | | |writing important files. | |-----------------+------------------+------------------------------| | * -i |Prompts for | | | * --interactive|permission before | | | |each action. | | |-----------------+------------------+------------------------------| | * -S suffix |Adds a suffix to | | | * --suffix |backup files. |The default suffix is . | | suffix | | | |-----------------+------------------+------------------------------| | * -s |Creates a symbolic|Without this option, a hard | | * --symbolic |link. |link is created. | |-----------------+------------------+------------------------------| | * -v |Displays actions | | | * --verbose |on screen. | | +-------------------------------------------------------------------+
Each file in Linux has an owner. This attribute gives users some privacy. It can also be used by the root owner to protect files from casual use or accidental damage.
The owner of a file is usually the user who created the file. However, the owner may be changed by either the present owner or the root user.
Valid owners are listed in the /etc/passwd file.
An owner is identified by the user account or a user id .
Type: chown options owner files .
The owner can be identified by the user account or the user id .
To copy the owner from another file, type: chown options --reference= source target .
The chown Command (Change Owner) +-------------------------------------------------------------------+ | Option | Action |Comments| |--------------------+-------------------------------------+--------| | * -c |Displays information about files that| | | * --changes |are changed. | | |--------------------+-------------------------------------+--------| | |Changes the ownership of both | | |--dereference |symbolic links and the files to which| | | |they point. | | |--------------------+-------------------------------------+--------| | * -h |Changes the ownership of a symbolic | | | * --no-dereference|link, but not of the file to which it| | | |points. | | |--------------------+-------------------------------------+--------| |--reference= file |Copies owner from the file listed. | | |--------------------+-------------------------------------+--------| | * -r |Changes ownership of sub-directories | | | * --recursive |and their contents. | | |--------------------+-------------------------------------+--------| | * -v |Displays actions on screen. | | | * --verbose | | | +-------------------------------------------------------------------+
A group is a collection of users who have the same access to devices and files on the system. Each file in Linux is assigned to a group.
In most cases, a file is assigned to its owner's main group, which is usually the same as the owner's account name. However, users may belong to more than one group.
Valid owners are listed in the /etc/group file.
The group may be changed by either the present owner or the root user.
The group attribute lets the owner define whether other users can work with the file, and what they can do with it. It can also be used by the root owner to protect files from casual use or accidental damage.
Use the chgrp (change group) command. Only the owner of a file or the root user can change the group. Valid groups are listed in /etc/ group.
Type: chgrp options group files .
The group can be identified by name or group id .
To copy the group from another file, type: chgrp options --reference= source target .
The chgrp Command (Change Group) +-------------------------------------------------------------------+ | Option | Action |Comments| |---------------+------------------------------------------+--------| | * -c |Displays information about changed files. | | | * --changes | | | |---------------+------------------------------------------+--------| |--reference= |Copies the group from the file listed. | | |file | | | |---------------+------------------------------------------+--------| | * -r |Changes the group for sub-directories and | | | * --recursive|their contents. | | |---------------+------------------------------------------+--------| | * -v |Displays actions on screen. | | | * --verbose | | | +-------------------------------------------------------------------+
Permissions define how users can work with a file or directory - or whether they can work with it at all. Permissions are also called access rights, or file or access permissions.
The three types of permissions are:
A hyphen ( - ) in the list of file attributes indicates that no permission is set.
In addition, a file may be set as Sticky. That means that only the user or root user can delete it.
Permissions are set for three categories of users:
[Warning]
Depending on the file, setting the user id to the root user can create security problems.
[Note]
The root user can read, write, and execute all files on the system.
Occasionally, you may find some graphic tools that do not allow the root user complete access. However, the root user can always access all files from the command line.
Type: ls -l file .
The first attributes on each line are the type of file and the permissions. For example, a typical file might display: -rwxr-xr-- :
The other nine characters are the permissions:
The chmod command has two modes: the symbolic (or character) and the absolute (or octal) mode.
As well as the usual options in any command, the symbolic mode uses a series of individual arguments. Use the symbolic mode if you want to assign the permissions carefully, and in full.
By contrast, the absolute mode uses octal notation to reduce the arguments in the commands to three characters. Despite its name, absolute mode is quick and efficient once you learn it.
Type: chmod options group action permissions file .
The possible entries for each part of this command is explained in one of the tables below.
The group and permissions arguments can use more than one entry. For example, you could use ug in the group argument to set permissions for both the user and the group.
The chmod Command (Change Permissions): Options +-------------------------------------------------------------------+ | Option | Action |Comments| |---------------+------------------------------------------+--------| | * -c |Displays information about changed files. | | | * --changes | | | |---------------+------------------------------------------+--------| |--reference= |Copies permissions from the file listed. | | |file | | | |---------------+------------------------------------------+--------| | * -R |Applies changes to sub-directories and | | | * --recursive|their contents. | | |---------------+------------------------------------------+--------| | * -v |Displays information about all files. | | | * --verbose | | | +-------------------------------------------------------------------+
The chmod Command (Change Permissions): Group Settings for Symbolic Mode +-------------------------------------------------------------------+ | Group |Argument| |----------------------------------------------------------+--------| |All. User, Group and Others. |a | |----------------------------------------------------------+--------| |Group. |g | |----------------------------------------------------------+--------| |Other users on the system who do not belong to the user's |o | |group. | | |----------------------------------------------------------+--------| |User. The owner of the file. |u | +-------------------------------------------------------------------+
The chmod Command (Change Permissions): Action Settings for Symbolic Mode +-------------------------------------------------------------------+ | Action |Argument| |----------------------------------------------------------+--------| |Adds permission. |+ | |----------------------------------------------------------+--------| |Removes permission. |- | |----------------------------------------------------------+--------| |Sets only the permissions specified and removes all other |= | |permissions. | | +-------------------------------------------------------------------+
The chmod Command (Change Permissions): Permission Settings for Symbolic Mode +-------------------------------------------------------------------+ | Action |Argument| |----------------------------------------------------------+--------| |Do not give permission. Use as a place holder when setting|- | |multiple permissions. | | |----------------------------------------------------------+--------| |Set to group's current permissions. |g | |----------------------------------------------------------+--------| |Set to other's current permissions. |o | |----------------------------------------------------------+--------| |Set read permission. |r | |----------------------------------------------------------+--------| |Set user or group id . |s | |----------------------------------------------------------+--------| |Set sticky. |t | |----------------------------------------------------------+--------| |Set to user's current permissions. |u | |----------------------------------------------------------+--------| |Set write permission. |w | |----------------------------------------------------------+--------| |Set execute permission. |x | +-------------------------------------------------------------------+
Type: chmod number .
The number is usually three digits. From left to write, the digits stand for the permissions for the user, the group, and other users.
You can calculate the digit to use by adding the octal number for each permission. Calculating Numbers for the chmod Command Absolute Mode, Section 9.19.4.3. You must use at least three digits in Absolute Mode.
A fourth number is sometimes added as a prefix to set user or group id or to set sticky. See The chmod Command (Change Permissions): The 4th Digit in Absolute Mode, Section 9.19.4.5. "The chown Command (Change Permissions): The 4th Digit in Absolute Mode," page 200.
To calculate each digit in Absolute Mode (See The chmod Command (Permissions): Absolute Mode, Section 9.19.4.4), add the octal numbers for the permissions you want to set. For example, the number to set read and execute permission is 5 (4+1).
The first three digits are for the user, group, and others. For example, 754 gives:
You may also use a fourth digit. See The chmod Command (Change Permissions): The 4th Digit in Absolute Mode, Section 9.19.4.5.
+--------------------------------+ | Permission |Octal Number| |-------------------+------------| |Read permission. |4 | |-------------------+------------| |Write permission. |2 | |-------------------+------------| |Execute permission.|1 | |-------------------+------------| |No permissions. |0 | +--------------------------------+
+------------------------+ |Permission |Octal Number| |-----------+------------| |User id . |4 | |-----------+------------| |Group id . |2 | |-----------+------------| |Set sticky.|1 | +------------------------+
[Note]
The fourth digit in Absolute Mode is optional.
All files include:
To change the creation time, use the cp command without the -a or --archive options.
To change the other times, type: touch options file .
The Touch Command (Changing Date Attributes) +-------------------------------------------------------------------+ | Option | Action | Comments | |-----------------+--------------------------------+----------------| | * -a |Changes the access time to the | | | * --time-access|current time. | | |-----------------+--------------------------------+----------------| | * -m |Changes the modification time to| | | * --time-mtime |the current time. | | |-----------------+--------------------------------+----------------| | |Uses the time specified instead |Use the format: | |-t time |of the current time. | | | | |ccyymmddhhmm.ss.| +-------------------------------------------------------------------+
You can set special file attributes on a native Linux (ext2) filesystem. These attributes affect how the file can be edited, deleted, or backed up.
These special attributes are in addition to the usual set of ownership, permissions and dates. They are not automatically assigned to a file.
Type: chattr options actions attributes files .
Do not put a space between the action and attribute argument.
The chattr Command (Change Attributes): Options +-------------------------------------------------------------------+ |Option| Action |Comments| |------+---------------------------------------------------+--------| |-R |Changes attributes of sub-directories and their | | | |contents. | | |------+---------------------------------------------------+--------| |-V |Displays actions on screen. | | +-------------------------------------------------------------------+
The chattr Command (Change Attributes): Actions +-------------------------------------------------------------------+ | Action |Argument| |----------------------------------------------------------+--------| |Adds the attribute. |+ | |----------------------------------------------------------+--------| |Removes the attribute. |- | |----------------------------------------------------------+--------| |Sets only the attribute specified. Removes other |= | |attributes. | | +-------------------------------------------------------------------+
The chattr Command (Change Attributes): Attributes +-------------------------------------------------------------------+ | Attribute |Argument| |----------------------------------------------------------+--------| |Date and time are not updated when the file is modified. |A | |----------------------------------------------------------+--------| |Material can only be added to the end of the file. |a | |Existing contents cannot be changed. | | |----------------------------------------------------------+--------| |File can only be modified by the root user. |i | |----------------------------------------------------------+--------| |File cannot be recovered when deleted. |s | |----------------------------------------------------------+--------| |File cannot be deleted. |u | |----------------------------------------------------------+--------| |Do not include in backup using dump. |d | +-------------------------------------------------------------------+
Type: lsattr options files .
Files are displayed one line at a time.
Options for the lsattr Command (List Attributes) +----------------------------------------------------------+ |Option| Action |Comments| |------+------------------------------------------+--------| |-a |Shows all files in the directory listed. | | |------+------------------------------------------+--------| |-d |Lists directories' attributes, not files'.| | +----------------------------------------------------------+
User's Guide