Cheatsheets:LinuxCLI/FileOps: Difference between revisions

From Wikibase
Jump to navigation Jump to search
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"
! Command !! Shows
! Option !! Effect
|-
| <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>-l</syntaxhighlight> || long format: permissions, owner, size, date
|-
|-
| <syntaxhighlight lang="bash" inline>ls -a</syntaxhighlight> || hidden files (dotfiles)
| <syntaxhighlight lang="bash" inline>-a</syntaxhighlight> || hidden files (dotfiles)
|-
|-
| <syntaxhighlight lang="bash" inline>ls -h</syntaxhighlight> || human-readable sizes (combine with -l)
| <syntaxhighlight lang="bash" inline>-h</syntaxhighlight> || human-readable sizes (combine with -l)
|-
|-
| <syntaxhighlight lang="bash" inline>ls -R</syntaxhighlight> || recurse into subdirectories
| <syntaxhighlight lang="bash" inline>-R</syntaxhighlight> || recurse into subdirectories
|-
|-
| <syntaxhighlight lang="bash" inline>ls -t</syntaxhighlight> || sort by modification time, newest first
| <syntaxhighlight lang="bash" inline>-t</syntaxhighlight> || sort by modification time, newest first
|-
| <syntaxhighlight lang="bash" inline>ls *.txt</syntaxhighlight> || glob: only matching names
|}
|}


Line 51: Line 52:


<syntaxhighlight lang="bash" copy>
<syntaxhighlight lang="bash" copy>
mkdir newdir               # create a directory
mkdir newdir                     # create a directory
mkdir -p dir/subdir/newsubsubdir             # create parents as needed
mkdir -p dir/subdir/newsubsubdir # create parents as needed
mkdir -m 755 dir           # set permissions at creation
mkdir -m 755 dir                 # set permissions at creation
rmdir emptydir             # remove an EMPTY directory
rmdir emptydir                   # remove an EMPTY directory
rmdir -p dir/subdir/emptysubdir             # ... and its empty parents
rmdir -p dir/subdir/emptysubdir   # ... and its empty parents
touch file.txt             # create an empty file (or refresh its mtime)
touch file.txt                   # create an empty file (or refresh its mtime)
touch -t 202501011200 f   # set a specific timestamp
touch -t 202501011200 f           # set a specific timestamp
rm file.txt               # delete a file
rm file.txt                       # delete a file
rm -r dir/                 # delete a directory and its contents
rm -r dir/                       # delete a directory and its contents
rm -f file                 # never prompt, ignore missing files
rm -f file                       # never prompt, ignore missing files
rm -i file                 # prompt before each delete
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"
! Command !! Does
! Option !! Effect
|-
|-
| <syntaxhighlight lang="bash" inline>grep pattern file</syntaxhighlight> || print matching lines
| <syntaxhighlight lang="bash" inline>-i</syntaxhighlight> || case-insensitive
|-
|-
| <syntaxhighlight lang="bash" inline>grep -i pattern file</syntaxhighlight> || case-insensitive
| <syntaxhighlight lang="bash" inline>-n</syntaxhighlight> || include line numbers
|-
|-
| <syntaxhighlight lang="bash" inline>grep -n pattern file</syntaxhighlight> || include line numbers
| <syntaxhighlight lang="bash" inline>-v</syntaxhighlight> || invert: lines that do NOT match
|-
|-
| <syntaxhighlight lang="bash" inline>grep -v pattern file</syntaxhighlight> || invert: lines that do NOT match
| <syntaxhighlight lang="bash" inline>-r</syntaxhighlight> || search a directory recursively
|-
|-
| <syntaxhighlight lang="bash" inline>grep -r pattern dir/</syntaxhighlight> || search a directory recursively
| <syntaxhighlight lang="bash" inline>-l</syntaxhighlight> || only filenames with matches
|-
|-
| <syntaxhighlight lang="bash" inline>grep -l pattern dir/*</syntaxhighlight> || only filenames with matches
| <syntaxhighlight lang="bash" inline>-c</syntaxhighlight> || count matching lines
|-
|-
| <syntaxhighlight lang="bash" inline>grep -c pattern file</syntaxhighlight> || count matching lines
| <syntaxhighlight lang="bash" inline>-E</syntaxhighlight> || extended regex, e.g. <syntaxhighlight lang="bash" inline>grep -E 'a|b' file</syntaxhighlight>
|-
| <syntaxhighlight lang="bash" inline>grep -E 'a|b' file</syntaxhighlight> || extended regex
|}
|}


Line 121: Line 124:


<syntaxhighlight lang="bash" copy>
<syntaxhighlight lang="bash" copy>
find . -name "*.py"           # by name (glob)
find . -name "*.py"                 # by name (glob)
find /home -type f           # only files
find /home -type f                   # only files
find /home -type d           # only directories
find /home -type d                   # only directories
find . -mtime -7             # modified in the last 7 days
find . -mtime -7                     # modified in the last 7 days
find . -size +100M           # larger than 100 MB
find . -size +100M                   # larger than 100 MB
find . -name "*.log" -delete # delete matches
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''' — 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.
'''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">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.
'''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>=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:
'''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

Languages: English · français · Esperanto

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
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 -rf is irreversible — there is no trash bin. Double-check the path (and your current directory with pwd) 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)

locate searches an index refreshed by updatedb — files created since the last update are invisible to it. find is 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 (d directory, - file, l symlink) plus three triplets: owner, group, others. So drwxr-xr-x is 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
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 -l shows 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