Shell Tools and Scripting Guide
Comprehensive guide to shell commands, scripting, and advanced techniques
Basic Shell
$PATH is basically a list of locations where my Shell is going to search for programs to run everything I run a command.
pwd
= print_working_directory
cd
= change_directory
cd -
= quick switch prev directory
ls [OPTION]... [FILE]...
= [] is for optional, … is for any number of,
if no values provided the arguement is called a flag, and if value has to provided its called an option.
drwxrwxrwt
= d indicates directory, next 3 indicate perms for owner, for group and for everyones else, x
means that its executable?
mv
= can rename or move, mv old_name new_name
cp
= copy files, cp file new_file_name
ctrl+l
= clear shell
> and < and >> and |
= steam modifiers, basically keyboard is default input and console print is default put, you can change the stream of a program using angle brackets. and >>
is for appending and |
is for making output of one input of another
eg) cat < hello.txt > copied_file.txt
tail
= outputs the n amount of lines, can be used in a pipe operation
grep
= for searching, but very useful at the end of the command chain using |
eg)
curl --head --silent google.com | grep --ignore-case content-length | cut --delimiter=' ' -f2
xdg-open
= opens files with appropriate program
Advanced Shell
Shell considers spaces really important so can’t use those freely. foo=bar
and foo = bar
are not the same. The latter implies foo program is called with arguements.
$0
- Name of the script$1
to$9
- Arguments to the script.$1
is the first argument and so on.$@
- All the arguments$#
- Number of arguments$?
- Return code of the previous command$$
- Process identification number (PID) for the current script!!
- Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doingsudo !!
$_
- Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typingEsc
followed by.
orAlt+.
ls *.sh
= finds all the files with .sh extension, maybe you can use regex patterns here?
mkdir {abc,bcd}
= expands into two seperate mkdir commands with each of them?
?
= matches only one character in scripts
#!/user/local/bin/python
= shebang, can use it to just call a file directly from the shell as such ./script.py
without having to specify python3
grep -R stuff -c 10 /
Finding Directories:
find . -name src -type d
= find all directories named src
find . -path '*/test/*.py' -type f
= find all python files that have a folder named test in their path
find . -mtime -1
= find all files modified in the last day
find . -size +500k -size -10M -name '*.tar.gz'
= = find all zip files with size in range 500k to 10M
Regex:
.
means “any single character” except newline*
zero or more of the preceding match+
one or more of the preceding match[abc]
any one character ofa
,b
, andc
(RX1|RX2)
either something that matchesRX1
orRX2
^
the start of the line$
the end of the line
General Command Line:
CTRL C
is SIGINT
, a signal to interrupt basically, a signal to the process that is working
CTRL \
is SIGQUIT
, signals are an IPC cocnept, used to communicate between processes on a low level
kill
is used to send SIGTERM
for termination
Ctrl-Z
will prompt the shell to send a SIGTSTP
signal, short for Terminal Stop (i.e. the terminal’s version of SIGSTOP
)
To background an already running program you can do Ctrl-Z
followed by bg
. Note that backgrounded processes are still children processes of your terminal and will die if you close the terminal (this will send yet another signal, SIGHUP
)
Port-forwarding a server to redirect traffic to me:
ssh -R 80:localhost:80 tinyserver
You specify, that a connection made to the port 80 of tinyserver is to be forwarded to port 80 on your local machine. That means if someone connects to the small and slow server with a webbrowser, he gets the response of the webserver running on your local machine. The tinyserver, which has not enough diskspace for the big website, has no webserver running. But people connecting to tinyserver think so.