Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




14.4. Manipulating Files at the Shell Prompt

Files can be manipulated using one of the graphical file managers, such as Nautilus or Konqueror. They can also be manipulated using a shell prompt, which is often faster. This section explains how to manipulate files at the shell prompt.

14.4.1. Creating Files

You can create new files either with applications (such as text editors) or by using the command touch, which will create an empty file that you can use to add text or data. To create a file with touch, type the following at a shell prompt.

touch <filename>

Replace <filename> with the name of your choice. If you run a directory listing, you can see that the file contains zero (0) bytes of information because it is an empty file. For example, typing the command ls -l newfile at the shell prompt returns the following output:

-rw-rw-r--    1 sam      sam             0 Apr 10 17:09 newfile

14.4.2. Copying Files

Like so many other Linux features, there is a variety of ways to manipulate files and directories. You can also use wildcards, as explained in Section 13.11.5 Wildcards and Regular Expressions, to make the process of copying, moving, or deleting multiple files and directories faster.

To copy a file, type the following command.

cp <source> <destination>

Replace <source> with the name of the file you want to copy, and <destination> with the name of the directory where you want the file to go.

So, to copy the file sneakers.txt to the directory tigger/ in your home directory, move to your home directory and type:

cp sneakers.txt tigger/

You can use both relative and absolute pathnames with cp. Our home directory is the parent of the directory tigger; tigger is one directory down from our home directory.


To learn more about relative and absolute pathnames, refer to Section 13.4 Changing Directories with cd.

Read the cp man page (type man cp at the shell prompt) for a full list of the options available with cp. Among the options you can use with cp are the following:

  • -i — interactive. Prompts you to confirm if the file is going to overwrite a file in your destination. This is a handy option because it can help prevent you from making mistakes.

  • -r — recursive. Rather than just copying all the specified files and directories, this will copy the whole directory tree, subdirectories and all.

  • -v — verbose. Shows the progress of the files as they are being copied.

Now that you have the file sneakers.txt in the tigger directory, use cp -i to copy the file again to the same location.

cp -i sneakers.txt tigger
cp: overwrite 'tigger/sneakers.txt'?

To overwrite the file that is already there, press [Y] and then [Enter]. If you do not want to overwrite the file, press [N] and [Enter].

14.4.3. Moving Files

To move files, use the mv command. For more about mv, see the mv man page (type man mv).

Common options for mv include the following:

  • -i — interactive. This will prompt you if the file you have selected will overwrite an existing file in the destination directory. This is a good option, because like the -i option for cp, you will be given the chance to make sure you want to replace an existing file.

  • -f — force. Overrides the interactive mode and moves without prompting. Unless you know what you are doing, this option is dangerous; be very careful about using it until you become more comfortable with your system.

  • -v — verbose. Shows the progress of the files as they are being moved.

If you want to move a file out of your home directory and into another existing directory, type the following (you will need to be in your home directory):

mv sneakers.txt tigger

Alternatively, the same command using absolute pathnames looks like

mv sneakers.txt /home/newuser/sneakers.txt /home/newuser/tigger

14.4.4. Deleting Files and Directories

You learned about creating files with the touch command, and you created the directory tigger using mkdir.

Now you need to learn how to delete files and directories. Deleting files and directories with the rm command is a straightforward process. See the rm man page for more information. Options for removing files and directories include:

  • -i — interactive. Prompts you to confirm the deletion. This option can stop you from deleting a file by mistake.

  • -f — force. Overrides interactive mode and removes the file(s) without prompting. This might not be a good idea, unless you know exactly what you are doing.

  • -v — verbose. Shows the progress of the files as they are being removed.

  • -r — recursive. Will delete a directory and all files and subdirectories it contains.

To delete the file piglet.txt with the rm command, type:

rm piglet.txt


Once a file or directory is removed with the rm command, it is gone permanently and cannot be retrieved.

Use the -i (interactive) option to give you a second chance to think about whether or not you really want to delete the file.

rm -i piglet.txt
rm: remove 'piglet.txt'?

You can also delete files using the wildcard *, but be careful, because you can easily delete files you did not intend to throw away.

To remove a file using a wildcard, you would type:

rm pig*

The above command will remove all files in the directory which start with the letters pig.

You can also remove multiple files using the rm command. For example:

rm piglet.txt sneakers.txt

You can use rmdir to remove a directory (rmdir foo, for example), but only if the directory is empty. To remove directories with rm, you must specify the -r option.

For example, if you want to recursively remove the directory tigger you would type:

rm -r tigger

If you want to combine options, such as forcing a recursive deletion, you can type:

rm -rf tigger

A safer alternative to using rm for removing directories is the rmdir command. With this command, you will not be allowed to use recursive deletions, so a directory which has files in it will not be deleted.


The rm command can delete your entire file system! If you are logged in as root and you type the simple command rm -rf /, you are in trouble; this command will recursively remove everything on your system.

Read the rmdir man page (man rmdir) to find out more about this command.

  Published under the terms of the GNU General Public License Design by Interspire