ls# show hidden files and foldersls -a# show files in long formatls -l# human readable file sizesls -h# show inode numbersls -i# can be used when ls not availableecho *
Create directory
# crashes if the path does not existmkdir /path/to/folder# creates path if required for folder to be createdmkdir -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: verbosecp -prv <source> <destination># i: interactive mode# ask before overwritingcp -i file1 file2# copy file1 and file2 into dircp file1 file2 dir# copy files from inside dir1 to dir2cp 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 directorycp -u <source> <destination># a: archive mode# Copy the files and directories and all of their attributes, including ownerships and permissionscp -a <source> <destination>
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 namefind /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 daysfind /path/to/directory -mtime -7# execute command on resultsfind /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 pattern
matches
*
All files
g*
starting with g
b*.txt
text 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/**/*.log
all log files inside project folder
Links
Create symbolic links
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.txtln -s orig_directory link_directory> rm original.txt> cat link.txtcat: link.txt: No such file or directory
Create hard links
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.txtls -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)> cathello worldhello world
head
outputs top of file
tail
outputs bottom of file
# first 5 lines, default is 10head -n 5 <file># last 5 lines, default is 10tail -n 5 <file># keep the file open and stream new lines in real timetail -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 searchls -al /usr/bin | grep 'zip'# find "fox" in multiple filesgrep 'fox' sample*.txt# find "fox" in multiple files# do not print file namesgrep -h 'fox' sample*.txt