One-liners
Some Bash commands can be written as a "one-liner." if
and while
statements, for example, can be written in a slightly different way to chain commands.
Bash Variables
Set variable to path where script was called from
Say you have a path, /home/username/scripts/system/update_system.sh
, and your current directory is /home/username/scripts/
. If you call ./system/update_system.sh
from the /home/username/scripts/
directory, the value of $CWD
below would be /home/username/scripts
:
Set variable to path where script exists
Say you have a path, /home/username/scripts/system/update_system.sh
, and your current directory is /home/username/scripts/
. If you call ./system/update_system.sh
from the /home/username/scripts/
directory, the value of $THIS_DIR
below would be /home/username/scripts/system/
.
Get formatted timestamp
Set a variable to a timestamp when the variable was initialized.
Format the timestamp using +%?
, where the ?
is one of the below:
Value | Date Part | # Digits |
---|---|---|
%Y |
Year | 4 (YYYY ) |
%m |
Month | 2 (mm ) |
%d |
Day | 2 (dd ) |
%H |
Hour (24h) | 2 (HH ) |
%I |
Hour (12h) | 2 (HH ) |
%M |
Minutes | 2 (MM ) |
%S |
Seconds | 2 (SS ) |
%p |
AM/PM (12h only) | 2 (AM /PM ) |
You can also create a function and call it in a string to add a timestamp, for example to name a file or directory.
find
one-liners
The find
command on Unix machines searches for files/directories that match a pattern. You can chain commands on the results with -exec <logic> {} +
, for example to remove all results of the find
command.
Find & remove
You can add an -exec
statement to a find
command to do something with the results of find
.
Find & remove files
Find & remove dirs
Search & replace in a file name
Some characters are invalid for filenames, i.e. :
, on certain OSes (looking at you, Windows). This command can search for symbols/patterns in a string and replace them. In the example below, we search for any file with a :
anywhere in the name and replace it with a -
symbol:
find /path/to/your/directory -type f -name '*:*' -exec bash -c 'mv "$0" "${0//:/-}"' {} \;
Exclusion strings
Use ! -exec sh -c 'ls "$1"/<your-find-pattern>/dev/null 2>&1' _ {} \;
, replacing <your-find-pattern>
with a search string, to write an exclusion list for the find
command. This will return all results not matching a given pattern.
Print all directories that do not contain a specific filename pattern
Example: print all directories that do not have a file ending in .part
Print every file that does not contain a specific filename pattern
Example: Print every file in the current directory that does not end in .part
hostname
one-liners
Get host's primary IP address
Pipeing to/from files
Echo multiple lines into file with EOF
Echo lines into file | |
---|---|
User & Group commands
Check if Linux user exists
Check if Linux group exists
Check if command exists & runs
Check if Bash command exists | |
---|---|
Example with docker
command:
Check if Docker command exists | |
---|---|
stat
one-liners
Get chmod of a file or directory
You can add an alias to your ~/.bash_aliases
file to call the stat
command with variable directory paths:
Misc. one-liners
Get a timestamp
You can call the date
command with a string format, i.e. +"%Y-%m-%d_%H:%M"
, to get a formatted datetime string.
Get timestamp and assign to a variable | |
---|---|
Timestamp function for Bash scripts | |
---|---|
Repeat a command with a sleep
You can write a while
loop as a one-liner:
Example: repeat the ls
command every 5 seconds:
rsync one-liners
Sync path with rsync
rsync
is an incredible useful tool for synchronizing files. It can be used to sync local-to-local, remote-to-remote, or local-to-remote/remote-to-local.
rsync
flags reference:
arg | description |
---|---|
-r |
Recursive copy (unnecessary with -a ) |
-a |
Archive mode, includes recursive transfer |
-z |
Compress the data |
-v |
Verbose/detailed info during transfer |
-h |
Human readable output |
--progress |
Show a progress bar during transfer |