Linux: Directories, Files, and File Contents
Basics
Text editors:
viVim: an improved version ofvi(vi and improved). It has 2 modes: execute mode and insert mode.
The i key enables the insert mode.
ESC to exit the insert mode.
: to enter execute mode
:qto quit Vim
:wq to save and quit
:qa to quit without saving
/ to search for text
dd to delete a line
You don't have to be in insert mode to cut and paste texts.
EmacsgVim: a graphical version ofVimgedit: GUI based editor used in the GNOME desktop environmentGNU nano: a simple text editor.
CTRL+k to delete an entire lie
CTRL+O to save file
CTRL+x to close a file
Searching for Files
file
Very simple tool for finding file data type.
file backup1
stat
statprovides detailed information about a file or directory.
stat myfile
locate
locate performs a quick search for any specified file names and paths stored in the mlocate database. To use this command type locate [options] <string>. The following options are supported:
-rsearch for file names using regular expressions-cdisplay the n umber of matching entries found-ereturns only files that exist at the time of the search-lIgnore the casing in the names or paths-n <number of entries>return the first few matches up to the specified number
locate backup to locate anything that contains backup in the name
locate -i backup to make a case insensitive search
locate -ic backup to return only the number of entries found
updatedb is used to build a database of files based on the /etc/updatedb.conf file to update the /var/lib/mlocate/mlocate.db database file. /etc/updatedb.conf contains path that should be excluded. PRUNEPATH is used to specify a path that need to not be included while building the database (ex: PRUNEPATH="/etc"). locate may provide faster result because it is searching from a database. It might also provide inaccurate result because the database can be outdated
find
find is also used to search specific location for files and directories that adhere to search criteria. The command looks like find [options] <search locations> <search criteria> [actions]. Ex: find /home/user -type f -name myfile.txt to search in /home/user files with names matching myfile.txt. find command performs live search of the file system and in a specific location.
find / -type d -name 'log' to search from the root directory any directory that has log in its name.
find / -type f -name 'messages to look for files from the root directory when messages in their names.
find /var/log -type f -size +100k to search for files /var/logs with at lease 100kb of size.
find /var/log -type f -mmin -45 to look for files that haven't been updated for the past 45 minutes
find /var/log -type f -size 0 -or -size +100k -and -mmin -45 to locate files with size 0 or greater than 100k and updated within the last 45 minutes
which
which displays the complete path of a specified command by searching the directories assigned to the PATH variable. ex: which ssh
whereis
whereis is used to display various details associated with a command. ex: whereis ls. The proper way to run this is whereis [options] [directory name] <filename>
Viewing and Searching for content
head to see the beginning of a file. By default it display the fist 10 lines of a file. head [options] <filename>. use -n to specify the number of line to be shown. Ex: head -n 50 /etc/apache2/httpd.conf
tail to see the end of a file. it is useful for monitoring log files. It also displays the last 10 lines of a file. tail [options] <filename>. ex: tail -n 20 /var/log/boot.log. -f allows following changes made to the file.
more to view large files by page. more [options] <filename>. It is often used with a pipe operator to view output of another command. Ex: cat /var/log/syslog | more
less to view large files by page. It is an ehancement of more command with advanced navigation. less [options] <filename>. ex: cat /var/log/syslog | less
awk (Aho, Weinberger, and Kernighan) is a language for pattern matching in structured data.
grep(Global Regular Expression Print) is used to search contents for lines that match a given pattern. grep is a case sensitive command.
cut is used to extract specific sections of a file. It is efficient for column based data.
Counting and Sorting data
wc (word count) counts lines, words, and characters in a text. wc /var/log/error.log
sort sorts data in logical order. sort [options] file. Ex: sort -h prices.txt to sort numbers.
uniq filter out data for duplicates. It is usually combined with sort and | to provide a powerful functionality. ex: sort /var/log/auth.log | uniq -c. Or simply uniq input.txt
xargs (extended arguments) constructs and execute command line from standard input. ex: echo "file1.txt" | xargs cat,
Comparing files
diff
diff shows the differences between two files line by line. diff [options] file1 file2
-hignores changes in the amount of whitespace-wignores all whitespace differences-imakes the comparison case-insentives-tpreserves tab characters-c,-ushow differences in readable format for collaborative environments
ex: diff -i -u config_old.conf config_new.conf
sdiff
sdiff displays the differences side by side. sdiff [options] file1 file2
ex: sdiff old.txt new.txt
lsof List Open File
lsof show which files are currently open and which processes are using them. lsof [options] [target]
ex: lsof /var/log/syslog
File Links
Linux hard links points to only one file.
A symbolic link is like a shortcut to a file.
Use ln command to create hard and symbolic links. ln [options] <source> <targe>
ln original.conf hardlink.conf to create a hardlink
ln -s original.conf softlink.conf to create a soft or symbolic link
/dev directory
/dev stores special files that represent the file system's hardware and virtual devices.
Block Devices
Represents hardware devices that handles data in fixed-sized blocks such as hard drives, SSDs, and USB flash drives.
Character Devices
Represents devices that handle data continuously one character at at time such as serial ports, keyboards, and terminals
Special Character Devices
Help the system with tasks: /dev/null, /dev/zero, and /dev/urandom