0% found this document useful (0 votes)
28 views4 pages

System Cheetsheet

The document provides a comprehensive overview of various Linux command-line utilities and their options, including file manipulation commands like mkdir, cp, mv, and rm, as well as text processing tools like grep, sed, and awk. It also covers permissions, regex usage, and basic scripting techniques in Bash. Additionally, it includes examples for archiving files and using wildcards, along with detailed explanations of command syntax and options.

Uploaded by

timur yasar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

System Cheetsheet

The document provides a comprehensive overview of various Linux command-line utilities and their options, including file manipulation commands like mkdir, cp, mv, and rm, as well as text processing tools like grep, sed, and awk. It also covers permissions, regex usage, and basic scripting techniques in Bash. Additionally, it includes examples for archiving files and using wildcards, along with detailed explanations of command syntax and options.

Uploaded by

timur yasar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ls: -l, -a (. ..

), -A, -t (sort by time, newest first), -S (sort by file size, largest first), -h (human readable), -r (reverse) ,-d (directory)
wildcards: * (any ch), ? (any single ch), [abc] (a or b or c), [!abc] (not). char classes: alnum, alpha (alphabetic), digit, lower, upper. [A-Za-z0-9]
mkdir: -p (creates parents), cp: cp file1 file2 (copy file1 to file2) cp file1 file2 dir1 (copy file1 and file2 to dir1), -r (recursive), -u (update),
mv: move and rename, mv file1 file2. rm: -r (recursive) -f (force). ln: ln file link, -s (soft)-r(relative). type(command type), file(file type), which (executable of
command)
alias: alias name=’command’, unalias name. > stdout, >> append, 2> stderr, 2>&1 (stderr to stdout), &> (both stdout and stderr), /dev/null (dispose unwanted
output), < stdin. cat: cat > file (place text in file). sort: -n (numeric), -r (reverse). uniq: -c (count), -i (ignore case) (to use uniq, file must be sorted). wc: -l (lines), -w
(words), -c (bytes). tee: tee filename. head, tail: -n 5 (prints 5 line), date (prints the current time), du -s (shows the disk usage), ps aux (processes, in first column it
prints the user of process).
echo: $((2 + 2)) (arithmetic expansion), {1..5}, {001..15}, {Z..A}, A{1,2,3} (gets A1, A2, A3) (brace exp), $USER (parameter exp), echo $(ls) (command
substution), “” double quotes (expansions that start with $ works, space works, new line works), ‘ ’ single quotes (space works, new line works), echo “scale=3; 5/3” |
bc. grep. id (display user identity), history (use with grep to search word in history), !uniq repeats the last command you ran that starts with uniq. !?uniq repeats the
last command containing the string uniq anywhere in it. printenv. less, touch, stat.

Permissions (files: r= allows to be opened and read, w = allows to be written or truncated, does not allow files to be renamed or deleted. x = allows to be executed,
must be readable. directories: r = Allows a directory's contents to be listed if the execute attribute is also set. w = Allows files within a directory to be created, deleted,
and renamed if the execute attribute is also set. x = Allows a directory to be entered, e.g., cd directory.). default permissions 666 for files 777 for directories. chmod
777 filename (read=4, write=2, execute=1), umask 022, su, sudo, chown (change owner), chgrp(change group), passwd (change passwd). id -g -n your_username (to
find my group name). other syntax: chmod u=rwx,g=rx,o=r file.txt (u =user, g = group, o=others), also we can just add 1 permission without setting all with this
syntax: chmod u+s file.txt :chown [owner][:[group]] file...

locate string, Find: find ~ -type f (example), -type d (directories) f (files) l (symbolic link), -name “name”, -size +1M (b=512 bytes, c=byte, w = 2byte, k = kilobytes,
M= megabytes, G=gigabytes), -cmin n, -cmin -n (last modified n minutes ago), -mmin n (content last modified), -cnewer file (newer than specified file), -newer
file(content newer rhan specified file) -ctime n (last modified n days ago), -mtime n (content last modified), -empty, -group groupname, -perm 0600, -user username,
-or, -and, -not, \( \) (to group expressions), -delete, -print, -print0, -exec rm {} \;, -exec ls -l ‘{}’ + (to improve efficiency), xargs (find ~ -type f -name 'foo*' -print |
xargs ls -l), xargs --null (-print0 | xargs --null ls -l), -depth, -maxdepth, -mindepth.
find [PATH...] [EXPRESSION] -name PATTERN, -iname PATTERN, -type TYPE File type:f → regular file, d → directory, l → symbolic link, c → character device,
b → block device, -size N File size (e.g., +1M for files larger than 1MB), -user USER Files owned by a specific user, -group GROUP Files belonging to a specific
group, -mtime N Files modified N*24 hours ago(+N = more than N days, -N = less than N days), -atime N Files accessed N*24 hours ago, -ctime N Files changed
N*24 hours ago, -empty Matches empty files or directories, -exec CMD {} \;Executes CMD on found files (`{}` is replaced with the filename), -delete Deletes found
files (use with care!), -perm MODE Matches file permission bits (e.g., 644 or /u+x)

