Become a Shell Wizard in ~12 Minutes
Modified: July 19 2024
Link to video.
The Basics of Shell
Shell, terminal, console, command line, all are essentially the same thing.
Commands
ls
: lists out all items in the current (target) directory- You can pass some flags to change the output
-latrh
will produce long list format, all files, sorted by time, in reverse, with human readable file sizes- You can also use a path as an argument, as well as a combination of both flags and path
cd
: change directorypwd
: shows path to current work directoryecho
: lets you print some textcat
: lets you print the content of a filetouch
: creates a file if it doesn’t exist; changes the timestamp if it doescp
: lets you copy a file or directory to a target destinationmove
: like copy, but moves it insteadrm
: removes a file or directory- Need to use
-r
to delete a directory - You can add
f
to forcefully delete it
- Need to use
ln
: creates a symlink in a file to another location- Similar to a reference object in programming
less
: lets you view text content in a scrollable format- Often more useful than
cat
, since you can search easily
- Often more useful than
more
: less than less, but more on the screen and can only go forwardman
: shows the manual for the command you need- Similar to using
--help
after a command - One documentation I’d love to go through is note taking manual pages for all these commands.
- Similar to using
grep
: lets you pattern match against text content- You can install
ripgrep
which is likegrep
, but faster
- You can install
find
: finds a file or directory- Syntax can be weird, so might be better to install
fd
, a faster easierfind
- Syntax can be weird, so might be better to install
sed
: a stream editor, allowing you to make changes to an incoming stream of text- Usually useful to find and replace text
awk
: allows for programming logic- Useful for one-liners, grabbing specific things from a file
sort
: sorts text contenthead
: lets you see the first few lines of a filetail
: lets you see the last few lines of a file
The pipe operator, |
, allows you to chain commands into one another. The standard output from your first command will be fed into your second command, so on so forth.
xargs
: takes the contents of whatever you pipe into it and splits it into different chunks- For each chunk, it will pass it as an argument into whatever command specified
Subshell, or $(command)
, injects the output of whatever command/pipeline you defined, placing it wherever you defined the subshell.
You can use the >
command, or redirection operator, sends the standard output of the source file into whatever file you choose. Two greater than signs will append, not overwrite, or creates it if it doesn’t exist.
fzf
: a fuzzy finder. Run it by itself and you can easily search for items.compgen
: combine with-c
to see all commands
Fun Commands
Let’s say we take the following command: compgen -c | fzf | xargs man
. This will produce all available commands in a fuzzy finder, then anyone we choose will produce it’s manual.
We can take this a step further. If we use alias
, a method of generating our own commands, and call it something like fman
, we can write the following statement to produce a new command:
alias fman = 'compgen -c | fzf | xargs man'
Running fman
from now on will execute the pipeline we described earlier. To make it easier, we can always make sure it’s there by adding the above code to your bash.bashrc
file, by adding the line at the end of the file.
In order to find the largest file in a directory, we can use:
du -ah . | sort -hr | head -n 10
Here, we are using du
to get file sizes, with the -ah
for aggregate view with human readable text. That is fed into a sort
in reverse order, and finally, head
to view with just 10 lines at a time.
Some helpful hotkeys:
ctrl+c
: kill active processctrl+k
: exit shellctrl+l
: clear screenctrl+z
: put process in backgroundctrl+a
: go to the front of the linectrl+e
: go to the end of the linectrl+b
: go back one characterctrl+f
: go forward one characteralt+f
: go forward one wordalt+b
: go back one word!!
: run previous command (also known as bang bang)!<cmd>
: run previous matching commandctr+x ctrl+e
: open line in $EDITOR
Big Tips for Success
- Use the
man
pages (ortldr
) - Use the pipelines and subshells for a good time
- Use alias and scripts to save your work
- Use
fzf