0% found this document useful (0 votes)
7 views

Mid Prep Linux

Linux commands

Uploaded by

Manzi Vaillant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Mid Prep Linux

Linux commands

Uploaded by

Manzi Vaillant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COSC 8312 Intro Linux

Command Summary [ Cheat sheet ]


Table of Contents
1. Week 3: Basic Commands
2. Week 4: VIM Editor
3. Week 5: Text Processing Commands
4. Week 6: Bash Scripting for Beginners

Week 3: Basic Commands

Filesystem Commands

ls : List directory contents


ls -a : Show all files (including hidden)
ls -A : Show almost all (excluding . and ..)
ls -l : Long listing format
ls -lh : Long list, human-readable format
ls -S : Sort by file size
ls -s : Print allocated size of each file
ll : Alias for ls -l
pwd : Print working directory
stat filename : Display file or filesystem status

File Editing

touch newFile : Create a new file or update timestamps


touch -a file : Change access time
touch -m file : Change modified time
touch -c file : Do not create file, only modify timestamp if exists
touch -d "2024-01-01" file : Specify date/time string for timestamp
touch -t 202401010101 file : Specify timestamp in [[CC]YY]MMDDhhmm[.ss] format
nano file : Open file in nano editor
vim file : Open file in vim editor
cat file : Print file content directly in the terminal
which program : Show the full path of shell commands

File Operations

cp source destination : Copy files and directories


diff file1 file2 : Compare files line by line
rm fileName : Remove files or directories
rm -rf directory : Force recursive deletion for non-empty directory
rmdir directoryName : Remove empty directories
mv oldPath newPath : Move or rename files and directories
mv *.txt otherFolder/ : Move all .txt files to another folder

Bash Configuration

vim ~/.bashrc : Edit user's bash configuration file using vim editor
vim /etc/skel/.bashrc : Edit default bash configuration file using vim editor

Command Alias

alias ll='ls -la' : Create a temporary alias


echo "alias ll='ls -la'" >> ~/.bashrc : Add permanent alias to bashrc

Permissions

File permissions format: d.rwx.rwx.rwx (filetype.user.group.other)


d or - : d irectory or file
rwx : r ead w rite e x ecute
For directory r: list content, w: modify content (create, delete, rename), x: access directory and its content
chmod : Change file mode bits
chmod +x fileName : Add execute permission to all
chmod -rwx fileName : Remove all permissions
chmod g-w fileName : Remove write permission for group
chmod u+rwx fileName : Add read, write, and execute permissions for the user

Resource Usage

free : Display amount of free and used memory


free -m : Display memory usage in megabytes
free -h : Display memory usage in human-readable format
df : Report file system disk space usage
df -h : Human-readable output
df -i : List inode information instead of block usage
htop : Interactive process viewer
uptime : Show how long the system has been running

Package Management (Debian-based)

sudo apt update : Update package lists


apt search packageName : Search for packages
sudo apt install packageName : Install packages
sudo apt remove packageName : Remove packages
sudo apt autoremove : Remove automatically installed dependencies that are no longer needed
sudo apt upgrade : Upgrade installed packages
sudo apt dist-upgrade : Intelligently handle changing dependencies with new versions of packages

Managing Systemd Units

systemctl status programName : Check the status of a service


sudo systemctl disable programName : Disable a service from starting at boot
sudo systemctl enable programName : Enable a service to start at boot
sudo systemctl stop programName : Stop a running service
sudo systemctl start programName : Start a service
sudo systemctl restart programName : Restart a service

Viewing Logs

/var/log : Directory containing log files


dmesg : Print or control the kernel ring buffer
head path/to/log/file : Display the beginning of a file (10 lines)
tail path/to/log/file : Display the end of a file (10 lines)
tail -n 30 file : Display the last 30 lines
tail -f file : Follow the file in real-time
journalctl -u ssh : Show messages related to the SSH unit
journalctl -fu ssh : Follow SSH unit logs in real-time

Week 4: VIM Editor

Basic VIM Commands

:w : Write (save) file


:q : Quit
:wq : Write and quit
:q! : Force quit without saving
i : Enter insert mode
Esc : Return to normal mode
Shift+A : Append at end of line
u : Undo
0 : Go to beginning of line
$ : Go to end of line
x : Delete a character
dd : Delete (cut) a line
hjkl : Navigate left, down, up, right
:! command : Execute terminal command from VIM
:r path/to/file : Insert content of another file