Archiving: gzip filename: -c (write output to stdout and keep the original file), -d (decompress), -f (force), -l (list), -r(recursive), -t (test) -number (amount of
compression), gunzip filename.gz: -c (write output to stdout and keep the original file). bzip2 filename, bunzip2 filename, zip -r playground.zip playground, -r
(recursive), unzip filename. tar: tar cf playground.tar playground (example), c (create), x (extract), r (append), t (list the contents), f (filename),
tar cf - --files-from=- , z (gzip compress, if you use this option then file extension will be .tgz), j (bzip2 compress, if you use this option then file extension will
be .tbz), tar xzf file.tgz (extracts), --wildcards “” (to use expansion). Example tar usage for multiple files: find /usr/bin/ -type f -name '*zip*' -print0 | tar --null -cf
zip_files.tar --files-from=- or this is same (find /usr/bin/ -type f -name '*zip*' | tar czf zip_files.tgz -T -)

Grep: grep ‘searchtext’ file* (for all files that start with file) (example), -i (ignore case), -v (invert), -c (matching count for each file), -l (just filenames that have
matches), -L (just filenames that doesn’t have any matches), -n (line number for each matching), -h (matching lines without file names), -H (with filename but this is
default when there is more than one file), -E (extended regex), -w (matches whole words only, if we search “c”, don’t get the lines with “tcp” but gets the lines
contains “ c “). -R (with this option we can specify a directory as an input to grep, it recursively searchs all the files in the specified directory)
Example: grep -c "zip" dirlist* | grep -v ":0$" (lists the number of times the word zip occurs in files in which it occurs at least once.)

Basic Regex: . ^ $ [ ] *(zero or more), . (any character), ^ (beginning of the line), $ (end of the line), [ab] (matching a or b), [[:characterclass:]], [^ab] (not a and b),
[A-Z] [0-9] (char ranges), char classes: alnum, word, alpha, blank, digit, lower, punct, space, upper, xdigit (hexadecimal).

(In extended regex we don’t need to escape basic regex chars to use but in basic regex, we need to escape extended regex characters)
Extended Regex: ( ) { } ? + |, ? (match an element zero or one time), * (zero or more times), + (one or more times), {n} (exactly n times), {n, m} (at least n, no more
than m), {n, } (n or more times), {, m} (no more than m times), | (or logic eklemeye yarıyor), ( ) (grouping parantheses)

Text Processing: cat: -A( shows ^I $ etc.), -n(numbers lines),-s (suppresses the output of multiple blank lines).
sort(sort can accept multiple files): -b (ignore leading blanks), -f (ignore case), -n (numeric sort), -r (reverse), -g (real numbers) -k (key) (for example -k 5 sorts
according to fifth field and fields starts from 1), -t (field seperator, spaces and tabs are used as default delimiters between fields). (In sort command we can sort
different keys different, for example If we execute sort -k 1,2 -k 3n filename, we specified 1,2 which means “start at field 1 and end at field 2.” In the second
instance, we specified 3n, which means field 3 is the sort key and that the sort should be numeric.) sort Example: sort -k 3.7nbr -k 3.1nbr -k 3.4nbr distros.txt. (3.7
means seventh character within the third field).

uniq: -c (count), -d (output only repeated lines), -f n (Ignore n leading fields in each line, there is no separator option, default seperator is whitespace), -i (ignore
case), -s n (skip the leading -n chars of each line), -u (output only unique lines).
cut: -c list (Extract the portion of the line defined by list. The list may consist of one or more comma-separated numerical ranges.), -f list (one or more fields we can
use -f 3 or -f 3,4,5 or -f 3-5), -d (delimeter, default is single tab character don’t forget to set it to space if you will use space as delimeter).
Example cut usage : cat food.txt | tr -s " " | cut -f 4 -d " " , another example: cut -f 2- -d “ “ (gets from 2nd field to end of line)
paste (opposite of cut, rather than extracting a column of text from a file, it adds one or more columns of text to a file, example usage: paste distros-dates.txt distros-
versions.txt). join (like db join, both files must start with same shared key field and both files must be sorted), comm (compares two text, comm file1 file2). Diff
(differences between files, diff file1 file2). patch (it is used to update just changed files, to do this it gets a diff file from diff command). tr (translate, usage: tr a-z A-
Z): -s (delete repeated instances of a character, tr -s “ab”, tr -s “ “). rev (reversing the order of characters in each line of text)

