Table of Contents
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:
- Open a terminal and run commands
- Navigate directories with
cd - List files with
ls
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 file3Common uses:
- Quickly create placeholder files
- Prepare multiple files for later editing
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- If
backup.txtdoes not exist, it is created as a copy ofsource.txt. - If it does exist, its contents are overwritten (no confirmation by default).
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:
- Creates
backup_mydir/if it doesn’t exist - Copies all files and subdirectories from
mydir/into it
Copy a directory into another directory:
cp -r mydir/ /path/to/dir/Preserving attributes with `cp -a`
cp -a (archive mode) tries to preserve:
- Permissions
- Timestamps
- Ownership (when possible)
- Symbolic links
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:
report.txtno longer exists in the current directory- It exists inside
/home/user/Documents/
Renaming a file
Use mv with the same directory but a new name:
mv oldname.txt newname.txtYou can also rename into a different directory and name at once:
mv oldname.txt /tmp/newname.txtMoving 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.txtRemove 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 somedirThis removes:
- The directory itself
- All files and subdirectories underneath
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:
rm -rf *rm -rf /rm -rf .
To reduce risk:
- Always double-check the path
- Consider running
lsfirst to see what you’re about to delete
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.txtThis prints the entire file to the terminal (good for small files).
Showing beginning or end of a file
- First 10 lines by default:
head file.txt- Last 10 lines by default:
tail file.txtThese 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.shExample outputs might be:
JPEG image dataBourne-Again shell script, ASCII text executable
Timestamps and size with `ls -l`
You already know ls, but ls -l (long listing) is especially useful for file operations:
ls -lTypical columns include:
- Permissions and file type
- Owner and group
- Size (in bytes)
- Last modification time
- Name
For human-readable sizes:
ls -lhCreating 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 linknamelinknameacts like a shortcut pointing to/path/to/realfile- If the original file is deleted, the link becomes “broken”
Example:
ln -s /var/log/syslog syslog_linkHard 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:
- Deleting one does not remove the other
- Changes via one name appear via the other
Hard links:
- Generally only work within the same filesystem
- Can’t be made to directories (under normal restrictions)
Copy vs Move vs Link: When to Use What
- Use
cpwhen you want a separate copy of the data: - Backups
- Templates you will modify independently
- Use
mvwhen you want to relocate or rename: - Organizing files into directories
- Renaming files as they’re processed
- Use links when you want multiple names for the same data or a convenient shortcut:
- Symbolic links: shortcuts to files or directories (even across filesystems)
- Hard links: multiple directory entries pointing to the same data
Safe Practices for File Operations
- Use tab-completion to avoid typos in paths.
- Check before destructive commands:
- Run
lson a directory beforerm -r:
ls /path/to/dir
rm -r /path/to/dir- Avoid
sudowithrmunless absolutely necessary. - Prefer incremental steps:
- Move to a temporary or backup location first:
mv important_dir/ important_dir.bak/- Once you’re sure you don’t need it, then remove the backup.
Practicing these commands on test directories in your home folder is the best way to get comfortable with everyday file operations.