Master Essential Linux Commands for Files and Directories
This guide explains Linux directory structures, absolute and relative paths, and provides detailed usage, options, and examples for common commands such as ls, cd, pwd, mkdir, rmdir, cp, rm, mv, and file‑viewing tools like cat, tac, nl, more, less, head, and tail.
Linux uses a hierarchical directory tree with the root "/" at the top; other directories are attached via mounting and can be detached by unmounting. Understanding absolute paths (starting with "/") and relative paths (based on the current directory) is essential before using file‑system commands.
Common Directory Commands
ls : list directory contents. Example: ls -al ~ shows all files, including hidden ones, with detailed attributes.
cd : change the current directory. Syntax: cd [absolute|relative path]. Examples demonstrate using absolute paths, relative paths, cd ~ to go home, and cd .. to move up one level.
pwd : print the working directory. Use pwd -P to resolve symbolic links to the real path.
mkdir : create directories. Syntax: mkdir [-mp] directory. Options: -m sets permissions, -p creates parent directories as needed. Example shows creating nested directories with mkdir -p test1/test2/test3/test4 and setting mode 711 to obtain drwx--x--x.
rmdir : remove empty directories. Syntax: rmdir [-p] directory. The -p option removes parent directories if they become empty.
File Copy, Move, and Removal
cp : copy files or directories. Syntax: cp [-adfilprsu] source destination. Options include -a (archive), -r (recursive), -p (preserve attributes), -i (interactive), etc. Example copies .bashrc to /tmp/bashrc with and without prompting.
rm : remove files or directories. Syntax: rm [-fir] target. Options: -f (force), -i (interactive), -r (recursive, dangerous for directories).
mv : move or rename files/directories. Syntax: mv [-fiu] source destination. Demonstrates moving a copied bashrc into a new directory and renaming the directory.
Viewing File Contents
cat : display file from the beginning. Options include -A, -b, -E, -n, -T, -v. Example shows cat /etc/issue.
tac : display file in reverse order (last line first). Example: tac /etc/issue.
nl : number lines with various formatting options ( -b, -n, -w). Example: nl /etc/issue.
more and less : paginate output. Both support navigation keys; less also allows backward scrolling and searching.
head : show the first N lines (default 10). Use head -n 20 file for 20 lines.
tail : show the last N lines (default 10) and can follow a file with -f. Example: tail -n 20 /etc/man.config.
All commands can be explored further with man [command] for detailed documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