sed usage: sed ‘s/front/back’, s is substution (search and replace), we can specify line number like this: sed '1s/front/back/', $ is last line, we can use basic regex, 1,5
means from line 1 to 5, first~step such as 5~5, starts from 5th line and every 5th line thereafter, (addr1, +n) (match addr1 and the following n lines), addr! (Match all
lines except addr, which may be any of the forms listed earlier.) -n (no auto print) sed -n '1,5p' distros.txt (normally sed prints every lines but if we use -n it does not,
then with 1,5p we can print first 5 lines). sed -n “/apple/p” file (only prints lines that contains apple word, d for delete instead of p),
-i option does the conversion in place. sed 's/b/B/g' (g option means global and changes every b char in the line). sed -f distros.sed distros.txt (running sed script from
file). Example: sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/' distros.txt (this changes the date format, sed takes subexpressions from regex), sed -r
option uses extended regex. sed 's/a/b/g; s/c/d/g;' file (we can do multiple assignments in a single sed command as in this example)
Example sed usage (Ali, Ahmet swap): sed -r 's/Ali(.*)Ahmet/TEMP1\1Ali/; s/Ahmet(.*)Ali/TEMP2\1Ahmet/; s/TEMP1/Ahmet/; s/TEMP2/Ali/' aliahmet.txt

* Matches any characters ? Matches any single character [characters] Matches any character that is a member of the set characters [!characters]
Matches any character that is not a member of the set characters [[:class:]] Matches any character that is a member of the specified class
[:alnum:] Matches any alphanumeric character [:alpha:] Matches any alphabetic character [:digit:] Matches any numeral [:lower:] Matches any
lowercase letter [:upper:] Matches any uppercase letter
Some regex examples:
Date: DD/MM/YYY or DD.MM.YYYY (all of them are digits) = ^([0-9]{2}[/\.]){2}[0-9]{4}$
Phone number: (nnn) nnn-nnnn or nnn nnn-nnnn = ^\(?[0-9]{3}\)? [0-9]{3}-[0-9]{4}$
IP Address: 255.255.255.255 (001, 010 so there is no 1 or 2 digit number) = ^(([01][0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([01][0-9]{2}|2[0-4][0-9]|25[0-5])$
IP Address: 255.255.255.255 (1, 10 so there are 1 and 2 digit numbers) =
^(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
Email Address: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Password: min 8 char, upper/lower letter and digit = ^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}$ ( (?=.*[a-z]) look forward, are there any lowercase char)
Regex notes:
“-“ is used in [] such as [a-z], so if you want to use “-“ character in [], you need to escape like [\-\*\+/] (+ or – or * or /)

Bash Script
Read an input: read -p "Enter a non-negative integer: " num, some read options: -r (raw mode, do not interpret backslash characters as escapes), -s (silent),
-t seconds (timeout)

if [ "$grade" -ge 90 ]; then gradeGroup=$((grade / 10)) factorial=1 factorial=1


echo "Letter Grade: A" counter=$num counter=1
elif [ "$grade" -ge 80 ]; then case “$gradeGroup” in
echo "Letter Grade: B" 10|9) while [ "$counter" -gt 1 ]; do until [ "$counter" -gt "$num" ]; do
else echo "Letter Grade: A" factorial=$((factorial * counter)) factorial=$((factorial * counter))
echo "Letter Grade: F" ;; counter=$((counter - 1)) counter=$((counter + 1))
fi 8) done done
echo "Letter Grade: B"
;; echo "Factorial of $num is: $factorial" echo "Factorial of $num is: $factorial"
# Positional argument 7) if [[ ! "$op" =~ ^[\+\-\*/]$ ]]; then
#!/bin/bash echo "Letter Grade: C" #Regex check echo "Error: Invalid operator '$op'. Must
;; be one of +, -, *, /"
grade=$1 6) if ! [[ "$num" =~ ^[0-9]+$ ]]; then usage
echo "Letter Grade: D" echo "Error: Input must be a non-negative fi
# Check if input is provided ;; integer." #Function with exit
if [ -z "$grade" ]; then *) exit 1 usage() {
echo "Usage: $0 <grade>" echo "Letter Grade: F" fi echo "Usage: $0 <first_number> …”
exit 1 ;; exit 1
fi esac }
# Read each filename from files.list usage() {
factorial() { while read -r course_file; do echo "Usage: $0 <first_number> …”
local n=$1 # Check if the file exists before processing return
if [ ! -f "$course_file" ]; then }
if ! [[ "$n" =~ ^[0-9]+$ ]]; then echo "File $course_file not found!" if ! [[ "$a" =~ $number_regex ]]; then
echo "Error: Input must be a non- continue echo "Error: First number '$a' is not a valid number."
negative integer." fi usage
return 1 fi
fi # Count number of courses (lines) in the current file
course_count=$(wc -l < "$course_file")
local result=1 echo "File: $course_file has $course_count courses." #!/bin/bash
while [ "$n" -gt 1 ]; do # posit-param2: script to display all arguments
result=$((result * n)) # (c) Inner while loop to read each course line
n=$((n - 1)) while read -r course_name; do count=1
done word_count=$(echo "$course_name" | wc -w) while [[ $# -gt 0 ]]; do
echo " '$course_name' has $word_count words." echo "Argument $count = $1"
echo "$result" done < "$course_file" count=$((count + 1))
} shift
echo "-----------------------------" done
#Function usage
result=$(factorial "$input") done < files.list # shift moves the $2 to $1 and $3 to $2 so we can reach
echo "Factorial of $input is: $result" every parameters from just 1 parameter $1
for i in A B C D; do echo $i; done for i in distros*.txt; do while getopts "f:o:v" opt; do #to get options
OR if [[ -e "$i" ]]; then case "$opt" in
for i in {A..D}; do echo $i; done echo "$i" f) file=$OPTARG ;;
OR fi o) output=$OPTARG ;;
for i in distros*.txt; do echo "$i"; done done v) verbose=true ;;
*) echo "Usage: $0 [-f input_file] [-o output_file] [-v]" >&2; exit 1 ;;
esac
done

