Files

Listing files

ls
 
# show hidden files and folders
ls -a
 
# show files in long format
ls -l
 
# human readable file sizes
ls -h
 
# show inode numbers
ls -i
 
# can be used when ls not available
echo *

Create directory

# crashes if the path does not exist
mkdir /path/to/folder
 
# creates path if required for folder to be created
mkdir -p /path/to/folder

Copy Files

cp

  • flags
    • p: preserve
    • r: recursive
    • v: verbose
    • u: update
    • a: archive
# p: preserve metadata including last modified date
# r: recursive
# v: verbose
cp -prv <source> <destination>
 
# i: interactive mode
# ask before overwriting
cp -i file1 file2
 
# copy file1 and file2 into dir
cp file1 file2 dir
 
# copy files from inside dir1 to dir2
cp dir1/* dir2
 
# u: update mode
# When copying files from one directory to another, only copy files that either don't exist or are newer than the existing corresponding files, in the destination directory
cp -u <source> <destination>
 
# a: archive mode
# Copy the files and directories and all of their attributes, including ownerships and permissions
cp -a <source> <destination>

rsync

rsync <options> <source> <destination>

Moving and Renaming files

  • flags
    • i: interactive
    • u: update
    • v: verbose
mv <source> <destination>
 
# rename
mv old_name.txt new_name.txt
 
# move into directory
mv file.txt /target/directory/

Removing files

  • Tip: always use ls to make sure what files are covered by the pattern
  • flags
    • i: interactive mode
    • r: recursive
    • f: force
    • v: verbose
rm <file>
 
# Ask before removing file
rm -i <file>
 
# recursively remove all files from directory
rm -r <directory>
 
# silently force remove all files recursively
# ignores non-existent files
rm -rf <directory>

Create an empty file

  • touch creates an empty file, or update timestamps if file already exists
touch file.txt
 
# update only access time
touch -a file.txt
 
# update only modification time
touch -m file.txt

File metadata

  • stat is used to display detailed inode metadata about a file
> stat file.txt
  File: file.txt
  Size: 6               Blocks: 8          IO Block: 4096   regular file
Device: 254,3   Inode: 262398      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ kartoos)   Gid: ( 1000/ kartoos)
Access: 2026-07-05 23:55:09.342641340 +0530
Modify: 2026-07-08 20:25:22.832577946 +0530
Change: 2026-07-08 20:25:22.832577946 +0530
 Birth: 2026-07-05 23:55:09.338641315 +0530
  • Output includes:
    • Size in bytes
    • Blocks: number of 512-byte blocks allocated; IO Block: filesystem block size in bytes
    • File type: Derived from File Types
    • Device ID: device the file resides on (we can see major,minor from lsblk)
    • Inode ID
    • File Permissions, UID, GID
    • Hard Links count
    • Access time (atime): last time file was read
    • Modify time (mtime): last time file content was changed
    • Change time (ctime): last time inode metadata was changed (permissions, owner, link count, or content)
      • content writes also update the inode because the inode stores mtime, file size, and block pointers - all of which may change on a write
      • ctime is a superset of mtime: every mtime change triggers a ctime change, but not vice versa (e.g. chmod updates ctime only)
    • Birth time (btime): time when the file was originally created on the filesystem

Searching for files

  • locate can be used to search
    • It utilizes a database index which is updated periodically (generally daily)
      • updatedb can be used manually to update db index
    • The files to be searched maybe obsolete
    • It is fast and easy
locate bin/zip
  • find can be used to search for files in a directory tree
    • perform regex and wildcard pattern search
    • can perform tests like:
      • type
      • size
      • last modified
      • permission
      • belonging to user or group
    • can include logical operators for tests like or, and, not etc.
    • can perform actions
      • print (default)
      • delete
      • user defined using
        • -exec <command> {}
        • -ok <command> {}
    • options
      • min/max depth
      • avoid directories mounted on other filesystems
# by name
find /path/to/directory -name "*.txt"
 
# by type (f=file, d=directory, l=symlink)
find /path/to/directory -type f
 
# by size (>10MB)
find /path/to/directory -size +10M
 
# modified in last 7 days
find /path/to/directory -mtime -7
 
# execute command on results
find /path/to/directory -name "*.log" -exec rm {} \;

glob pattern

  • * matches any character including none
  • ? matches single character
  • ** matches all files and subdirectories recursively
  • [characters] matches any character in the set
  • [!characters] or [^characters] matches any character not in the set
  • [[:class:]] matches any character in member of class
    • [:alnum:]: alphanumeric
    • [:alpha:]: alphabets
    • [:digit:]: digits
      • same as [0-9]
    • [:lower:]: lowercase letters
    • [:upper:]: uppercase letters
  • It is highly discouraged to use [a-z] or [A-Z] because of locale issues as well as modern linux does not follow ASCII order of characters
glob patternmatches
*All files
g*starting with g
b*.txttext files starting with b
Data???”Data” followed by 3 characters
[abc]*starting with a or b or c
BACKUP.[0-9][0-9][0-9]”BACKUP.” followed by 3 numbers
[[:upper:]]*beginning with upper case letter
[![:digit:]]*not beginning with number
*[[:lower:]123]ending with lowercase letter or 1 or 2 or 3
project/**/*.logall log files inside project folder
  • aka soft links
  • store reference to another file or directory
  • similar to shortcut
  • breaks if original file is deleted
  • can use relative path or absolute path
  • breaks if the symlink cannot find the relative or absolute path
    • relative path can be useful to refer files inside the project so it does not break on another machine while moving whole project
    • absolute path can be useful if the original file stays permanently at one place, the symlink itself can be moved anywhere in the system and it will still work