Buffer Management

:e path/to/file : Edit a file in a new buffer


:bp : Switch to previous buffer
:bn : Switch to next buffer
:enew : Open new empty buffer

Visual Mode and Text Manipulation


v : Enter visual mode (for selecting text)
:sort : Sort selected lines
y : Yank (copy) selected text
p : Put (paste) yanked or deleted text

Search and Replace

:%s/oldText/newText/g : Find and replace globally


:%s/oldText/newText/gc : Find and replace with confirmation

File Navigation

gg : Go to beginning of file
Shift+G : Go to end of file

Split Windows

:split path/to/file : Open file in horizontal split


:vsplit path/to/file : Open file in vertical split
Ctrl+w w : Switch between split windows

Line Numbers

:set number : Show line numbers


:set relativenumber : Show relative line numbers
:set nonumber : Hide line numbers

VIM Command-Line Options

vim +20 file : Open file at line 20


vim -o file1 file2 : Open multiple files in horizontal splits
vim -O file1 file2 : Open multiple files in vertical splits

VIM Configuration

.vimrc : VIM configuration file in home directory

Week 5: Text Processing Commands

grep (Global Regular Expression Print)

grep [options] pattern [file...] : Search for patterns in files


grep -v "pattern" file : Show lines that don't contain the pattern
grep word * : Search for word in all files in the current directory
grep -n word file : Show line numbers with matches
grep -c word file : Count occurrences of the word
grep -i word file : Case-insensitive search
grep -r word directory : Recursive search in directories
grep -h word directory : Show matching lines without filenames
grep -o word file : Display only the matching parts of lines
grep ^word file : Match lines starting with 'word'
grep word$ file : Match lines ending with 'word'
grep -l word * : Show only filenames with matches
grep -w word file : Match whole words only
grep -e word1 -e word2 file : Search for multiple patterns
grep -q word file : Quiet mode, no output (useful in scripts)
grep --color word file : Highlight matching text

cut

cut [options] [file] : Remove sections from each line of files


cut -b 1 file : Extract the first byte of each line
cut -b 4,5,6,7 file : Extract specific bytes
cut -b 4-7 file : Extract a range of bytes
cut -b -10 file : Extract first 10 bytes
cut -c 4-7 file : Extract characters (similar to bytes)
cut -f 1,2 file : Extract fields (tab-delimited by default)
cut -d " " -f 1,2 file : Use space as delimiter and extract fields 1 and 2

sed (Stream Editor)


sed 's/word/newWord/' file : Replace first occurrence of 'word' with 'newWord'
sed 's/word/newWord/3' file : Replace the 3rd occurrence
sed '1 s/word/newWord/' file : Replace in the first line only
sed 's/word/newWord/g' file : Global substitution (all occurrences)
sed 's/word/newWord/2g' file : Replace from the 2nd occurrence onwards
sed -e 's/word1/newWord1/g' -e 's/word2/newWord2/g' file : Multiple substitutions
sed '/pattern/s/word/newWord/' file : Substitute only in lines matching a pattern
sed -i 's/word/newWord/' file : Edit file in-place
sed 's/word//' file : Delete 'word' from the file
sed '2d' file : Delete the second line
sed '1,4d' file : Delete lines 1 through 4
sed '/pattern/d' file : Delete lines matching a pattern
sed -n '/usr/p' file : Print only lines containing '/usr'
sed -i 's/ *$//' file : Remove trailing spaces
sed -i 's/[[:space:]]*$//' file : Remove trailing whitespace (including tabs)
sed -i '/^$/d' file : Remove empty lines
sed -i 's/[a-z]/\U&/g' file : Convert lowercase to uppercase
sed -i 's/[A-Z]/\L&/g' file : Convert uppercase to lowecase
sed 11q file : Display first 11 lines (similar to head)

awk

awk '{print}' file : Print entire file


