File and Directory Commands
File and directory operations make up the largest share of everyday command-line use. This section covers the core usage of ls, cd, mkdir, cp, mv, rm, and find — all included by default on Debian-family distributions, with nothing extra to install.
Listing directory contents: ls
Section titled “Listing directory contents: ls”ls # list the current directory's contentsls -l # detailed info: permissions, owner, size, modified timels -a # include hidden files (those starting with .)ls -lh # detailed info, with sizes in human-readable units (K/M/G)ls -lt # sort by modified time, newest firstFlags like -l, -a, and -h can be combined — ls -lah, for example, is equivalent to enabling all three at once.
Changing directories: cd
Section titled “Changing directories: cd”cd /path/to/dir # switch to an absolute pathcd relative/dir # switch to a path relative to the current directorycd ~ # switch to the current user's home directorycd - # switch back to the previous directorycd .. # switch to the parent directoryCreating directories: mkdir
Section titled “Creating directories: mkdir”mkdir new-dir # create a single directorymkdir -p a/b/c # create nested directories, creating parents as neededWithout -p, the command fails if any parent directory (a, a/b in the example) doesn’t already exist.
Copying files and directories: cp
Section titled “Copying files and directories: cp”cp source.txt target.txt # copy a filecp -r source-dir target-dir # recursively copy an entire directorycp -i source.txt target.txt # prompt before overwriting an existing targetCopying a directory requires -r (recursive) — without it, the command fails with an error saying the target is a directory.
Moving and renaming: mv
Section titled “Moving and renaming: mv”mv old-name.txt new-name.txt # rename (moving within the same directory is equivalent to renaming)mv file.txt /path/to/dir/ # move a file into a target directoryThere’s no separate rename command — renaming is, under the hood, just “moving to a new filename in the same directory.”
Deleting files and directories: rm
Section titled “Deleting files and directories: rm”rm file.txt # delete a filerm -r dir/ # recursively delete a directory and its contentsrm -rf dir/ # recursive delete without confirmation prompts (force)Locating files: find
Section titled “Locating files: find”find . -name "*.txt" # search by filename in the current directory and subdirectoriesfind /path -type d # match only directories (-type f matches only files)find . -mtime -1 # find files modified within the last dayfind . -size +10M # find files larger than 10MBfind supports combining criteria across name, type, size, and modified time — the overall structure of the command is “where to search + what to filter by,” and multiple filters can be stacked together. By comparison, the locate command is faster, but relies on a pre-built index database and won’t reflect files created after the index was last updated. The two suit different situations: find is the better fit whenever precise, real-time results matter.