ln -s original.txt link.txt
ln -s orig_directory link_directory
 
> rm original.txt
> cat link.txt
cat: link.txt: No such file or directory
  • Creates another filename pointing to the same inode
    • A hard link references a file and cannot reference a directory
      • This is to avoid circular loops
    • A hard link cannot reference a file outside its own filesystem
      • ext4 file cannot hard link to any virtual filesystems like tmpfs/devtmpfs etc.
    • A hard link cannot reference to a file outside the partition
      • because inode pool is different b/w partitions
      • exception is btrfs or zfs which uses unified inode pool
  • File remains accessible even if original filename is deleted
  • Works even if you move the original file or the link to a different place
ln original.txt link.txt
 
# show the same inode for original.txt and link.txt
ls -li *.txt
 
> rm original.txt
> cat link.txt
<contents>

Viewing file contents

  • cat
    • show all contents of file
cat <file>
 
# writing anything on stdin will be repeated to stdout
# to stop, send EOF (Ctrl + D)
> cat
hello world
hello world
  • head
    • outputs top of file
  • tail
    • outputs bottom of file
# first 5 lines, default is 10
head -n 5 <file>
 
# last 5 lines, default is 10
tail -n 5 <file>
 
# keep the file open and stream new lines in real time
tail -f <file>
  • more
    • only moves forward
      • modern version can move backward as well
    • loads entire file before opening
  • less
    • stands for “less is more”
    • improvement over more and has more features
    • can navigate forward and backward
    • loads file in pieces and hence faster
    • man, git log etc. uses it internally
more <file>
less <file>
  • Keyboard shortcuts for less
    • Inspired from vi editor shortcuts
    • Page Down / Space: move one page forward
    • Page Up / b : move one page backward
    • Down Arrow / j: Scroll one line forward
    • Up Arrow / k: Scroll one line backward
    • g: beginning
    • G: end
    • h: Help screen
    • q: Quit
    • Searching
      • /pattern: Search forward
      • ?pattern: Search backward
      • After pressing enter for searching
        • n: next match
        • N: previous match
      • -i: Toggle case sensitive/insensitive

Searching within files

grep

  • Global Regular Expression Print
grep [options] regex [file...]
  • flags
    • i: ignore case
    • v: invert match
    • c: print count of matches
    • l: print name of each file with match
    • L: print name of each file without match
    • h: do not print file names
    • q: silent mode
    • E: extended regex
# pipe output from other program and search
ls -al /usr/bin | grep 'zip'
 
# find "fox" in multiple files
grep 'fox' sample*.txt
 
# find "fox" in multiple files
# do not print file names
grep -h 'fox' sample*.txt