Basic Bash commands
Selected basics
ls # List contents of directory
Add
-1
to display results in a single column. Add -l
to see more details. Add -a
to show hidden files and folders. Use ls "/[dir_name]"
to set absolute path and navigate there.pwd # Print working directory
mv # Move directory or rename
touch file.txt # Create file.txt in current folder
rm # Remove file/directory (add -r to delete everything inside it i.e. recursively)
Get
ranger
(console file manager) to improve the entire navigation experience. When using ranger
, you can use Shift
+ S
to navigate to the current directory. Type exit
after that to close the instance and return to ranger. Use Shift
+ Q
to quit ranger. Consider adding alias r='. ranger'
to your .bashrc
file which will allow you to use ranger
with r
and let the shell follow ranger
, so that when exiting with l
, the current folder in ranger
is also opened in your shell.Refresh/reload profile
. ~/.bashrc
Create a symlink
ln -s [target_path] [link_path]
# Example: Links the .ssh folder inside a WSL to the corresponding folder on Windows
# i.e. a shortcut at ~/.ssh will be created that, when opened, will show the contents of C:\Users\Kim\.ssh
ln -s /mnt/c/Users/Kim/.ssh ~/.ssh
Set a variable
echo "Please enter a bucket name: "; read bucket; export MYBUCKET=$bucket
Save variable to .bashrc
echo "export MYBUCKET=$MYBUCKET" >> ~/.bashrc
Download file
wget https://www.somewhere.com/file.zip
Unzip file
unzip file.zip -d ~/webapp1
Set & remove alias
alias # Show all
alias rm="rm -i" # Add alias
unalias rm # Remove alias
To set it permanently for all future bash sessions add such line to your .bashrc
file in your $HOME directory.
To set it permanently, and system wide (all users, all processes) add set variable in /etc/environment
.
Convert DOS file format to ISO, ASCI or 7bit
This should only be required when you do something silly such as creating text files in your WSL in Windows Explorer and then hope you can use them in the same way in Linux. Reference: https://linux.die.net/man/1/dos2unix (DOS/MAC to UNIX text file format converter).
sudo apt install dos2unix # Install dos2unix
dos2unix -c iso test.sh newtest.sh # Converts by creating new file
Viewing files
cat -n [file_name] [file_name] # Concatenate files with row numbers
less [file_name] # Scroll through file
head -5 [file_name] # Display first 5 rows of file
head -c 5 [file_name] # Display first 5 characters of file
tail -5 [file_name] # Display last 5 rows of file
Working with SSH
# List all added keys
ssh-add -l
# Generate a new key using ed25519 algorithm and adding your email as meta data
ssh-keygen -t ed25519 -C "[your_github_email@example.com]"
# Show fingerprint of a generated key
ssh-keygen -lf ~/.ssh/id_ed25519.pub
# Copy public key to server
ssh-copy-id -i ~/.ssh/[id_ed25519.pub] [username]@[ip]
# Example configuration to store in ~/.ssh/config
Host [server_name]
HostName [x.y.z.w or url]
IdentityFile [~/.ssh/id_ed25519]
IdentitiesOnly yes
User [username]
Port [port]
Bash scripts
Full update, upgrade and clean-up
#!/bin/bash
rm -rf /var/lib/dpkg/lock-frontend
rm -rf /var/lib/dpkg/lock
apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get autoremove -y
apt-get autoclean -y
echo 'Update, upgrade, and clean-up executed.'
Useful resources
General
- Online Bash Shell
- ShellCheck – shell script analysis tool
- TLDR pages/help for commands
- Awesome List of CLI/TUI programs
Specific topics
- How to use grep command in Linux/Unix with examples
- Bash Test Operators Cheat Sheet - Kapeli
- Oh My Posh - Prompt theme engine for any shellAdd
eval "$(oh-my-posh init bash --config ~/.poshthemes/catppuccin_macchiato.omp.json)"
to.bashrc
- Ranger - Console file manager