This page covers essential commands for creating, copying, moving, deleting, and viewing files.
6. touch
- Create Empty Files / Update Timestamps
Purpose: The touch
command has two primary uses:
- To create new, empty files.
- To update the access and modification timestamps of existing files without altering their content.
Syntax: touch [OPTIONS] FILE...
Examples:
touch new_file.txt touch existing_file.log
7. cp
- Copy Files and Directories
Purpose: The cp
command is used to copy files and directories from one location to another.
Syntax: cp [OPTIONS] SOURCE DESTINATION
Key Options:
-r
or-R
: (recursive) Required when copying directories, as it copies the directory and all its contents.-i
: (interactive) Prompts before overwriting an existing file.-v
: (verbose) Explains what is being done.
Examples:
cp file1.txt /tmp/backup/ cp -r my_docs /mnt/usb_drive/ cp -i document.pdf ~/archive/
8. mv
- Move/Rename Files and Directories
Purpose: The mv
command is used for two main operations:
- Moving files or directories from one location to another.
- Renaming files or directories.
Syntax: mv [OPTIONS] SOURCE DESTINATION
Key Options:
-i
: (interactive) Prompts before overwriting an existing file.-v
: (verbose) Explains what is being done.
Examples:
mv old_name.txt new_name.txt mv report.pdf ~/reports/ mv -v project_data /backup/old_projects/
9. rm
- Remove Files and Directories
Purpose: The rm
command is used to remove (delete) files and directories. Use with caution, as deleted items are typically not recoverable.
Syntax: rm [OPTIONS] FILE...
Key Options:
-r
or-R
: (recursive) Required to remove directories and their contents.-f
: (force) Forces deletion without prompting for confirmation. Extremely dangerous; use with extreme care.-i
: (interactive) Prompts before every removal. Highly recommended for safety.
Examples:
rm unwanted_file.txt rm -r old_project_folder rm -i sensitive_data.log
10. cat
- Concatenate and Display File Content
Purpose: The cat
command (short for concatenate) is primarily used to display the content of files on the standard output. It can also be used to concatenate multiple files into one.
Syntax: cat [OPTIONS] [FILE...]
Key Options:
-n
: (number) Displays line numbers along with the content.-b
: (number non-blank) Displays line numbers for non-blank lines.>
: (redirection) Used to redirect output to a file (e.g.,cat file1.txt > file2.txt
copies file1 to file2).>>
: (append redirection) Used to append output to an existing file (e.g.,cat file1.txt >> file2.txt
appends file1 to file2).
Examples:
cat my_document.txt cat -n script.sh cat file1.txt file2.txt > combined.txt