Basic navigation
pwd— print the current working directory.cd— change directory (e.g.,cd /var/logorcd ..to go up one level).ls— list directory contents.
ls options:
-a— include hidden files (names starting with a dot).-l— long listing format (shows permissions, ownership, size, timestamp).-h— human-readable sizes (e.g.,-lh).
ls -alh to see detailed listings including hidden files with readable sizes.
Copying, moving, and deleting
cp— copy files and directories. Use-r(or-a) to copy directories recursively.mv— move or rename files and directories.rm— remove files. Use-rto remove directories and their contents recursively; add-fto force.rmdir— remove empty directories only.
Be careful with
rm -r (or rm -rf). It permanently deletes directories and their contents if used without caution.Safer deletion tips:
- Use
rm -ito prompt before each removal. - Run
ls -lafirst to verify the files you plan to delete. - Consider moving important data to a temporary backup directory before deletion, e.g.,
mv important/ ~/trash/.
Creating and updating files and directories
mkdir— create a directory. Use-pto create parent directories as needed (e.g.,mkdir -p projects/data/reports).rmdir— remove an empty directory.touch— update access and modification times; if the file does not exist,touchcreates an empty file.
Viewing file contents
cat— concatenate and display files (prints entire contents).head— display the first 10 lines of a file by default; use-nto change the number of lines displayed (e.g.,head -n 5 file.txt).tail— display the last 10 lines by default; use-nto change the number of lines (e.g.,tail -n 50 /var/log/messages).more/less— view file contents one page at a time (lessis generally preferred for navigation).
Commands at a glance
Hands-on demo
Below is a step-by-step demo transcript showing common file-management workflows: creating directories, creating files, copying/moving files, and removing files and directories safely.- Note: the leading
dindrwxr-xr-xindicates a directory.
echo ... > filenamewrites a line to a file (overwrites if the file exists).catprints the entire file;headprints the first lines;tailprints the last lines.
data directory:
rmdir:
rmdir fails because data contains files and/or subdirectories. Remove a non-empty directory with rm -r:
Links and references
- Red Hat Enterprise Linux Documentation
- Linux coreutils — cp, mv, rm, ls, mkdir, touch
- The Linux Command Line (book)