Some bash script features: Some commands:


“$#” : gets the number of arguments provided basename /usr/local/bin/kt.txt : it gets the filename (int this example we will get
$1 : gets the first argument kt.txt as output)
$0 : gets the script itself basename -s .txt /usr/local/bin/kt.txt : it prints kt because of suffix option -s
${4:-2} : gets the fourth argument, but if it is not provided or is empty, it ${var%pattern}, Example: filename_wo_extension=${filename_wo_path%.txt}
defaults to 2. (it removes the .txt pattern matching)
exit 1: exits from script (even we call inside function) # --- (d) Initialize array (0-11 for Jan-Dec) ---
command substution: $(validate “$input”) (it calls the functions or bash declare -a month_histogram
commands) for ((i=0; i<12; i++)); do
result = $(( result * num )) (we can use variables directly without $) month_histogram[$i]=0
$? : gets the result of last function that returned done
echo "Usage: $0 <non-negative-integer>" >&2 (prints to std err)

File Expressions String expressions


Expression Is True If... Expression Is True If...
file1 -ef file2 file1 and file2 have the same inode numbers string string is not null.
(the two filenames refer to the same file by hard
linking). -n string The length of string is greater than zero.
file1 -nt file2 file1 is newer than file2. -z string The length of string is zero.
file1 -ot file2 file1 is older than file2. string1 == string1 and string2 are equal
-d file file exists and is a directory. string2
-e file file exists. string1 != string2 string1 and string2 are not equal.
-f file file exists and is a regular file. string1 > string2 string1 sorts after string2.
-L file file exists and is a symbolic link. string1 < string2 string1 sorts before string2.
-r file file exists and is readable (has readable
permission for
the effective user). Integer expressions
-w file file exists and is writable (has write permission Expression Is True If...
for the integer1 -eq integer2 integer1 is equal to integer2.
effective user).
integer1 -ne integer2 integer1 is not equal to integer2.
-x file file exists and is executable (has execute/search
permission for the effective user). integer1 -le integer2 integer1 is less than or equal to integer2.
-s file file exists and has a length greater than zero. integer1 -lt integer2 integer1 is less than integer2.
integer1 -ge integer2 integer1 is greater than or equal to integer2.
integer1 -gt integer2 integer1 is greater than integer2.
Expansions to manage empty variables:
${parameter:-word} : If parameter is unset (i.e., does not exist) or is empty, this expansion results in the value of word. If parameter is not empty, the expansion
results in the value of parameter
${parameter:=word} : If parameter is unset or empty, this expansion results in the value of word. In addition, the value of word is assigned to parameter. If
parameter is not empty, the expansion results in the value of parameter.
${parameter:?word} : If parameter is unset or empty, this expansion causes the script to exit with an error, and the contents of word are sent to standard error. If
parameter is not empty, the expansion results in the value of parameter.
${parameter:+word} : If parameter is unset or empty, the expansion results in nothing. If parameter is not empty, the value of word is substituted for parameter;
however, the value of parameter is not changed.

