To copy the permissions from one file to another use:
chmod --reference=<source file to copy permissions from> <target file>
Cron is used to execute scheduled commands
View file system disk space usage.
$ df -h
Estimate file space usage (can be used on directories, because directories are files too 😉)
$ du -h video.mp4
View file system disk space usage.
$ find ./ -name "*.html"
Really Useful:
$ find ./ type f -name "main.html" -exec rm {} \;
Somewhat Useful:
To append to stream, use >>
The following maps the file redirect to File descriptor:
File | File Descriptor |
---|---|
STDIN | 0 |
STDOUT | 1 |
STDERR | 2 |
To join two streams you can do thing 2>&1
. This will join stderr into stdout.
To pipe output from one program to another, use: |
.
<
: Redirect stdin>
: Redirect stdout2>
: Redirect stderrMove the cursor around when typing commands.
Delete part of a line of text.
Command history can be gone through via the ↑↓. In the middle of your history? Use Ctrl + c to get back to the start!
Here are some commands to further explore your bash history:
Manage running processes
bg
to start the process in the backgroundjobs
and fg %<Number>
to bring it back to the foregroundControl the screen
ln - Link files. Make a file that links to another file/directory.
There are two types of links, hard link and symbolic link.
$ ln -s /path/to/file /path/to/symlink
$ ls
Flags, ordered by personal usefulness
nmcli
is a command-line tool for controlling the NetworkManager.
Arguments:
-a
, --ask
: Interact. Fill in missing arguments.$ nmcli dev wifi list
$ nmcli dev wifi connect "Name Of Network" -a
Use readlink -f <file>
to print the full path of a file.
recode
allows you to convert a text file from one encoding to another, for example, ascii to html. This can be useful when you want to convert code that is full of templates (i.e. <>) to html. Example:
cat main.cpp | recode ascii..html | xclip
rsync
is remote sync. It can be used to sync files between hosts. It's great for transferring files to/from another host.
Example:
$ rsync -rltvPh /source/path drc@127.0.0.1:/target/path
Args:
.sh
extension are bash scripts.
One liner:
for i in {1..9}; do echo ${i}; done
Multiple Lines:
for i in {1..9}
do
echo ${i}
done
Different ranges:
for i in {1..9}; do echo ${i}; done
for i in apple banana cherry; do echo ${i}; done
for i in $(<Command>); do echo ${i}; done
ARR=('apple' 'banana' 'cherry')
for i in "${ARR[@]}"; do echo ${i}; done
Break and continue work as expected.
Works in the same fashion as a for-loop, however, the loop continues until the control expression is false.
For example, this command prints every line in a file:
while read line;
do
echo "$line"
done < file.txt
Slurm is used for cluster management and job scheduling. Slurm has three key features:
Submit a bash script to slurm. Slurm will schedule this bash script given the arguments presented in the script.
$ sbatch script.sh
Script example:
#! /bin/bash
#################### Batch Headers ####################
#SBATCH -p drcluster # Get it? DRC cluster ;)
#SBATCH -J hello_world # Custom name
#SBATCH -o results-%j.out # stdout/stderr redirected to file
#SBATCH -N 3 # Number of nodes
#SBATCH -n 1 # Number of cores (tasks)
#######################################################
python hello_world.py
sacct
or squeue
)%j
is job number)Environment variables:
scancel
is used to cancel a task
$ scancel <jobID>
scontrol
allows you to view or alter a job's details
View job details:
$ scontrol show job 14
Suspend a job:
$ sudo scontrol suspend 14
Continue a job:
$ scontrol resume 14
Give up on a job:
$ scontrol release 14
sinfo
provides information about the cluster
$ sinfo
PARTITION AVAIL TIMELIMIT NODES STATE NODELIST
drcluster* up infinite 3 idle node[02-04]
squeue
displays all submitted jobs.
$ squeue
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
22 drcluster hostname drc14 PD 0:00 3 (PartitionConfig)
A list of state codes can be found HERE.
srun
allows you to run parallel jobs directly from the command line. See sbatch
for command line arguments.
$ srun --nodes=3 hostname
node03
node02
node04
To run an interactive srun
session:
$ srun --pty /bin/bash
sshfs (Secure SHell FileSystem) allows you to mount a filesystem via ssh onto your local machine.
Example:
$ sshfs user@example.org: ~/local/dir
gzip
to make a tar ball (.tar.gz
).
$ tar -czvf compress.tar.gz /path/to/compress
Remove files while being archived:
tar -czf compressed.tar.gz --remove-files path/to/compress
$ tar -xzvf compress.tar.gz
Terminal Multiplexer. Used to open a lot of sessions in a single window. Can create tabs and split a window into multiple parts. Tmux sessions stay active even after closing a connection (ssh), so it is a handy way to keep your work or have running process run in the background when you're not connected to the host.
My personal ".tmux.conf" file can be found HERE. My plugin install script can be found HERE.
To enter command mode: Ctrl + b, :
Tmux plugin manager: TPM
Tmux Resurrect: Restore tmux environment after system restart.
~/.tmux/resurrect/
. With continuum the tmux environment is saved every 15 minutes. Every once in a while, saved sessions need to be purged.ln -sf <desired resurrect> last
. Will need to restore restore the session afterwards.$ tmux
$ tmux kill-session -t <session name >
$ tmux ls
$ tmux a -t <session name>
:swap-window -s <source window number > -t <target window number>
: Create a new windowList contents of directories in a beautiful tree-like format
$ tree
xargs
allows you to transform inputs from one command and run them with another command.
By default xargs
takes input, separates it by blanks, and executes a command for each argument. Example:
echo 'one two three' | xargs mkdir # This will make three directories
Useful arguments:
-d
: Change the delimiter to something other than space, i.e. xargs -d "\n"
-p
: Display each command and prompt the user to execute itTo use xclip
to copy to the clipboard for Ctrl +
v functionality use:
xclip -selection c
It is recommended to make an alias for this in your bashrc profile.
Used to adjust the screen (resolution, size, orientation, etc.)
$ xrandr
$ xrandr --auto --output <Screen> --mode <Resolution, ex 1440x1080>