awk '{print $1}' file : Print first field of each line
awk '{print $1,$2}' file : Print first and second fields
awk -F ":" '{print $1}' file : Use ':' as field separator
awk -F ":" '{print $1" "$6" "$3}' file : Print specific fields with custom separator
awk 'BEGIN{FS=":"; OFS="-"} {print $1,$6,$3}' file : Set input and output field separators
awk '{print $NF}' file : Print last field
awk '/\/dev\/loop/ {print $1}' file : Print first field of lines matching a pattern
awk '{print $1"\t"$3+$4}' file : Print first field and sum of third and fourth fields
awk 'length($0) > 7' file : Print lines longer than 7 characters
awk '{if ($NF == "pattern") print $0}' file : Print lines where last field matches a pattern
awk 'BEGIN{for(i=1;i<=10;i++) print "The square of",i,"is",i*i;}' : Generate a table of squares
awk '$1 ~ /^[b,c]/ {print $0}' file : Print lines where first field starts with 'b' or 'c'
awk 'match($0, /o/) {print $0 "has \"o\" character at" RSTART}' file : Print lines containing 'o' with position
awk 'NR==7,NR==11 {print NR, $0}' file : Print lines 7 through 11 with line numbers
awk 'END {print NR}' file : Print total number of lines

find

find /path -name "*.txt" : Find all .txt files in the specified path
find . -name name : Search by name in the current directory
find . -iname nAme : Case-insensitive name search
find . -name name -type f : Search for files by name (use -type d for directory)
find . -name name -type f -exec rm {} + : Find and remove matching files
find /path -name "*.log" -type f -exec truncate -s 0 {} + : Truncate all log files to size 0
find . -mmin -10 : Find files modified less than 10 minutes ago
find . -mtime -10 : Find files modified less than 10 days ago
find . -size +5M : Find files larger than 5 MB (can use-/+ and G:GB, M: MG, k: KB)
find . -empty : Find empty files and directories
find . -perm 777 : Find files with specific permissions
find /path -exec chown user1:grp {} + : Change owner and group for all found files
find . -name name -type f -maxdepth 1 : Search only in the current directory (not recursively)

Week 6: Bash Scripting for Beginners

Basics

Shebang: #!/bin/bash (first line of every bash script)


Comments: Start with #
echo $SHELL : Display current shell
which bash : Show path to bash executable

Variables

Declaring variables: myvariable="some text"


Using variables: echo $myvariable
Command substitution: var=$(ls) (store the output of the ls commands)

Environment Variables

Usually in uppercase
env : Display all environment variables

Basic Math

expr : Evaluate expressions


expr 20 + 30 : Addition, return 40 (same for -,and /)
expr 10 \* 5 : Multiplication, return 50 (escape * )
Using variables: expr $myvar + 4

If Statements

if [ $myvar -eq 200 ]


then
echo "The condition is true"
else
echo "The condition is not true"
fi

Comparison operators: -eq , -ne , -gt , -lt , -ge , -le


File test operators: -f (file exists), -d (directory exists)

if [ -f /path/to/file ]
then
echo "The file exists"
else
echo "The file does not exist"
fi

Exit Codes

$? : Contains the exit status of the last command


0 : The command was successfully executed
Any other number : The command wasn't successfully executed
exit x : Force script to exit with status x

While Loops

while [ condition ]
do
# commands
sleep 0.5
done

sleep 0.5 (It waits 0.5 sec before the next execution of the loop)

Universal Update Script Example

#!/bin/bash
release_file=/etc/os-release
if grep -q "Arch" $release_file
then
# Arch-based system
sudo pacman -Syu
elif grep -q "Ubuntu" $release_file || grep -q "Debian" $release_file
then
# Ubuntu/Debian-based system
sudo apt update
sudo apt dist-upgrade
fi

For Loops
for item in 1 2 3 4 5
do
echo $item
done

# Alternate syntax
for item in {1..5}
do
echo $item
done

# Example: Compress all log files in logfiles/ directory


for file in logfiles/*.log
do
tar -czvf $file.tar.gz $file
done

Script Storage Best Practices

Store scripts in /usr/local/bin for system-wide access (you'll be able to execute scripts from everywhere)
Make root the owner of system-wide scripts
Add custom directories to PATH: export PATH=/path/to/scripts:$PATH

Remember to make your scripts executable with chmod +x script_name.sh before running them.

#AngeBHD - OCT 2024

You might also like