guest@linuxthings:~/guides$ man file-ops
Basic File Operations
Copying (cp)
cp source dest
cp file1 file2 dir/ # Copy multiple files to directory
cp -r dir1 dir2 # Recursively copy directory
cp -i file1 file2 # Interactive (ask before overwrite)
cp -p file1 file2 # Preserve permissions and timestamps
cp -a dir1 dir2 # Archive mode (recursive + preserve all attributes)
cp -u src/* dest/ # Copy only files that are newer than the destination
cp -v file dest/ # Verbose: print each file as it is copiedcp -r copies contents but not attributes such as timestamps and ownership. Use cp -a when you want a faithful copy, and rsync for large or repeated copies.
Moving/Renaming (mv)
mv oldname newname # Rename
mv file1 file2 destination/ # Move files
mv -i file dest/ # Ask before overwriting an existing file
mv -n file dest/ # Never overwrite an existing fileRemoving (rm)
Deleted files are generally not recoverable in Linux. Use with caution.
rm file
rm -i file* # Ask before deleting each file
rm -r dir # Recursive delete (for directories)
rm -f file # Force (no confirmation, ignore missing files)
rm -rf dir # Force recursive delete (DANGEROUS)
rm -- -oddname # Delete a file whose name starts with a dashBefore a destructive rm with a wildcard, run ls with the same pattern to see exactly what will match. Be especially careful combining sudo with rm -r.
Creation
mkdir dirname
mkdir -p a/b/c # Create parent directories as needed
mkdir -m 700 private # Create with specific permissions
touch filename # Create empty file or update timestamp of existing file
> emptyfile # Create (or truncate) a file with redirection
echo "text" > file # Create a file with contentRemoving Directories
rmdir emptydir # Remove an empty directory
rmdir -p a/b/c # Remove c, then b, then a (all must be empty)
rm -r fulldir # Remove a directory and everything in itLinks (ln)
ln -s target linkname # Create symbolic (soft) link (Recommended)
ln target linkname # Create hard link
readlink linkname # Show where a symbolic link points
readlink -f linkname # Follow a chain of links to the final target
ls -l linkname # Also shows: linkname -> targetHard link | Symbolic link | |
|---|---|---|
Points to | The file's data (inode) | The file's path |
Original deleted or renamed | Link keeps working | Link breaks ("dangling") |
Across filesystems | No | Yes |
To directories | No | Yes |
Path Helpers
basename /home/smith/report.txt # report.txt
basename /home/smith/report.txt .txt # report
dirname /home/smith/report.txt # /home/smithComparing Files
diff file1 file2 # Show line-by-line differences
diff -u old new # Unified format (the style git uses)
diff -r dir1 dir2 # Compare two directory trees
cmp file1 file2 # Find the first differing byte (works on binaries)
comm file1 file2 # Lines unique to each sorted file, and common lines
sha256sum file # Checksum to verify a file is unchanged
sha256sum -c sums.txt # Verify files against a list of checksumsPrefer SHA-256 checksums over MD5 or SHA-1, which are no longer considered secure.