Essential Commands (25%)
Log into graphical and text mode consoles
ssh user@host
Search for files
- Main commands:
find
Find files with depth 3 and size above 2 mb:
find . -maxdepth 3 -type f -size +2M
Find files with permission 777 and remove them:
find /home/user -perm 777 -exec rm '{}' +
Find files based on how many times they have been accessed (atime) or modified (mtime):
find /etc -iname "*.conf" -mtime -180 –print
List a directory with permissions:
ls –la /path/to/files
Find files in directory larger than 100 mb:
find /dir/ -type f –size +100m –depth
Find 100 larges files in a dir:
find . –type f | grep * | ls –li | sort –k1 –r | head –n 101 > biggestfiles.txt
Find files that match ‘rwx’:
find . – type f | ls –l | grep ‘rwx’ > files_that_match.txt
Evaluate and compare the basic file system features and options
- Main commands:
df
Printout disk free space in a human readable format:
df -h
See which file system type each partition is:
df -T
See more details with file
command about individual devices:
file -sL /dev/sda1 (or other device)
File system fatures:
- Ext: "Extended Filesystem". Old, deprecated.
- Ext2: no journaling, max file size 2TB. Lower writes to disk = good for USB sticks etc.
- Ext3: journaling (journal, ordered, writeback), max file size 2TB
- Journaling: file changes and metadata are written to a journal before being committed. If a system crashes during an operation, the journal can be used to bring back the system files quicker with lower likeliness of corrupted files.
- Ext4: From 2008. Supports up to 16TB file size. Can turn off journaling optionally.
- Fat: From Microsoft. No journaling. Max file size 4 GB.
See more in Storage Management chapter (insert link)
Compare, create and edit text files
- Main commands:
diff
Compare two files:
diff -u <file1> <file2>
Compare two dirs:
diff -ur <dir1> <dir2>
Create a file:
touch <file>
Edit files:
Editors: vim, nano
In-line editing: sed
Compare binary files
Hex to string/bin conversion:
xxd <infile> <outfile>
Dump binary file in hex/octal:
od <file> # octal
od -x <file> # hex
Compare files byte by byte:
cmp <file1> <file2>
Use input-output redirection
Output from command to file:
Command > <file>
Append output to file:
Command >> <file>
Output error from command to file:
Command 2> <error-file>
Pipe output from command as input to command:
Command1 | Command 2
Analyze text using basic regular expressions
- Main commands:
grep
Examples:
- Quantifiers: * 0 or more, + 1 or more, ? zero or 1., single character (.)
- [a-z]: match any lowercase character
- \s whitespace or \S not a whitespace
- \d digit or \D not a digit
- \w word or \W not a word
- \^word: search for word at start of line
- \word$: search for word at end of line
- Note: remember that special characters needs to be escaped (\)
Search regex with pattern:
grep <pattern> <file>
Find the word text in file.txt:
cat file.txt | grep “text”
Search case-insensitive:
cat file.txt | grep –i “text”
Search two different words with grep:
egrep –w ‘passw0rd’|‘otherpassword’ /etc/passwd
Display only the words containing search term
cat file.txt | grep –w ‘Test’
Archive, backup, compress, unpack, and uncompress files
- Main commands: tar
To extract an uncompressed archive:
tar -xvf /path/to/foo.tar
To create an uncompressed archive:
tar -cvf /path/to/foo.tar /path/to/foo/
To extract a .gz archive:
tar -xzvf /path/to/foo.tgz
To create a .gz archive:
tar -czvf /path/to/foo.tgz /path/to/foo/
To list the content of an .gz archive:
tar -ztvf /path/to/foo.tgz
To extract a .bz2 archive:
tar -xjvf /path/to/foo.tgz
To create a .bz2 archive:
tar -cjvf /path/to/foo.tgz /path/to/foo/
To extract a .tar in specified Directory:
tar -xvf /path/to/foo.tar -C /path/to/destination/
Create, delete, copy, and move files and directories
- Main commands: touch, mv, rm, cp
Create hard and soft links
- Main comands: ln
Hard links points to the contents of the file, while soft links point to the name/location of the file.
Hard link: ln <file> <hard-link>
Soft link: ln -s <file> <soft-link>
List, set, and change standard file permissions
- Main commands: chmod, chown
Read, and use system documentation
- Main commands:
man, mandb, info, apropos, whatis
See /usr/share/doc
Manage access to the root account
Access root: su
Log on as another user: su <user>
Edit sudoers file: visudo
or vim /etc/sudoers
File content structure:
Defaults secure_path="/usr/sbin:/usr/bin:/sbin" # directories for sudo
root ALL=(ALL) ALL # ALL hosts, user (root) can run as ALL users with ALL
commands
tecmint ALL=/bin/yum update # user (tecmint) can run yum as root
gacanepa ALL=NOPASSWD:/bin/updatedb # user (gacanepa) can run updatedb without
password
%admin ALL=(ALL) ALL # same as first line, with group (admin)