Cheatsheets:LinuxCLI/FileOps: Difference between revisions
Apply review suggestions: ls/grep/tar option tables, align creating/removing snippet, find -exec comment, rewrite chmod intro. AI-assisted (RonzzWikiCowriter). (via update-page on MediaWiki MCP Server) |
|||
| Line 29: | Line 29: | ||
'''ls''' — the workhorse: | '''ls''' — the workhorse: | ||
<syntaxhighlight lang="bash" copy> | |||
ls [options] [dir] # list directory contents | |||
ls *.txt # glob: only matching names | |||
</syntaxhighlight> | |||
{| class="wikitable" | {| class="wikitable" | ||
! | ! Option !! Effect | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-l</syntaxhighlight> || long format: permissions, owner, size, date | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-a</syntaxhighlight> || hidden files (dotfiles) | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-h</syntaxhighlight> || human-readable sizes (combine with -l) | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-R</syntaxhighlight> || recurse into subdirectories | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-t</syntaxhighlight> || sort by modification time, newest first | ||
|} | |} | ||
| Line 51: | Line 52: | ||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
mkdir newdir | mkdir newdir # create a directory | ||
mkdir -p dir/subdir/newsubsubdir | mkdir -p dir/subdir/newsubsubdir # create parents as needed | ||
mkdir -m 755 dir | mkdir -m 755 dir # set permissions at creation | ||
rmdir emptydir | rmdir emptydir # remove an EMPTY directory | ||
rmdir -p dir/subdir/emptysubdir | rmdir -p dir/subdir/emptysubdir # ... and its empty parents | ||
touch file.txt | touch file.txt # create an empty file (or refresh its mtime) | ||
touch -t 202501011200 f | touch -t 202501011200 f # set a specific timestamp | ||
rm file.txt | rm file.txt # delete a file | ||
rm -r dir/ | rm -r dir/ # delete a directory and its contents | ||
rm -f file | rm -f file # never prompt, ignore missing files | ||
rm -i file | rm -i file # prompt before each delete | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 97: | Line 98: | ||
'''grep''' — search inside files: | '''grep''' — search inside files: | ||
<syntaxhighlight lang="bash" copy> | |||
grep [options] pattern file # print matching lines | |||
</syntaxhighlight> | |||
{| class="wikitable" | {| class="wikitable" | ||
! | ! Option !! Effect | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-i</syntaxhighlight> || case-insensitive | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-n</syntaxhighlight> || include line numbers | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-v</syntaxhighlight> || invert: lines that do NOT match | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-r</syntaxhighlight> || search a directory recursively | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-l</syntaxhighlight> || only filenames with matches | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-c</syntaxhighlight> || count matching lines | ||
|- | |- | ||
| <syntaxhighlight lang="bash" inline> | | <syntaxhighlight lang="bash" inline>-E</syntaxhighlight> || extended regex, e.g. <syntaxhighlight lang="bash" inline>grep -E 'a|b' file</syntaxhighlight> | ||
|} | |} | ||
| Line 121: | Line 124: | ||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
find . -name "*.py" | find . -name "*.py" # by name (glob) | ||
find /home -type f | find /home -type f # only files | ||
find /home -type d | find /home -type d # only directories | ||
find . -mtime -7 | find . -mtime -7 # modified in the last 7 days | ||
find . -size +100M | find . -size +100M # larger than 100 MB | ||
find . -name "*.log" -delete | find . -name "*.log" -delete # delete matches | ||
find . -name "*.tmp" -exec rm {} \; | find . -name "*.tmp" -exec rm {} \; # run a command on each match | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 144: | Line 147: | ||
== Permissions & ownership == | == Permissions & ownership == | ||
'''chmod''' — | '''chmod''' — change permissions. Two notations: | ||
* '''Numeric (octal):''' three digits for owner, group, others; each digit is the sum of its permission values (r=4, w=2, x=1), e.g. <syntaxhighlight lang="bash" inline>755</syntaxhighlight> = rwx for owner, r-x for group and others. | |||
* '''Symbolic:''' pick who — <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) — then how — <syntaxhighlight lang="bash" inline>+</syntaxhighlight> add, <syntaxhighlight lang="bash" inline>-</syntaxhighlight> remove, <syntaxhighlight lang="bash" inline>=</syntaxhighlight> set exactly. | |||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
| Line 172: | Line 178: | ||
<blockquote> | <blockquote> | ||
'''Reading <syntaxhighlight lang="bash" inline | '''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> | </blockquote> | ||
| Line 199: | Line 205: | ||
== Archives == | == Archives == | ||
'''tar''' — <syntaxhighlight lang="bash" inline>c</syntaxhighlight> | '''tar''' — the options, then examples: | ||
{| class="wikitable" | |||
! Option !! Effect | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-c</syntaxhighlight> || create an archive | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-x</syntaxhighlight> || extract | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-t</syntaxhighlight> || list contents | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-f</syntaxhighlight> || the archive file name — always followed by the archive | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-z</syntaxhighlight> || gzip compression → <syntaxhighlight lang="text" inline>.tar.gz</syntaxhighlight> | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-j</syntaxhighlight> || bzip2 compression → <syntaxhighlight lang="text" inline>.tar.bz2</syntaxhighlight> | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-v</syntaxhighlight> || verbose: list each file as it is processed | |||
|- | |||
| <syntaxhighlight lang="bash" inline>-C</syntaxhighlight> || change to a directory before extracting | |||
|} | |||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
Revision as of 11:48, 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:
ls [options] [dir] # list directory contents
ls *.txt # glob: only matching names
| Option | Effect |
|---|---|
-l |
long format: permissions, owner, size, date |
-a |
hidden files (dotfiles) |
-h |
human-readable sizes (combine with -l) |
-R |
recurse into subdirectories |
-t |
sort by modification time, newest first |
Creating & removing
mkdir newdir # create a directory
mkdir -p dir/subdir/newsubsubdir # create parents as needed
mkdir -m 755 dir # set permissions at creation
rmdir emptydir # remove an EMPTY directory
rmdir -p dir/subdir/emptysubdir # ... 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,f/b move forward/backward one page, q quit
head -n 5 file.txt # show first 5 lines of a file. If no -n parsed, default is 10
tail -n 50 app.log # show last 50 lines of a file. If no -n parsed, default is 10
tail -f app.log # follow a growing log, Ctrl+C to stop
Searching
grep — search inside files:
grep [options] pattern file # print matching lines
| Option | Effect |
|---|---|
-i |
case-insensitive |
-n |
include line numbers |
-v |
invert: lines that do NOT match |
-r |
search a directory recursively |
-l |
only filenames with matches |
-c |
count matching lines |
-E |
extended regex, e.g. grep -E 'a|b' file
|
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 {} \; # run a command on each match
locate — faster index-based search:
locate file.txt # instant filename lookup
locate folder/subfolder # instant partial path lookup
sudo updatedb # refresh the index (requires root permission)
locatesearches an index refreshed byupdatedb— files created since the last update are invisible to it.findis slower but always up to date.
Permissions & ownership
chmod — change permissions. Two notations:
- Numeric (octal): three digits for owner, group, others; each digit is the sum of its permission values (r=4, w=2, x=1), e.g.
755= rwx for owner, r-x for group and others. - Symbolic: pick who —
u(owner),g(group),o(others),a(all) — then how —+add,-remove,=set exactly.
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 . # ... only for 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
Hard links cannot span filesystems or point at directories.
ls -lshows a symlink's target with->.
Archives
tar — the options, then examples:
| Option | Effect |
|---|---|
-c |
create an archive |
-x |
extract |
-t |
list contents |
-f |
the archive file name — always followed by the archive |
-z |
gzip compression → .tar.gz
|
-j |
bzip2 compression → .tar.bz2
|
-v |
verbose: list each file as it is processed |
-C |
change to a directory before extracting |
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