Bash/scripts
From Christoph's Personal Wiki
History functions
history # display history for current tty !! # repeat last command !foo # repeat last command starting with 'foo' !N # execute command number N from history output !foo:p # _show_ last command starting with 'foo' but do _not_ execute it !-N$:p # _show_ N-commands ago but do _not_ execute it !$ # grab last argument from previous command !* # grab all arguments from previous command ^F^f # change first instance of "F" to "f" in last command (i.e., ^error^correction)
Command movements
Note: The carret ^
symbols means Ctrl+
.
^w # erase word under cursor ^u # erase from here to beginning of the line ^a # move the cursor to the beginning of the line ^e # move the cursor to the end of the line
Brace expansion
Instead of doing this:
cp filename filename-old cp filename-old filename
Try this:
cp filename{,-old} cp filename{-old,}
- Create multiple files
touch a{1,2,3}b # creates 'a1b', 'a2b', and 'a3b' touch {f1,f2,f3,}o # creates 'f1o', 'f2o', 'f3o', and 'o'
- List all files '
/usr/bin/foo
', '/usr/sbin/foo
', '/usr/local/bin/foo
', and '/usr/local/sbin/foo
':
ls /usr/{,local/}{,s}bin/foo
Word modifiers
h # remove a trailing file name component, leaving only the head. t # remove all leading file name components, leaving the tail. r # remove a trailing suffix of the form .xxx, leaving the basename. e # remove all but the trailing suffix.
- First list a file, then cd into its directory
ls -l foo/bar.txt cd !$:h # changes pwd to 'foo'
- Create a file and a directory as the basename of the file
touch foo.txt mkdir !$:r # creates a directory called 'foo' (without the .txt)
- Stacked word modifiers: Download a file to
/tmp
, and then create a directory for the contents of that tar file in/usr/local/src
. Then extract the contents of the tarball into the newly created directory:
cd /tmp wget http://www.example.com/path/SomePackageName.tar.gz cd /usr/local/src/ mkdir !-2$:t:r:r # creates a directory called 'SomePackageName' cd !$ # enter the above directory tar xvzf /tmp/!-4$:t # extract the contents of 'SomePackageName.tar.gz' here
- Substitution:
ls Foo # file/directory does not exist, so: !!:s/Foo/foo/ # change 'Foo' to 'foo' in last command
- Substitution (global):
ls Foo1; ls Foo2 # neither file/directory exist, so: !!:gs/F/f/ # replace all 'F' with 'f' in previous command