Add and format a number of Linux commands

This commit is contained in:
James Skemp 2022-04-27 17:08:02 -05:00
parent 6ce01c9cc7
commit bfff02983c

View File

@ -1,129 +1,142 @@
# Linux
SSH into a server with Raspberry Pi's default user.
```
## SSH
Type `exit` to exit out of a SSH session.
```bash
# SSH into a server with Raspberry Pi's default user.
ssh pi@<ip_address>
...
exit
```
List all files in a directory, with full paths, into a text file.
```
find wwwroot -type f > files.txt
```
# SSH into a server as a particular user.
ssh <user_name>@<ip_address>
Print working directory (current directory).
```
pwd
```
By itself, move to the current user's home directory. Tilde is short for home directory.
```
cd
cd ~
```
Output current user's name.
```
# Output current user's name.
whoami
```
Make a new directory
```
mkdir name
mkdir name1 name2 name3
```
Make a new directory and sub-directory/ies
```
mkdir -p path/to/directory
```
Look at a file or multiple files.
```
cat file.txt
cat file1.txt file2.txt
cat file?.txt
cat file*
```
Write to a file.
```
echo "Some text" > file.txt
echo "Some more text" >> file.txt
```
File viewer.
```
less file.txt
# q to quit
```
Move file(s)/directories to a directory
```
mv file.txt dir1
mv file1.txt file2.txt dir1
mv file1.txt file2.txt new-dir dir1
```
Rename a file.
```
mv file.txt new-name.txt
```
Copy a file
```
cp path/to/file.txt .
cp file.txt copy-file.txt
```
Remove/delete a file.
```
rm file.txt
```
Remove/delete an empty directory.
```
rmdir path/to/directory
```
Remove/delete a directory, even if it has files.
```
rmdir -r path/to/directory
```
Count number of files and folders in home directory.
```
ls ~ | wc -l
```
View disk space.
```bash
df -h
```
Package management:
```bash
sudo dpkg --audit
sudo dpkg --get-selections
```
View OS information.
```bash
cat /etc/os-release
```
Reboot
## System management
```bash
# Reboot
sudo reboot
sudo shutdown -r now
# Reboot in 5 minutes.
sudo shutdown -r 5
# Shutdown
sudo poweroff
```
Shutdown
## File system
### Information
```bash
sudo poweroff
# View disk space.
df
df -h
# Count number of files and folders in home directory.
ls ~ | wc -l
```
### Navigation
```bash
# Print working directory (current directory).
pwd
# By itself, move to the current user's home directory. Tilde is short for home directory.
cd
cd ~
```
### Modification
```bash
# Make a new directory.
mkdir name
mkdir name1 name2 name3
# Make a new directory and sub-directory/ies.
mkdir -p path/to/directory
# Move file(s)/directories to an existing directory.
mv file.txt dir1
mv file1.txt file2.txt dir1
mv file1.txt file2.txt new-dir dir1
# Rename a file.
mv file.txt new-name.txt
# Copy a file
cp path/to/file.txt .
cp file.txt copy-file.txt
# Remove/delete a file.
rm file.txt
# Remove/delete an empty directory.
rmdir path/to/directory
# Remove/delete a directory, even if it has files.
rmdir -r path/to/directory
```
### File viewing and creation
```bash
# Look at a file or multiple files.
cat file.txt
cat file1.txt file2.txt
cat file?.txt
cat file*
# Write to a file.
echo "Some text" > file.txt
echo "Some more text" >> file.txt
# File viewer. Press q to quit.
less file.txt
# List all files in a directory, with full paths, into a text file.
find wwwroot -type f > files.txt
```
### Hidden content
```bash
# Make a hidden directory.
mkdir .dir1
# Make a hidden file.
mkdir .hide.txt
# List all hidden files/folders.
ls -a
```
## System information
```bash
# View OS information.
cat /etc/os-release
# View system information on Ubuntu, which is displayed on SSH login:
landscape-sysinfo
# System monitoring.
top
# Prettier version of top.
htop
# Memory usage.
free
free -h
# Show in MBs.
free -m
```
## Package management
```bash
# List installed packages.
dpkg --list
sudo dpkg --audit
sudo dpkg --get-selections
```
## Permissions
@ -138,9 +151,7 @@ getent group
# List all groups a user (username) is in.
groups username
```
```bash
# Add write access to group.
chmod g+w file-or-directory
@ -156,19 +167,3 @@ chmod ugo+rwx file-or-directory
# Only read to all.
chmod a=r file-or-directory
```
## Hidden
Make a hidden directory.
```
mkdir .dir1
```
Make a hidden file.
```
mkdir .hide.txt
```
List all hidden files/folders.
```
ls -a
```