Appendix B — Linux Cheat Sheet
B.1 Getting help
B.2 Working w/ directories
B.2.1 Directory content
# List the content of the current directory
ls .
# List the content of the current directory (shortcut)
ls
# List the content of the folder <sancho> (relative path)
ls sancho
# List the content of the folder <sancho> (absolute path)
ls /media/sancho
# List the content including hidden files
ls -a
# List the content in a table-like format
ls -l
# List the content in a table-like format with hidden files
ls -al
# List the content recursively
ls -R
# List the content in a table-like format and human-readable
ls -lh
# List the content in a tree-like format
tree
# List the content including hidden files
tree -a
# Limit to two levels
tree -L 2
B.2.3 Creating directories
B.2.4 Copying directories
B.2.5 Moving directories
B.2.6 Renaming directories
B.2.7 Deleting directories
B.3 Working w/ files
B.3.1 Creating files
# Create a new empty file in the current directory
touch newfile.txt
# Create a new file and add content on-the-fly
echo 'Line 1\nLine 2' > newfile.txt
# Create and open a new file in the NANO text editor
# (press CTRL + X, then Y and ENTER to save and exit)
nano newfile.txt
# Create a new file and add content interactively
# (press CTRL + D to save the file)
cat > newfile.txt
B.3.2 Editing files
B.3.3 Printing file content
# Print file content
cat file.txt
# Open a file for interactive reading
less file.txt
# Print the first 10 lines of a file
head file.txt
# Print the first 6 lines of a file
head -n 6 file.txt
# Print the last 10 lines of a file
tail file.txt
# Print the last 6 lines of a file
tail -n 6 file.txt
# Dynamic - Useful for logs
tail -f file.txt
B.3.4 Copying files
B.3.5 Moving files
B.3.6 Renaming files
B.3.7 Deleting file
B.3.8 Joining files
B.4 Useful commands
# Clear the terminal
clear
# Get the size of the current directory
du -sh .
# Print the command lines history
history
# Search for a previous command
# CTRL + R and type some characters of the command
# Create a new screen
screen -S screen_name
# Detach a screen
# (press CTRL + A + D to leave the screen)
# Reattach a screen
screen -r screen_name
# List existing screens
screen -ls
# Kill all R processes for a specific user
killall -u jdoe R
# Kill all RStudio processes for a specific user
killall -u jdoe rsession
# System monitoring utilities
top
htop
btop
# Count number of files (and folders) in the current directory
ls | wc -l
# List all files/folders matching a pattern
ls | grep "\\.md$"
# List all files/folders matching a pattern (case insensitive)
ls | grep -i "\\.md$"
Code source
:::