Expansions That Return Variable Names: The shell has the ability to return the names of variables. This is used in some rather exotic situations.
${!prefix*} , ${!prefix@} : These expansions returns the names of existing variables with names beginning with prefix.

String Operations
${#parameter} : expands into the length of the string contained by parameter. Normally, parameter is astring; however, if parameter is either @ or *, then the
expansion results in the number of positional parameters.
${parameter:offset}, ${parameter:offset:length} : These expansions are used to extract a portion of the string contained in parameter. The extraction begins at offset
characters from the beginning of the string and continues until the end of the string, unless length is specified.(index starts from 0)
${parameter#pattern}, ${parameter##pattern} : These expansions remove a leading portion of the string contained in parameter defined by pattern. pattern is a
wildcard pattern like those used in pathname expansion. The difference in the two forms is that the # form removes the shortest match, while the ## form removes the
longest match. Example: foo=file.txt.zip; echo ${foo#*.} (prints txt.zip); echo ${foo##*.} (prints zip)
${parameter%pattern}, ${parameter%%pattern} : These expansions are the same as the previous # expansions, except they remove text from the end of the string
contained in parameter rather than from the beginning. Example: foo=file.txt.zip; echo ${foo%.*} (prints file.txt); echo ${foo%%.*} (prints file)
${parameter/pattern/string}, ${parameter//pattern/string}, ${parameter/#pattern/string}, ${parameter/%pattern/string}: This expansion performs a search-and-
replace operation upon the contents of parameter.

text=$(cat <<EOF Arrays in Bash


Hello arr=(a b c)
World first element : ${arr[0]}
EOF all elements: ${arr[@]}
) Length : ${#arr[@]}
Today date as in YYYY-MM-DD format: date +%F Example:
Some sed examples: fruits=("apple" "banana" "cherry" "date")
sed -r 's|([0-9]{2})/([0-9]{2})/([0-9]{4})|\3-\2-\1|g' distros.txt (| ayraç)
sed '5,10s/apple/orange/' fruits.txt (from line 5 to line 10) for fruit in "${fruits[@]}"; do
sed '3d' myfile.txt (deletes 3rd line) sed '5,7d' myfile.txt (from 5 to 7) echo "Fruit: $fruit"
done
sed '/DEBUG/d' log.txt (delete lines that contains DEBUG word)
sed 's/$/ <-- end/' notes.txt (add text to end of line) echo "Total fruits number: ${#fruits[@]}"
sed -i 's/foo/bar/g' file.txt (change file in place with -i option) # indexed access
sed -n '/^Error:/p' error.log (prints the lines that start with Error: ) for ((i=0; i<${#fruits[@]}; i++)); do
echo "Fruit at index $i is: ${fruits[$i]}"
done

Code Examples

# longest-word: find longest string in a file # Loop in files that ends with .txt
while [[ -n "$1" ]]; do
if [[ -r "$1" ]]; then for filename in ${sdir}/*.txt; do
max_word= if [ -e “$filename” ]; then
max_len=0 filename_wo_path=$(basename $filename)
for i in $(strings "$1"); do filename_wo_extension=${filename_wo_path%.txt}
len="$(echo -n "$i" | wc -c)" cp $filename ${bdir}/${filename_wo_extension}_${today}.txt
if (( len > max_len )); then ((counter++))
max_len="$len" fi
max_word="$i" done
fi
done
echo "$1: '$max_word' ($max_len characters)"
fi
shift
done
input_file="students.txt" # Find largest file in log folder
#!/bin/bash
if [ ! -f "$input_file" ]; then
echo "File not found: $input_file" log_dir="/var/log"
exit 1
fi largest_file=""
largest_size=0
while read -r name n1 n2 n3; do
avg=$(( (n1 + n2 + n3) / 3 )) for file in "$log_dir"/*; do
if [ -f "$file" ]; then
if (( avg >= 90 )); then size=$(stat -c%s "$file")
grade="A" if (( size > largest_size )); then
elif (( avg >= 80 )); then largest_size=$size
grade="B" largest_file=$file
elif (( avg >= 70 )); then fi
grade="C" fi
elif (( avg >= 60 )); then done
grade="D"
else echo "Largest log file: $largest_file"
grade="F" echo "Size: $largest_size bytes"
fi

echo "$name - Avg: $avg - Grade: $grade"


done < "$input_file"

#student.txt example:
Ali 90 85 80
Ayşe 70 60 65
Can 100 100 90

You might also like