Cheatsheets:LinuxCLI/FileOps: Difference between revisions
Created page with "{{DesktopLinux}}" |
Fill in Linux CLI file operations cheatsheet. AI-assisted (RonzzWikiCowriter). (via update-page on MediaWiki MCP Server) |
||
| Line 1: | Line 1: | ||
{{DesktopLinux}} | {{DesktopLinux}} | ||
The everyday Linux command-line file operations: navigate, create, copy, move, inspect, search, and archive files. | |||
New to the command line? Every command here has a built-in manual — try <syntaxhighlight lang="bash" inline>man ls</syntaxhighlight> or [https://tldr.sh tldr] for short examples; [https://explainshell.com explainshell.com] annotates any command line. | |||
== Basic example: a small file workflow == | |||
<syntaxhighlight lang="bash" line highlight="2,3,5,6" copy> | |||
pwd # where am I? | |||
mkdir -p notes/drafts # create nested directories | |||
cd notes/drafts | |||
touch todo.txt # create an empty file | |||
cp todo.txt todo.bak # copy | |||
mv todo.bak ../todo.md # move + rename in one step | |||
ls -la # inspect the results | |||
head -n 3 todo.txt # peek at the contents | |||
</syntaxhighlight> | |||
== Navigation & listing == | |||
<syntaxhighlight lang="bash" copy> | |||
pwd # print the current directory | |||
cd dir # enter dir | |||
cd .. # go up one level | |||
cd ~ # home directory | |||
cd - # previous directory | |||
</syntaxhighlight> | |||
'''ls''' — the workhorse: | |||
{| class="wikitable" | |||
! Command !! Shows | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls</syntaxhighlight> || names only | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls -l</syntaxhighlight> || long format: permissions, owner, size, date | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls -a</syntaxhighlight> || hidden files (dotfiles) | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls -h</syntaxhighlight> || human-readable sizes (combine with -l) | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls -R</syntaxhighlight> || recurse into subdirectories | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls -t</syntaxhighlight> || sort by modification time, newest first | |||
|- | |||
| <syntaxhighlight lang="bash" inline>ls *.txt</syntaxhighlight> || glob: only matching names | |||
|} | |||
== Creating & removing == | |||
<syntaxhighlight lang="bash" copy> | |||
mkdir newdir # create a directory | |||
mkdir -p a/b/c # create parents as needed | |||
mkdir -m 755 dir # set permissions at creation | |||
rmdir emptydir # remove an EMPTY directory | |||
rmdir -p a/b # ... and its empty parents | |||
touch file.txt # create an empty file (or refresh its mtime) | |||
touch -t 202501011200 f # set a specific timestamp | |||
rm file.txt # delete a file | |||
rm -r dir/ # delete a directory and its contents | |||
rm -f file # never prompt, ignore missing files | |||
rm -i file # prompt before each delete | |||
</syntaxhighlight> | |||
<blockquote> | |||
'''<syntaxhighlight lang="bash" inline>rm -rf</syntaxhighlight> is irreversible''' — there is no trash bin. Double-check the path (and your current directory with <syntaxhighlight lang="bash" inline>pwd</syntaxhighlight>) before deleting recursively. | |||
</blockquote> | |||
== Copying & moving == | |||
<syntaxhighlight lang="bash" copy> | |||
cp file.txt backup.txt # copy a file | |||
cp -r src/ dest/ # copy a directory recursively | |||
cp -a src/ dest/ # recursive + preserve attributes | |||
cp -i file.txt dest/ # prompt before overwriting | |||
cp file1 file2 dest/ # several files into a directory | |||
mv file.txt newname.txt # rename | |||
mv file.txt dir/ # move into a directory | |||
mv -i file.txt dir/ # prompt before overwriting | |||
mv src/ dest/ # move/rename a directory | |||
</syntaxhighlight> | |||
== Viewing files == | |||
<syntaxhighlight lang="bash" copy> | |||
cat file.txt # print the whole file | |||
cat file1 file2 # concatenate, in order | |||
cat -n file.txt # with line numbers | |||
less file.txt # scrollable pager: /search, g/G jump, q quit | |||
more file.txt # simpler pager, space = next page | |||
head file.txt # first 10 lines | |||
head -n 5 file.txt # first 5 lines | |||
tail file.txt # last 10 lines | |||
tail -n 50 app.log # last 50 lines | |||
tail -f app.log # follow a growing log, Ctrl-C to stop | |||
</syntaxhighlight> | |||
== Searching == | |||
'''grep''' — search inside files: | |||
{| class="wikitable" | |||
! Command !! Does | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep pattern file</syntaxhighlight> || print matching lines | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -i pattern file</syntaxhighlight> || case-insensitive | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -n pattern file</syntaxhighlight> || include line numbers | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -v pattern file</syntaxhighlight> || invert: lines that do NOT match | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -r pattern dir/</syntaxhighlight> || search a directory recursively | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -l pattern dir/*</syntaxhighlight> || only filenames with matches | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -c pattern file</syntaxhighlight> || count matching lines | |||
|- | |||
| <syntaxhighlight lang="bash" inline>grep -E 'a|b' file</syntaxhighlight> || extended regex | |||
|} | |||
'''find''' — search by name, type, age, size: | |||
<syntaxhighlight lang="bash" copy> | |||
find . -name "*.py" # by name (glob) | |||
find /home -type f # only files | |||
find /home -type d # only directories | |||
find . -mtime -7 # modified in the last 7 days | |||
find . -size +100M # larger than 100 MB | |||
find . -name "*.log" -delete # delete matches | |||
find . -name "*.tmp" -exec rm {} \; | |||
</syntaxhighlight> | |||
'''locate''' — fast, from an index: | |||
<syntaxhighlight lang="bash" copy> | |||
locate file.txt # instant filename lookup | |||
updatedb # refresh the index (root) | |||
</syntaxhighlight> | |||
<blockquote> | |||
'''<syntaxhighlight lang="bash" inline">locate</syntaxhighlight> searches an index refreshed by <syntaxhighlight lang="bash" inline">updatedb</syntaxhighlight>''' — files created since the last update are invisible to it. <syntaxhighlight lang="bash" inline">find</syntaxhighlight> is always accurate but slower. | |||
</blockquote> | |||
== Permissions & ownership == | |||
'''chmod''' — two notations. Numeric: one digit per owner/group/others, where r=4, w=2, x=1. Symbolic: <syntaxhighlight lang="bash" inline>u</syntaxhighlight>=owner, <syntaxhighlight lang="bash" inline>g</syntaxhighlight>=group, <syntaxhighlight lang="bash" inline>o</syntaxhighlight>=others, <syntaxhighlight lang="bash" inline>a</syntaxhighlight>=all, with <syntaxhighlight lang="bash" inline>+</syntaxhighlight> to add, <syntaxhighlight lang="bash" inline>-</syntaxhighlight> to remove, <syntaxhighlight lang="bash" inline>=</syntaxhighlight> to set. | |||
<syntaxhighlight lang="bash" copy> | |||
chmod 755 script.sh # rwxr-xr-x | |||
chmod 644 file.txt # rw-r--r-- | |||
chmod u+x script.sh # add execute for the owner | |||
chmod -R 755 dir/ # recursive | |||
chown alice file.txt # change owner | |||
chown alice:dev file.txt # owner and group | |||
chown -R alice:dev dir/ | |||
chgrp dev file.txt # change group only | |||
stat file.txt # perms, size, timestamps, inode | |||
file archive.tar # detect the file type | |||
</syntaxhighlight> | |||
{| class="wikitable" | |||
! Digit !! Permissions | |||
|- | |||
| 7 || rwx | |||
|- | |||
| 6 || rw- | |||
|- | |||
| 5 || r-x | |||
|- | |||
| 4 || r-- | |||
|} | |||
<blockquote> | |||
'''Reading <syntaxhighlight lang="bash" inline">ls -l</syntaxhighlight>:''' the first column is 10 characters — one type (<syntaxhighlight lang="text" inline>d</syntaxhighlight> directory, <syntaxhighlight lang="text" inline>-</syntaxhighlight> file, <syntaxhighlight lang="text" inline>l</syntaxhighlight> symlink) plus three triplets: owner, group, others. So <syntaxhighlight lang="text" inline>drwxr-xr-x</syntaxhighlight> is a directory, owner rwx, group r-x, others r-x. | |||
</blockquote> | |||
== Disk usage == | |||
<syntaxhighlight lang="bash" copy> | |||
du -sh dir/ # total size of a directory, human-readable | |||
du -sh * # size of each item | |||
du -h --max-depth=1 dir/ # per subdirectory | |||
df -h # free space per mounted filesystem | |||
df -h . # the filesystem holding the current dir | |||
</syntaxhighlight> | |||
== Links == | |||
<syntaxhighlight lang="bash" copy> | |||
ln target hardlink # hard link: same file, another name | |||
ln -s target symlink # symbolic link: points to a path | |||
ln -s /usr/local/bin/tool ~/bin/tool | |||
</syntaxhighlight> | |||
<blockquote> | |||
'''Prefer relative symlinks''' (<syntaxhighlight lang="bash" inline>ln -s ../target link</syntaxhighlight>) so they survive the tree being moved. Hard links cannot span filesystems or point at directories. <syntaxhighlight lang="bash" inline>ls -l</syntaxhighlight> shows a symlink's target with <syntaxhighlight lang="text" inline>-></syntaxhighlight>. | |||
</blockquote> | |||
== Archives == | |||
'''tar''' — <syntaxhighlight lang="bash" inline>c</syntaxhighlight>=create, <syntaxhighlight lang="bash" inline>x</syntaxhighlight>=extract, <syntaxhighlight lang="bash" inline">t</syntaxhighlight>=list, <syntaxhighlight lang="bash" inline">z</syntaxhighlight>=gzip, <syntaxhighlight lang="bash" inline">j</syntaxhighlight>=bzip2; <syntaxhighlight lang="bash" inline">f</syntaxhighlight> always names the archive: | |||
<syntaxhighlight lang="bash" copy> | |||
tar -czf archive.tar.gz dir/ # create a gzipped archive | |||
tar -xzf archive.tar.gz # extract (compression auto-detected) | |||
tar -tzf archive.tar.gz # list contents | |||
tar -xzf archive.tar.gz -C dest/ # extract into a directory | |||
tar -cjf archive.tar.bz2 dir/ # bzip2 instead of gzip | |||
</syntaxhighlight> | |||
'''zip / unzip:''' | |||
<syntaxhighlight lang="bash" copy> | |||
zip -r archive.zip dir/ # zip a directory recursively | |||
zip archive.zip a.txt b.txt # zip specific files | |||
unzip archive.zip # extract | |||
unzip -l archive.zip # list contents | |||
unzip archive.zip -d dest/ # extract into a directory | |||
unzip -o archive.zip # overwrite without asking | |||
</syntaxhighlight> | |||
== Text utilities == | |||
<syntaxhighlight lang="bash" copy> | |||
sort file.txt # sort lines alphabetically | |||
sort -n file.txt # numeric sort | |||
sort -r file.txt # reverse | |||
sort -u file.txt # unique lines | |||
sort -k2 file.txt # sort by the 2nd field | |||
diff file1 file2 # line-by-line differences | |||
diff -u file1 file2 # unified diff (patch format) | |||
diff -r dir1 dir2 # compare directories | |||
cmp file1 file2 # byte compare: report first difference | |||
echo "hello" # print text | |||
echo -n "no newline" # omit the trailing newline | |||
echo "hi" > file.txt # overwrite a file with output | |||
echo "hi" >> file.txt # append to a file | |||
</syntaxhighlight> | |||
<blockquote> | |||
'''<syntaxhighlight lang="bash" inline>></syntaxhighlight> overwrites, <syntaxhighlight lang="bash" inline>>></syntaxhighlight> appends''' — one character decides whether an existing file survives. | |||
</blockquote> | |||
== Further reading == | |||
* <syntaxhighlight lang="bash" inline>man <command></syntaxhighlight> — the built-in manual, always installed | |||
* [https://www.gnu.org/software/coreutils/manual/ GNU Coreutils manual] — the official docs for most of these commands | |||
* [https://tldr.sh tldr] — community-maintained short examples | |||
* [https://explainshell.com explainshell.com] — annotate any command line | |||
* [https://linuxjourney.com Linux Journey] — beginner-friendly tutorial | |||
Revision as of 11:24, 23 August 2026
Quick reference for smart people — part of our Linux cheatsheets collection.
The everyday Linux command-line file operations: navigate, create, copy, move, inspect, search, and archive files.
New to the command line? Every command here has a built-in manual — try man ls or tldr for short examples; explainshell.com annotates any command line.
Basic example: a small file workflow
pwd # where am I?
mkdir -p notes/drafts # create nested directories
cd notes/drafts
touch todo.txt # create an empty file
cp todo.txt todo.bak # copy
mv todo.bak ../todo.md # move + rename in one step
ls -la # inspect the results
head -n 3 todo.txt # peek at the contents
Navigation & listing
pwd # print the current directory
cd dir # enter dir
cd .. # go up one level
cd ~ # home directory
cd - # previous directory
ls — the workhorse:
| Command | Shows |
|---|---|
ls |
names only |
ls -l |
long format: permissions, owner, size, date |
ls -a |
hidden files (dotfiles) |
ls -h |
human-readable sizes (combine with -l) |
ls -R |
recurse into subdirectories |
ls -t |
sort by modification time, newest first |
ls *.txt |
glob: only matching names |
Creating & removing
mkdir newdir # create a directory
mkdir -p a/b/c # create parents as needed
mkdir -m 755 dir # set permissions at creation
rmdir emptydir # remove an EMPTY directory
rmdir -p a/b # ... and its empty parents
touch file.txt # create an empty file (or refresh its mtime)
touch -t 202501011200 f # set a specific timestamp
rm file.txt # delete a file
rm -r dir/ # delete a directory and its contents
rm -f file # never prompt, ignore missing files
rm -i file # prompt before each delete
rm -rfis irreversible — there is no trash bin. Double-check the path (and your current directory withpwd) before deleting recursively.
Copying & moving
cp file.txt backup.txt # copy a file
cp -r src/ dest/ # copy a directory recursively
cp -a src/ dest/ # recursive + preserve attributes
cp -i file.txt dest/ # prompt before overwriting
cp file1 file2 dest/ # several files into a directory
mv file.txt newname.txt # rename
mv file.txt dir/ # move into a directory
mv -i file.txt dir/ # prompt before overwriting
mv src/ dest/ # move/rename a directory
Viewing files
cat file.txt # print the whole file
cat file1 file2 # concatenate, in order
cat -n file.txt # with line numbers
less file.txt # scrollable pager: /search, g/G jump, q quit
more file.txt # simpler pager, space = next page
head file.txt # first 10 lines
head -n 5 file.txt # first 5 lines
tail file.txt # last 10 lines
tail -n 50 app.log # last 50 lines
tail -f app.log # follow a growing log, Ctrl-C to stop
Searching
grep — search inside files:
| Command | Does |
|---|---|
grep pattern file |
print matching lines |
grep -i pattern file |
case-insensitive |
grep -n pattern file |
include line numbers |
grep -v pattern file |
invert: lines that do NOT match |
grep -r pattern dir/ |
search a directory recursively |
grep -l pattern dir/* |
only filenames with matches |
grep -c pattern file |
count matching lines |
grep -E 'a|b' file |
extended regex |
find — search by name, type, age, size:
find . -name "*.py" # by name (glob)
find /home -type f # only files
find /home -type d # only directories
find . -mtime -7 # modified in the last 7 days
find . -size +100M # larger than 100 MB
find . -name "*.log" -delete # delete matches
find . -name "*.tmp" -exec rm {} \;
locate — fast, from an index:
locate file.txt # instant filename lookup
updatedb # refresh the index (root)
locatesearches an index refreshed by
updatedb— files created since the last update are invisible to it.
findis always accurate but slower.
Permissions & ownership
chmod — two notations. Numeric: one digit per owner/group/others, where r=4, w=2, x=1. Symbolic: u=owner, g=group, o=others, a=all, with + to add, - to remove, = to set.
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod u+x script.sh # add execute for the owner
chmod -R 755 dir/ # recursive
chown alice file.txt # change owner
chown alice:dev file.txt # owner and group
chown -R alice:dev dir/
chgrp dev file.txt # change group only
stat file.txt # perms, size, timestamps, inode
file archive.tar # detect the file type
| Digit | Permissions |
|---|---|
| 7 | rwx |
| 6 | rw- |
| 5 | r-x |
| 4 | r-- |
Reading
ls -l: the first column is 10 characters — one type (
ddirectory,-file,lsymlink) plus three triplets: owner, group, others. Sodrwxr-xr-xis a directory, owner rwx, group r-x, others r-x.
Disk usage
du -sh dir/ # total size of a directory, human-readable
du -sh * # size of each item
du -h --max-depth=1 dir/ # per subdirectory
df -h # free space per mounted filesystem
df -h . # the filesystem holding the current dir
Links
ln target hardlink # hard link: same file, another name
ln -s target symlink # symbolic link: points to a path
ln -s /usr/local/bin/tool ~/bin/tool
Prefer relative symlinks (
ln -s ../target link) so they survive the tree being moved. Hard links cannot span filesystems or point at directories.ls -lshows a symlink's target with->.
Archives
tar — c=create, x=extract,
t
=list,
z
=gzip,
j
=bzip2;
f
always names the archive:
tar -czf archive.tar.gz dir/ # create a gzipped archive
tar -xzf archive.tar.gz # extract (compression auto-detected)
tar -tzf archive.tar.gz # list contents
tar -xzf archive.tar.gz -C dest/ # extract into a directory
tar -cjf archive.tar.bz2 dir/ # bzip2 instead of gzip
zip / unzip:
zip -r archive.zip dir/ # zip a directory recursively
zip archive.zip a.txt b.txt # zip specific files
unzip archive.zip # extract
unzip -l archive.zip # list contents
unzip archive.zip -d dest/ # extract into a directory
unzip -o archive.zip # overwrite without asking
Text utilities
sort file.txt # sort lines alphabetically
sort -n file.txt # numeric sort
sort -r file.txt # reverse
sort -u file.txt # unique lines
sort -k2 file.txt # sort by the 2nd field
diff file1 file2 # line-by-line differences
diff -u file1 file2 # unified diff (patch format)
diff -r dir1 dir2 # compare directories
cmp file1 file2 # byte compare: report first difference
echo "hello" # print text
echo -n "no newline" # omit the trailing newline
echo "hi" > file.txt # overwrite a file with output
echo "hi" >> file.txt # append to a file
>overwrites,>>appends — one character decides whether an existing file survives.
Further reading
man <command>— the built-in manual, always installed- GNU Coreutils manual — the official docs for most of these commands
- tldr — community-maintained short examples
- explainshell.com — annotate any command line
- Linux Journey — beginner-friendly tutorial