SyntaxStudy
Sign Up
Linux / Bash Creating, Copying, and Moving Files
Linux / Bash Beginner 1 min read

Creating, Copying, and Moving Files

File management is a core daily task in Linux administration. The `touch` command creates empty files or updates the access and modification timestamps of existing ones. `mkdir` creates directories, and with the `-p` flag it creates entire nested path hierarchies without errors if intermediate directories already exist. The `cp` command copies files and directories. By default it copies a single file, but with `-r` (recursive) it copies entire directory trees. The `-p` flag preserves metadata like timestamps and permissions. `mv` moves or renames files and directories. Since moving within the same filesystem is just a metadata operation (renaming an inode reference), it is instantaneous regardless of file size. Careful use of `rm` is critical because Linux has no recycle bin by default — deleted files are gone immediately. The `-r` flag removes directories recursively, and `-f` suppresses confirmation prompts. Always double-check paths before using `rm -rf`. Consider aliasing `rm` to `rm -i` in interactive sessions to force confirmation prompts.
Example
# Create an empty file
touch notes.txt

# Create multiple files at once
touch file1.txt file2.txt file3.txt

# Update timestamp of an existing file
touch -t 202401151200 notes.txt

# Create a directory
mkdir projects

# Create nested directories in one command
mkdir -p projects/webapp/src/components

# Copy a file
cp notes.txt notes_backup.txt

# Copy a file to a directory
cp notes.txt /tmp/

# Copy a directory recursively
cp -r projects/ projects_backup/

# Copy preserving metadata (permissions, timestamps)
cp -rp projects/ /backup/projects/

# Move (rename) a file
mv notes.txt notes_v2.txt

# Move a file to another directory
mv notes_v2.txt /tmp/

# Move multiple files into a directory
mv file1.txt file2.txt file3.txt /tmp/

# Remove a file
rm file1.txt

# Remove a file with confirmation
rm -i file2.txt

# Remove a directory and all contents
rm -rf projects_backup/

# Remove empty directory
rmdir empty_dir/