File Management

File Management

These commands help you create, modify, move, and delete files and directories on your Linux system.

cat

Concatenates and displays file contents.

cat [options] [file(s)]

Options:

Examples:

# Display file contents
cat file.txt

# Display file with line numbers
cat -n file.txt

# Concatenate multiple files
cat file1.txt file2.txt > combined.txt

touch

Creates a new empty file or updates the timestamp of an existing file.

touch [options] file(s)

Options:

Examples:

# Create a new empty file
touch newfile.txt

# Update timestamp without creating if file doesn't exist
touch -c existing.txt

# Set specific timestamp
touch -t 202504101200 file.txt

mkdir

Creates directories.

mkdir [options] directory(s)

Options:

Examples:

# Create a directory
mkdir data

# Create nested directories
mkdir -p projects/web/css

# Create directory with specific permissions
mkdir -m 755 scripts

cp

Copies files and directories.

cp [options] source destination

Options:

Examples:

# Copy a file
cp file.txt backup/

# Copy a directory and its contents
cp -r directory/ backup/

# Copy with confirmation before overwriting
cp -i important.txt backup/

# Copy preserving attributes
cp -p config.txt /etc/

mv

Moves or renames files and directories.

mv [options] source destination

Options:

Examples:

# Rename a file
mv oldname.txt newname.txt

# Move a file to another directory
mv file.txt /home/user/documents/

# Move with confirmation before overwriting
mv -i important.txt /backup/

rm

Removes files or directories.

rm [options] file(s)

Options:

Examples:

# Remove a file
rm file.txt

# Remove a directory and its contents
rm -r directory/

# Remove with confirmation
rm -i important.txt

# Force remove without warning
rm -f unwanted.txt

rmdir

Removes empty directories.

rmdir [options] directory(s)

Options:

Examples:

# Remove an empty directory
rmdir emptydir/

# Remove nested empty directories
rmdir -p parent/child/grandchild/

ln

Creates links between files.

ln [options] target link_name

Options:

Examples:

# Create a hard link
ln target.txt link.txt

# Create a symbolic link
ln -s /path/to/target symlink

# Create a symbolic link to a directory
ln -s /var/www/html webroot

chmod

Changes file permissions.

chmod [options] mode file(s)

Modes:

Common permissions:

Examples:

# Make a file executable by owner
chmod u+x script.sh

# Set read/write permissions for owner, read for others
chmod 644 file.txt

# Apply permissions recursively to a directory
chmod -R 755 directory/

chown

Changes file owner and group.

chown [options] owner[:group] file(s)

Options:

Examples:

# Change owner of a file
sudo chown user1 file.txt

# Change owner and group
sudo chown user1:group1 file.txt

# Change ownership recursively
sudo chown -R www-data:www-data /var/www/

diff

Compares files line by line.

diff [options] file1 file2

Options:

Examples:

# Compare two files
diff file1.txt file2.txt

# Side-by-side comparison
diff -y file1.txt file2.txt

# Unified format (for patches)
diff -u original.txt modified.txt > changes.patch

file

Determines file type.

file [options] file(s)

Options:

Examples:

# Check file type
file document.pdf

# Show MIME type
file -i image.jpg

du

Estimates file space usage.

du [options] [file(s)]

Options:

Examples:

# Show directory sizes in human-readable format
du -h /home/user

# Show total size of a directory
du -sh /var/log

# Show sizes of all files and directories
du -ah /etc

gzip/gunzip

Compresses or decompresses files.

gzip [options] file(s)
gunzip [options] file(s).gz

Options (gzip):

Examples:

# Compress a file
gzip large_file.txt

# Compress with maximum compression
gzip -9 large_file.txt

# Compress while keeping the original
gzip -k large_file.txt

# Decompress a file
gunzip file.txt.gz

tar

Archives files.

tar [options] [archive-file] [file(s)]

Options:

Examples:

# Create a tar archive
tar -cf archive.tar file1 file2

# Create a compressed tar archive (tarball)
tar -czf archive.tar.gz directory/

# Extract a tar archive
tar -xf archive.tar

# Extract a compressed tar archive
tar -xzf archive.tar.gz

# List contents of an archive
tar -tf archive.tar

zip/unzip

Creates or extracts ZIP archives.

zip [options] zipfile file(s)
unzip [options] zipfile

Options (zip):

Options (unzip):

Examples:

# Create a zip archive
zip archive.zip file1 file2

# Create a zip archive with a directory
zip -r archive.zip directory/

# Extract a zip archive
unzip archive.zip

# List contents of a zip archive
unzip -l archive.zip

fzf

A command-line fuzzy finder for files, commands, and more.

fzf [options]

Options:

Examples:

# Find a file interactively
vim $(fzf)

# Search with preview
fzf --preview 'cat {}'

# Use with history search
history | fzf

sha256sum

Compute and check SHA256 message digest.

sha256sum [options] [file(s)]

Options:

Examples:

# Generate checksum for a file
sha256sum file.iso > file.iso.sha256

# Verify checksum
sha256sum -c file.iso.sha256