Cheatsheets:LinuxCLI/FileOps: Difference between revisions
| Line 194: | Line 194: | ||
<blockquote> | <blockquote> | ||
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> | </blockquote> | ||
Revision as of 11:45, 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 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:
| 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 — 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 — 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 . # ... 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 — 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