Kahibaro
Discord Login Register

2.2.3 File operations

Overview

This chapter focuses on the most common file operations from the command line: creating, copying, moving, renaming, deleting, and inspecting basic file properties. You’ll practice with simple but essential commands that you will use constantly in daily Linux work.

You should already know how to:

Here we concentrate specifically on what you can do to files.


Creating Files

Empty files with `touch`

touch is the simplest way to create an empty file:

touch notes.txt

If notes.txt doesn’t exist, it is created as an empty file. If it already exists, touch updates its modification time (useful for scripts and build tools).

You can create multiple files at once:

touch file1 file2 file3

Common uses:

Copying Files and Directories

Copying is done with cp. At minimum, you give it a source and a destination.

Copying a single file

cp source.txt backup.txt

Copy a file into a directory:

cp source.txt /path/to/dir/

The destination here is a directory, so source.txt keeps its name.

Copying multiple files

You can copy several files into a directory in one go:

cp file1 file2 file3 /path/to/dir/

The last argument is the destination directory; everything before it is treated as a source file.

Copying directories recursively

By default, cp handles only files. To copy directories, you must use the recursive option -r (or -R):

cp -r mydir/ backup_mydir/

This:

Copy a directory into another directory:

cp -r mydir/ /path/to/dir/

Preserving attributes with `cp -a`

cp -a (archive mode) tries to preserve:

Example:

cp -a original_dir/ copy_dir/

This is often used for backups or migrations when you want the copy to be as close to the original as possible.


Moving and Renaming Files

Moving files changes where a file is located. Renaming is just a special case of moving: same directory, different name. Both are done with mv.

Moving a file to another directory

mv report.txt /home/user/Documents/

After this:

Renaming a file

Use mv with the same directory but a new name:

mv oldname.txt newname.txt

You can also rename into a different directory and name at once:

mv oldname.txt /tmp/newname.txt

Moving multiple files

Similar to cp, multiple sources, one destination directory:

mv file1 file2 /path/to/dir/

Moving directories

mv can move or rename directories without needing -r:

mv olddir/ newdir/
mv olddir/ /path/to/dir/

No copy is made; the directory itself is relocated or renamed.


Deleting Files and Directories

Deleting from the command line is permanent. There is no trash/recycle bin by default for rm, so be careful.

Deleting files with `rm`

Remove a single file:

rm file.txt

Remove multiple files:

rm file1 file2 file3

If a file is write-protected, rm may ask for confirmation.

Deleting directories

To delete an empty directory, it’s safer to use rmdir:

rmdir emptydir

To delete a directory and everything inside it, use rm -r (recursive):

rm -r somedir

This removes:

Dangerous flags: `-f` and wildcards

-f (force) ignores non-existent files and never asks for confirmation:

rm -rf somedir

This is powerful and dangerous, especially when combined with wildcards like *.

Examples of dangerous commands you should avoid unless you know exactly what you’re doing:

To reduce risk:

Viewing File Contents (Quick Operations)

You’ll see more detailed tools in a dedicated text-viewing chapter, but a few very basic operations are commonly used alongside file management.

Showing a file with `cat`

cat file.txt

This prints the entire file to the terminal (good for small files).

Showing beginning or end of a file

  head file.txt
  tail file.txt

These are often used just to check what kind of data is in a file.


Basic File Information

Checking file type with `file`

file looks at a file’s contents (not just its extension) to guess what it is:

file picture.jpg
file script.sh

Example outputs might be:

Timestamps and size with `ls -l`

You already know ls, but ls -l (long listing) is especially useful for file operations:

ls -l

Typical columns include:

For human-readable sizes:

ls -lh

Creating and Working with Links

Linux supports two main types of links: hard links and symbolic links (symlinks). Full details are covered elsewhere, but you need the basic commands now.

Symbolic links with `ln -s`

Create a symbolic (soft) link:

ln -s /path/to/realfile linkname

Example:

ln -s /var/log/syslog syslog_link

Hard links with `ln`

Create a hard link:

ln original.txt copy_link.txt

Both original.txt and copy_link.txt now refer to the same underlying data:

Hard links:

Copy vs Move vs Link: When to Use What

Safe Practices for File Operations

    ls /path/to/dir
    rm -r /path/to/dir
    mv important_dir/ important_dir.bak/

Practicing these commands on test directories in your home folder is the best way to get comfortable with everyday file operations.

Views: 65

Comments

Please login to add a comment.

Don't have an account? Register now!