Skip to content

Instantly share code, notes, and snippets.

@ph-One
Created March 4, 2016 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ph-One/5f9d78471b7cb6a6b293 to your computer and use it in GitHub Desktop.
Save ph-One/5f9d78471b7cb6a6b293 to your computer and use it in GitHub Desktop.

SHell Time Savers

Note: spaces and casing matter, always!

Unless otherwise stated, all commands are executed against a GNU/Linux OS.

History

Re-run the last command

ls
!!          # ls
!! -a       # You can append to the command: `ls -a`
sudo !!     # You can prepend to the previous command: `sudo ls`
sudo !! -a  # You can prepend and append to the command: `sudo ls -a`
BASH ZSH FiSH CSH KSH
yes yes no yes no

Run the most recent command that starts with a specific string

ls -salt
!ls         # Will re-run the most recent command that starts with "ls": `ls -salt`
BASH ZSH FiSH CSH KSH
yes yes no yes no

Run a specific command from history

history     # Returns a numbered list of previously executed commands
!77         # Runs command 77 from `history`
BASH ZSH FiSH CSH KSH
yes yes no yes no

Run a previous command containing a specific string

Can be more useful than using alias in some cases.

docker inspect --format '{{ .NetworkSettings.IPAddress }} :: {{.Name}}' $(docker ps -q)  # dips
!? dips     # Will re-run the most recent command containing the string, " dips"
BASH ZSH FiSH CSH KSH
yes yes no no no

General

Duplicate/Backup a File or Directory

cp filename.txt{,.bak}
cp -R dirname{,.bak}
BASH ZSH FiSH CSH KSH
yes yes yes yes yes

Create multiple files/dirs at once

mkdir {one,two,three}   # Generates directories one/, two/, three/
	# mkdir one
	# mkdir two
	# mkdir three

touch README{1,2,3}.md  # Generates files, README1.md, README2.md, README3.md
	# touch README1.md
	# touch README2.md
	# touch README3.md
mkdir -p /opt/ibm/{app1,app2}/{var,conf}/
	# mkdir -p /opt/ibm/app1/var/
	# mkdir -p /opt/ibm/app1/conf/
	# mkdir -p /opt/ibm/app2/var/
	# mkdir -p /opt/ibm/app2/conf/
touch {one,two,three}/README{1,2,3}.md  # one/, two/, three/ must exist first
	# touch one/README1.md
	# touch one/README2.md
	# touch one/README3.md
	# touch two/README1.md
	# touch two/README2.md
	# touch two/README3.md
	# touch three/README1.md
	# touch three/README2.md
	# touch three/README3.md

tree one/ two/ three/
	one/
	├── README1.md
	├── README2.md
	└── README3.md
	two/
	├── README1.md
	├── README2.md
	└── README3.md
	three/
	├── README1.md
	├── README2.md
	└── README3.md
BASH ZSH FiSH CSH KSH
yes yes yes yes yes

Frequent Commands

tar.xz

tar xJf filename.tar.xz

tar.gz

tar xf filename.tar.gz  # Linux
gunzip filename.tar.gz && tar xf filename.tar  # AIX

tar.bz2

tar xjf filename.tar.bz2

Mark as Executable

chmod +x filename

[Resume] Downloading a File

wget -c http://url/to/file.tar.gz
curl -C -LO http://url/to/file.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment